Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Statsmodels Mixedlm (Mixed Linear Model) random effects

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 previous two sale prices/sale dates for each property. I have geocoded this entire dataset and fetched the elevation for each property. I am trying to understand the way in which the relationship between elevation and property price appreciation varies between different cities.

I have used statsmodels mixed linear model to regress price appreciation on elevation, holding a number of other factors constant, with cities as my groups category.

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

Entering mdf.random_effects returns a list of coefficients. Can I interpret this list as, essentially, the slope for each individual city (i.e., the individual regression coefficient relating Elevation to sale price appreciation)? Or are these results the intercepts for each City?

like image 471
Tommy Shay Avatar asked Nov 27 '17 00:11

Tommy Shay


People also ask

What is a random effect in a mixed model?

If it is clear that the researcher is interested in comparing specific, chosen levels of treatment, that treatment is called a fixed effect. On the other hand, if the levels of the treatment are a sample of a larger population of possible levels, then the treatment is called a random effect.

Does GLM have random effects?

GLMM is a further extension of GLMs that permits random effects as well as fixed effects in the linear predictor.

What is fixed and random effect in mixed model?

The fixed-effects model assumes that the individual-specific effect is correlated to the independent variable. The random-effects model allows making inferences on the population data based on the assumption of normal distribution.

What is the difference between LMER and Glmer?

The lmer() function is for linear mixed models and the glmer() function is for generalized mixed models.


2 Answers

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)
like image 116
North Laine Avatar answered Sep 24 '22 10:09

North Laine


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

like image 33
Daniel Wyatt Avatar answered Sep 22 '22 10:09

Daniel Wyatt