Skip to main content

Mixed (aka multilevel or hierarchical) models are linear models that include both fixed effects and random effects. They are used to model longitudinal or nested data.


Mixed models are linear models that include both fixed effects and random effects*. They are used to model longitudinal or nested data; such data do not have independent errors and mixed models can account for the arising correlations. Mixed models are also known as multilevel or hierarchical linear models.

A classic example is the estimation of test scores of students: if test scores are correlated within classes, schools, districts, etc., mixed models allow the modeler to simultaneously estimate the differences between individual students and between the groups to which they belong (with the possibility of including covariates at all levels).

In a mixed model, study units are thought of as sampled from a population; the fixed effects are estimates of the population average effect, whereas the random effects are specific to the study units. In matrix form, a mixed effects model might be: $$ \bf Y=X\boldsymbol\beta + Zb + \boldsymbol\varepsilon $$ where $\bf X$ is the design matrix, $\boldsymbol\beta$ is a vector of the population average effects, $\bf Z$ is a subset of the columns of $\bf X$, $\bf b$ is a vector of the unit specific deviations from the population effects, and $\boldsymbol \varepsilon$ is a vector of random errors.

* Note that here we follow terminology used in statistics, social sciences, and biostatistics; similar terminology ("fixed effects", "random effects") is also used in econometrics, but the meaning is different.


StatsExchangers often recommend the following resources for learning more about mixed models:

Software packages

Mixed models are available in the following statistical packages:

  • lme4, nlme, lmerTest, gamm4, glmmTMB, GLMMadaptive, mmrm, glmmLasso, MCMCglmm, glmmPQL, rstanarm and brms for R
  • MLwiN
  • xtreg, xtmixed, xtlogit, xtmelogit, xtmepoisson, and other xt* commands; user-contributed package GLLAMM for Stata
  • Mplus
  • HLM
  • statsmodels and PyMC4 for Python
  • MixedModels.jl for Julia