Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 28 ยท Other Models

Mixed-Effects Models

Model grouped or repeated data with mixed-effects models in Statsmodels. Fit random intercepts and slopes with MixedLM, read variance components and compare with plain OLS.

Ordinary regression assumes every row is an independent observation. Real data often comes in groups: students inside schools, patients measured repeatedly over time, customers inside stores. Rows from the same group resemble each other. Ignoring that makes standard errors too small, because 500 measurements from 10 schools are worth far less than 500 independent students.

A mixed-effects model (also called multilevel or hierarchical model) handles this by splitting effects into two kinds:

  • Fixed effects: the same for everyone, such as the average effect of study hours.
  • Random effects: they vary by group, such as each school's own baseline level, treated as draws from a distribution.

Fixed versus random

Fixed effect One value for the whole population.
Random intercept Each group has its own baseline.
Random slope Each group has its own trend.

Random effects share information across groups.

1 / 2

Random intercepts

The simplest model gives every group its own intercept. In Statsmodels, smf.mixedlm(formula, data, groups=...) fits it. The formula describes fixed effects; groups names the grouping column.

Python Random intercept model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the table, the first rows are fixed effects (Intercept, hours). The row Group Var is the variance of the random intercepts: how much schools differ in baseline (true value 25, as the standard deviation was 5). Scale is the residual variance within schools (true 16).

How much do groups matter? The ICC

The intraclass correlation (ICC) = group variance / (group variance + residual variance). It tells you the share of total variation that sits between groups. An ICC near 0 means grouping hardly matters; a large ICC means rows within a group are strongly alike and a plain OLS would be badly overconfident.

Python ICC and the cost of ignoring groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The intercept's standard error is far larger in the mixed model: OLS claims precision about the overall level that 20 schools simply cannot provide. The slope of hours, which varies within schools, is estimated well by both.

Random slopes

Sometimes the effect itself differs between groups: study hours may pay off more in some schools. Add re_formula="~hours" to give each group its own slope as well as its own intercept.

Python Random intercept and random slope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

m.random_effects holds each group's estimated deviations (called BLUPs). They are shrunk toward zero: groups with few observations are pulled more strongly toward the overall average, which is what makes mixed models stable.

Practical notes

  • Need enough groups. Variance components are poorly estimated with fewer than about 10 groups; with very few, treat the group as a fixed effect (C(group)).
  • Convergence warnings often mean the model is too complex for the data. Simplify the random part, rescale predictors, or try fit(method="lbfgs").
  • Repeated measures (same person over time) are one of the main uses; groups is the person.
  • Compare models with likelihood ratio tests only if both are fit by maximum likelihood (fit(reml=False)); REML likelihoods are not comparable across different fixed effects.
  • Non-normal outcomes (binary, counts with groups) need GEE (sm.GEE) or BinomialBayesMixedGLM; they are beyond a linear mixed model.

Mixed models share the "residuals and assumptions" mindset of Residual Diagnostics: check residuals within groups and the normality of the random effects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is ordinary OLS risky for students nested in schools?

    Observations within a group are not independent; ignoring this overstates precision, especially for group-level effects.
  2. 2. What does the ICC measure?

    ICC is group variance divided by total variance (group plus residual).
  3. 3. How do you give each group its own slope for x in mixedlm?

    re_formula defines the random-effects design; '~x' adds a random slope alongside the random intercept.
  4. 4. What is shrinkage in a mixed model?

    Partial pooling borrows strength across groups, stabilizing estimates for groups with little data.

Summary

  • Mixed models combine fixed effects with random effects that vary by group.
  • smf.mixedlm(formula, data, groups=...) fits random intercepts; re_formula adds random slopes.
  • Variance components and the ICC show how much groups matter; ignoring groups understates standard errors.
  • Use at least roughly 10 groups; watch convergence warnings.

Next: Quantile Regression