我目前正在尝试了解 MixedLM 中的随机效果。看着the docs,似乎只使用groups
参数,无exog_re
or re_formula
将简单地为每个组添加一个随机截距。文档中的一个示例:
# A basic mixed model with fixed effects for the columns of exog and a random intercept for each distinct value of group:
model = sm.MixedLM(endog, exog, groups)
result = model.fit()
因此,您会期望random_effects
在这种情况下返回城市的截距的方法,而不是系数/斜率。
要添加相对于其他特征之一的随机斜率,您可以执行与 statsmodels 的 Jupyter 教程中的示例类似的操作,使用斜率和截距:
model = sm.MixedLM.from_formula(
"Y ~ X", data, re_formula="X", groups=data["C"])
或仅使用斜率:
model = sm.MixedLM.from_formula(
"Y ~ X", data, re_formula="0 + X", groups=data["C"])
查看文档random_effects
,它表示它返回每个组的随机效应的平均值。然而,由于随机效应仅由截距引起,因此这应该等于截距本身。
MixedLMResults.random_effects()[source]
The conditional means of random effects given the data.
Returns:
random_effects : dict
A dictionary mapping the distinct group values to the means of the random effects for the group.
一些值得进一步研究的有用资源包括:
-
Docs对于 MixedLM 的公式版本
-
Docs对于 MixedLM 的结果
-
ThisJupyter 笔记本,包含使用 MixedLM (Python) 的示例
-
斯坦福教程混合模型 (R)
-
Tutorial关于固定效应和随机效应 (R)