Linear mixed effect model in statsmodel package

by user12077723   Last Updated August 01, 2020 14:19 PM

I try to use linear mixed effect model in Python statsmodels package. However, I have no idea how to conduct and interpret the result.

Group 1 (20 people) : base line & follow up

Group 2 (20 people) : base line & follow up

Two groups went through an experiment and neuroimaging was done before(baseline) & after(follow up) the experiment. Each group consists of about 20 people.

  • If I want to regress out the effects of covariates (3 distinct covariates are expected to be used as fixed factors) from the neuroimaging data, then linear mixed effects model is appropriate approach? If it is not, then how can I use linear mixed effects model in my dataset? (I am confused with the purpose of it)
  • Then, what would be the example code?
  • And how should I interpret the result to get values that I want?

Answers 1

The purpose of a mixed effects model is to incorporate fixed effects, which are typically variables that are predictors that you have interest in, variables that you control in an experiment, and confounders; and random effects which are variables such as grouping factors for which there are repeated measures.

In your case you have participants that are measured twice and you are interested in the change from baseline and whether this differs by treatment group. This is a common use case for mixed effects models, because it avoids the pitfalls of regressing change on baseline which causes bias due to mathematical coupling, and ANCOVA which can be biased when participants are not randomised into groups (or where the randomisation fails).

The statsmodels package in python can fit such a model

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 =
Robert Long
Robert Long
August 01, 2020 13:38 PM

Related Questions

Path analysis vs linear model

Updated February 28, 2018 23:19 PM

How to deal with autocorrelation in mixed models

Updated July 06, 2020 20:19 PM

linear combination and univariate normal

Updated October 14, 2019 05:19 AM

Distribution of a quadratic form

Updated October 14, 2019 16:19 PM