Search code examples
pythonregressionstatsmodelspatsy

Clustered standard errors in statsmodels with categorical variables (Python)


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?


Solution

  • 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.