在任何带有数据块和data=参数的模型中,我都会从data=()中得到一些违反直觉的行为。对于实际的模型,它似乎使用了run.jags的数据参数,但是在环境中搜索数据块中使用的任何内容。下面是一个非常简单的模型的示例:
data {
ylen <- length(y)
}
model {
for (i in 1:ylen) {
y[i] ~ dnorm(mu,1)
}
mu ~ dnorm(0,1/10^2)
}如果像这样运行它,就会得到一个错误:
> run.jags('trivial.jags',data=list(y=c(5,5,5,6)),monitor=c('ylen','mu'))
Error: The following error was obtained while attempting to parse the data:
Error in eval(expr, envir, enclos) : object 'y' not found但是,如果我在调用环境中创建一个变量"y“,就会使用它,但是以一种非常奇怪的方式使用:
> y<-c(-1,-1,-1)
> run.jags('trivial.jags',data=list(y=c(5,5,5,6)),monitor=c('ylen','mu'))
Compiling rjags model...
Calling the simulation using the rjags method...
Note: the model did not require adaptation
Burning in the model for 4000 iterations...
|**************************************************| 100%
Running the model for 10000 iterations...
|**************************************************| 100%
Simulation complete
Calculating summary statistics...
Note: The monitored variable 'ylen' appears to be non-stochastic; it will not be included
in the convergence diagnostic
Calculating the Gelman-Rubin statistic for 2 variables....
Finished running the simulation
JAGS model summary statistics from 20000 samples (chains = 2; adapt+burnin = 5000):
Lower95 Median Upper95 Mean SD Mode MCerr MC%ofSD SSeff AC.10 psrf
ylen 3 3 3 3 0 3 -- -- -- -- --
mu 3.8339 4.9742 6.0987 4.9747 0.57625 -- 0.0040089 0.7 20661 0.011 1.0001因此,您可以看到,它似乎从调用环境中使用y来计算长度,到达3,但使用数据列表中的y值来表示实际数据,即到达mu=5。
如果我使用rjags,它会像我预期的那样工作,使用data=参数来处理实际模型和数据块中派生变量的计算。
这是runjags中的一个bug吗?如何使用data=参数对run.jags()进行数据块中的计算?
我在runjags_2.0.3-2和runjags_2.0.4-2上尝试了这一点。
发布于 2016-09-29 14:48:47
是的,这是runjags中的一个bug,由于您的清晰和可复制的示例,下一个版本将对此进行修复!问题的根源在于试图保持与BUGS模型文本的兼容性,该模型文本可以包含一个数据列表(这与JAGS使用的数据块不同)。
同时,可能的解决办法是计算R中的ylen并将其传递给数据列表中的JAGS (也请参阅模型本身的#data#构造),或者在模型中直接使用length(y),例如:
model {
for (i in 1:length(y)) {
y[i] ~ dnorm(mu,1)
}
mu ~ dnorm(0,1/10^2)
}希望能帮上忙
哑光
https://stackoverflow.com/questions/39758231
复制相似问题