Python Statsmodels Mixedlm (Mixed Linear Model) random effects

前端 未结 2 1695
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 18:14

I am a bit confused about the output of Statsmodels Mixedlm and am hoping someone could explain.

I have a large dataset of single family homes, including the previo

相关标签:
2条回答
  • 2021-01-02 19:04

    In addition to North Laines answer, do note that in statsmodels-0.11.1 calling

    mdf.random_effects
    

    gives the differences between the group and the general model coefficients

    0 讨论(0)
  • 2021-01-02 19:18

    I'm currently trying to get my head around random effects in MixedLM aswell. Looking at the docs, it seems as though using just the groups parameter, without exog_re or re_formula will simply add a random intercept to each group. An example from the docs:

    # 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()
    

    As such, you would expect the random_effects method to return the city's intercepts in this case, not the coefficients/slopes.

    To add a random slope with respect to one of your other features, you can do something similar to this example from statsmodels' Jupyter tutorial, either with a slope and an intercept:

    model = sm.MixedLM.from_formula(
        "Y ~ X", data, re_formula="X", groups=data["C"])
    

    or with only the slope:

    model = sm.MixedLM.from_formula(
        "Y ~ X", data, re_formula="0 + X", groups=data["C"])
    

    Looking at the docs for random_effects, it says that it returns the mean for each groups's random effects. However, as the random effects are only due to the intercept, this should just be equal to the intercept itself.

    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.
    

    Some useful resources to look further at include:

    • Docs for the formula version of MixedML
    • Docs for the results of MixedML
    • This Jupyter notebook with examples for using MixedML (Python)
    • Stanford tutorial on mixed models (R)
    • Tutorial on fixed and random effects (R)
    0 讨论(0)
提交回复
热议问题