Fit a mixed model
On this page
Fit reaction time on days of sleep deprivation with a random intercept for each subject, then read the fixed effects, the variance components, and each subject’s estimated effect.
The data are sleepstudy: average reaction time per day for 18 subjects in a sleep-deprivation study, days 0 to 9. Reaction is in milliseconds. The example reads the file by URL with phibble (composer require phpstats/phibble) and runs with the phit extension or the plain-PHP polyfill (Extension or polyfill).
Code
use Phibble\DataFrame;use function Phit\fit_mixed_gaussian;$sleep = DataFrame::fromCsv( 'https://raw.githubusercontent.com/phpstats/datasets/main/csv/sleepstudy.csv',);$fit = fit_mixed_gaussian( 'Reaction ~ Days + (1 | Subject)', $sleep,);echo $fit, "\n";echo $fit->tidy(effects: 'ran_vals'), "\n";Output
Linear mixed model fit by REML
Formula: Reaction ~ Days + (1 | Subject)
REML criterion at convergence: 1786.5
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 1378.2 37.12
Residual 960.5 30.99
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 251.4051 9.7467 25.79 <2e-16
Days 10.4673 0.8042 13.02 <2e-16
group level term estimate std_error
1 Subject 308 (Intercept) 40.783709 9.475668
2 Subject 309 (Intercept) -77.849553 9.475668
3 Subject 310 (Intercept) -63.108567 9.475668
4 Subject 330 (Intercept) 4.406442 9.475668
5 Subject 331 (Intercept) 10.216189 9.475668
6 Subject 332 (Intercept) 8.221238 9.475668
7 Subject 333 (Intercept) 16.500494 9.475668
8 Subject 334 (Intercept) -2.996981 9.475668
9 Subject 335 (Intercept) -45.282127 9.475668
10 Subject 337 (Intercept) 72.182685 9.475668
11 Subject 349 (Intercept) -21.196249 9.475668
12 Subject 350 (Intercept) 14.111363 9.475668
13 Subject 351 (Intercept) -7.862221 9.475668
14 Subject 352 (Intercept) 36.378425 9.475668
15 Subject 369 (Intercept) 7.036381 9.475668
16 Subject 370 (Intercept) -6.362703 9.475668
17 Subject 371 (Intercept) -3.294273 9.475668
18 Subject 372 (Intercept) 18.115747 9.475668
Interpretation
(1 | Subject)gives each subject an intercept of its own, assumed to come from a normal distribution. The fit uses REML, aslme4::lmer()does by default.- The
Dayscoefficient is the average change in reaction time per additional day of sleep deprivation. The model gives every subject the same slope. - The Subject standard deviation (37.12) describes how baseline reaction times vary between subjects. The residual standard deviation (30.99) describes how each subject’s days vary around their own line.
$fit->tidy(effects: 'ran_vals')gives each subject’s predicted intercept relative to the average and its conditional standard error, as R’sranef(condVar = TRUE)and broom.mixed report them. They are shrunk toward zero, more so for subjects with fewer observations. Every subject here has ten days, so their conditional standard errors are equal.- In these data the slopes also differ between subjects;
(Days | Subject)fits a random slope as well. See Mixed models.
See also: Mixed models for the supported structures, the full result, singular fits, and fit_mixed_logistic()