如果您正在并行运行模型,则无法跟踪dic
或ped
。这样做的原因是该函数需要“ ......extend.jags
在同一模拟中的多个链”(强调添加,请参阅帮助文件并查找函数的参数)。并行运行时,每个核心只有一个链。monitor
extend.jags
但是,您可以在使用该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