In a very lucid and informative answer to a question about nested versus crossed random effect Crossed vs nested random effects: how do they differ and how are they specified correctly in lme4? (posted on 8 August 2016) Robert Long used a classes within schools example to illustrate the syntax to use in lmer() when the data are coded so that the nesting is "implicit".

In this example school, as well as class is a random effect. I need to deal with an example analogous to that in which school gets treated as a fixed effect. I think I have the syntax correct, but I would like to have this confirmed (if someone would be so kind).

First, to get the data that Robert Long used:

dt <- read.table("http://bayes.acs.unt.edu:8083/BayesContent/class/Jon/R_SC/Module9/lmm.data.txt",
                   header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)

Now create (as Robert Long did) an "explicitly nested" class factor:

dt$classID <- paste(dt$school, dt$class, sep=".")

Now I'd like to fit simple examples in which school is the only fixed effect predictor.

fit.expl <- lmer(extro ~ school + (1 | classID), data = dt)

fit.impl <- lmer(extro ~ school + (1 | class:school), data = dt)

Is the latter fit the correct way of dealing with implicit nesting of random effect within a fixed effect? The two fits seem to agree. The extractors fixef(), ranef() and logLik() seem to give the same results from the two fits (modulo some differences in labelling).

Are there any Traps for Young Players lurking around that I have failed to take into account?

  • $\begingroup$ Could you please add the link to the question or answer? $\endgroup$
    – Yves
    Jan 9, 2019 at 7:53
  • $\begingroup$ this basically seems correct (classID and class:school [converted into an interaction variable internally] are equivalent variables); don't have time to write a proper answer at the moment. $\endgroup$
    – Ben Bolker
    Jan 9, 2019 at 16:43

1 Answer 1


Yes, the two models you specified are equivalent and give identical results. In extro ~ school + (1 | school:class), we produce one fixed intercept, as the score of the reference school, (k - 1) fixed coefficients of school for differences among schools relative to the reference school, and random intercepts per class within each school.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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