Terran Melconian - 3 months ago 14

R Question

I'm getting some counterintuitive behavior from run.jags() in any model with a data block and a data= argument. It appears to be using the data argument to run.jags for the actual model, but searching the environment for anything used in the data block. Here is an example of a very simple model:

`data {`

ylen <- length(y)

}

model {

for (i in 1:ylen) {

y[i] ~ dnorm(mu,1)

}

mu ~ dnorm(0,1/10^2)

}

If I run it like so, I get an error:

`> 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

However, if I create a variable "y" in the calling environment it is used, but in a very strange way:

`> 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

So you can see that it appears to have used y from the calling environment for calculating the length, arriving at 3, but used the y value from the data list for the actual data, arriving at mu=5.

If I use rjags, it works as I expect, using the data= argument for both the actual model and the computation of derived variables in the data block.

Is this a bug in runjags? How can I get it to use the data= argument to run.jags() for computations in the data block?

I tried this on runjags_2.0.3-2 and runjags_2.0.4-2

Answer

Yes this is a bug in runjags, which will now be fixed for the next release thanks to your clear and reproducible example! The root of the problem is in trying to maintain compatibility with BUGS model text that can include a data list (which is a different thing to the data block being used by JAGS).

In the meantime the possible workarounds are to calculate ylen in R and pass it to JAGS in the list of data (also see the #data# construct within the model itself), or to use length(y) in the model directly e.g.:

```
model {
for (i in 1:length(y)) {
y[i] ~ dnorm(mu,1)
}
mu ~ dnorm(0,1/10^2)
}
```

Hope that helps,

Matt