#Nathaniel Mark #Intro to Econometrics #Spring 2017 #Recitation Eight - IV Regression #Packages install.packages("lmtest") library(lmtest) install.packages("sandwich") library(sandwich) install.packages("ggplot2") library(ggplot2) install.packages("systemfit") library(systemfit) #Generating data: #We observe n yearly markets for wool socks #We want to estimate the supply curve: #Question: How many more wool socks will the market produce if price goes up by 1 dollar? n<-500 WinterAveFarenheit<-rnorm(n,35,5) WinterMinFarenheit<-rnorm(n,WinterAveFarenheit-15,7) PWool<-rnorm(n,1,.25) demshifters<-rnorm(n,0,2000) supshifters<-rnorm(n,0,2000) #Price: P<-(25000-100*WinterAveFarenheit-20*WinterMinFarenheit+demshifters-supshifters+100*PWool)/4000 #True Curves: QS<-2000*P-100*PWool+supshifters QD<-25000-100*WinterAveFarenheit-20*WinterMinFarenheit-2000*P+demshifters #Looking at the data: #Data: plot(QS,P) #True Supply Curve (holding PWool constant): lines(2000*seq(from=4,to=7,length.out=n),seq(from=4,to=7,length.out=n)) #OLS ESTIMATION OLSSupplyW<-lm(QS~P+PWool) summary(OLSSupplyW,vcov.=vcovHC(OLSSupplyW,type="HC0")) #Now, we think we have a way to get rid of the simultaneous causality bias: IV #We have data on average temperature in the different markets #IS THIS A VALID IV? #To be valid, it must satisfy the two conditions: Relevance and Exogeneity #Relevance: Cov(Z,X) not equal to 0? Plot and calculate, or run F-test: #Plots qplot(WinterAveFarenheit,P) cor(WinterAveFarenheit,P) #F-test #Using Model Without PWool: #METHOD ONE: MANUALLY FirstStage1<-lm(P~WinterAveFarenheit) summary(FirstStage1) linearHypothesis(FirstStage1,c("WinterAveFarenheit=0"),test="F",vcov.=vcovHC(FirstStage1,type="HC0")) #F>10, so we say this is not a weak instrument. #METHOD TWO: USING DIAGNOSTICS OPTION JIIVSupply<-ivreg(QS~P|WinterAveFarenheit) summary(JIIVSupply,vcov.=vcovHC(JIIVSupply,type="HC0"),diagnostics=TRUE) #Using Model With PWool: FirstStage2<-lm(P~WinterAveFarenheit+PWool) linearHypothesis(FirstStage2,c("WinterAveFarenheit=0"),test="F",vcov.=vcovHC(FirstStage2,type="HC0")) #F>10, so we say this is not a weak instrument. #Exogeneity: #We cannot test if you are just-identified. #IV ESTIMATION #IV Packages: install.packages("AER") library(AER) #Basic Structure: #ivreg(Y~X1+X2...+W1+W2+...|Z1+Z2+...+W1+W2+...,data=dataframename(optional)) #IV using Average Winter Temperature as an IV without PWool: JIIVSupply<-ivreg(QS~P|WinterAveFarenheit) summary(JIIVSupply,vcov.=vcovHC(JIIVSupply,type="HC0")) #IV using Average Winter Temperature as IV with PWool JIIVSupplyW<-ivreg(QS~P+PWool|WinterAveFarenheit+PWool) summary(JIIVSupplyW,vcov.=vcovHC(JIIVSupplyW,type="HC0")) #Making a table including all three of these results: install.packages("stargazer") library(stargazer) #stargazer(nameoffirstmodel,nameofsecondmodel,...,,type="html",out="nameoffileyouaresaving.html") #If you use LaTex, no need for the second option. stargazer(OLSSupplyW,JIIVSupply,JIIVSupplyW,type="html",out="test.html") #This gives you homoskedastic errors though, so we have to add one more step: #Step 1: collect all standard errors using modelnameSE<-sqrt(diag(vcovHC(modelname,type="HC0"))) SEOLS<-sqrt(diag(vcovHC(OLSSupplyW,type="HC0"))) SEIV1<-sqrt(diag(vcovHC(JIIVSupply,type="HC0"))) SEIV2<- sqrt(diag(vcovHC(JIIVSupplyW,type="HC0"))) #Step 2: Use stargazer as above, but with the se options using the collected SEs in a list (in the right order!). stargazer(OLSSupplyW,JIIVSupply,JIIVSupplyW,type="html",out="test.html",se=list(SEOLS,SEIV1,SEIV2)) # ADDING ANOTHER IV - BECOMING OVER-IDENTIFIED: #Mostly same as before, except now we can also test for exogeneity! #Testing for relevance (i.e. F-Test) FirstStage3<-lm(P~WinterAveFarenheit+PWool+WinterMinFarenheit) linearHypothesis(FirstStage3,c("WinterAveFarenheit=0","WinterMinFarenheit=0"),test="F",vcov.=vcovHC(FirstStage3,type="HC0")) #F<10, so we say we now have weak instruments! We should remove one. #Testing for exogeneity (i.e. J-Test) #Method One: Manually: #Run Model: OIIVSupplyW<-ivreg(QS~P+PWool|WinterAveFarenheit+PWool+WinterMinFarenheit) summary(OIIVSupplyW,vcov.=vcovHC(OIIVSupplyW,type="HC0")) #Estimate residuals: res<-residuals(OIIVSupplyW) #Regress residuals on all instruments and exogenous vars: Jmodel<-lm(res~WinterAveFarenheit+PWool+WinterMinFarenheit) #Estimate the F-stat from an F test of the joint hypothesis that the instrument's coefficients are zero. Fstat<-linearHypothesis(Jmodel,c("WinterAveFarenheit=0","WinterMinFarenheit=0"),test="F")["2","F"] #Test if m*F-stat is larger than the critical value for a chi square with m-k degrees of freedom (the degree of overidentification): #i.e. do we reject the null of exogeneity? (2*Fstat>qchisq(.95,1)) #We do not reject the null of exogeneity. #If we did, we can only think at least one instrument is endogenous-- there is no way to test which. #Method Two: using the diagnostics option: summary(OIIVSupplyW,vcov.=vcovHC(OIIVSupplyW,type="HC0"), diagnostics= TRUE)