Instrumental Variables Estimation and Two Stage Least Squares (Chapter 15)

Comments will follow

setwd('...')

library('foreign')
library('AER') # install.packages('AER') # Package for estimating IV models

rm(list=ls())

# Data we will use
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/mroz.dta','mroz.dta',mode="wb")
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/wage2.dta','wage2.dta',mode="wb")
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/bwght.dta','bwght.dta',mode="wb")
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/card.dta','card.dta',mode="wb")
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/fertil1.dta','fertil1.dta',mode="wb")
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/jtrain.dta','jtrain.dta',mode="wb")

# 15.1
mroz<-read.dta('mroz.dta')

# OLS
s.lm.1<-summary(lm(lwage ~ educ, data=mroz))
s.lm.1

# fatheduc as IV for educ
s.lm.2<-summary(lm(educ ~ fatheduc, data=mroz[mroz$inlf!=0,]))
s.lm.2

# IV estimation
s.iv.1<-summary(ivreg(lwage ~ educ | fatheduc,
                      data=mroz))
s.iv.1

# 15.2
wage2<-read.dta('wage2.dta')

s.lm.1<-summary(lm(educ ~ sibs, data=wage2))
s.lm.1

s.iv.1<-summary(ivreg(lwage ~ educ | sibs,
                      data=wage2))
s.iv.1

# 15.3
bwght<-read.dta('bwght.dta')

s.lm.1<-summary(lm(packs ~ cigprice, data=bwght))
s.lm.1

s.iv.1<-summary(ivreg(lbwght ~ packs | cigprice,
                data=bwght))
s.iv.1

# 15.4
card<-read.dta('card.dta')

s.lm.1<-summary(lm(educ ~ nearc4 + exper + expersq + black + smsa + south +
                     smsa66 + reg662 + reg663 + reg664 + reg665 + reg666 + reg667 +
                     reg668 + reg669,
                   data=card))
s.lm.1

s.lm.2<-summary(lm.2<-lm(lwage ~ educ + exper + expersq + black + smsa + south +
                           smsa66 + reg662 + reg663 + reg664 + reg665 + reg666 + reg667 +
                           reg668 + reg669,
                         data=card))
s.lm.2

s.iv.1<-summary(ivreg(lwage ~ educ + exper + expersq + black + smsa + south +
                        smsa66 + reg662 + reg663 + reg664 + reg665 + reg666 + reg667 +
                        reg668 + reg669 | nearc4 + exper + expersq + black + smsa + south +
                        smsa66 + reg662 + reg663 + reg664 + reg665 + reg666 + reg667 +
                        reg668 + reg669,
                      data=card))
s.iv.1

# 15.5
mroz<-read.dta('mroz.dta')

s.lm.1<-summary(lm.1<-lm(lwage ~ educ + exper + expersq, data=mroz))
s.lm.1

s.lm.2a<-summary(lm.2a<-lm(educ ~ exper + expersq + motheduc + fatheduc,
                           data=mroz[mroz$inlf>0,]))
s.lm.2b<-summary(lm.2b<-lm(educ ~ exper + expersq,data=mroz[mroz$inlf>0,]))
anova(lm.2a,lm.2b)

s.iv.1<-summary(ivreg(lwage ~ educ + exper + expersq | exper +
                        expersq + motheduc + fatheduc, data=mroz))
s.iv.1

# 15.6
wage2<-read.dta('wage2.dta')

s.iv.1<-summary(ivreg(lwage ~ educ + exper + tenure + married + south + urban + black +
                        IQ | KWW + educ + exper + tenure + married + south + urban + black,
                      data=wage2))
s.iv.1

# 15.7
mroz<-read.dta('mroz.dta')

s.lm.1<-summary(lm.1<-lm(lwage ~ educ + exper + expersq, data=mroz))
s.lm.1

s.lm.2<-summary(lm.2<-lm(educ ~ exper + expersq + motheduc + fatheduc,
                           data=mroz[mroz$inlf!=0,]))

s.lm.3<-summary(lm.3<-lm(lwage ~ educ + exper + expersq + lm.2$resid,
                         data=mroz[mroz$inlf!=0,]))

s.lm.3

# 15.8
mroz<-read.dta('mroz.dta')

s.iv.1<-summary(iv.1<-ivreg(lwage ~ educ + exper + expersq | exper +
                        expersq + motheduc + fatheduc, data=mroz[mroz$inlf!=0,]))
s.lm.1<-summary(lm(iv.1$resid ~ exper + expersq + motheduc + fatheduc,
                   data=mroz[mroz$inlf!=0,]))
s.lm.1
428*.0009

s.iv.2<-summary(iv.2<-ivreg(lwage ~ educ + exper + expersq | exper +
                              expersq + motheduc + fatheduc + huseduc, data=mroz[mroz$inlf!=0,]))
s.lm.2<-summary(lm(iv.2$resid ~ exper + expersq + motheduc + fatheduc + huseduc,
                   data=mroz[mroz$inlf!=0,]))
s.lm.2
428*.0026

s.iv.2

# 15.9
fertil1<-read.dta('fertil1.dta')

s.iv.1<-summary(iv.1<-ivreg(kids ~ educ + age + agesq + black + east + northcen +
                              west + farm + othrural + town + smcity + y74 + y76 +
                              y78 + y80 + y82 + y84 |
                              meduc + feduc + age + agesq + black + east +
                              northcen + west + farm + othrural + town + smcity +
                              y74 + y76 + y78 + y80 + y82 + y84,
                            data=fertil1))
s.iv.1

lm.r<-lm(educ ~ meduc + feduc + age + agesq + black + east + northcen +
            west + farm + othrural + town + smcity + y74 + y76 + y78 + y80 + y82 + y84,
          data=fertil1)

lm.1<-lm(kids ~ educ + age + agesq + black + east + northcen + west + farm +
           othrural + town + smcity + y74 + y76 + y78 + y80 + y82 + y84 + lm.r$resid,
         data=fertil1)
summary(lm.1)

# 15.10
jtrain<-read.dta('jtrain.dta')

d.lscrap<-with(jtrain,lscrap[year==1988]-lscrap[year==1987])
d.hrsemp<-with(jtrain,hrsemp[year==1988]-hrsemp[year==1987])
d.grant<-with(jtrain,grant[year==1988]-grant[year==1987])

s.iv.1<-summary(ivreg(d.lscrap ~ d.hrsemp | d.grant))
s.iv.1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s