Introductory Econometrics Using R

Also covered using Python and Stata

#### Example 4.1 Wage equation

library(wooldridge)
library(psych)
library(stargazer)
library(car)
options(width=120)
wage_mols <- lm(lwage ~ educ + exper + tenure + 1, data=wage1)
stargazer(wage_mols, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                                lwage
## -----------------------------------------------
## educ                         0.092***
##                               (0.007)
##
## exper                         0.004**
##                               (0.002)
##
## tenure                       0.022***
##                               (0.003)
##
## Constant                     0.284***
##                               (0.104)
##
## -----------------------------------------------
## Observations                    526
## R2                             0.316
## Residual Std. Error      0.441 (df = 522)
## F Statistic           80.391*** (df = 3; 522)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

#### Example 4.2. Student performance

math_lin_lin <- lm(math10 ~ totcomp + staff + enroll + 1, data=meap93)
stargazer(math_lin_lin, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                               math10
## -----------------------------------------------
## totcomp                      0.0005***
##                              (0.0001)
##
## staff                          0.048
##                               (0.040)
##
## enroll                        -0.0002
##                              (0.0002)
##
## Constant                       2.274
##                               (6.114)
##
## -----------------------------------------------
## Observations                    408
## R2                             0.054
## Residual Std. Error      10.244 (df = 404)
## F Statistic           7.697*** (df = 3; 404)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
math_lin_log <- lm(math10 ~ ltotcomp + lstaff + lenroll + 1, data=meap93)
stargazer(math_lin_log, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                               math10
## -----------------------------------------------
## ltotcomp                     21.155***
##                               (4.056)
##
## lstaff                         3.980
##                               (4.190)
##
## lenroll                       -1.268*
##                               (0.693)
##
## Constant                    -207.665***
##                              (48.703)
##
## -----------------------------------------------
## Observations                    408
## R2                             0.065
## Residual Std. Error      10.182 (df = 404)
## F Statistic           9.420*** (df = 3; 404)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
stargazer(math_lin_lin, math_lin_log, type="text", align=TRUE)
##
## ===========================================================
##                                    Dependent variable:
##                                ----------------------------
##                                           math10
##                                     (1)            (2)
## -----------------------------------------------------------
## totcomp                          0.0005***
##                                   (0.0001)
##
## staff                              0.048
##                                   (0.040)
##
## enroll                            -0.0002
##                                   (0.0002)
##
## ltotcomp                                        21.155***
##                                                  (4.056)
##
## lstaff                                            3.980
##                                                  (4.190)
##
## lenroll                                          -1.268*
##                                                  (0.693)
##
## Constant                           2.274       -207.665***
##                                   (6.114)       (48.703)
##
## -----------------------------------------------------------
## Observations                        408            408
## R2                                 0.054          0.065
## Residual Std. Error (df = 404)     10.244        10.182
## F Statistic (df = 3; 404)         7.697***      9.420***
## ===========================================================
## Note:                           *p<0.1; **p<0.05; ***p<0.01

#### Example 4.3. Collage GPA

gpa_mols <- lm(colGPA ~ hsGPA + ACT + skipped + 1, data=gpa1)
stargazer(gpa_mols, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                               colGPA
## -----------------------------------------------
## hsGPA                        0.412***
##                               (0.094)
##
## ACT                            0.015
##                               (0.011)
##
## skipped                      -0.083***
##                               (0.026)
##
## Constant                     1.390***
##                               (0.332)
##
## -----------------------------------------------
## Observations                    141
## R2                             0.234
## Residual Std. Error      0.329 (df = 137)
## F Statistic           13.919*** (df = 3; 137)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

#### Example 4.4. Campus crime & enrollment

crime_ols <- lm(lcrime ~ lenroll + 1, data=campus)
stargazer(crime_ols, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                               lcrime
## -----------------------------------------------
## lenroll                      1.270***
##                               (0.110)
##
## Constant                     -6.631***
##                               (1.034)
##
## -----------------------------------------------
## Observations                    97
## R2                             0.585
## Residual Std. Error       0.895 (df = 95)
## F Statistic           133.792*** (df = 1; 95)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

ldist <- log(hprice2dist) hprice_mols <- lm(lprice ~ lnox +ldist + rooms + stratio + 1, data=hprice2) stargazer(hprice_mols, type="text", align=TRUE) ## ## =============================================== ## Dependent variable: ## --------------------------- ## lprice ## ----------------------------------------------- ## lnox -0.954*** ## (0.117) ## ## ldist -0.134*** ## (0.043) ## ## rooms 0.255*** ## (0.019) ## ## stratio -0.052*** ## (0.006) ## ## Constant 11.084*** ## (0.318) ## ## ----------------------------------------------- ## Observations 506 ## R2 0.584 ## Adjusted R2 0.581 ## Residual Std. Error 0.265 (df = 501) ## F Statistic 175.855*** (df = 4; 501) ## =============================================== ## Note: *p<0.1; **p<0.05; ***p<0.01 #### Example 4.6. Participation rates in 401k plans pension_mols <- lm(prate ~ mrate + age + totemp + 1, data=k401k) stargazer(pension_mols, type="text", align=TRUE) ## ## =============================================== ## Dependent variable: ## --------------------------- ## prate ## ----------------------------------------------- ## mrate 5.442*** ## (0.524) ## ## age 0.269*** ## (0.045) ## ## totemp -0.0001*** ## (0.00004) ## ## Constant 80.294*** ## (0.778) ## ## ----------------------------------------------- ## Observations 1,534 ## R2 0.100 ## Adjusted R2 0.098 ## Residual Std. Error 15.878 (df = 1530) ## F Statistic 56.379*** (df = 3; 1530) ## =============================================== ## Note: *p<0.1; **p<0.05; ***p<0.01 #### Example4.7. Job training (Only for the year 1987 and for nonunionized firms) jtrain87 <- subset(jtrain, jtrainyear==1987 & jtrain\$union==0)
job_mols <- lm(lscrap ~ hrsemp + lsales + lemploy + 1, data=jtrain87)
stargazer(job_mols, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                               lscrap
## -----------------------------------------------
## hrsemp                        -0.029
##                               (0.023)
##
## lsales                       -0.962**
##                               (0.453)
##
## lemploy                       0.761*
##                               (0.407)
##
## Constant                     12.458**
##                               (5.687)
##
## -----------------------------------------------
## Observations                    29
## R2                             0.262
## Residual Std. Error       1.376 (df = 25)
## F Statistic             2.965* (df = 3; 25)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

#### Example 4.8. RD and Sales

lrd_mols <- lm(lrd ~ lsales + profmarg + 1, data=rdchem)
stargazer(lrd_mols, type="text", align=TRUE)
##
## ===============================================
##                         Dependent variable:
##                     ---------------------------
##                                 lrd
## -----------------------------------------------
## lsales                       1.084***
##                               (0.060)
##
## profmarg                       0.022
##                               (0.013)
##
## Constant                     -4.378***
##                               (0.468)
##
## -----------------------------------------------
## Observations                    32
## R2                             0.918
## Residual Std. Error       0.514 (df = 29)
## F Statistic           162.231*** (df = 2; 29)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

#### Example 4.9. Parent’s education on birth weight

birth_mols <- lm(bwght ~ cigs + parity + faminc + motheduc + fatheduc + 1, data=bwght)
birth_ols <- lm(bwght ~ cigs + parity + faminc + 1, data=bwght)
stargazer(birth_mols, birth_ols, type="text", align=TRUE)
##
## ====================================================================
##                                   Dependent variable:
##                     ------------------------------------------------
##                                          bwght
##                               (1)                     (2)
## --------------------------------------------------------------------
## cigs                       -0.596***               -0.477***
##                             (0.110)                 (0.092)
##
## parity                     1.788***                 1.616***
##                             (0.659)                 (0.604)
##
## faminc                       0.056                  0.098***
##                             (0.037)                 (0.029)
##
## motheduc                    -0.370
##                             (0.320)
##
## fatheduc                    0.472*
##                             (0.283)
##
## Constant                  114.524***               114.214***
##                             (3.728)                 (1.469)
##
## --------------------------------------------------------------------
## Observations                 1,191                   1,388
## R2                           0.039                   0.035
## Residual Std. Error   19.789 (df = 1185)       20.018 (df = 1384)
## F Statistic         9.553*** (df = 5; 1185) 16.633*** (df = 3; 1384)
## ====================================================================
## Note:                                    *p<0.1; **p<0.05; ***p<0.01
linearHypothesis(birth_mols, c("motheduc = 0", "fatheduc =0"))
## Linear hypothesis test
##
## Hypothesis:
## motheduc = 0
## fatheduc = 0
##
## Model 1: restricted model
## Model 2: bwght ~ cigs + parity + faminc + motheduc + fatheduc + 1
##
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1   1187 465167
## 2   1185 464041  2    1125.7 1.4373  0.238

#### Exaploring further example 4.5.

attend_ols_r <- lm(atndrte ~ priGPA + 1, data=attend)
attend_ols <- lm(atndrte ~ priGPA + ACT + 1, data=attend)
stargazer(attend_ols_r, attend_ols, type="text", align=TRUE)
##
## =====================================================================
##                                    Dependent variable:
##                     -------------------------------------------------
##                                          atndrte
##                               (1)                      (2)
## ---------------------------------------------------------------------
## priGPA                     13.369***                17.261***
##                             (1.087)                  (1.083)
##
## ACT                                                 -1.717***
##                                                      (0.169)
##
## Constant                   47.127***                75.700***
##                             (2.873)                  (3.884)
##
## ---------------------------------------------------------------------
## Observations                  680                      680
## R2                           0.182                    0.291
## Residual Std. Error    15.425 (df = 678)        14.379 (df = 677)
## F Statistic         151.347*** (df = 1; 678) 138.651*** (df = 2; 677)
## =====================================================================
## Note:                                     *p<0.1; **p<0.05; ***p<0.01

#### Example4.10. Salary-pension tradeoff for teachers

meap_ols1 <- lm(lsalary ~ bensal + 1, data=meap93)
meap_ols2 <- lm(lsalary ~ bensal + lenroll + lstaff + 1, data=meap93)
meap_ols3 <- lm(lsalary ~ bensal + lenroll + lstaff + droprate + gradrate + 1, data=meap93)
stargazer(meap_ols1, meap_ols2, meap_ols3,   type="text", align=TRUE)
##
## ===========================================================================================
##                                               Dependent variable:
##                     -----------------------------------------------------------------------
##                                                     lsalary
##                               (1)                     (2)                     (3)
## -------------------------------------------------------------------------------------------
## bensal                     -0.825***               -0.605***               -0.589***
##                             (0.200)                 (0.165)                 (0.165)
##
## lenroll                                            0.087***                0.088***
##                                                     (0.007)                 (0.007)
##
## lstaff                                             -0.222***               -0.218***
##                                                     (0.050)                 (0.050)
##
## droprate                                                                    -0.0003
##                                                                             (0.002)
##
## Note:                                                           *p<0.1; **p<0.05; ***p<0.01