Introductory Econometrics Using R

Also covered using Python and Stata

library(wooldridge)
library(lmtest)
library(stargazer)
library(car)

Example 10.1. Static Phillips Curve

stPhillips <- lm(inf ~ unem, data =phillips)
stargazer(stPhillips, no.space=TRUE, type="text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 inf            
## -----------------------------------------------
## unem                          0.502*           
##                               (0.266)          
## Constant                       1.054           
##                               (1.548)          
## -----------------------------------------------
## Observations                    56             
## R2                             0.062           
## Adjusted R2                    0.045           
## Residual Std. Error       2.972 (df = 54)      
## F Statistic             3.579* (df = 1; 54)    
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Example 10.2. Effects of Inflation and Deficits on Interest Rates

summary(lm(i3 ~ inf + def + 1, data=intdef))
## 
## Call:
## lm(formula = i3 ~ inf + def + 1, data = intdef)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9948 -1.1694  0.1959  0.9602  4.7224 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.73327    0.43197   4.012  0.00019 ***
## inf          0.60587    0.08213   7.376 1.12e-09 ***
## def          0.51306    0.11838   4.334 6.57e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.843 on 53 degrees of freedom
## Multiple R-squared:  0.6021, Adjusted R-squared:  0.5871 
## F-statistic: 40.09 on 2 and 53 DF,  p-value: 2.483e-11

Example 10.3. Puerto Rican Employment and the Minimum Wage

summary(lm(lprepop ~ lmincov + lusgnp, data=prminwge))
## 
## Call:
## lm(formula = lprepop ~ lmincov + lusgnp, data = prminwge)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.117133 -0.036998 -0.005943  0.028182  0.113938 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -1.05442    0.76541  -1.378   0.1771  
## lmincov     -0.15444    0.06490  -2.380   0.0229 *
## lusgnp      -0.01219    0.08851  -0.138   0.8913  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0557 on 35 degrees of freedom
## Multiple R-squared:  0.6605, Adjusted R-squared:  0.6411 
## F-statistic: 34.04 on 2 and 35 DF,  p-value: 6.17e-09

Example 10.4. Effects of Personal Exemption on Fertility Rates

regf <- lm(gfr ~ pe + ww2 + pill, data=fertil3)
tsregf <-lm(gfr ~ pe + pe_1 + pe_2 + ww2 + pill, data=fertil3)
stargazer(regf, tsregf, no.space=TRUE, type="text")
## 
## =================================================================
##                                  Dependent variable:             
##                     ---------------------------------------------
##                                          gfr                     
##                              (1)                    (2)          
## -----------------------------------------------------------------
## pe                         0.083***                0.073         
##                            (0.030)                (0.126)        
## pe_1                                               -0.006        
##                                                   (0.156)        
## pe_2                                               0.034         
##                                                   (0.126)        
## ww2                       -24.238***             -22.126**       
##                            (7.458)                (10.732)       
## pill                      -31.594***             -31.305***      
##                            (4.081)                (3.982)        
## Constant                  98.682***              95.870***       
##                            (3.208)                (3.282)        
## -----------------------------------------------------------------
## Observations                  72                     70          
## R2                          0.473                  0.499         
## Adjusted R2                 0.450                  0.459         
## Residual Std. Error    14.685 (df = 68)       14.270 (df = 64)   
## F Statistic         20.378*** (df = 3; 68) 12.728*** (df = 5; 64)
## =================================================================
## Note:                                 *p<0.1; **p<0.05; ***p<0.01
linearHypothesis(tsregf, c("pe=0", "pe_1=0", "pe_2=0"))
## Linear hypothesis test
## 
## Hypothesis:
## pe = 0
## pe_1 = 0
## pe_2 = 0
## 
## Model 1: restricted model
## Model 2: gfr ~ pe + pe_1 + pe_2 + ww2 + pill
## 
##   Res.Df   RSS Df Sum of Sq     F  Pr(>F)  
## 1     67 15460                             
## 2     64 13033  3    2427.1 3.973 0.01165 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Example 10.5. Antidumping Filings and Chemical Imports

tsregc <- lm(lchnimp ~lchempi + lgas + lrtwex + befile6 + affile6 + afdec6, data=barium)
stargazer(tsregc, no.space=TRUE, type="text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               lchnimp          
## -----------------------------------------------
## lchempi                      3.117***          
##                               (0.479)          
## lgas                           0.196           
##                               (0.907)          
## lrtwex                        0.983**          
##                               (0.400)          
## befile6                        0.060           
##                               (0.261)          
## affile6                       -0.032           
##                               (0.264)          
## afdec6                        -0.565*          
##                               (0.286)          
## Constant                      -17.803          
##                              (21.045)          
## -----------------------------------------------
## Observations                    131            
## R2                             0.305           
## Adjusted R2                    0.271           
## Residual Std. Error      0.597 (df = 124)      
## F Statistic           9.064*** (df = 6; 124)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
100*(exp(tsregc$coef[7]) -1)
##    afdec6 
## -43.17791

Example 10.6. Election Outcomes and Economic Performance

summary(lm(demvote ~ partyWH + incum + partyWH:gnews + partyWH:inf +1, data=subset(fair, fair$year<1996)))
## 
## Call:
## lm(formula = demvote ~ partyWH + incum + partyWH:gnews + partyWH:inf + 
##     1, data = subset(fair, fair$year < 1996))
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.059643 -0.036072 -0.005397  0.018386  0.102723 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    0.481062   0.012263  39.228   <2e-16 ***
## partyWH       -0.043475   0.040459  -1.075   0.2996    
## incum          0.054390   0.023417   2.323   0.0347 *  
## partyWH:gnews  0.010847   0.004127   2.628   0.0190 *  
## partyWH:inf   -0.007702   0.003257  -2.365   0.0319 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04957 on 15 degrees of freedom
## Multiple R-squared:  0.6629, Adjusted R-squared:  0.573 
## F-statistic: 7.374 on 4 and 15 DF,  p-value: 0.001716

Example 10.7. Housing Investment and Prices

reghi <- lm(linvpc ~ lprice, data=hseinv)
reghit <- lm(linvpc ~ lprice + t, data=hseinv)
stargazer(reghi, reghit, no.space=TRUE, type="text")
## 
## =================================================================
##                                  Dependent variable:             
##                     ---------------------------------------------
##                                        linvpc                    
##                              (1)                    (2)          
## -----------------------------------------------------------------
## lprice                     1.241***                -0.381        
##                            (0.382)                (0.679)        
## t                                                 0.010***       
##                                                   (0.004)        
## Constant                  -0.550***              -0.913***       
##                            (0.043)                (0.136)        
## -----------------------------------------------------------------
## Observations                  42                     42          
## R2                          0.208                  0.341         
## Adjusted R2                 0.189                  0.307         
## Residual Std. Error    0.155 (df = 40)        0.144 (df = 39)    
## F Statistic         10.530*** (df = 1; 40) 10.080*** (df = 2; 39)
## =================================================================
## Note:                                 *p<0.1; **p<0.05; ***p<0.01

Example 10.8. Fertility Equation

regt <- lm(gfr ~ pe + ww2 + pill + t + 1, data=fertil3)
regtsq <- lm(gfr ~ pe + ww2 + pill + t + tsq, data=fertil3)
stargazer(regt, regtsq, no.space=TRUE, type="text")
## 
## =================================================================
##                                  Dependent variable:             
##                     ---------------------------------------------
##                                          gfr                     
##                              (1)                    (2)          
## -----------------------------------------------------------------
## pe                         0.279***               0.348***       
##                            (0.040)                (0.040)        
## ww2                       -35.592***             -35.880***      
##                            (6.297)                (5.708)        
## pill                        0.997                 -10.120        
##                            (6.262)                (6.336)        
## t                         -1.150***              -2.531***       
##                            (0.188)                (0.389)        
## tsq                                               0.020***       
##                                                   (0.005)        
## Constant                  111.769***             124.092***      
##                            (3.358)                (4.361)        
## -----------------------------------------------------------------
## Observations                  72                     72          
## R2                          0.662                  0.727         
## Adjusted R2                 0.642                  0.706         
## Residual Std. Error    11.849 (df = 67)       10.739 (df = 66)   
## F Statistic         32.837*** (df = 4; 67) 35.094*** (df = 5; 66)
## =================================================================
## Note:                                 *p<0.1; **p<0.05; ***p<0.01

Example 10.9. Puerto Rican Employment

summary(lm(lprepop ~ lmincov + lusgnp + t, data=prminwge))
## 
## Call:
## lm(formula = lprepop ~ lmincov + lusgnp + t, data = prminwge)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.049493 -0.024425 -0.009596  0.017055  0.086354 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -8.696298   1.295764  -6.711 1.04e-07 ***
## lmincov     -0.168695   0.044246  -3.813 0.000552 ***
## lusgnp       1.057351   0.176637   5.986 8.98e-07 ***
## t           -0.032354   0.005023  -6.442 2.31e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03793 on 34 degrees of freedom
## Multiple R-squared:  0.8471, Adjusted R-squared:  0.8336 
## F-statistic: 62.78 on 3 and 34 DF,  p-value: 6.007e-14

Example 10.10. Housing Investment

summary(lm(linvpc ~ lprice + t, data=hseinv))
## 
## Call:
## lm(formula = linvpc ~ lprice + t, data = hseinv)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.45092 -0.08583 -0.01734  0.08517  0.26024 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.913060   0.135613  -6.733    5e-08 ***
## lprice      -0.380961   0.678835  -0.561  0.57787    
## t            0.009829   0.003512   2.798  0.00794 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1436 on 39 degrees of freedom
## Multiple R-squared:  0.3408, Adjusted R-squared:  0.307 
## F-statistic: 10.08 on 2 and 39 DF,  p-value: 0.000296
uhat<-resid(lm(linvpc~t, data=hseinv))
detrend <- lm(uhat ~ lprice + t, data=hseinv)
trend <- lm(linvpc ~ lprice + t, data=hseinv)
stargazer(detrend, trend, column.labels=c("Detrend", "Trend"),  no.space=TRUE, type="text")
## 
## ==========================================================
##                                   Dependent variable:     
##                               ----------------------------
##                                    uhat         linvpc    
##                                  Detrend         Trend    
##                                    (1)            (2)     
## ----------------------------------------------------------
## lprice                            -0.381        -0.381    
##                                  (0.679)        (0.679)   
## t                                 0.002        0.010***   
##                                  (0.004)        (0.004)   
## Constant                          -0.072       -0.913***  
##                                  (0.136)        (0.136)   
## ----------------------------------------------------------
## Observations                        42            42      
## R2                                0.008          0.341    
## Adjusted R2                       -0.043         0.307    
## Residual Std. Error (df = 39)     0.144          0.144    
## F Statistic (df = 2; 39)          0.157        10.080***  
## ==========================================================
## Note:                          *p<0.1; **p<0.05; ***p<0.01

Example 10.11. Effects of Antidumping Filings

antid_month <- lm(lchnimp ~ lchempi + lgas + lrtwex + befile6 + affile6 + afdec6 + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec + 1, data=barium)
stargazer(antid_month, single.row = TRUE, no.space=TRUE, type="text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               lchnimp          
## -----------------------------------------------
## lchempi                  3.265*** (0.493)      
## lgas                      -1.278 (1.389)       
## lrtwex                     0.663 (0.471)       
## befile6                    0.140 (0.267)       
## affile6                    0.013 (0.279)       
## afdec6                    -0.521* (0.302)      
## feb                       -0.418 (0.304)       
## mar                        0.059 (0.265)       
## apr                       -0.451* (0.268)      
## may                        0.033 (0.269)       
## jun                       -0.206 (0.269)       
## jul                        0.004 (0.279)       
## aug                       -0.157 (0.278)       
## sep                       -0.134 (0.268)       
## oct                        0.052 (0.267)       
## nov                       -0.246 (0.263)       
## dec                        0.133 (0.271)       
## Constant                  16.779 (32.429)      
## -----------------------------------------------
## Observations                    131            
## R2                             0.358           
## Adjusted R2                    0.262           
## Residual Std. Error      0.601 (df = 113)      
## F Statistic           3.712*** (df = 17; 113)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
linearHypothesis(antid_month, c("feb=0", "mar=0", "apr=0", "may=0", "jun=0", "jul=0", "aug=0", "sep=0", "oct=0", "nov=0", "dec=0"))
## Linear hypothesis test
## 
## Hypothesis:
## feb = 0
## mar = 0
## apr = 0
## may = 0
## jun = 0
## jul = 0
## aug = 0
## sep = 0
## oct = 0
## nov = 0
## dec = 0
## 
## Model 1: restricted model
## Model 2: lchnimp ~ lchempi + lgas + lrtwex + befile6 + affile6 + afdec6 + 
##     feb + mar + apr + may + jun + jul + aug + sep + oct + nov + 
##     dec + 1
## 
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1    124 44.247                           
## 2    113 40.844 11    3.4032 0.8559 0.5852
antid_season <- lm(lchnimp ~ lchempi + lgas + lrtwex + befile6 + affile6 + afdec6 + spr + sum + fall + 1, data=barium)
stargazer(antid_season, single.row = TRUE, no.space=TRUE, type="text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               lchnimp          
## -----------------------------------------------
## lchempi                  3.078*** (0.486)      
## lgas                       0.565 (1.000)       
## lrtwex                    1.101** (0.425)      
## befile6                    0.077 (0.265)       
## affile6                   -0.083 (0.273)       
## afdec6                   -0.621** (0.295)      
## spr                       -0.041 (0.151)       
## sum                       -0.152 (0.169)       
## fall                      -0.067 (0.154)       
## Constant                 -26.522 (23.297)      
## -----------------------------------------------
## Observations                    131            
## R2                             0.310           
## Adjusted R2                    0.258           
## Residual Std. Error      0.603 (df = 121)      
## F Statistic           6.032*** (df = 9; 121)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
linearHypothesis(antid_season, c("spr=0", "sum=0", "fall=0"))
## Linear hypothesis test
## 
## Hypothesis:
## spr = 0
## sum = 0
## fall = 0
## 
## Model 1: restricted model
## Model 2: lchnimp ~ lchempi + lgas + lrtwex + befile6 + affile6 + afdec6 + 
##     spr + sum + fall + 1
## 
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1    124 44.247                           
## 2    121 43.940  3   0.30748 0.2822 0.8381