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. Two specific mixed effects models are “random intercepts models”, where all responses in a single group are additively shifted by a value that is specific to the group, and “random slopes models”, where the values follow a mean trajectory that is linear in observed covariates, with both the slopes and intercept being specific to the group. The Statsmodels MixedLM implementation allows arbitrary random effects design matrices to be specified for the groups, so these and other types of random effects models can all be fit.
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. Some limitations of the current implementation are that it does not support structure more complex on the residual errors (they are always homoscedastic), and it does not support crossed random effects. We hope to implement these features for the next release.
import statsmodels.api as sm import statsmodels.formula.api as smf
data = sm.datasets.get_rdataset(“dietox”, “geepack”).data
md = smf.mixedlm(“Weight ~ Time”, data, groups=data[“Pig”]) mdf = md.fit() print(mdf.summary())
Detailed examples can be found here
There some notebook examples on the Wiki: Wiki notebooks for MixedLM
The data are partitioned into disjoint groups. The probability model for group i is:
Y = X*beta + Z*gamma + epsilon
Y, X and Z must be entirely observed. beta, Psi, and sigma^2 are estimated using ML or REML estimation, and gamma and epsilon are random so define the probability model.
The mean structure is E[Y|X,Z] = X*beta. If only the mean structure is of interest, GEE is a good alternative to mixed models.
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:
1. Three different parameterizations are used here in different places. The regression slopes (usually called fe_params) are identical in all three parameterizations, but the variance parameters differ. The parameterizations are:
All three parameterizations can be “packed” by concatenating fe_params together with the lower triangle of the dependence structure. Note that when unpacking, it is important to either square or reflect the dependence structure depending on which parameterization is being used.
2. The situation where the random effects covariance matrix is singular is numerically challenging. Small changes in the covariance parameters may lead to large changes in the likelihood and derivatives.
3. The optimization strategy is to optionally perform a few EM steps, followed by optionally performing a few steepest descent steps, followed by conjugate gradient descent using one of the scipy gradient optimizers. The EM and steepest descent steps are used to get adequate starting values for the conjugate gradient optimization, which is much faster.