Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot mixed effect model results by group in Python

I'm using a Mixed effect model with statsmodel in python and I cannot find a way to plot the results by group level. My goal is to get to this kind of plot:

enter image description here

which is possible in R.

For reference, in my dataframe df I have three columns = metric, experiment_name, country.

This is the code I'm using

import statsmodels.api as sm
import statsmodels.formula.api as smf

formula = "metric ~ experiment_name"

md = smf.mixedlm(formula, df, groups=df["country"])
mdf = md.fit()
print(mdf.summary())

The main problem is that I cannot figure out how to get the group level results. I have googled quite a lot with no luck.

like image 418
Barbara Avatar asked Jul 04 '26 05:07

Barbara


1 Answers

I found a way to get the results at group level, at least for the random effects.

mdf.random_effects

and to plot it a lollipop plot with matplotlib after converting the data to a dataframe and transposing.

like image 174
Barbara Avatar answered Jul 05 '26 20:07

Barbara