Introductory Econometrics Using R

Also covered using Python and Stata

library(wooldridge)
library(stargazer)
library(AER)
library(Hmisc)

#### Example 16.5. Labor Supply of Married, Working Women

lreg1 <- ivreg(hours ~ lwage + educ + age + kidslt6 + nwifeinc | educ + age + kidslt6 + nwifeinc + exper + expersq, data = mroz)
lreg2 <- ivreg(lwage ~ hours  + educ + + exper + expersq | educ + age + kidslt6 + nwifeinc + exper + expersq, data = mroz)

stargazer(lreg1, lreg2, keep.stat=c("n"), no.space=TRUE, type="text")
##
## =========================================
##                  Dependent variable:
##              ----------------------------
##                   hours          lwage
##                    (1)            (2)
## -----------------------------------------
## lwage          1,639.556***
##                 (470.576)
## hours                           0.0001
##                                (0.0003)
## educ           -183.751***     0.110***
##                  (59.100)       (0.016)
## age               -7.806
##                  (9.378)
## kidslt6          -198.154
##                 (182.929)
## nwifeinc         -10.170
##                  (6.615)
## exper                           0.035*
##                                 (0.019)
## expersq                         -0.001
##                                (0.0005)
## Constant       2,225.662***     -0.656*
##                 (574.564)       (0.338)
## -----------------------------------------
## Observations       428            428
## =========================================
## Note:         *p<0.1; **p<0.05; ***p<0.01

#### Example 16.6. Inflation and Openness

oreg1 <- lm(open ~ lpcinc + lland, data = openness)
oreg2 <- ivreg(inf ~ open + lpcinc | lland + lpcinc, data = openness)
stargazer(oreg1, oreg2, keep.stat=c("n", "rsq", "adj.rsq"), no.space=TRUE, type="text")
##
## =========================================
##                  Dependent variable:
##              ----------------------------
##                   open           inf
##                   OLS       instrumental
##                               variable
##                   (1)            (2)
## -----------------------------------------
## open                          -0.337**
##                                (0.144)
## lpcinc           0.546          0.376
##                 (1.493)        (2.015)
## lland          -7.567***
##                 (0.814)
## Constant       117.085***      26.899*
##                 (15.848)      (15.401)
## -----------------------------------------
## Observations      114            114
## R2               0.449          0.031
## =========================================
## Note:         *p<0.1; **p<0.05; ***p<0.01

#### *Example 16.7. Testing the Permanent Income Hypothesis

preg1 <- ivreg(gc ~ gy + r3 | gy_1 + gc_1 + r3_1, data = consump)
u <- resid(preg1)
u1 <- Lag(u, 1)
preg2 <- lm(u ~ u1)
consump2<- subset(consump, !is.na(gy_1))
consump2 <- cbind(consump2, u)
preg3 <- ivreg(gc ~ gy + r3 + u1 | gy_1 + gc_1 + r3_1 + u1, data = consump2)

stargazer(preg1, preg2, preg3, keep.stat=c("n", "rsq", "adj.rsq"), no.space=TRUE, type="text")
##
## ==============================================
##                     Dependent variable:
##              ---------------------------------
##                   gc         u         gc
##              instrumental   OLS   instrumental
##                variable             variable
##                  (1)        (2)       (3)
## ----------------------------------------------
## gy             0.586***             0.986**
##                (0.135)              (0.439)
## r3             -0.0003              -0.0004
##                (0.001)              (0.001)
## u1                        -0.105     -0.600
##                           (0.180)   (0.593)
## Constant       0.008**    0.0001    -0.0004
##                (0.003)    (0.001)   (0.010)
## ----------------------------------------------
## Observations      35        34         34
## R2              0.678      0.011     0.454
## Adjusted R2     0.658     -0.020     0.400
## ==============================================
## Note:              *p<0.1; **p<0.05; ***p<0.01

#### Example 16.8. Effect of Prison Population on Violent Crime Rates

prreg1 <- ivreg(gcriv ~ gpris + gpolpc + gincpc + cunem + cblack + cmetro + cag0_14 + cag15_17 + cag18_24 + cag25_34 | gpolpc + gincpc + cunem + cblack + cmetro + cag0_14 + cag15_17 + cag18_24 + cag25_34 +  final1 + final2, data=prison)

prreg2 <- lm(gcriv ~ gpris + gpolpc + gincpc + cunem + cblack + cmetro + cag0_14 + cag15_17 + cag18_24 + cag25_34, data=prison)

stargazer(prreg1, prreg2, keep.stat=c("n", "rsq", "adj.rsq"), no.space=TRUE, type="text")
##
## =========================================
##                  Dependent variable:
##              ----------------------------
##                         gcriv
##               instrumental       OLS
##                 variable
##                    (1)           (2)
## -----------------------------------------
## gpris           -0.967***     -0.168***
##                  (0.352)       (0.048)
## gpolpc            0.073         0.094
##                  (0.070)       (0.058)
## gincpc          0.926***       0.960***
##                  (0.179)       (0.151)
## cunem            0.730**        0.407
##                  (0.358)       (0.279)
## cblack           -0.015         -0.011
##                  (0.042)       (0.035)
## cmetro           -1.151         -0.392
##                  (1.273)       (1.042)
## cag0_14           3.170        4.293**
##                  (2.304)       (1.908)
## cag15_17          6.661       12.898***
##                  (4.366)       (2.899)
## cag18_24         -0.919         1.815
##                  (2.668)       (2.025)
## cag25_34        -4.369**        -2.562
##                  (2.044)       (1.599)
## Constant          0.036         -0.005
##                  (0.024)       (0.014)
## -----------------------------------------
## Observations       714           714
## R2               -0.245         0.106
## Note:         *p<0.1; **p<0.05; ***p<0.01