nlsBoot和foreach%dopar%:范围界定问题

nlsBoot和foreach%dopar%:范围界定问题,r,foreach,scope,nls,statistics-bootstrap,R,Foreach,Scope,Nls,Statistics Bootstrap,我想为nlsfits做一个循环的残差引导。我使用nlsBoot,为了减少计算时间,我想并行地(在目前的Windows7系统上)这样做。下面是一些代码,它再现了我的问题: #function for fitting Falge2000 <- function(GP2000,alpha,PAR) { (GP2000*alpha*PAR)/(GP2000+alpha*PAR-GP2000/2000*PAR) } #some data PAR <- 10:1600 GPP <-

我想为
nls
fits做一个循环的残差引导。我使用
nlsBoot
,为了减少计算时间,我想并行地(在目前的Windows7系统上)这样做。下面是一些代码,它再现了我的问题:

#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)

有没有办法在并行循环中使用
nlsBoot
?或者我需要修改函数吗?(我可以尝试使用
for
循环,而不是
lappy

通过将
mod
对象的创建移动到
%dopar%
循环中,看起来一切正常。此外,这会自动导出
df1
对象,因此您可以删除
.export
参数

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))

}

ttt您是否尝试过在
%dopar%
循环中创建
mod
对象(而不是自动导出)?谢谢。虽然这适用于简化的示例,但不适用于我的实际问题。最后,我从函数中取出了
nlsBoot
的代码,并在返回系数的过程中对其进行了一些简化。
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))

}