Also covered using Python and Stata

library(wooldridge)
library(psych)
library(stargazer)
library(car)

#### Example 5.2 Birth weight equaiton, Standar Errors

bwght_full <- lm(lbwght  ~ cigs  + lfaminc + 1, data=bwght)
x <- nobs(bwght_full)/2
bwght2 <- bwght[1:x, ]
birth_half <- lm(lbwght  ~ cigs  + lfaminc + 1, data=bwght2)

stargazer(bwght_full, birth_half,  type="text", align=TRUE)
##
## ====================================================================
##                                   Dependent variable:
##                     ------------------------------------------------
##                                          lbwght
##                               (1)                      (2)
## --------------------------------------------------------------------
## cigs                       -0.004***                -0.005***
##                             (0.001)                  (0.001)
##
## lfaminc                     0.016***                 0.019**
##                             (0.006)                  (0.008)
##
## Constant                    4.719***                4.706***
##                             (0.018)                  (0.027)
##
## --------------------------------------------------------------------
## Observations                 1,388                     694
## R2                           0.026                    0.030
## Residual Std. Error    0.188 (df = 1385)        0.196 (df = 691)
## F Statistic         18.310*** (df = 2; 1385) 10.519*** (df = 2; 691)
## ====================================================================
## Note:                                    *p<0.1; **p<0.05; ***p<0.01

#### Example 5.3 Economic model of crime

crime_ols <- lm(narr86 ~ pcnv + avgsen + tottime + ptime86 + qemp86 + 1, data=crime1)
crime_ols_r <- lm(narr86 ~ pcnv + ptime86 + qemp86 + 1, data=crime1)
stargazer(crime_ols, crime_ols_r, type="text", align=TRUE)
##
## =====================================================================
##                                    Dependent variable:
##                     -------------------------------------------------
##                                          narr86
##                               (1)                      (2)
## ---------------------------------------------------------------------
## pcnv                       -0.151***                -0.150***
##                             (0.041)                  (0.041)
##
## avgsen                       -0.007
##                             (0.012)
##
## tottime                      0.012
##                             (0.010)
##
## ptime86                    -0.039***                -0.034***
##                             (0.009)                  (0.009)
##
## qemp86                     -0.103***                -0.104***
##                             (0.010)                  (0.010)
##
## Constant                    0.706***                 0.712***
##                             (0.033)                  (0.033)
##
## ---------------------------------------------------------------------
## Observations                 2,725                    2,725
## R2                           0.043                    0.041
## Residual Std. Error    0.841 (df = 2719)        0.842 (df = 2721)
## F Statistic         24.289*** (df = 5; 2719) 39.096*** (df = 3; 2721)
## =====================================================================
## Note:                                     *p<0.1; **p<0.05; ***p<0.01
linearHypothesis(crime_ols, c("avgsen = 0", "tottime =0"))
## Linear hypothesis test
##
## Hypothesis:
## avgsen = 0
## tottime = 0
##
## Model 1: restricted model
## Model 2: narr86 ~ pcnv + avgsen + tottime + ptime86 + qemp86 + 1
##
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1   2721 1927.3
## 2   2719 1924.4  2     2.879 2.0339  0.131
resid <- resid(crime_ols_r)
resid_ols <- lm(resid ~ pcnv + avgsen + tottime + ptime86 + qemp86 + 1, data=crime1)

summary(resid_ols)
##
## Call:
## lm(formula = resid ~ pcnv + avgsen + tottime + ptime86 + qemp86 +
##     1, data = crime1)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -0.9869 -0.4243 -0.2937  0.3514 11.4409
##
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.005711   0.033152  -0.172    0.863
## pcnv        -0.001297   0.040855  -0.032    0.975
## avgsen      -0.007049   0.012412  -0.568    0.570
## tottime      0.012095   0.009577   1.263    0.207
## ptime86     -0.004839   0.008917  -0.543    0.587
## qemp86       0.001022   0.010397   0.098    0.922
##
## Residual standard error: 0.8413 on 2719 degrees of freedom
## Multiple R-squared:  0.001494,   Adjusted R-squared:  -0.0003423
## F-statistic: 0.8136 on 5 and 2719 DF,  p-value: 0.5398
LM <- summary(resid_ols)\$r.squared * nobs(resid_ols) # LM = N*Rsq
LM
## [1] 4.070729