Search code examples
pythonstatsmodels

'IV2SLS' object has no attribute 'pinv_wexog'


I import IV2SLS from statsmodels.sandbox.regression.gmm,

but it failed to do heteroscedasticity robust covariance.

from statsmodels.sandbox.regression.gmm import IV2SLS

endog = cig_1995['lpackpc']
exog = cig_1995[['constant', 'lravgprs']]
instrument = cig_1995[['constant', 'rtaxso']]
results = IV2SLS(endog=endog,exog=exog,instrument=instrument).fit()
results = results.get_robustcov_results(cov_type='HC1')

AttributeError: 'IV2SLS' object has no attribute 'pinv_wexog'

Solution

  • You can use the linearmodels package which contains a working and tested implementation of IV2SLS. The syntax is nearly the same as that incomplete version in statsmodels. The only difference is how you specify the covariance estimator, which is part of fit.

    from linearmodels import IV2SLS
    
    endog = cig_1995['lpackpc']
    exog = cig_1995[['constant', 'lravgprs']]
    instrument = cig_1995[['constant', 'rtaxso']]
    results = IV2SLS(endog=endog,exog=exog,instrument=instrument).fit(cov_type="robust")