Solved – error when running JAGS

bayesianhierarchical-bayesianjagsr

In an attempt to learn JAGS I am trying to fit a line to data points. The data points have errors in both directions i.e. along the xaxis and yaxis.

Here is my model:

N = length(x)
DataSet = list(x = x, y = y, xerr = 0.155, yerr = 0.29, N = N)

# Define the model:
modelGregory = "
model {
  for ( i in 1:N ) {
    xtrue[i] ~ dnorm(mu, tau)
    ymodel[i] <- alpha + beta * xtrue[i]
    ytrue[i] ~ dnorm(ymodel[i], sigma)
  }

  for ( i in 1:N ){
    x[i] ~ dnorm( xtrue[i], xerr )
    y[i] ~ dnorm( ytrue[i], yerr )
  }
  mu ~ dnorm(0, 10)
  tau ~ dnorm(0, 50)
  alpha ~ dnorm(0, 10)
  beta ~ dnorm(0, 10)
  sigma ~ dt(0, 5, 1) # cauchy distribution with dof = 1
}
"

library('rjags')

# Run the chains
jags <-jags.model(textConnection(modelGregory), data = DataSet, 
              n.chains = 3, n.adapt = 100)

# update(jags, n.iter = 500)

# jags.samples(jags, c('alpha', 'beta', 'sigma'), 300)

When I run the code, I get the following error:

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 30
   Unobserved stochastic nodes: 35
   Total graph size: 109

Initializing model
Deleting model

Error in jags.model(textConnection(modelGregory), data = DataSet, n.chains = 3,  : 
  Error in node xtrue[1]
Invalid parent values

Can someone please explain to me why I am getting an error?
I believe that the error might be because of tau. In STAN, I'd define tau as real<lower=0> tau; If the error is because of tau how do I fix it?

Also, I would really appreciate it if someone explains to me what is Compiling model graph and graph information.

Note that the 2 last lines of the script are a comment because I know the error is coming from jags <- jags.model( ... )

Thanks!

Best Answer

As your error message says

  Error in node xtrue[1]
Invalid parent values

the xtrue variable has invalid parent values, so let's check what is it

xtrue[i] ~ dnorm(mu, tau)
...
mu ~ dnorm(0, 10)
tau ~ dnorm(0, 50)

$\tau$ cannot have a normal distribution since it is a precision parameter, i.e. inverse of variance, and variance cannot be negative, while you use as a prior for it a symmetric distribution centered at zero...

In such case you should use a distribution that does not allow negative values (e.g. truncated normal, as noticed by Rasmus Bååth, or gamma). See also Andrew Gelmans blog, his paper "Prior distributions for variance parameters in hierarchical models" (2006, Bayesian Analysis), or this thread.

Related Question