我正在并行运行 JAGS,即每个链都在单独的进程/线程中。我打电话run.jags(..., method = "rjparallel", params = c(..., "dic", "ped"))
。我收到以下错误:
jags The DIC, PED, pD, full.pD and pOpt cannot be assessed when using parallel or separate chains
嗯....
是不是因为 DIC 和 PED 是根据所有链计算的,而不仅仅是一个?难道不能在
run.jags
中完成吗?从所有线程收集结果后函数?有什么办法可以解决这个问题吗?并行运行时如何获得 DIC 和 PED?一定有可能,对吧?
最佳答案
如果您并行运行模型,则无法跟踪 dic
或 ped
。这样做的原因是 extend.jags
函数需要“...多个链WITHIN同一模拟”(强调,请参阅帮助文件并查找
参数)。并行运行时,每个核心只有一条链。extend.jags
函数的 >monitor
但是,您可以在使用 extract
函数拟合模型后收集这些数据。这是一个简单 JAGS 模型的可重现示例,用于说明如何执行此操作。
library(runjags)
# generate data
y <- rnorm(1000, 3, 10)
# the model
modelstring="
model{
mu ~ dnorm(0, 0.001)
tau ~ dgamma(0.001,0.001)
sigma <- 1 / sqrt(tau)
for(i in 1:1000){
y[i] ~ dnorm(mu, tau)
}
}
"
# save this model string in your working directory
fileconn <- file("simple_norm.R")
writeLines(modelstring, fileconn)
close(fileconn)
# fit the model
model = run.jags(model = "simple_norm.R",
data = data_list,
monitor = c("mu", "sigma"),
n.chains = 3,
burnin = 1000,
sample = 5000,
method = "rjparallel"
)
# collect DIC and ped
my_dic <- extract(model, what = "dic")
my_ped <- extract(model, what = "ped")
# the output
> my_dic
Mean deviance: 7411
penalty 1.979
Penalized deviance: 7413
> my_ped
Mean deviance: 7411
penalty 3.961
Penalized deviance: 7415
关于r - 并行运行 JAGS 时的 DIC 和 PED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50982487/