Earn the Mixed Effects Certificate
Practice the exercises below. When you feel ready, attempt the quiz to earn a verifiable certificate you can share on LinkedIn.
Attempt the quiz→
Home › Practice Exercises › Mixed Effects Models Exercises in R: 15 Practice Problems
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.
By Selva Prabhakaran · Published May 11, 2026 · Last updated May 11, 2026
library (lme4)
library (lmerTest)
library (dplyr)
library (performance)
▶ Run
↺ Reset
Exercise 1: Random intercept
Difficulty: Intermediate.
Show solution
data (sleepstudy)
lmer (Reaction ~ Days + (1 | Subject), data = sleepstudy)
▶ Run
↺ Reset
Exercise 2: Random slope
Difficulty: Advanced.
Show solution
data (sleepstudy)
lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
▶ Run
↺ Reset
Exercise 3: Uncorrelated random slope
Difficulty: Advanced.
Show solution
data (sleepstudy)
lmer (Reaction ~ Days + (Days || Subject), data = sleepstudy)
▶ Run
↺ Reset
Exercise 4: Summary with p-values
Difficulty: Intermediate.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
summary (fit)
▶ Run
↺ Reset
Exercise 5: Random effects estimates
Difficulty: Intermediate.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
ranef (fit)
▶ Run
↺ Reset
Exercise 6: Fixed effects
Difficulty: Beginner.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
fixef (fit)
▶ Run
↺ Reset
Exercise 7: Confidence intervals via profile
Difficulty: Advanced.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (1 | Subject), data = sleepstudy)
confint (fit)
▶ Run
↺ Reset
Exercise 8: ICC (intraclass correlation)
Difficulty: Advanced.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ 1 + (1 | Subject), data = sleepstudy)
performance:: icc (fit)
▶ Run
↺ Reset
Exercise 9: Nested random effect
Difficulty: Advanced.
Show solution
df <- tibble (school = rep (1 : 3 , each = 20 ), student = rep (1 : 10 , 6 ),
score = rnorm (60 ))
lmer (score ~ 1 + (1 | school/ student), data = df)
▶ Run
↺ Reset
Exercise 10: Crossed random effects
Difficulty: Advanced.
Show solution
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)
▶ Run
↺ Reset
Exercise 11: Compare nested models
Difficulty: Advanced.
Show solution
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)
▶ Run
↺ Reset
Exercise 12: glmer (logistic mixed)
Difficulty: Advanced.
Show solution
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)
▶ Run
↺ Reset
Exercise 13: Predict random effects
Difficulty: Advanced.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
predict (fit, newdata = sleepstudy[1 : 5 , ])
▶ Run
↺ Reset
Exercise 14: Marginal vs conditional predictions
Difficulty: Advanced.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
# Marginal (population): re.form = NA
predict (fit, newdata = sleepstudy[1 : 3 , ], re.form = NA )
▶ Run
↺ Reset
Exercise 15: Diagnostic plot
Difficulty: Intermediate.
Show solution
data (sleepstudy)
fit <- lmer (Reaction ~ Days + (Days | Subject), data = sleepstudy)
plot (fit)
▶ Run
↺ Reset
What to do next
Linear-Regression-Exercises (shipped), fixed-effects baseline.
Bayesian-Statistics-Exercises (shipped), Bayesian alternative.
← Previous Machine Learning Exercises
Next → Network Analysis Exercises
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→