How to do 2SLS IV regression using statsmodels python?
Asked Answered
F

2

8

I'm trying to do 2 stage least squares regression in python using the statsmodels library:

from statsmodels.sandbox.regression.gmm import IV2SLS
                 
resultIV = IV2SLS(dietdummy['Log Income'],
                  dietdummy.drop(['Log Income', 'Diabetes']),
                  dietdummy.drop(['Log Income', 'Reads Nutri')

Reads Nutri is my endogenous variable my instrument is Diabetes and my dependent variable is Log Income.

Did I do this right? It is much different than the way I would do it on Stata.

Also, when I do resultIV.summary(), I get a TypeError (something to do with the F statistic being nonetype). How can I resolve this?

Foe answered 3/5, 2016 at 18:48 Comment(1)
Is the model meant to know that because ReadsNutri isnt in the instruments it is the endogenous variable? When I do as you have done for statsmodels I get weird resultsAssumption
S
9

I found this question when I wanted to do an IV2SLS regression myself and had the same problem. So, just for everybody else who landed here.

The documentation of statsmodels shows how to use this command. Your arguments are endog, exog, and instrumentin that order where exog includes variables which are instrumented and instrument the instruments and other control variables. In that sense, your model is fine.

The TypeError you found is currently an open bug in versions 0.6.0 and 0.8.1. and will be fixed in 0.9.0 according to the milestone.

Update (28.06.2018): Version 9.0.0 was released on 15 May and should include a fix for the aforementioned bug.

Serene answered 24/3, 2017 at 13:42 Comment(0)
A
1

Personally, I found the IV2SLS function in linearmodels 4.5 to be more intuitive than the statsmodels version, as it has separate parameters for the dependent variable and the endogenous variable(s), whereas the statsmodels version doesn't. The results I got from the linearmodels function lined up with what I would get with an Excel add-in I got through school.

If you choose to use the linearmodels function, this guide should also help. For instance, it showed me that I needed to add in a constant for my function to produce the correct output.

Ariosto answered 28/3, 2021 at 21:19 Comment(1)
I guess the link should be bashtage.github.io/linearmodels/iv/iv/…, no? Since you're talking about linearmodelsHeterozygote

© 2022 - 2024 — McMap. All rights reserved.