suppressPackageStartupMessages({
library(sandwich)
library(lmtest)
})
# 1. Simulate Time Series Data with AR(1) Errors (rho = 0.7)
set.seed(42)
T_obs <- 200
e <- rnorm(T_obs, mean = 0, sd = 1)
u <- numeric(T_obs)
for (t in 2:T_obs) {
u[t] <- 0.7 * u[t - 1] + e[t]
}
x <- rnorm(T_obs, mean = 2, sd = 1)
y <- 1.5 + 2.0 * x + u
# 2. Fit OLS Model
model <- lm(y ~ x)
# 3. Naive OLS Standard Errors (Uncorrected)
cat("--- Standard OLS (Understates SE due to AR(1) errors) ---\n")--- Standard OLS (Understates SE due to AR(1) errors) ---
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.43088 0.22317 6.4115 1.034e-09 ***
x 1.96745 0.10043 19.5895 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
--- Newey-West HAC (Lag = 4) ---
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.430880 0.241621 5.922 1.385e-08 ***
x 1.967446 0.082786 23.765 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
--- Newey-West HAC (Automatic Andrews Bandwidth) ---
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.430880 0.251564 5.6879 4.569e-08 ***
x 1.967446 0.067042 29.3464 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1