Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的foreach循环中调用函数_R_Foreach_Parallel Processing - Fatal编程技术网

在R中的foreach循环中调用函数

在R中的foreach循环中调用函数,r,foreach,parallel-processing,R,Foreach,Parallel Processing,我尝试使用foreach循环,但是如果我尝试在循环中拟合Gumbel分布,我会得到错误 {:任务1失败中出错-“必须定义dgumbel函数” 我读过很多关于函数和foreach循环的帖子,它们似乎都建议将“.export=functionname”放在foreach行中。我这样做了,但不明白为什么它仍然没有运行。如果我排除Gumbel fistdist行并只拟合正态分布,那么一切都很好。 如果我使用普通for循环,Gumbel分布的fitdist也可以正常工作 # create data tha

我尝试使用foreach循环,但是如果我尝试在循环中拟合Gumbel分布,我会得到错误

{:任务1失败中出错-“必须定义dgumbel函数”

我读过很多关于函数和foreach循环的帖子,它们似乎都建议将“.export=functionname”放在foreach行中。我这样做了,但不明白为什么它仍然没有运行。如果我排除Gumbel fistdist行并只拟合正态分布,那么一切都很好。 如果我使用普通for循环,Gumbel分布的fitdist也可以正常工作

# create data that will be used to fit a distribution
cum_prec <- matrix (nrow= 80, ncol=10)
x = c(1:6, 8:10)
for ( i in x){
  cum_prec[,i] <- rnorm(80,400, 50)}
cum_prec[,7] <- rGumbel(80,400, 50)


# package fitdistrplus does not work for the gumbel distribution. Therefore, the gumbel distribution has to be added manually.

dgumbel <- function(x,a,b) {1/b*exp((a-x)/b)*exp(-exp((a-x)/b))}
pgumbel <- function(q,a,b) {exp(-exp((a-q)/b))}
qgumbel <- function(p,a,b) {a-b*log(-log(p))}


library(doParallel)
cl <- makeCluster(4)
registerDoParallel(cl)




clim <- foreach (n=1:10,  .export=c("dgumbel", "pgumbel","qgumbel"), .combine= cbind,  .packages= "fitdistrplus") %dopar%


{
    rep=10000


normal <- matrix(nrow=rep, ncol=10)
x = c(1:6, 8:10)
for ( i in x){
  Normal<-fitdist(cum_prec[,i],"norm")
  normal[,i] <- rnorm(rep,Normal$estimate[1], Normal$estimate[2])
}



Gumbel<-fitdist(cum_prec[,7],"gumbel", start=list(a=0,b=1),optim.method="Nelder-Mead")
normal[,7] <- rGumbel(rep,Gumbel$estimate[1], Gumbel$estimate[2])


SU <- vector("numeric", rep)
for (i in 1:rep){
  su = (quantile(normal[,1], probs=runif(1,0,1))+ quantile(normal[,2], probs=runif(1,0,1)) + quantile(normal[,3], probs=runif(1,0,1)) 
        + quantile(normal[,4], probs=runif(1,0,1))+ quantile(normal[,5], probs=runif(1,0,1))+  quantile(normal[,6], probs=runif(1,0,1))
        + quantile(normal[,7], probs=runif(1,0,1))+  quantile(normal[,8], probs=runif(1,0,1))+  quantile(normal[,9], probs=runif(1,0,1))
        + quantile(normal[,10], probs=runif(1,0,1)))/10

  SU[i] <- su 
}

} 

stopCluster(cl)
#创建将用于适应分发的数据

cum_prec你需要在registerDoParallel(cl)
之前添加
clusterExport(cl,c(“dgumbel”,“pgumbel”,“qgumbel”,“rgumbel”)
语句。我简单地定义了
rgumbel非常感谢,@AndreyKolyadin!clusterExport()解决了gumbel函数的问题。foreach不会返回任何奇怪的内容,这是对的。我从“clim Edit:它现在可以与‘return(SU)’一起使用”开始思考。我将“n=1:10”更改为“I=1:10”。我不知道这有什么不同,但它可以工作。