Problems

Besides using JAGS for my own work, I have helped a fair number of people with their Bayesian models implemented in JAGS. As a result, I have come across a small set of problems that occur relatively frequently and I thought it would be helpful to go through these problems. More specifically, we will talk about:

  • Bad data
  • Incorrect order of parameters in distributions
  • Bad initial values
  • Unidentifiable parameters and mixing problems

These problems vary in how easy or hard it is to diagnose them. For instance, the first and second items in the above list immediately give an error message. The third item sometimes gives an error message but sometimes it does not, which can be perplexing to users. Finally, the last one does not give an error, which makes it very hard to diagnose and therefore makes it an even more important problem to know about. I describe below what each of these problems entail, why they arise, and what can be done to avoid them.

Bad data

How can data be bad? Often times, when JAGS breaks, we immediately think that something is wrong with how we specified the model, particularly for complex models. As a result, we might spend a lot of time trying to figure that out when, in some cases, the problem might lie with the data.

1) Binomial example

To keep things simple, let’s say that we are interested in estimating \(\beta_0\) and \(\beta_1\) in the following model:

\[y_i \sim Binom(N_i,\pi_i)\] where

\[\pi_i=\frac{exp(\beta_0 + \beta_1 x_i)}{1+exp(\beta_0 + \beta_1 x_i)}\]

In these expressions, \(y_i\), \(N_i\), and \(x_i\) are the data provided by the user. We know that, because of this assumption, the following must be true:

\[0 \le y_i \le N_i\]

Yet, if this is not true (e.g., perhaps people mistyped something when entering the field data), JAGS will complain.

You can see this in the code below. I first simulate data:

Here is the JAGS model specification in the file “binomial model”.

model{
  for (i in 1:nobs){
    prob[i] <- exp(b0+b1*x1[i])/(1+exp(b0+b1*x1[i]))
    y[i]~dbinom(prob[i],n[i])  
  }

  b0 ~ dnorm(0,1/10)
  b1 ~ dnorm(0,1/10)
}

Here is the code to fit the JAGS model.

## 
## Processing function input....... 
## 
## Done. 
##  
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 1000
##    Unobserved stochastic nodes: 2
##    Total graph size: 8007
## 
## Initializing model
## Deleting model
## Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains, : Error in node y[1]
## Node inconsistent with parents

2) Poisson example

While many of my examples are focused on the binomial distribution, bad things can happen with other distributions as well. For example, say that we are interested in the following Poisson regression:

\[y_i \sim Poisson(exp(\beta_0 + \beta_1 x_i))\]

By definition, \(y_i\) has to be a non-negative integer (i.e., 0,1,2,…,\(\infty\)). However, if this is not true, JAGS will complain.

You can see this in the code below. I first simulate data:

Here is the JAGS model specification in the file “poisson model”.

model{
  for (i in 1:nobs){
    media[i] <- exp(b0+b1*x1[i])
    y[i]~dpois(media[i])  
  }

  b0 ~ dnorm(0,1/10)
  b1 ~ dnorm(0,1/10)
}

Here is the code to fit the JAGS model.

## 
## Processing function input....... 
## 
## Done. 
##  
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 1000
##    Unobserved stochastic nodes: 2
##    Total graph size: 5007
## 
## Initializing model
## Deleting model
## Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains, : LOGIC ERROR:
## Invalid shape in LGMix::updateShape
## Please send a bug report to martyn_plummer@users.sourceforge.net

Incorrect order of parameters in distributions

It is important to realize that JAGS expects parameters to be provided in a very specific order. As a result, sometimes you might get errors if the order of parameters are switched. Again, I will pick on the binomial distribution in my example but similar problems might arise with the other distributions as well.

Say that I want to analyze the data from my “Binomial example” shown above but I misspecify the binomial distribution by changing the order of its parameters. Here is the JAGS model specification in the file “misspec binomial model”.

model{
  for (i in 1:nobs){
    prob[i] <- exp(b0+b1*x1[i])/(1+exp(b0+b1*x1[i]))
    y[i]~dbinom(n[i],prob[i])  
  }

  b0 ~ dnorm(0,1/10)
  b1 ~ dnorm(0,1/10)
}

Here is the code to fit the JAGS model.

## 
## Processing function input....... 
## 
## Done. 
##  
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 1000
##    Unobserved stochastic nodes: 2
##    Total graph size: 8007
## 
## Initializing model
## Deleting model
## Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains, : Error in node y[1]
## Node inconsistent with parents



Comments?

Send me an email at