【问题标题】:Python Statsmodels Mixedlm (Mixed Linear Model) random effectsPython Statsmodels Mixedlm(混合线性模型)随机效应
【发布时间】:2018-05-10 04:30:38
【问题描述】:

我对 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 返回一个系数列表。我可以将此列表解释为每个城市的斜率(即,将高程与销售价格升值相关的个体回归系数)吗?或者这些结果是每个城市的截距?

【问题讨论】:

    标签: python statsmodels mixed-models random-effects


    【解决方案1】:

    我目前也在尝试了解 MixedLM 中的随机效果。查看the docs,似乎只使用groups 参数,不使用exog_rere_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 Jupyter 笔记本,包含使用 MixedML (Python) 的示例
    • Stanford tutorial 混合模型 (R)
    • Tutorial 关于固定和随机效应 (R)

    【讨论】:

      【解决方案2】:

      除了 North Laines 的回答,请注意在 statsmodels-0.11.1 调用中

      mdf.random_effects
      

      给出组和一般模型系数之间的差异

      【讨论】:

      • 我正在查看link 源代码,mdf.random_effects 应该返回“将不同的group 值映射到给定数据的组的随机效应的条件均值的字典。 "但我不太清楚条件手段是如何工作的。知道如何在给定随机变量的情况下计算单个截距和斜率吗?为什么会有区别?
      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 2017-04-03
      • 2018-07-26
      • 2014-12-25
      • 1970-01-01
      相关资源
      最近更新 更多