作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个用于合并和组织数据的函数,然后使用基本R中的并行函数并行运行MCMC链。我的函数如下。
dm100zip <- function(y, n.burn = 1, n.it = 3000, n.thin = 1) {
y <- array(c(as.matrix(y[,2:9]), as.matrix(y[ ,10:17])), c(length(y$Plot), 8, 2))
nplots <- nrow(y)
ncap1 <- apply(y[,1:8, 1],1,sum)
ncap2 <- apply(y[,1:8, 2],1,sum)
ncap <- as.matrix(cbind(ncap1, ncap2))
ymax1 <- apply(y[,1:8, 1],1,sum)
ymax2 <- apply(y[,1:8, 2],1,sum)
# Bundle data for JAGS/BUGS
jdata100 <- list(y=y, nplots=nplots, ncap=ncap)
# Set initial values for Gibbs sampler
inits100 <- function(){
list(p0=runif(1, 1.1, 2),
p.precip=runif(1, 0, 0.1),
p.day = runif(1, -.5, 0.1))
}
# Set parameters of interest to monitor and save
params100 <- c("N", "p0")
# Run JAGS in parallel for improved speed
CL <- makeCluster(3) # set number of clusters = to number of desired chains
clusterExport(cl=CL, list("jdata100", "params100", "inits100", "ymax1", "ymax2", "n.burn", "jag", "n.thin")) # make data available to jags in diff cores
clusterSetRNGStream(cl = CL, iseed = 5312)
out <- clusterEvalQ(CL, {
library(rjags)
load.module('glm')
jm <- jags.model("dm100zip.txt", jdata100, inits100, n.adapt = n.burn, n.chains = 1)
fm <- coda.samples(jm, params100, n.iter = n.it, thin = n.thin)
return(as.mcmc(fm))
})
out.list <- mcmc.list(out) # group output from each core into one list
stopCluster(CL)
return(out.list)
}
clusterExport
函数中使用的n.burn,n.it和n.thin。例如,
dm100zip.list.nain <- dm100zip(NAIN, n.burn = 1, n.it = 3000, n.thin = 1) # returns error
n.burn = 1
n.it = 1000
n.thin = 1
dm100zip.list.nain <- dm100zip(NAIN, n.burn = 1, n.it = 3000, n.thin = 1)
ClusterExport
函数使用全局环境中的对象,而不使用运行
ClusterExport
的函数分配的值?有没有解决的办法?
最佳答案
默认情况下,clusterExport在全局环境中查找“ varlist”指定的变量。在您的情况下,应在dm100zip函数的本地环境中查看。为此,请使用clusterExport“ envir”参数:
clusterExport(cl=CL, list("jdata100", "params100", "inits100", "ymax1",
"ymax2", "n.burn", "jag", "n.thin"),
envir=environment())
关于r - 如何在R函数中将对象导出到并行集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739876/
我是一名优秀的程序员,十分优秀!