在 Python 中按组绘制混合效应模型结果 [英] Plot mixed effect model results by group in Python
问题描述
我在 python 中使用带有 statsmodel 的混合效应模型,但找不到按组级别绘制结果的方法.我的目标是达到这种情节:
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:
这是 可能在 R.
作为参考,在我的数据帧 df
中,我有三列 = metric
、experiment_name
、country
.
For reference, in my dataframe df
I have three columns = metric
, experiment_name
, country
.
这是我正在使用的代码
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.
推荐答案
我找到了一种在组级别获得结果的方法,至少对于随机效应是这样.
I found a way to get the results at group level, at least for the random effects.
mdf.random_effects
并在转换数据后用 matplotlib 绘制 棒棒糖图到数据帧并转置.
and to plot it a lollipop plot with matplotlib after converting the data to a dataframe and transposing.
这篇关于在 Python 中按组绘制混合效应模型结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!