データ ブロックと data= 引数を持つ任意のモデルで、run.jags() から直感に反する動作が発生します。実際のモデルの run.jags に data 引数を使用しているように見えますが、データ ブロックで使用されているものを環境で検索しています。非常に単純なモデルの例を次に示します。
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 のバグですか? data= 引数を使用して run.jags() をデータ ブロック内の計算に使用するにはどうすればよいですか?
runjags_2.0.3-2 と runjags_2.0.4-2 でこれを試しました