I'm currently writing code to run simulations for 2D spatially adaptive spline fitting techniques, within this code I have a for loop that should change the x, y and z values that I build my model from with each iteration.
N.B. To run this code you will require the MRSea package from github. This can
be downloaded using:
devtools::install_git("https://github.com/lindesaysh/MRSea.git")
After some very useful comments (thanks guys), I have reduced the code down to this:
simulate <- function(niter=5, npoints=10, nknots=9, fitmeasure="AIC", startknots=5, minknots=2,maxknots=9, knotgap=0 ) {
require(MRSea)
x <- matrix(nrow=niter, ncol=npoints)
y <- matrix(nrow=niter, ncol=npoints)
rnd <- matrix(nrow=niter, ncol=npoints)
mygrid <- expand.grid(seq(0,1, length=npoints), seq(0,1, length=npoints))
for (i in 1:niter) {
x[i,] <- runif(npoints)
y[i,] <- runif(npoints)
mu <- x[i,]*sin(4*pi*y[i,])
sigma <- diff(0.25 * range(mu))
rnd[i,] <- rnorm(npoints)
z <- mu + sigma*rnd[i,]
dat <- data.frame(x.pos=x[i,], y.pos=y[i,], response=z)
init <- glm(response ~ 1, data=dat)
knotgrid <- getKnotgrid(coordData = cbind(x[i,],y[i,]), numKnots = nknots, plot=F)
distMats <- makeDists(cbind(x[i,],y[i,]), na.omit(knotgrid))
salsa2dlist <- list(fitnessMeasure = fitmeasure, knotgrid = knotgrid,
startKnots=startknots, minKnots=minknots, maxKnots=maxknots, gap=knotgap)
predgrid <- makeDists(mygrid, na.omit(knotgrid))$dataDist
sim <- runSALSA2D(model=init, salsa2dlist, d2k=distMats$dataDist,
k2k=distMats$knotDist, splineParams=NULL, tol=0, chooserad=F,
panels=NULL, suppress.printout=TRUE)
}
return(x) # Can return x,y or rnd to see the issue
}
simulate()
I have identified that the removing the following line of code allows the runif() lines to run correctly and produce different values:
sim <- runSALSA2D(model=init, salsa2dlist, d2k=distMats$dataDist,
k2k=distMats$knotDist, splineParams=NULL, tol=0, chooserad=F,
panels=NULL, suppress.printout=TRUE)
Does anyone know why this line may be causing problems for my code and potential ways to solve it?
Thanks.