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")
  • $\begingroup$ 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"). $\endgroup$
    – Henrik
    Commented Jul 18, 2013 at 10:37
  • $\begingroup$ @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... $\endgroup$ Commented Jul 18, 2013 at 17:30
  • $\begingroup$ Given @Henrik's answer, the title of this question is likely to be confusing for future searchers and readers... $\endgroup$
    – Silverfish
    Commented Oct 25, 2015 at 14:03
  • $\begingroup$ @Silverfish I'm happy to rename it if you have a suggestion. How's "Syntax for nested and non-nested factors in lme4"? $\endgroup$ Commented Oct 27, 2015 at 2:54
  • $\begingroup$ 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! $\endgroup$
    – Silverfish
    Commented Oct 27, 2015 at 10:29

1 Answer 1


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.

  • $\begingroup$ 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? $\endgroup$ Commented Jul 18, 2013 at 18:46
  • $\begingroup$ 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. $\endgroup$
    – Henrik
    Commented Jul 18, 2013 at 18:55

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.