Method · Statistics Library

Linear mixed-effects model (random intercept)

A linear mixed-effects model extends ordinary regression to grouped data by adding a random intercept per group, so the fixed-effect coefficients and their p-values correctly account for the fact that observations within the same group are correlated rather than independent.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

Imagine measuring students nested within schools, or the same patient measured several times. Rows within the same group (the same school, the same patient) tend to be more alike than rows from different groups: students at one school share a teacher and a building; repeated measures on one patient share that patient's baseline health. Ordinary regression has no way to know this and treats every single row as equally, independently informative, which is simply not true.

A mixed model fixes this by giving each group its own small adjustment to the overall starting point (a 'random intercept') on top of the usual predictor effects that are shared across everyone. This absorbs the shared, group-level baseline so that the predictor effects you actually care about (the 'fixed effects') are estimated honestly, without pretending correlated rows are independent evidence.

The intraclass correlation (ICC) answers a genuinely useful follow-up question: how much of the leftover variation is actually BETWEEN groups versus just noise within a group? An ICC near 0 means the grouping barely matters (ordinary regression would have been fine); an ICC near 1 means groups differ enormously from each other, and the grouping structure is essential to get right.

A linear mixed-effects model, or LMM, is also called a multilevel model, because it models data in levels such as students within schools.

Practitioner explanation

KernelStats fits y_ig = beta0 + beta1*x1_ig + ... + u_g + epsilon_ig via statsmodels' mixedlm (REML by default, matching statsmodels' own default; no ML/REML toggle is exposed), with u_g ~ N(0, sigma_u^2) the random intercept per group and epsilon_ig ~ N(0, sigma_eps^2) the residual. Each fixed-effect coefficient gets a Wald Z-TEST (not a t-test) of H0: beta=0, using the large-sample normal approximation; the product uses z rather than t deliberately, and this is a large-sample approximation, not an exact small-sample test.

ICC = sigma_u^2 / (sigma_u^2 + sigma_eps^2) is reported directly as the proportion of leftover variance living between groups, letting the analyst judge whether the grouping structure was worth modeling at all (ICC near 0) or is essential (ICC near 1). An exactly redundant/collinear set of fixed-effect predictors is caught explicitly: a raw statsmodels 'Singular matrix' failure is translated into a plain-language explanation naming the collinear predictors, rather than leaking solver-internal terminology to the user.

This function's SCOPE is explicitly limited and stated as such: it fits a RANDOM-INTERCEPT model only. Random SLOPES (letting a predictor's effect itself vary by group), crossed random effects (two or more independent grouping factors), and generalized (non-normal response) mixed models are all explicitly out of scope; a user needing any of those needs a different tool, and the product does not silently approximate them with the simpler random-intercept structure.

When to use it

Consider it when

Observations are grouped or clustered (students within schools, repeated measures within subjects, samples within sites) and the within-group correlation would make ordinary regression's standard errors and p-values too optimistic, requiring a random intercept per group.

Use caution when

You need random SLOPES (a predictor's effect varying by group), two or more CROSSED random-effect factors, or a non-normal (generalized) response, none of which this random-intercept-only function supports; or when the grouping factor has fewer than 2 groups, which the model refuses outright.

Formulas and symbols

Random-intercept mixed model

yig=β0+β1x1ig+⋯+ug+εig,ug∼N(0,σu2), εig∼N(0,σε2)y_{ig} = \beta_0 + \beta_1 x_{1ig} + \dots + u_g + \varepsilon_{ig}, \qquad u_g \sim N(0,\sigma_u^2),\ \varepsilon_{ig}\sim N(0,\sigma_\varepsilon^2)

beta_j the fixed effects, shared across all groups; u_g the random intercept for group g, capturing that group's baseline deviation.

Intraclass correlation (ICC)

ICC=σu2σu2+σε2\text{ICC} = \dfrac{\sigma_u^2}{\sigma_u^2 + \sigma_\varepsilon^2}

The proportion of total leftover variance attributable to between-group differences, versus within-group residual noise.

Assumptions and checks

  • Normality (of residuals, not of the raw data). Non-normal residuals make the model's standard errors and any inference on the fixed-effect coefficients approximate rather than exact.

    In KernelStats: Shapiro-Wilk on the model residuals (for 3 to 5000 residuals), reported directly on the card.

  • Mixed models assume the random intercepts (the group-level deviations, e.g. per-subject or per-site effects) are drawn from a normal distribution; a strongly non-normal or heavy-tailed distribution of true group effects can distort the estimated variance components and the shrinkage applied to each group's estimate.

    In KernelStats: Shapiro-Wilk on the estimated random-intercept values (for 3 to 5000 groups), reported directly on the card.

  • Independence of observations. The whole point of a mixed model is to account for non-independence WITHIN a group (e.g. repeated measures on the same subject); the model still assumes independence BETWEEN groups, and any additional dependency not represented by the grouping structure (e.g. subjects nested in clinics that are themselves related) biases the standard errors.

    In KernelStats: Not checked by KernelStats; only the single specified grouping variable's structure is modeled; a more complex (e.g. crossed or nested multi-level) dependency structure needs a more general model this function does not fit.

Alternatives

  • Linear Regression. The intraclass correlation turns out to be near 0, meaning the grouping structure contributes negligible extra correlation and ordinary regression would give essentially the same, valid answer.
  • Repeated-Measures ANOVA. You have a balanced repeated-measures design with the same measurement occasions for every subject and want the classical repeated-measures ANOVA framing instead.

Worked example

Scenario

Predicting penguin body mass from flipper length, with island as the random-intercept grouping factor (three islands, unequal penguin counts per island).

Result

Flipper length: beta = 45.0 g per mm (95% CI [41.3, 48.7]), z = 23.8, p < .001; random-intercept variance sigma_u^2 = 16,818, residual variance sigma_eps^2 = 146,471, ICC = 0.10 (REML).

Interpretation

About a tenth of the leftover variance in body mass (after flipper length) sits between islands. That is modest but not zero, so rows from the same island are somewhat alike and the random intercept is a reasonable safeguard. With only three islands, though, the between-island variance is estimated from three group effects and is very uncertain; a grouping factor with so few levels is often better entered as a fixed effect.

Dataset: penguins.csv

Common misconceptions

Misconception

Since ordinary regression and a mixed model both give you coefficients and p-values, it doesn't matter much which one you use on grouped data.

Correction

Plain OLS assumes every row is an independent piece of evidence. When rows are actually grouped (correlated within groups), OLS standard errors come out too small and its p-values too optimistic: it effectively pretends you have more independent information than you really do. The random intercept in a mixed model absorbs the shared group-level baseline specifically so the fixed-effect inference reflects the true amount of independent evidence in the data.

Read more: Misconception: a non-significant result proves there is no effect

Misconception

A low ICC means the grouping variable was a waste of effort to include and should just be dropped from the analysis.

Correction

A low ICC means the grouping structure explains little of the LEFTOVER variance in this particular model: it does not mean including the random intercept was wrong or harmful. It correctly reflects that ordinary regression would have given a similar answer here; a mixed model with a low ICC is not a mistake, it is confirmation that the simpler model would have been adequate.

References

  1. Fitting Linear Mixed-Effects Models Using lme4, Bates, D., Machler, M., Bolker, B. and Walker, S. (2015), Journal of Statistical Software, 67(1), 1-48.
  2. Applied Longitudinal Data Analysis: Modeling Change and Event Occurrence, Singer, J. D. and Willett, J. B. (2003).