Investigation: Mixed effects models#
Mixed models represent variation across sampling units and partially pool their estimates. The random-effects structure should follow the design and scientific target. Prediction for a new participant differs from prediction for another observation of a known participant.
Question. Compare pooled and mixed models in sleepstudy, then fit crossed participant/item intercepts to lexical-decision trials.
Before running: write the result you expect and what would challenge it. Data are simulated unless explicitly identified otherwise.
# Open this notebook from book/notebooks (or book/exercises).
course_path <- if (file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
stopifnot(file.exists(course_path))
source(course_path)
set.seed(432)
library(lme4)
d <- sleepstudy
pooled <- lm(Reaction~Days,d)
random_intercept <- lmer(Reaction~Days+(1|Subject),d,REML=FALSE)
random_slope <- lmer(Reaction~Days+(Days|Subject),d,REML=FALSE)
# Comparable ML fits, same observations and response.
AIC(pooled,random_intercept,random_slope)
fixef(random_slope)
VarCorr(random_slope)
isSingular(random_slope)
random_slope@optinfo$conv$lme4$messages
Loading required package: Matrix
| df | AIC | |
|---|---|---|
| <dbl> | <dbl> | |
| pooled | 3 | 1906.293 |
| random_intercept | 4 | 1802.079 |
| random_slope | 6 | 1763.939 |
- (Intercept)
- 251.405104848485
- Days
- 10.4672859595958
Groups Name Std.Dev. Corr
Subject (Intercept) 23.7798
Days 5.7168 0.081
Residual 25.5919
NULL
Partial pooling and the prediction target#
sleepstudy records reaction times under sleep deprivation across repeated days. Random intercepts and slopes describe participant variation. Variance components may be zero; nonnegativity is not significance. AIC is a penalized likelihood criterion, not unexplained variance. REML is useful for variance estimation, but likelihood comparisons across fixed-effect specifications require ML.
trials <- simulate_trials()
s <- split_groups(trials$subject)
fit <- lmer(rt~frequency+length+(1|subject)+(1|item),trials[s$train,],REML=FALSE)
summary(fit)
pred <- predict(fit,trials[s$test,],allow.new.levels=TRUE)
c(new_participant_mse=mse(trials$rt[s$test],pred))
# New subjects have no estimated subject deviation; known item effects remain available.
stopifnot(length(intersect(trials$subject[s$train],trials$subject[s$test]))==0)
isSingular(fit)
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: rt ~ frequency + length + (1 | subject) + (1 | item)
Data: trials[s$train, ]
AIC BIC logLik deviance df.resid
8966.4 8994.8 -4477.2 8954.4 834
Scaled residuals:
Min 1Q Median 3Q Max
-3.4955 -0.6331 -0.0024 0.6553 3.2808
Random effects:
Groups Name Variance Std.Dev.
item (Intercept) 562.4 23.72
subject (Intercept) 2718.1 52.13
Residual 2045.9 45.23
Number of obs: 840, groups: item, 30; subject, 28
Fixed effects:
Estimate Std. Error t value
(Intercept) 642.674 21.856 29.404
frequency -28.178 2.748 -10.254
length 12.041 1.840 6.544
Correlation of Fixed Effects:
(Intr) frqncy
frequency -0.543
length -0.739 0.125
The simulated trials include crossed subject/item intercepts but no random frequency slopes; a real design may justify slopes too. Report convergence warnings and singular fits, then simplify or revise the model for a stated reason. This split does not test new items. Do not claim a mixed model automatically solves every generalization problem.
Interpret and verify#
Justify grouping factors and random slopes, inspect singularity and convergence, and state whether prediction targets known or new groups.
Failure to consider: Variance components can be zero; nonnegativity does not imply significance. Ignoring clustering can invalidate standard errors without necessarily biasing a fixed-effect point estimate.
Report the target, the numerical result with its units, the relevant uncertainty, and one limitation. Distinguish what is established by the simulation from what would require real data.
AI audit: critique the saved faulty claim in the companion exercise before using any live assistant. If you use AI, record the relevant suggestion, your independent check, and the final decision.
Connect trial dependence to the observational unit and use it to choose the validation split.
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.7.7
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lme4_1.1-30 Matrix_1.4-1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 splines_4.2.1 MASS_7.3-57
[4] uuid_1.1-0 lattice_0.20-45 rlang_1.0.6
[7] fastmap_1.1.0 minqa_1.2.4 fansi_1.0.3
[10] tools_4.2.1 grid_4.2.1 nlme_3.1-157
[13] utf8_1.2.2 cli_3.3.0 htmltools_0.5.3
[16] digest_0.6.29 lifecycle_1.0.1 crayon_1.5.1
[19] IRdisplay_1.1 nloptr_2.0.3 repr_1.1.4
[22] base64enc_0.1-3 vctrs_0.4.1 IRkernel_1.3.0.9000
[25] glue_1.6.2 evaluate_0.15 pbdZMQ_0.3-7
[28] compiler_4.2.1 pillar_1.8.0 boot_1.3-28
[31] jsonlite_1.8.0
Attribution#
Notebook authored by Ven Popov, and edited by Krista Bond, Charles Wu, Patience Stevens, Amy Sentis, and Fiona Horner.
Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.