PUBHLTH 740 Presentation about Auto Correlation

Time series regression models that incorporate correlated error terms are referred to as “Auto Correlated” models.

These error terms are said to be first order auto regressive if \( \Large \epsilon_{t} \) is correlated with \( \Large \epsilon_{t-1} \).

Simple Linear regression model.

\( \LARGE Y_{t} = \beta_{0} + \beta_{1}X_{t} + \epsilon_{t} \)

\( \LARGE \epsilon_{t} \overset{i.i.d}{\sim} \mathcal{N}(0,\sigma^2) \)

set.seed(3)
beta0 <- 2
beta1 <- 0.5
epsilon <- rnorm(30)
xvar <- 1:30
yvar <- beta0 + beta1 * xvar + epsilon
dat <- data.frame(xvar, yvar)
library(ggplot2)
ggplot(dat, aes(x = xvar, y = yvar)) + geom_point(shape = 1) + geom_smooth(method = lm, 
    se = FALSE) + ggtitle("Fitted Plot")

plot of chunk unnamed-chunk-2


library(ggplot2)
m = lm(yvar ~ xvar, data = dat)
r <- residuals(m)
yh <- predict(m)
dat.res <- data.frame(yh, r)
ggplot(dat.res, aes(x = yh, y = r)) + geom_point(shape = 1) + geom_hline(yintercept = 0, 
    colour = "blue") + ggtitle("Residual Plot")

plot of chunk unnamed-chunk-3

Durbin - Watson Test

d.w <- dwtest(m, order.by = NULL, alternative = c("two.sided"), iterations = 15, 
    exact = NULL, tol = 1e-10)
(d.w$p.value)
## [1] 0.9121

Auto Correlation Model

\( \LARGE Y_{i} = \beta_{0} + \beta_{1}X_{t} + \epsilon_{t} \)

\( \LARGE \epsilon_{t} = \rho\epsilon_{t-1} + u_{t} \)

\( \LARGE u_{t} \sim \mathcal{N}(0,\sigma^2) \)

set.seed(7)
xvar.yac <- 1:30
ep <- rep(NA, 30)
ep[1] <- 3
for (i in 2:30) {
    ep[i] <- 0.9 * ep[i - 1] + rnorm(1)
}

yac <- beta0 + beta1 * xvar.yac + ep
library(ggplot2)
dat.yac <- data.frame(xvar.yac, yac)
ggplot(dat.yac, aes(x = xvar.yac, y = yac)) + geom_point(shape = 1) + geom_smooth(method = lm, 
    se = FALSE) + ggtitle("Fitted Plot")

plot of chunk unnamed-chunk-7

library(ggplot2)
m.yac = lm(yac ~ xvar.yac, data = dat.yac)
r.yac <- residuals(m.yac)
yh.yac <- predict(m.yac)
dat.res <- data.frame(yh.yac, r.yac)
ggplot(dat.res, aes(x = yh.yac, y = r.yac)) + geom_point(shape = 1) + geom_hline(yintercept = 0, 
    colour = "blue") + ggtitle("Residual Plot")

plot of chunk unnamed-chunk-8

Durbin - Watson Test


dw.yac <- dwtest(m.yac, order.by = NULL, alternative = c("two.sided"), iterations = 50, 
    exact = NULL, tol = 1e-10)
(dw.yac$p.value)
## [1] 1.138e-09


# yts <- ts(yac,frequency = 1, start=c(1,1)) acf(yts, type = 'correlation')
# acf(yts, type = 'correlation',plot = FALSE)

# r1 <- c(1,.888,.802,.707) r2 <- c(.888,1,.888,.802) r3 <-
# c(.802,.888,1,.888) r4 <- c(.707,.802,.888,1)

# (mat <-matrix(c(r1,r2,r3,r4),nrow = 4, byrow=TRUE))
library(Matrix)
set.seed(2)
xvar <- 1:30
size <- 1000
yhat <- matrix(NA, nrow = size, ncol = 30)
for (j in 1:size) {

    ep <- rep(NA, 30)
    ep[1] <- 3
    for (i in 2:30) {
        ep[i] <- 0.9 * ep[i - 1] + rnorm(1)
    }

    yvar <- beta0 + beta1 * xvar + ep

    dat <- data.frame(yvar = yvar, xvar = xvar)
    m = lm(yvar ~ xvar, data = dat)
    yhat[j, ] <- as.vector(predict(m))

}

# yhat[1:10,1:10]

final <- cov(yhat)[1:10, 1:10]
colnames(final) <- c("y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y10")
rownames(final) <- c("y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y10")
final
##        y1    y2    y3    y4    y5    y6    y7    y8    y9   y10
## y1  2.652 2.554 2.456 2.358 2.260 2.163 2.065 1.967 1.869 1.771
## y2  2.554 2.466 2.378 2.290 2.202 2.114 2.027 1.939 1.851 1.763
## y3  2.456 2.378 2.300 2.222 2.144 2.066 1.988 1.910 1.832 1.754
## y4  2.358 2.290 2.222 2.154 2.086 2.018 1.950 1.882 1.814 1.746
## y5  2.260 2.202 2.144 2.086 2.028 1.970 1.912 1.854 1.796 1.738
## y6  2.163 2.114 2.066 2.018 1.970 1.922 1.874 1.826 1.777 1.729
## y7  2.065 2.027 1.988 1.950 1.912 1.874 1.835 1.797 1.759 1.721
## y8  1.967 1.939 1.910 1.882 1.854 1.826 1.797 1.769 1.741 1.712
## y9  1.869 1.851 1.832 1.814 1.796 1.777 1.759 1.741 1.722 1.704
## y10 1.771 1.763 1.754 1.746 1.738 1.729 1.721 1.712 1.704 1.696
tril(final)
## 10 x 10 Matrix of class "dtrMatrix"
##        y1    y2    y3    y4    y5    y6    y7    y8    y9   y10
## y1  2.652     .     .     .     .     .     .     .     .     .
## y2  2.554 2.466     .     .     .     .     .     .     .     .
## y3  2.456 2.378 2.300     .     .     .     .     .     .     .
## y4  2.358 2.290 2.222 2.154     .     .     .     .     .     .
## y5  2.260 2.202 2.144 2.086 2.028     .     .     .     .     .
## y6  2.163 2.114 2.066 2.018 1.970 1.922     .     .     .     .
## y7  2.065 2.027 1.988 1.950 1.912 1.874 1.835     .     .     .
## y8  1.967 1.939 1.910 1.882 1.854 1.826 1.797 1.769     .     .
## y9  1.869 1.851 1.832 1.814 1.796 1.777 1.759 1.741 1.722     .
## y10 1.771 1.763 1.754 1.746 1.738 1.729 1.721 1.712 1.704 1.696