Pergunta

I would like to do bootstrap of residuals for nls fits in a loop. I use nlsBoot and in order to decrease computation time I would like to do that in parallel (on a Windows 7 system at the moment). Here is some code, which reproduces my problem:

#function for fitting
Falge2000 <- function(GP2000,alpha,PAR) {
  (GP2000*alpha*PAR)/(GP2000+alpha*PAR-GP2000/2000*PAR)
}

#some data
PAR <- 10:1600
GPP <- Falge2000(-450,-0.73,PAR) + rnorm(length(PAR),sd=0.0001)
df1 <- data.frame(PAR,GPP)

#nls fit
mod <- nls(GPP~Falge2000(GP2000,alpha,PAR),start=list(GP2000=-450,alpha=-0.73),data=df1, upper=c(0,0),algorithm="port")

#bootstrap of residuals
library(nlstools)
summary(nlsBoot(mod,niter=5))
#works

#now do it several times
#and in parallel
library(foreach)
library(doParallel)

cl <- makeCluster(1)
registerDoParallel(cl)

ttt <- foreach(1:5, .packages='nlstools',.export="df1") %dopar% {
  res <- nlsBoot(mod,niter=5)
  summary(res)

}
#Error in { : 
#task 1 failed - "Procedure aborted: the fit only converged in 1 % during bootstrapping"

stopCluster(cl)

I suspect this an issue with environments and after looking at the code of nlsBoot the problem seems to arise from the use of an anonymous function in a lapply call:

l1 <- lapply(1:niter, function(i) {
    data2[, var1] <- fitted1 + sample(scale(resid1, scale = FALSE), 
        replace = TRUE)
    nls2 <- try(update(nls, start = as.list(coef(nls)), data = data2), 
        silent = TRUE)
    if (inherits(nls2, "nls")) 
        return(list(coef = coef(nls2), rse = summary(nls2)$sigma))
})
if (sum(sapply(l1, is.null)) > niter/2) 
    stop(paste("Procedure aborted: the fit only converged in", 
        round(sum(sapply(l1, is.null))/niter), "% during bootstrapping"))

Is there a way to use nlsBoot in a parallel loop? Or do I need to modify the function? (I could try to use a for loop instead of lapply.)

Foi útil?

Solução

By moving the creation of the mod object into the %dopar% loop, it looks like everything works OK. Also, this automatically exports the df1 object, so you can remove the .export argument.

ttt <- foreach(1:5, .packages='nlstools') %dopar% {
  mod <- nls(GPP~Falge2000(GP2000,alpha,PAR),start=list(GP2000=-450,alpha=-0.73),data=df1, upper=c(0,0),algorithm="port")
  res <- nlsBoot(mod,niter=5)
  capture.output(summary(res))

}

However, you might need to work out what you want returned. Using capture.output was just to see if things were working, since summary(res) seemed to only return NULL.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top