Clustered standard errors in statsmodels with categorical variables (Python)

让人想犯罪 __ 提交于 2021-01-24 07:25:23

问题


I want to run a regression in statsmodels that uses categorical variables and clustered standard errors.

I have a dataset with columns institution, treatment, year, and enrollment. Treatment is a dummy, institution is a string, and the others are numbers. I've made sure to drop any null values.

df.dropna()    
reg_model = smf.ols("enroll ~ treatment + C(year) + C(institution)", df)
.fit(cov_type='cluster', cov_kwds={'groups': df['institution']})

I'm getting the following:

ValueError: The weights and list don't have the same length.

Is there a way to fix this so my standard errors cluster?


回答1:


You need cov_type='cluster' in fit.

cov_type is a keyword argument and not in the correct position when keywords are used as positional arguments. http://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.OLS.fit.html

In general, statsmodels does not guarantee backwards compatibility when keyword arguments are used as positional arguments, that is keyword positions might change in future versions.

However, I don't understand where the ValueError is coming from. Python has very informative tracebacks, and it is very useful when asking questions to add either the full traceback or at least the last few lines that show where the exception is raised.



来源:https://stackoverflow.com/questions/54349525/clustered-standard-errors-in-statsmodels-with-categorical-variables-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!