Greene. Econometric Analysis, 8ed.

Previous | HOME | Next

Chapter 3. Least Squares Regression

Table 3.1

Load libraries

library(AER)
library(stargazer)
library(knitr)
library(kableExtra)

Data Matrices

df <- as.data.frame(read.csv("data/TableF3-1.csv", fileEncoding="UTF-8-BOM", header=TRUE))
df1 <- df[, c(8, 7, 2, 5, 6)]
# df1$Constant=1 #To add a column populated with a constant 
kable(df1) %>%
  kable_styling(full_width = F) 
RealInv Trend RealGNP Interest Infl
2.484 1 87.1 9.23 3.4
2.311 2 88.0 6.91 1.6
2.265 3 89.5 4.67 2.4
2.339 4 92.0 4.12 1.9
2.556 5 95.5 4.34 3.3
2.750 6 98.7 6.19 3.4
2.828 7 101.4 7.96 2.5
2.717 8 103.2 8.05 4.1
2.445 9 102.9 5.09 0.1
1.878 10 100.0 3.25 2.7
2.076 11 102.5 3.25 1.5
2.168 12 104.2 3.25 3.0
2.356 13 105.6 3.25 1.7
2.482 14 109.0 3.25 1.5
2.637 15 111.6 3.25 0.8
Application 3.2 pp.30-32
coef(pp32a <- lm(RealInv ~ Trend + RealGNP + 1, data = df))
## (Intercept)       Trend     RealGNP 
##  -6.8555213  -0.1800237   0.1077841
coef(pp32b <- lm(RealInv ~ Trend + 1, data = df))
##  (Intercept)        Trend 
##  2.468009524 -0.006067857

Example 3.1

Partial Correlations, (& Table 3.2)

col2 <- lm(RealInv ~ Trend + RealGNP + Interest + Infl + 1, data = df)
coef(col2)
## (Intercept)       Trend     RealGNP    Interest        Infl 
## -6.21967209 -0.16088526  0.09908417  0.02017157 -0.01165919
col4 <- cor(df1)[1,-1]
col4
##      Trend    RealGNP   Interest       Infl 
## -0.1036346  0.1487904  0.5530208  0.1919226
pcor <- solve(cor(df1))
dcor <- 1/sqrt(diag(pcor))
col5 <- (-pcor * (dcor %o% dcor))[1,-1]
col5
##       Trend     RealGNP    Interest        Infl 
## -0.73284302  0.79226230  0.18602513 -0.09231543

Example 3.2

Fit of a Consumption Function

df <- as.data.frame(read.csv("data/TableF2-1.csv", fileEncoding="UTF-8-BOM", header=TRUE))
c(mean(df$X), mean(df$C))
## [1] 323.2727 273.2727
summary.aov(ols1 <- lm(C ~ X, data = df))
##             Df Sum Sq Mean Sq F value Pr(>F)  
## X            1   5768    5768   7.579 0.0224 *
## Residuals    9   6850     761                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ols2 <- lm(C ~ X, data = subset(df, df$W == 0))
ols3 <- lm(C ~ X + W, data = df)
stargazer(ols1, ols2, ols3, keep.stat=c("rsq"), no.space=TRUE, type="text")
## 
## ======================================
##               Dependent variable:     
##          -----------------------------
##                        C              
##             (1)      (2)       (3)    
## --------------------------------------
## X         0.685**  0.853***  0.858*** 
##           (0.249)  (0.099)   (0.085)  
## W                           -50.690***
##                              (5.932)  
## Constant  51.895    15.907    14.495  
##          (80.844)  (31.643)  (27.299) 
## --------------------------------------
## R2         0.457    0.937     0.946   
## ======================================
## Note:      *p<0.1; **p<0.05; ***p<0.01

Example 3.3

Analysis of Variance Table (& Table 3.4.)

df <- as.data.frame(read.csv("data/TableF3-1.csv", fileEncoding="UTF-8-BOM", header=TRUE))
summary(mols  <- lm(RealInv ~ Trend + RealGNP + Interest + Infl, data = df))$r.squared
## [1] 0.7878083
deviance(mols)
## [1] 0.2036798
summary.aov(sols <- lm(RealInv ~ 1, data = df))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Residuals   14 0.9599 0.06856
anova(sols, mols)
## Analysis of Variance Table
## 
## Model 1: RealInv ~ 1
## Model 2: RealInv ~ Trend + RealGNP + Interest + Infl
##   Res.Df     RSS Df Sum of Sq      F   Pr(>F)   
## 1     14 0.95989                                
## 2     10 0.20368  4   0.75621 9.2818 0.002125 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1