问题描述
我有一个函数调用(对jags.parallel
),当给定像n.iter = 100
这样的数字参数时,该函数调用有效,但是当该参数使用变量值n.iter = n.iter
时,该函数调用失败.看来这可能是jags.parallel
I have a function call (to jags.parallel
) that works when given a numerical argument like n.iter = 100
but fails when the argument uses a variable value, n.iter = n.iter
. This looks like it might be a bug in jags.parallel
该错误的最小可重现示例:
A minimal reproducible example of the error:
library(R2jags)
model.file <- system.file(package="R2jags", "model", "schools.txt")
J <- 8.0
y <- c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2)
sd <- c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6)
jags.data <- list("y","sd","J")
jags.params <- c("mu","sigma","theta")
jags.inits <- function(){
list("mu"=rnorm(1),"sigma"=runif(1),"theta"=rnorm(J))
}
然后这有效:
jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
n.iter=5000, model.file=model.file)
但这不是:
n.iter=5000
jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
n.iter=n.iter, model.file=model.file)
给出错误:
Error in checkForRemoteErrors(lapply(cl, recvResult)) :
3 nodes produced errors; first error: object 'n.iter' not found
我认为这与不将变量n.iter
导出到集群有关,但尚不清楚并行引擎jags.parallel使用的是什么.在将R传递给函数之前,是否有任何技巧可以欺骗R来评估n.iter
?
I gather this has something to do with not exporting the variable n.iter
to the cluster, but it is not clear what parallel engine jags.parallel is using. Is there any way to trick R to evaluate n.iter
before passing it to the function?
推荐答案
do.call()
在这种情况下是很好的入门朋友,因为(来自?do.call
):
do.call()
is a great go-to friend in situations like this because (from ?do.call
):
我确认以下工作有效,并通过结果对象的打印方法显示的所有数字生成与您的jagsfit.p
匹配的结果:
I confirmed that the following works, producing results that match your jagsfit.p
through all digits displayed by the result object's print method:
jagsfit.p2 <- do.call(jags.parallel,
list(data=jags.data, inits=jags.inits, jags.params,
n.iter=n.iter, model.file=model.file))
这篇关于奇怪的jags.parallel错误/避免在函数调用中进行惰性求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!