Key Concepts covered in the lecture include:
1. Autocovariance and Autocorrelation
2. The First Law of Geography
3. Moran's I
4. Durbin-Watson Test for rho (autocorrelation)
5. Cochrane-Orcutt test for autocorrelation
6. Stationarity
7. Differencing
The in-class exercise is a redo of Lab 1, which uses time series modeling and assessment of autocorrelation.
Part 1: Test the autocorrelation within the best model from Lab 1
# install.packages('lmtest') install.packages('orcutt')
library(lmtest)
## Warning: package 'lmtest' was built under R version 2.15.3
## Loading required package: zoo
## Attaching package: 'zoo'
## The following object(s) are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(orcutt)
co2 <- read.csv("E:/Quant/inClassExercises/InClassExerciseData/co2_LAB1.csv")
# Recreate best model Parce through data to obtan a subset - only use one
# observation site
mauna <- co2[co2$site == 0, ]
sitexlm <- lm(co2 ~ time + I(time^2), data = mauna)
# Examine autocorrelation within the dataset.
dwtest(sitexlm)
##
## Durbin-Watson test
##
## data: sitexlm
## DW = 0.3067, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
# null hypothesis = the autocorrelation of the residuals is 0 Reject null
# hypothesis. There is some type of autocorrelation in the residuals.
# We can futher see these temportal trends by creating a correlogram of
# the residuals
acf(sitexlm$residuals) #There is a strong seasonal trend in the residuals
Part 2: Create a time series model
# Use differencing to see the trends...
mloa.ts <- ts(mauna$co2, start = min(mauna$year), frequency = 12)
# mloa.ts
plot(decompose(mloa.ts)) #This shows the variations - in the data. We can clearly see the seasonal effects
# Find the autocorrelation function value... Using the ACF function...
mllm <- lm(co2 ~ time, data = mauna)
test <- acf(resid(mllm))
summary(test)
## Length Class Mode
## acf 27 -none- numeric
## type 1 -none- character
## n.used 1 -none- numeric
## lag 27 -none- numeric
## series 1 -none- character
## snames 0 -none- NULL
# test$acf#returns a list of 27 acf values; one for each year. Year 2 is
# 0.8752593
test$acf[2]
## [1] 0.8753
# Using the cochrane-orcutt value. Produces a similar value to the
# autocorrelation factor. This is another way to create it.
cochrane.orcutt(mllm)$rho
## [1] 0.8772
# returns 0.8771816
Part 3: Create a new model that uses a seasonal factor
# Create a seasonal factor...
seasonal <- cycle(mloa.ts)
# seasonal
Time <- time(mloa.ts)
# Time
# Use the seasonal factor as a model parameter
lmx <- lm(co2 ~ time + factor(seasonal), data = mauna)
ts.plot(cbind(mloa.ts, lmx$fitted), lty = 1:2, col = c(1, 2)) #Look how closely it fits!!!
# Do some diagnostics...
dwtest(lmx)
##
## Durbin-Watson test
##
## data: lmx
## DW = 0.051, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
# null hypothesis = the autocorrelation of the residuals is 0 DW = 0.3067;
# p-value < 2.2e-16 Reject null hypothesis. There is some type of
# autocorrelation in the residuals. However, we can accept this model
# because of its great match of the original data. It is an improvement
# over the original.