1

I have a cross-sectional dataset and am doing some simple econometric exercises with it using the statsmodels package in Python. At a certain point I tried to run a fixed effects regression model using the mixedlm function:

y, X = dmatrices("target ~ amt_income_total + amt_credit + name_housing_type", data=data) md = sm.MixedLM(y, X, groups=data["name_income_type"]) mdf = md.fit(method=["lbfgs"]) mdf.summary() 

So far so good. The code block above runs fine. But if I try to cluster my standard errors, something that would work fine with the OLS model:

mdf = md.fit(method=["lbfgs"],cov_type="cluster",cov_kwds={'groups': data["name_income_type"]}) 

I get the following error:

AttributeError Traceback (most recent call last) Cell In[84], line 3 1 md = sm.MixedLM(y, X, groups=data["name_income_type"]) 2 mdf = md.fit(method=["lbfgs"]) ----> 3 mdf = md.fit(method=["lbfgs"],cov_type="cluster",cov_kwds={'groups': data["name_income_type"]}) 4 mdf.summary() File /opt/anaconda3/lib/python3.11/site-packages/statsmodels/regression/mixed_linear_model.py:2192, in MixedLM.fit(self, start_params, reml, niter_sa, do_cg, fe_pen, cov_pen, free, full_output, method, **fit_kwargs) 2190 # Try optimizing one or more times 2191 for j in range(len(method)): -> 2192 rslt = super(MixedLM, self).fit(start_params=packed, 2193 skip_hessian=True, 2194 method=method[j], 2195 **fit_kwargs) 2196 if rslt.mle_retvals['converged']: 2197 break File /opt/anaconda3/lib/python3.11/site-packages/statsmodels/base/model.py:600, in LikelihoodModel.fit(self, start_params, method, maxiter, full_output, disp, fargs, callback, retall, skip_hessian, **kwargs) 597 Hinv = None 599 # TODO: add Hessian approximation and change the above if needed --> 600 mlefit = LikelihoodModelResults(self, xopt, Hinv, scale=1., **kwds) 602 # TODO: hardcode scale? 603 mlefit.mle_retvals = retvals ... --> 242 xu = results.model.wexog * results.wresid[:, None] 244 hessian_inv = np.asarray(results.normalized_cov_params) 246 # experimental support for freq_weights AttributeError: 'MixedLM' object has no attribute 'wexog' 

Is it possible at all to compute these types of standard errors for a fixed effects model out of the box with statsmodels or another mainline data science package for Python?

These types of regressions are very standard in the economics literature and strikes me odd that there would be no way to do it without hardcoding those.

NB: I understand I can just get the fixed effects by demeaning the relevant variables and then obtaining the clustered standard errors with a OLS object, but I just want to make sure there is no automated way to do it.

4
  • MixedLM is a random effects model and not a fixed effects model. OLS with fixed effects in the design matrix estimates a fixed effects model. (I never looked at whether or how cluster robust standard errors could be computed for random effects models like MixedLM. (Note: GEE defaults to cluster robust standard errors.) Commented Jan 9 at 22:16
  • @Josef Thank you for the clarification. I understand that MixedLM is a random effects model, but if I just use random intercepts (and not random slopes) does it not reduce to a fixed effects model? Am I missing something? Commented Jan 10 at 10:41
  • No, random (mixed) intercepts are still shrunk or biased towards the common or prior distribution similar to penalizing them to a common mean. Fixed effects are like standard OLS coefficients and are treated for each groups as separate coefficients without any direct link between them. (covariance of parameter estimates only comes from the estimation itself without a prior (or common) distribution effect.) Commented Jan 12 at 22:29
  • @Josef I see, thank you! Commented Jan 13 at 19:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.