gpt4 book ai didi

r - 奇怪的 jags.parallel 错误/避免函数调用中的延迟评估

转载 作者:行者123 更新时间:2023-12-03 23:31:30 25 4
gpt4 key购买 nike

我有一个函数调用(到 jags.parallel ),当给定一个像 n.iter = 100 这样的数字参数时它可以工作。但当参数使用变量值时失败,n.iter = n.iter .这看起来可能是 jags.parallel 中的错误

错误的最小可重现示例:

    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来评估 n.iter在将其传递给函数之前?

最佳答案

do.call()在这种情况下是一个很好的 friend ,因为(来自 ?do.call):

If 'quote' is 'FALSE', the default, then the arguments are evaluated (in the calling environment, not in 'envir').



我确认以下有效,产生与您的 jagsfit.p 相符的结果通过结果对象的打印方法显示的所有数字:
jagsfit.p2 <- do.call(jags.parallel, 
list(data=jags.data, inits=jags.inits, jags.params,
n.iter=n.iter, model.file=model.file))

关于r - 奇怪的 jags.parallel 错误/避免函数调用中的延迟评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com