You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Adding a wild cluster bootstrap cov_type to models #8437
Hi! @s3alfisc and I are working on implementing wild cluster bootstrapping into python and we wanted to add it as a pull request to statsmodels. It's still a work in progress, but is there a straightforward way of adding a new cov_type?
Would you be able to tell me which files to look at and which classes to inherit to implement?
I can see that there's a cov_type file in base but not sure if adding to that is enough
That would be great
For which model do you want to add the new cov_type?
some details:
If you use bootstrapping, then it will not go through the sandwich code.
The selection and dispatch of robust cov_type methods is in get_robustcov_results
Those are not fully standardized and there is still some code duplication that never got cleaned up
For OLS and other linear model, the methods is in class statsmodels.regression.linear_model.RegressionResults
Most other models (at least outside tsa), like discrete, GLM, use the generic implementation from base, base.model.LikelihoodModelResults.__init__ calls function statsmodels.base.covtype.get_robustcov_results
I just see that GLMResults.__init__ also explicitly calls that function.
(I guess GLMResults does this because super is called at the beginning, but there is some processing before being able to create the final robust cov_params.)
If there are model specific cov_types, then they may be included in the fit method,
for example GLM has adjustments for OIM versus EIM and for excess dispersion, scale different from one in e.g. quasi-Poisson.
The aim is to apply it to linear models, so OLS and the like. I'm not actually sure about the properties in adding it to likelihood models or non-linear estimators.
For OLS and other linear model, the methods is in class statsmodels.regression.linear_model.RegressionResults
I think I can add it here and provisionally to base.get_robustcov_results and see what happens 💥
Write most code outside the classes as in base.covtype so it can be generalized or reused later, and then just add it to
statsmodels.regression.linear_model.RegressionResults.get_robustcov_results
For example if cov_type in ['fixed scale', 'fixed_scale']:
was added specifically and only to the linear Regression models.
Implementing it only for the linear models will be easier than generically or for other models.
Recreating a model with different options or data is more fragile in other models because of many different extra keywords used in the XXXModel __init__ across models
Indeed, the exogenous variables are fixed in the wild bootstrap. We decided to implement the wild bootstrap instead of a block bootstrap because a) it seems to perform really well in a range of edge cases (e.g. few clusters, wildly different cluster sizes, few treated clusters etc) and b) because it can be implemented in really performant ways.
The simulation evidence that it works well is (to the best of my knowledge) so far restricted to linear models estimated by OLS - some of the main researchers in the literature do not consider it a good idea to wild (cluster) bootstrap nonlinear models (see e.g. this reply by @droodmanhere or these slides by MacKinnon, which form a very good introduction to the wild bootstrap.
We plan to implement a range of new versions of the wild bootstrap, as discussed in the paper by MacKinnon, Nielsen & Webb (2022), which seem to perform even better than the "classical" wild cluster bootstrap as introduced in Cameron et al. As a side product, we might also implement a very fast jackknifed based estimator for CRV3 covariance matrices as described in in yet another paper by MacKinnon, Nielsen and Webb =)
Thanks for the references.
I will try to catch up with those after the upcoming release. I have not looked at this literature since I implemented the cluster robust sandwiches.
AFAIR, I have seen only bootstrap with resampling clusters for nonlinear, nongaussian or asymmetric models, GLM, Poisson,...
Aside to the model under null constraints:
GLM has fit_constrained by reparameterizing the design matrix. However that is not implemented for OLS, WLS. At the time I implemented fit_constrained, I skipped OLS mainly because of the missing offset option in the linear regression models.
mainly as reference for myself: #2054 seems to be the last time I looked at this
We should have cluster robust score or lagrange multiplier test which would be the asymptotic competitor to the bootstrap version.
However, I never tried it out for cluster robust (AFAIR)
Activity
[-]Adding a new `cov_type` to models[/-][+]Adding a wild cluster bootstrap `cov_type` to models[/+]bashtage commentedon Oct 12, 2022
Look in the linear regression code to see how the covariance estimators are defined. It isn't that clean IIRC.
josef-pkt commentedon Oct 12, 2022
That would be great
For which model do you want to add the new cov_type?
some details:
If you use bootstrapping, then it will not go through the sandwich code.
The selection and dispatch of robust cov_type methods is in
get_robustcov_resultsThose are not fully standardized and there is still some code duplication that never got cleaned up
For OLS and other linear model, the methods is in class statsmodels.regression.linear_model.RegressionResults
Most other models (at least outside tsa), like discrete, GLM, use the generic implementation from
base,base.model.LikelihoodModelResults.__init__calls functionstatsmodels.base.covtype.get_robustcov_resultsI just see that
GLMResults.__init__also explicitly calls that function.(I guess GLMResults does this because super is called at the beginning, but there is some processing before being able to create the final robust cov_params.)
If there are model specific cov_types, then they may be included in the
fitmethod,for example GLM has adjustments for OIM versus EIM and for excess dispersion, scale different from one in e.g. quasi-Poisson.
amichuda commentedon Oct 12, 2022
Great! We're planning on adding it to
linearmodelsas well ;)A cursory glance tells me that if we just add to
get_robustcov_resultsinbasethat should propagate?statsmodels/statsmodels/base/covtype.py
Line 67 in 55aff1e
The aim is to apply it to linear models, so OLS and the like. I'm not actually sure about the properties in adding it to likelihood models or non-linear estimators.
I think I can add it here and provisionally to
base.get_robustcov_resultsand see what happens 💥josef-pkt commentedon Oct 12, 2022
Write most code outside the classes as in base.covtype so it can be generalized or reused later, and then just add it to
statsmodels.regression.linear_model.RegressionResults.get_robustcov_results
For example
if cov_type in ['fixed scale', 'fixed_scale']:was added specifically and only to the linear Regression models.
Implementing it only for the linear models will be easier than generically or for other models.
Recreating a model with different options or data is more fragile in other models because of many different extra keywords used in the XXXModel
__init__across modelsjosef-pkt commentedon Oct 12, 2022
quick question
The wild clusterbootstrap is a residual bootstrap with fixed exog, or do you also randomize over which clusters are included?
s3alfisc commentedon Oct 13, 2022
Hi @josef-pkt and @bashtage , thanks so much for your feedback!
Indeed, the exogenous variables are fixed in the wild bootstrap. We decided to implement the wild bootstrap instead of a block bootstrap because a) it seems to perform really well in a range of edge cases (e.g. few clusters, wildly different cluster sizes, few treated clusters etc) and b) because it can be implemented in really performant ways.
The simulation evidence that it works well is (to the best of my knowledge) so far restricted to linear models estimated by OLS - some of the main researchers in the literature do not consider it a good idea to wild (cluster) bootstrap nonlinear models (see e.g. this reply by @droodman here or these slides by MacKinnon, which form a very good introduction to the wild bootstrap.
We plan to implement a range of new versions of the wild bootstrap, as discussed in the paper by MacKinnon, Nielsen & Webb (2022), which seem to perform even better than the "classical" wild cluster bootstrap as introduced in Cameron et al. As a side product, we might also implement a very fast jackknifed based estimator for CRV3 covariance matrices as described in in yet another paper by MacKinnon, Nielsen and Webb =)
josef-pkt commentedon Oct 13, 2022
Great,, I'm looking forward to the PR.
Thanks for the references.
I will try to catch up with those after the upcoming release. I have not looked at this literature since I implemented the cluster robust sandwiches.
AFAIR, I have seen only bootstrap with resampling clusters for nonlinear, nongaussian or asymmetric models, GLM, Poisson,...
Aside to the model under null constraints:
GLM has fit_constrained by reparameterizing the design matrix. However that is not implemented for OLS, WLS. At the time I implemented fit_constrained, I skipped OLS mainly because of the missing
offsetoption in the linear regression models.josef-pkt commentedon Oct 13, 2022
mainly as reference for myself: #2054 seems to be the last time I looked at this
We should have cluster robust score or lagrange multiplier test which would be the asymptotic competitor to the bootstrap version.
However, I never tried it out for cluster robust (AFAIR)