# Order of nested random effects in lme4

I just fit a model in lme4, and I'm wondering what the heck I fit...

I have individuals id, and each is measured pass/fail on items that can be described using two factors, f1 and f2. My theory says that id and f2 can interact. So I want to compare these models:

# f2 doesn't matter
m1 <- lmer(pass ~ f1 + (1|id), family="binomial")

# f2 is a fixed effect, doesn't interact with individuals
m2 <- lmer(pass ~ f1 + f2 + (1|id), family="binomial")

# f2 is a nested random effect, interacting with individuals
m3 <- lmer(pass ~ f1 + (1|f2/id), family="binomial")


First, am I right that there's a random effect of each level of f2, and each individual's level of f2 is partially pooled, depending on the number of observations for that individual?

Second, does it make sense to fit f2 as a fixed effect with random individuals nested within each level of f2? How does one write that?

Third, what's this, with f2 and id reversed in the formula? I typed it by accident, but now I want to understand it.

m3weird <- lmer(pass ~ f1 + (1|id/f2), family="binomial")

• nested means that individuals did not see all levels of f2. Is that true? If each individual did see all levels of f2 (i.e., a usual repeated-measures factor) it's not nested but you should have random slopes for f2 as in: lmer(pass ~ f1 + (f2|id), family="binomial"). – Henrik Jul 18 '13 at 10:37
• @Henrik you read my mind, all indivs did see all levels of f2. But how does it make sense to have slopes at all? All I have are categorical factors, no continuous predictors... – Jack Tanner Jul 18 '13 at 17:30
• Given @Henrik's answer, the title of this question is likely to be confusing for future searchers and readers... – Silverfish Oct 25 '15 at 14:03
• @Silverfish I'm happy to rename it if you have a suggestion. How's "Syntax for nested and non-nested factors in lme4"? – Jack Tanner Oct 27 '15 at 2:54
• That sounds a bit better, thanks. Sometimes it isn't possible to pick the best title for a question until you've seen an answer, unfortunately! – Silverfish Oct 27 '15 at 10:29

As all participants did see all levels of your factor f1, the factor is not nested within id. A factor is nested within another factor if each instantiation of the higher order factor does not see all instantiations of the lower order factor (e.g., a factor is nested within id if id1 saw levels A and B, but not levels C and D, whereas id2 saw C & D but not levels A & B).

If you have simple within-subject factors, you want to have random slopes for the within-subject factors (i.e., in addition to the random intercept per id, each id can also have idiosyncratic effects for each factor level). This is discussed extensively in Barr et al. (see also here):

Barr, D. J., Levy, R., Scheepers, C., & Tily, H. J. (2013). Random effects structure for confirmatory hypothesis testing: Keep it maximal. Journal of Memory and Language, 68(3), 255–278. doi:10.1016/j.jml.2012.11.001

I suggest you use function mixed from package afex to obtain the p-values for your data using parametric bootstrap.

If only f2 is within-subjects:

mixed(pass ~ f1 * f2 + (f2|id), family="binomial", args.test = list(nsim = 10), method = "PB")


If both f1 and f2 are within-subject factors:

mixed(pass ~ f1 * f2 + (f1*f2|id), family="binomial", args.test = list(nsim = 10), method = "PB")


Note that you want to set nsim to high values for real applications (high means $> 1000$), but this usually takes time.

• aha! So I think what I have is crossed factors, not nested. Is crossed the same as what you call within-subject? And I'm still confused by "slopes" here - it's really random id intercepts within each level of f1*f2, right? – Jack Tanner Jul 18 '13 at 18:46
• Have a look at the Barr et al. paper. They explain what random slopes are extensively. If you then still have question, I am glad to help. – Henrik Jul 18 '13 at 18:55