phit

Random-effect intervals

A mixed model estimates each school's random effect as a conditional mode, the most likely value of the effect given the data and the estimated variance components. Phit\fit_lmer() also returns the conditional variance of each mode, the quantity that lme4 reports with ranef(condVar = TRUE). Those variances give each school an interval.

This example fits the school scores data with random intercepts, then with random study-hours slopes. phiz's random_effects_plot() draws the caterpillar plot from the fit alone, using the reported variances. The intervals treat the fixed effects and variance components as known, as lme4's do.

Random effects
$fit = DataFrame::fromQuery(StudentScore::query()) |> lmer('score ~ study_hours + (1 | school)');

$fit['random'][0]['effects'];           // conditional modes, one row per school
$fit['random'][0]['effects_cond_var'];  // their conditional covariance, one matrix per school

random_effects_plot($fit, level: 0.95);
$fit = phplyr(DataFrame::fromQuery(StudentScore::query()))->lmer('score ~ study_hours + (1 | school)');

$fit['random'][0]['effects'];           // conditional modes, one row per school
$fit['random'][0]['effects_cond_var'];  // their conditional covariance, one matrix per school

random_effects_plot($fit, level: 0.95);

Caterpillar plot

Each point is a school's conditional mode, its estimated deviation from the fixed effects. Each bar is a 95% interval of ±z standard deviations, where the standard deviation is the square root of the conditional variance. Schools are sorted by their intercept.

Variance components

Term Variance Std. dev.
school: (Intercept) 20.8666 4.5680
Residual 29.0940 5.3939

REML fit on 250 students in 12 schools. AIC = 1,591.3.

Conditional modes and standard deviations

School Students (Intercept) Cond. SD Closed form
school-01 18 0.8926 1.2248 1.2248
school-02 24 1.7890 1.0704 1.0704
school-03 15 -2.5419 1.3322 1.3322
school-04 22 -5.7326 1.1152 1.1152
school-05 20 0.5818 1.1661 1.1661
school-06 27 2.8431 1.0122 1.0122
school-07 19 9.9943 1.1944 1.1944
school-08 21 -5.5026 1.1398 1.1398
school-09 16 2.4371 1.2933 1.2933
school-10 25 0.2354 1.0499 1.0499
school-11 23 0.1785 1.0921 1.0921
school-12 20 -5.1747 1.1661 1.1661

With one random intercept, the conditional variance of a school's effect is τ²σ² / (σ² + nτ²), where τ² is the school variance, σ² is the residual variance, and n is the number of students. The closed-form column computes it from the variance components and matches the reported value. Schools with more students have smaller conditional standard deviations.