lme4.
glmer: generalized linear mixed models.The independence assumption; why it matters; examples of non-independence.
Independence means that each observation in your dataset is generated by a separate, unrelated causal process.
Standard statistical methods (t-tests, OLS regression, ANOVA) assume independence.
When this assumption is violated:
The problem: Treating non-independent observations as independent makes you overconfident in your results.
You test whether a new drug increases happiness:
✓ Independence is reasonable here
Each person contributes exactly one data point.
You test whether people respond differently to metaphor type A vs. B:
✓ Independence is reasonable here
No repeated measures, no item effects to worry about.
Each observation is independent—no nesting structure.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 725.84046 1.571113 461.991212 3.060339e-302
groupB -20.93401 2.221890 -9.421717 1.194484e-17
Interpretation: - Intercept (groupA) = 725.84 (mean of group A) - groupB coefficient = -20.93 (difference from A) - Group B is about 20 units lower than Group A
Non-independence occurs when different observations are systematically related, i.e., they were produced by the same generative process.
Common sources of non-independence in behavioral research:
The sleepstudy dataset tracks reaction times in 18 subjects over subsequent days of sleep deprivation:
Reaction Days Subject
1 249.5600 0 308
2 258.7047 1 308
3 250.8006 2 308
💭 Check-in
Based on the structure of this study, what might be a source of non-independence?
Let’s pretend we don’t know about the nested structure:
💭 Check-in
What’s not pictured here?
Subjects differ in:
Some subjects are consistently faster; others are consistently slower.
Mixed effects models let us account for both types of variance.
Fixed vs. random effects; random intercepts; random slopes.
Mixed effects models combine:
Goal: Get better estimates of fixed effects by accounting for nested structure.
Determining which factors to include as fixed vs. random effects is not always straightforward. Here’s a rough guide, but we’ll discuss it again later too:
How do you decide?
Standard regression:
\[Y = \beta_0 + \beta_1 X + \epsilon\]
Mixed effects model with random intercepts:
\[Y = (\beta_0 + u_0) + \beta_1 X + \epsilon\]
Where:
Random intercepts allow each group (e.g., subject) to have its own baseline:
Same slope, different intercepts.
Basic model with random intercepts:
Breaking down the syntax:
Reaction ~ Days: Fixed effect of Days on Reaction(1 | Subject): Random intercept for each Subject
1 = intercept| = “grouped by”Subject = grouping variableAfter fitting the model, you can extract the random effects:
(Intercept)
308 40.783710
309 -77.849554
310 -63.108567
330 4.406442
These are deviations from the overall intercept (fixed effect).
Each subject’s fitted intercept = fixed intercept + random deviation:
(Intercept)
251.4051
[1] 40.78371
(Intercept)
292.1888
Random slopes allow the effect of X to vary by group:
Model with random intercepts AND slopes:
Estimate Std. Error t value
(Intercept) 251.40510 6.824597 36.838090
Days 10.46729 1.545790 6.771481
Breaking down the syntax:
Reaction ~ Days: Fixed effect of Days(1 + Days | Subject): Random effects for Subject
1 = random interceptDays = random slope for Days| = grouped bySubject = grouping variableAfter fitting the model, you can extract the random effects:
(Intercept) Days
308 2.258551 9.198976
309 -40.398738 -8.619681
310 -38.960409 -5.448856
330 23.690620 -4.814350
Now, we have deviations from the intercept and from the Days slope.
Each subject’s fitted slope = fixed slope + random deviation:
Days
10.46729
[1] 9.198976
Days
19.66626
Subject 308’s RT increases by ~19.67 ms per day (vs. population average of ~10.47 ms).
Without random slopes: - Assumes the effect is the same for everyone - Underestimates uncertainty in the fixed effect - Can lead to false positives
With random slopes: - Acknowledges that effects vary across individuals - Provides more conservative (realistic) estimates - Better generalization to new subjects
Rule of thumb: If a variable varies within your grouping variable, include it as a random slope.
You can include multiple sources of random effects:
Common in psycholinguistics: - Random effects for subjects (people vary) - Random effects for items (stimuli vary) - Called “crossed random effects”
Mixed effects models help when:
Key components:.
lme4Building and evaluating models; model comparisons; best practices and common issues.
lme4 packageMost common R package for mixed models:
Main functions: - lmer(): Linear mixed effects models - glmer(): Generalized linear mixed models (logistic, Poisson, etc.)
Let’s fit a model with random intercepts only:
Note: REML = FALSE uses maximum likelihood estimation, which we need for model comparison.
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: Reaction ~ Days + (1 | Subject)
Data: sleepstudy
AIC BIC logLik -2*log(L) df.resid
1802.1 1814.9 -897.0 1794.1 176
Scaled residuals:
Min 1Q Median 3Q Max
-3.2347 -0.5544 0.0155 0.5257 4.2648
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 1296.9 36.01
Residual 954.5 30.90
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error t value
(Intercept) 251.4051 9.5062 26.45
Days 10.4673 0.8017 13.06
Correlation of Fixed Effects:
(Intr)
Days -0.380
Now let’s add random slopes for the Days effect:
This allows both the intercept AND the slope to vary by subject.
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: Reaction ~ Days + (1 + Days | Subject)
Data: sleepstudy
AIC BIC logLik -2*log(L) df.resid
1763.9 1783.1 -876.0 1751.9 174
Scaled residuals:
Min 1Q Median 3Q Max
-3.9416 -0.4656 0.0289 0.4636 5.1793
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 565.48 23.780
Days 32.68 5.717 0.08
Residual 654.95 25.592
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error t value
(Intercept) 251.405 6.632 37.907
Days 10.467 1.502 6.968
Correlation of Fixed Effects:
(Intr)
Days -0.138
The likelihood of a model refers to the probability of the observed data under that model’s parameter estimates.
Question: Does adding variable \(X\) improve the model over a model without \(X\)?
Approach: Likelihood ratio test (LRT)
Data: sleepstudy
Models:
model_reduced: Reaction ~ (1 + Days | Subject)
model_full: Reaction ~ Days + (1 + Days | Subject)
npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq)
model_reduced 5 1785.5 1801.4 -887.74 1775.5
model_full 6 1763.9 1783.1 -875.97 1751.9 23.537 1 1.226e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Interpretation:: Adding Days significantly improves fit
Fixed effects (population-level):
(Intercept) Days
251.40510 10.46729
Random effects (subject-specific deviations):
(Intercept) Days
308 2.815789 9.075507
309 -40.047855 -8.644152
310 -38.432497 -5.513471
330 22.831765 -4.658665
A generalized linear mixed model (GLMM) is a general framework for fitting regression models with various link functions (e.g., logit) and random effects.
glm) and logistic regression.glmer is to glm as lmer is to lm.Scenario: Testing accuracy on a memory task
Subject Trial RT Correct
1 1 1 443.9524 0
2 2 1 476.9823 0
3 3 1 655.8708 0
4 4 1 507.0508 0
What if we use regular logistic regression?
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.333121392 1.267280721 -3.4192277 0.0006279914
RT 0.001789766 0.002405329 0.7440834 0.4568259948
Problem: This ignores that we have 30 trials per subject! Standard errors will be too small.
Add random effects for subjects:
Generalized linear mixed model fit by maximum likelihood (Laplace
Approximation) [glmerMod]
Family: binomial ( logit )
Formula: Correct ~ RT + (1 | Subject)
Data: binary_data
AIC BIC logLik -2*log(L) df.resid
174.0 187.2 -84.0 168.0 597
Scaled residuals:
Min 1Q Median 3Q Max
-0.2395 -0.1897 -0.1784 -0.1685 6.4595
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 0 0
Number of obs: 600, groups: Subject, 20
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.333121 1.277856 -3.391 0.000697 ***
RT 0.001790 0.002425 0.738 0.460513
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
RT -0.983
optimizer (Nelder_Mead) convergence code: 0 (OK)
boundary (singular) fit: see help('isSingular')
Syntax breakdown: - Correct ~ RT: Fixed effect of RT on accuracy - (1 | Subject): Random intercept for each subject - family = binomial(link = "logit"): Logistic regression
Generalized linear mixed model fit by maximum likelihood (Laplace
Approximation) [glmerMod]
Family: binomial ( logit )
Formula: Correct ~ RT + (1 | Subject)
Data: binary_data
AIC BIC logLik -2*log(L) df.resid
174.0 187.2 -84.0 168.0 597
Scaled residuals:
Min 1Q Median 3Q Max
-0.2395 -0.1897 -0.1784 -0.1685 6.4595
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 0 0
Number of obs: 600, groups: Subject, 20
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.333121 1.277856 -3.391 0.000697 ***
RT 0.001790 0.002425 0.738 0.460513
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
RT -0.983
optimizer (Nelder_Mead) convergence code: 0 (OK)
boundary (singular) fit: see help('isSingular')
Scenario: Testing accuracy on a memory task
Choosing random effects; dealing with convergence warnings; reporting results.
Mixed effects models can get complex pretty quickly. Researchers end up running into a number of common questions:
We’ll tackle each of these one by one.
Selecting fixed vs. random effects can be challenging. There’s not even universal consensus on the best way to do this!
Some ways to think about it:
Barr et al. (2013): “Keep it maximal”
Example:
Bates et al. (2015): “Parsimonious mixed models”
Example:
Common issues:
Step 1: Check your data - Are there enough observations per group? - Any extreme outliers?
Step 2: Rescale continuous predictors
Step 3: Try different optimizer
Step 4: Simplify random effects structure
Start by removing correlations between random effects:
Step 5: Remove random slopes if necessary
Use model comparison to determine which random effects are necessary (Bates et al., 2015).
Template for reporting:
We fit a linear mixed effects model predicting reaction time from days of sleep deprivation. The model included random intercepts and random slopes for days grouped by subject. Sleep deprivation significantly increased reaction time (β = 10.47, SE = 1.55), with each additional day of deprivation associated with an ~10.5 ms increase in RT. A likelihood ratio test confirmed that including days as a fixed effect significantly improved model fit, χ²(1) = 45.85, p < 0.001.
Essential information:
Key papers:
Tutorials:
Documentation:
lme4 package documentationy; random slopes model correlated variance in y ~ x.CSS 211 | UC San Diego