Estimating general random-effect structures with JAGS

Fri 28 June 2013 by Adrian Brasoveanu

Given the importance of being able to estimate general random-effect structures for mixed-effects models (see this paper by Barr, Levy, Scheepers & Tily, for example), I put together a tutorial on how to estimate such models in R and JAGS.

This is easy to do for mixed-effects linear models in R with the lmer() function, but the advantage of being able to do it with JAGS/BUGS is that we can then use basically the same code to estimate general random-effect structures for any other models, e.g., binomial probit/logit mixed-effects models, ordinal probit/logit mixed-effects models etc.

Mixed-effect models contain factors, or more generally covariates, with both fixed and random effects. We constrain the values for at least one set of effects (intercepts and/or slopes) to come from a normal distribution. This is what the random-effects assumption usually means (usually; in general, random effects can come from any distribution). There are at least three sets of assumptions that one can make about the random effects for the intercept and/or the slope of regression lines that are fitted to grouped data (in all our cases: grouped by subjects):

  • Models of type 1: only intercepts are random, but slopes are identical for all subjects
  • Models of type 2: both intercepts and slopes are random, but they are independent
  • Models of type 3: both intercepts and slopes are random and there is a correlation between them

The R & JAGS script is available here. The material in this presentation is based on Gelman & Hill (2007), Kery (2010), and notes here and here.

Contents of the script:

  1. first, we generate a random-coefficients data set under a model of type 2 where both intercepts and slopes are uncorrelated random effects
  2. we then fit both a random-intercepts (type 1) and a random-coefficients model without correlation (type 2) to this dataset (both in R with lmer() and in JAGS)
  3. next, we generate a second data set that includes a correlation between random intercepts and random slopes and adopt the random-coefficients model with correlation between intercepts and slopes (type 3) to analyze it
  4. there is only one way to do this in R with lmer(); but we estimate this model with JAGS in 2 different ways:
    • by separately modeling the sd.s of the 2 random-effect distributions (the intercepts and the slopes) and the correlation between the 2 distributions
    • by modeling all three parameters simultaneously with a scaled inverse Wishart prior; this second way has much better mixing and it also generalizes immediately to more complex random effects structures involving 3 or more correlated random effects
  5. finally, we turn to general random-effect structures: we simulate a third data set that includes 3 correlated random effects (intercepts and 2 distinct slope vectors) and show how the random-coefficients model with correlations between intercepts and slopes generalizes to account for this kind of random-effect structure; we do the analysis both in R with lmer() and in JAGS with a scaled inverse Wishart prior