Linear Mixed Effects Models

Linear Mixed Effects models are used for regression analyses involving dependent data. Such data arise when working with longitudinal and other study designs in which multiple observations are made on each subject. Some specific linear mixed effects models are

  • Random intercepts models, where all responses in a group are additively shifted by a value that is specific to the group.

  • Random slopes models, where the responses in a group follow a (conditional) mean trajectory that is linear in the observed covariates, with the slopes (and possibly intercepts) varying by group.

  • Variance components models, where the levels of one or more categorical covariates are associated with draws from distributions. These random terms additively determine the conditional mean of each observation based on its covariate values.

The statsmodels implementation of LME is primarily group-based, meaning that random effects must be independently-realized for responses in different groups. There are two types of random effects in our implementation of mixed models: (i) random coefficients (possibly vectors) that have an unknown covariance matrix, and (ii) random coefficients that are independent draws from a common univariate distribution. For both (i) and (ii), the random effects influence the conditional mean of a group through their matrix/vector product with a group-specific design matrix.

A simple example of random coefficients, as in (i) above, is:

Yij=β0+β1Xij+γ0i+γ1iXij+ϵij

Here, Yij is the jth measured response for subject i, and Xij is a covariate for this response. The “fixed effects parameters” β0 and β1 are shared by all subjects, and the errors ϵij are independent of everything else, and identically distributed (with mean zero). The “random effects parameters” γ0i and γ1i follow a bivariate distribution with mean zero, described by three parameters: var(γ0i), var(γ1i), and cov(γ0i,γ1i). There is also a parameter for var(ϵij).

A simple example of variance components, as in (ii) above, is:

Yijk=β0+η1i+η2j+ϵijk

Here, Yijk is the kth measured response under conditions i,j. The only “mean structure parameter” is β0. The η1i are independent and identically distributed with zero mean, and variance τ12, and the η2j are independent and identically distributed with zero mean, and variance τ22.

statsmodels MixedLM handles most non-crossed random effects models, and some crossed models. To include crossed random effects in a model, it is necessary to treat the entire dataset as a single group. The variance components arguments to the model can then be used to define models with various combinations of crossed and non-crossed random effects.

The statsmodels LME framework currently supports post-estimation inference via Wald tests and confidence intervals on the coefficients, profile likelihood analysis, likelihood ratio testing, and AIC.

Examples

In [1]: import statsmodels.api as sm

In [2]: import statsmodels.formula.api as smf

In [3]: data = sm.datasets.get_rdataset("dietox", "geepack").data

In [4]: md = smf.mixedlm("Weight ~ Time", data, groups=data["Pig"])

In [5]: mdf = md.fit()

In [6]: print(mdf.summary())
         Mixed Linear Model Regression Results
========================================================
Model:            MixedLM Dependent Variable: Weight    
No. Observations: 861     Method:             REML      
No. Groups:       72      Scale:              11.3669   
Min. group size:  11      Log-Likelihood:     -2404.7753
Max. group size:  12      Converged:          Yes       
Mean group size:  12.0                                  
--------------------------------------------------------
             Coef.  Std.Err.    z    P>|z| [0.025 0.975]
--------------------------------------------------------
Intercept    15.724    0.788  19.952 0.000 14.179 17.268
Time          6.943    0.033 207.939 0.000  6.877  7.008
Group Var    40.394    2.149                            
========================================================

Detailed examples can be found here

There are some notebook examples on the Wiki: Wiki notebooks for MixedLM

Technical Documentation

The data are partitioned into disjoint groups. The probability model for group i is:

Y=Xβ+Zγ+Q1η1++Qkηk+ϵ

where

  • ni is the number of observations in group i

  • Y is a ni dimensional response vector

  • X is a nikfe dimensional matrix of fixed effects coefficients

  • β is a kfe-dimensional vector of fixed effects slopes

  • Z is a nikre dimensional matrix of random effects coefficients

  • γ is a kre-dimensional random vector with mean 0 and covariance matrix Ψ; note that each group gets its own independent realization of gamma.

  • Qj is a ni×qj dimensional design matrix for the jth variance component.

  • ηj is a qj-dimensional random vector containing independent and identically distributed values with variance τj2.

  • ϵ is a ni dimensional vector of i.i.d normal errors with mean 0 and variance σ2; the ϵ values are independent both within and between groups

Y,X,{Qj} and Z must be entirely observed. β, Ψ, and σ2 are estimated using ML or REML estimation, and γ, {ηj} and ϵ are random so define the probability model.

The marginal mean structure is E[Y|X,Z]=Xβ. If only the marginal mean structure is of interest, GEE is a good alternative to mixed models.

Notation:

  • covre is the random effects covariance matrix (referred to above as Ψ) and scale is the (scalar) error variance. There is also a single estimated variance parameter τj2 for each variance component. For a single group, the marginal covariance matrix of endog given exog is scaleI+ZcovreZ, where Z is the design matrix for the random effects in one group.

References

The primary reference for the implementation details is:

  • MJ Lindstrom, DM Bates (1988). Newton Raphson and EM algorithms for linear mixed effects models for repeated measures data. Journal of the American Statistical Association. Volume 83, Issue 404, pages 1014-1022.

See also this more recent document:

All the likelihood, gradient, and Hessian calculations closely follow Lindstrom and Bates.

The following two documents are written more from the perspective of users:

Module Reference

The model class is:

MixedLM(endog, exog, groups[, exog_re, ...])

Linear Mixed Effects Model

The result class is:

MixedLMResults(model, params, cov_params)

Class to contain results of fitting a linear mixed effects model.