Mixed Effects Models Exercises in R: 15 Practice Problems

Fifteen practice problems on mixed-effects models in R: lme4, random intercepts and slopes, nested groups, REML, model comparison. Hidden solutions.

RRun this once before any exercise
library(lme4) library(lmerTest) library(dplyr) library(performance)

  

Exercise 1: Random intercept

Difficulty: Intermediate.

Show solution
RInteractive R
data(sleepstudy) lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)

  

Exercise 2: Random slope

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)

  

Exercise 3: Uncorrelated random slope

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy)

  

Exercise 4: Summary with p-values

Difficulty: Intermediate.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) summary(fit)

  

Exercise 5: Random effects estimates

Difficulty: Intermediate.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) ranef(fit)

  

Exercise 6: Fixed effects

Difficulty: Beginner.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) fixef(fit)

  

Exercise 7: Confidence intervals via profile

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy) confint(fit)

  

Exercise 8: ICC (intraclass correlation)

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ 1 + (1 | Subject), data = sleepstudy) performance::icc(fit)

  

Exercise 9: Nested random effect

Difficulty: Advanced.

Show solution
RInteractive R
df <- tibble(school = rep(1:3, each = 20), student = rep(1:10, 6), score = rnorm(60)) lmer(score ~ 1 + (1 | school/student), data = df)

  

Exercise 10: Crossed random effects

Difficulty: Advanced.

Show solution
RInteractive R
df <- tibble(rater = sample(1:5, 100, replace = TRUE), subject = sample(1:20, 100, replace = TRUE), y = rnorm(100)) lmer(y ~ 1 + (1 | rater) + (1 | subject), data = df)

  

Exercise 11: Compare nested models

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) f1 <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy, REML = FALSE) f2 <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy, REML = FALSE) anova(f1, f2)

  

Exercise 12: glmer (logistic mixed)

Difficulty: Advanced.

Show solution
RInteractive R
df <- tibble(group = rep(1:5, each = 20), x = rnorm(100), y = rbinom(100, 1, 0.3)) glmer(y ~ x + (1 | group), data = df, family = binomial)

  

Exercise 13: Predict random effects

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) predict(fit, newdata = sleepstudy[1:5, ])

  

Exercise 14: Marginal vs conditional predictions

Difficulty: Advanced.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) # Marginal (population): re.form = NA predict(fit, newdata = sleepstudy[1:3, ], re.form = NA)

  

Exercise 15: Diagnostic plot

Difficulty: Intermediate.

Show solution
RInteractive R
data(sleepstudy) fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) plot(fit)

  

What to do next

  • Linear-Regression-Exercises (shipped), fixed-effects baseline.
  • Bayesian-Statistics-Exercises (shipped), Bayesian alternative.

Ready to earn the Mixed Effects Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz