# R's lmer cheat-sheet

There's a lot of discussion going on on this forum about the proper way to specify various hierarchical models using lmer.

I thought it would be great to have all the information in one place. A couple of questions to start:

1. How to specify multiple levels, where one group is nested within the other. is it (1|group1:group2) or (1+group1|group2)?
2. What's the difference between (~1 +....) and (1 | ...) and (0 | ...) etc.?
3. How to specify group level interactions?

Basically any thoughts are welcome.

-
The manual and three vignettes for the lme4 package can be found on CRAN – Henry Jul 17 '11 at 22:54
There are, in addition to the CRAN materials, lecture slides plus draft chapters of a book Doug is writing on (G)LMMs and R with lme4 available from r-forge – Gavin Simpson Jul 18 '11 at 7:41

What's the difference between (~1 +....) and (1 | ...) and (0 | ...) etc.?

Say you have variable V1 predicted by categorical variable V2, which is treated as a random effect, and continuous variable V3, which is treated as a linear fixed effect. Using lmer syntax, simplest model (M1) is:

V1 ~ (1|V2) + V3


This model will estimate:

P1: A global intercept

P2: Random effect intercepts for V2 (i.e. for each level of V2, that level's intercept's deviation from the global intercept)

P3: A single global estimate for the effect (slope) of V3

The next most complex model (M2) is:

V1 ~ (1|V2) + V3 + (0+V3|V2)


This model estimates all the parameters from M1, but will additionally estimate:

P4: The effect of V3 within each level of V2 (more specifically, the degree to which the V3 effect within a given level deviates from the global effect of V3), while enforcing a zero correlation between the intercept deviations and V3 effect deviations across levels of V2.

This latter restriction is relaxed in a final most complex model (M3):

V1 ~ (1+V3|V2) + V3


In which all parameters from M2 are estimated while allowing correlation between the intercept deviations and V3 effect deviations within levels of V2. Thus, in M3, an additional parameter is estimated:

P5: The correlation between intercept deviations and V3 deviations across levels of V2

Usually model pairs like M2 and M3 are computed then compared to evaluate the evidence for correlations between fixed effects (including the global intercept).

Now consider adding another fixed effect predictor, V4. The model:

V1 ~ (1+V3*V4|V2) + V3*V4


would estimate:

P1: A global intercept

P2: A single global estimate for the effect of V3

P3: A single global estimate for the effect of V4

P4: A single global estimate for the interaction between V3 and V4

P5: Deviations of the intercept from P1 in each level of V2

P6: Deviations of the V3 effect from P2 in each level of V2

P7: Deviations of the V4 effect from P3 in each level of V2

P8: Deviations of the V3-by-V4 interaction from P4 in each level of V2

P9 Correlation between P5 and P6 across levels of V2

P10 Correlation between P5 and P7 across levels of V2

P11 Correlation between P5 and P8 across levels of V2

P12 Correlation between P6 and P7 across levels of V2

P13 Correlation between P6 and P8 across levels of V2

P14 Correlation between P7 and P8 across levels of V2

Phew, That's a lot of parameters! And I didn't even bother to list the variance parameters estimated by the model. What's more, if you have a categorical variable with more than 2 levels that you want to model as a fixed effect, instead of a single effect for that variable you will always be estimating k-1 effects (where k is the number of levels), thereby exploding the number of parameters to be estimated by the model even further.

-
+1, great answer! – mpiktas Jul 18 '11 at 5:18
+1 Superb answer! – Gavin Simpson Jul 18 '11 at 7:35
@Mike Lawrence Thanks for the answer! how is a 3 level model estimated then? where one grouping factor is nested within another? – DBR Jul 18 '11 at 23:52
DBR, I don't think you know what levels are. You've been asking about this forever. Craft a question that actually details your experiment design and demonstrates your interpretation of "level". – John Jul 19 '11 at 12:50
I think DBR is referring to levels in the hierarchy. What I described is a 2-level hierarchical model, with observations nested within subjects, and DBR is asking about 3-level hierarchies, an example of which might be test items within students within schools where you want to model both students and schools as random effects, with students nested within schools. In such cases I presume that the school level deviations are first computed then the student-from-school deviations. – Mike Lawrence Jul 20 '11 at 20:07