Ranjan Maitra
2012-Nov-29 22:27 UTC
[R] instrumental variables regression using ivreg (AER) or tsls (sem)
Dear friends, I am trying to understand and implement instrumental variables regression using R. I found a small (simple) example here which purportedly illustrates the mechanics (using 2-stage least-squares): http://www.r-bloggers.com/a-simple-instrumental-variables-problem/ Basically, here are the R commands (reproducible example) from that site: # ------ begin R library(AER) library(lmtest) data("CollegeDistance") cd.d<-CollegeDistance simple.ed.1s<- lm(education ~ distance,data=cd.d) cd.d$ed.pred<- predict(simple.ed.1s) simple.ed.2s<- lm(wage ~ urban + gender + ethnicity + unemp + ed.pred , data=cd.d) # ------ end R This yields the following summary: summary(simple.ed.2s) Call: lm(formula = wage ~ urban + gender + ethnicity + unemp + ed.pred, data = cd.d) Residuals: Min 1Q Median 3Q Max -3.1692 -0.8294 0.1502 0.8482 3.9537 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -2.053604 1.675314 -1.226 0.2203 urbanyes -0.013588 0.046403 -0.293 0.7697 genderfemale -0.086700 0.036909 -2.349 0.0189 * ethnicityafam -0.566524 0.051686 -10.961 < 2e-16 *** ethnicityhispanic -0.529088 0.048429 -10.925 < 2e-16 *** unemp 0.145806 0.006969 20.922 < 2e-16 *** ed.pred 0.774340 0.120372 6.433 1.38e-10 *** --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Residual standard error: 1.263 on 4732 degrees of freedom Multiple R-squared: 0.1175, Adjusted R-squared: 0.1163 F-statistic: 105 on 6 and 4732 DF, p-value: < 2.2e-16 Question: Assuming that the above illustration is correct, I was wondering how I could mimic these calculations using the ivreg () in AER or tsls () in sem? Any suggestions? Many thanks in advance, and best wishes, Ranjan -- Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. For those needing to send personal or professional e-mail, please use appropriate addresses. ____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
Achim Zeileis
2012-Dec-02 09:32 UTC
[R] instrumental variables regression using ivreg (AER) or tsls (sem)
On Thu, 29 Nov 2012, Ranjan Maitra wrote:> Dear friends, > > I am trying to understand and implement instrumental variables > regression using R. > > I found a small (simple) example here which purportedly illustrates the > mechanics (using 2-stage least-squares): > > http://www.r-bloggers.com/a-simple-instrumental-variables-problem/The 1st stage regression is not quite correct in this example. My understanding is that he wants to use distance as an instrument for education and all other variables as instruments for themselves. However, the rest of the instruments have been erroneously left out of the 1st stage in his example.> Basically, here are the R commands (reproducible example) from that > site: > > # ------ begin R > > library(AER) > > library(lmtest) > > data("CollegeDistance") > > cd.d<-CollegeDistance > > simple.ed.1s<- lm(education ~ distance,data=cd.d)This should be: simple.ed.1s<- lm(education ~ urban + gender + ethnicity + unemp + distance, data=cd.d)> cd.d$ed.pred<- predict(simple.ed.1s) > > simple.ed.2s<- lm(wage ~ urban + gender + ethnicity + unemp + ed.pred , > data=cd.d)And then the same estimates could be obtained in ivreg() as: m <- ivreg(wage ~ urban + gender + ethnicity + unemp + education | urban + gender + ethnicity + unemp + distance, data = CollegeDistance) Or in tsls() as: m2 <- tsls(wage ~ urban + gender + ethnicity + unemp + education, ~ urban + gender + ethnicity + unemp + distance, data = CollegeDistance) Best, Z> # ------ end R > > > This yields the following summary: > > summary(simple.ed.2s) > > Call: > lm(formula = wage ~ urban + gender + ethnicity + unemp + ed.pred, > data = cd.d) > > Residuals: > Min 1Q Median 3Q Max > -3.1692 -0.8294 0.1502 0.8482 3.9537 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) -2.053604 1.675314 -1.226 0.2203 > urbanyes -0.013588 0.046403 -0.293 0.7697 > genderfemale -0.086700 0.036909 -2.349 0.0189 * > ethnicityafam -0.566524 0.051686 -10.961 < 2e-16 *** > ethnicityhispanic -0.529088 0.048429 -10.925 < 2e-16 *** > unemp 0.145806 0.006969 20.922 < 2e-16 *** > ed.pred 0.774340 0.120372 6.433 1.38e-10 *** > --- > Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 > > Residual standard error: 1.263 on 4732 degrees of freedom > Multiple R-squared: 0.1175, Adjusted R-squared: 0.1163 > F-statistic: 105 on 6 and 4732 DF, p-value: < 2.2e-16 > > > > Question: Assuming that the above illustration is correct, I was > wondering how I could mimic these calculations using the ivreg () in > AER or tsls () in sem? > > Any suggestions? > > Many thanks in advance, and best wishes, > Ranjan > > -- > Important Notice: This mailbox is ignored: e-mails are set to be > deleted on receipt. For those needing to send personal or professional > e-mail, please use appropriate addresses. > > ____________________________________________________________ > FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! > Visit http://www.inbox.com/photosharing to find out more! > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.