[R-sig-ME] Nesting notation for lmer

Douglas Bates bates at stat.wisc.edu
Tue Feb 23 19:45:41 CET 2010


On Tue, Feb 23, 2010 at 11:48 AM, McMahon, Sean <mcmahons at si.edu> wrote:
> I have seen online a few different approaches to creating formulas with nested random effects in lmer(), as well as indication that the program automatically interprets nesting based on indicator variables.  What is the final word on how to construct a three-level model that is nested?

> I'm not including real variables in the hope that this is straightforward enough to propose a simple template.

> For a simple random intercept model where an individual is nested within a group variable level1 which is in turn nested within a community variable level2 , would a simple, nested model be:

> lmer(y ~ 1 + (1 | level1) + (1 | level2) )  # this would assume that the program can intuit nestedness

> lmer(y ~ 1 + (1 |level2 : level1) + (1 | level2) )  # I've seen examples of this, but cannot get it to run with my data

Telling us that you cannot get it to run is not very informative.
What did you type and what was the response?  I'll give you a hint.
First you should check the structure of your data with the str()
function to determine if the variables level1 and level2 are stored as
factors.  If they are not stored as factors then convert them.

The distinction between the first and the second model specifications
only occurs when a factor is implicitly nested within another.  There
is an example of what this means in the Ch2.pdf file at
http://lme4.R-forge.R-project.org/book/  If the factors have been
constructed so that each distinct group has a distinct label then
either form will work.


> lmer(y ~ 1 + (1 | level1) + (level1 | level2) )  #  this runs, but with slightly different results from the first example, but I don't feel confident that it is correct.

And with good reason.  That model specification is peculiar and almost
certainly not what you want.




More information about the R-sig-mixed-models mailing list