我对 Statsmodels Mixedlm 的输出有点困惑,希望有人能解释一下。
我有一个大型单户住宅数据集,包括每处特性的前两个销售价格/销售日期。我已经对整个数据集进行了地理编码并获取了每个属性的高程。我试图了解海拔高度与房地产价格升值之间的关系在不同城市之间的变化方式。
我使用 statsmodels 混合线性模型来回归海拔高度的价格升值,保持许多其他因素不变,城市作为我的组类别。
md = smf.mixedlm('price_relative_ind~Elevation+YearBuilt+Sale_Amount_1+LivingSqFt',data=Miami_SF,groups=Miami_SF['City'])
mdf = md.fit()
mdf.random_effects
输入 mdf.random_effects 返回系数列表。我能否将此列表本质上解释为每个城市的斜率(即,将海拔与销售价格升值相关的个体回归系数)?或者这些结果是每个城市的截距?
最佳答案
我目前也在努力研究 MixedLM 中的随机效应。看着the docs ,似乎只使用 groups
参数,而不使用 exog_re
或 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对于 MixedML 的公式版本
- Docs MixedML的结果
- This带有使用 MixedML (Python) 示例的 Jupyter 笔记本
- Stanford tutorial混合模型 (R)
- Tutorial关于固定和随机效应 (R)
关于Python Statsmodels Mixedlm(混合线性模型)随机效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47502398/