0
$\begingroup$

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!

$\endgroup$
2
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ So as I anticipated, the error is from tau. In STAN, I'd put real<lower=0> tau. How can I write this is in JAGS? Do you suggest I use a prior other than a normal distribution for tau? I appreciate help on the statistics part also. $\endgroup$ – aloha Nov 30 '15 at 11:53
  • 1
    $\begingroup$ Just to get it to run, start by replacing tau ~ dnorm(0, 50) with tau ~ dnorm(0, 50) T(0, ), and similarly with the sigma parameter I guess. This truncates the distribution so that it is never lower than 0. $\endgroup$ – Rasmus Bååth Nov 30 '15 at 12:03
  • $\begingroup$ @RasmusBååth Thank you. Now it is working. However, I'd like to know your opinion statistics wise. Which one is statistically correct (1) to use a truncated distribution or (2) to use a positive distribution (Such as the gamma distribution?) $\endgroup$ – aloha Nov 30 '15 at 12:07
  • 1
    $\begingroup$ @po6 both cases are correct, those are just a different priors that can be used. $\endgroup$ – Tim Nov 30 '15 at 12:09
  • $\begingroup$ @RasmusBååth can you take a look to my question asked here stats.stackexchange.com/questions/184673/…. I am using a model defined on your site to get the model I want. Thanks! $\endgroup$ – aloha Dec 2 '15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.