admin管理员组

文章数量:1123947

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.

本文标签: