In class, we learned about time series. Time series can be described as: \[{y_t} = {TR_t} + {\epsilon_t}\]
y is the value of the time series at time t TR is the trend at time t the epsilon is the error at time t
No trend: \[{TR_t} = {\beta_0}\] Linear trend: \[{TR_t} = {\beta_0} + {\beta_1}t\] can be positive or negative Quadratic trend: \[{TR_t} = {\beta_0} + {\beta_1}t + {\beta_2}t^2\] can be increasing growth, decreasing growth, decline at an increasing rate or decline at a decreasing rate
library(faraway)
## Warning: package 'faraway' was built under R version 3.4.4
data(aatemp)
names(aatemp)
## [1] "year" "temp"
attach(aatemp)
plot(temp~year, type="l")
mod1 = lm(temp~year)
summary(mod1)
##
## Call:
## lm(formula = temp ~ year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.9843 -0.9113 -0.0820 0.9946 3.5343
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24.005510 7.310781 3.284 0.00136 **
## year 0.012237 0.003768 3.247 0.00153 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.466 on 113 degrees of freedom
## Multiple R-squared: 0.08536, Adjusted R-squared: 0.07727
## F-statistic: 10.55 on 1 and 113 DF, p-value: 0.001533
mod = lm(temp~year + I(year^2))
summary(mod)
##
## Call:
## lm(formula = temp ~ year + I(year^2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.0412 -0.9538 -0.0624 0.9959 3.5820
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.127e+02 3.837e+02 -0.554 0.580
## year 2.567e-01 3.962e-01 0.648 0.518
## I(year^2) -6.307e-05 1.022e-04 -0.617 0.539
##
## Residual standard error: 1.47 on 112 degrees of freedom
## Multiple R-squared: 0.08846, Adjusted R-squared: 0.07218
## F-statistic: 5.434 on 2 and 112 DF, p-value: 0.005591
mod = lm(temp ~ year + I(year^2) + I(year^3))
summary(mod)
##
## Call:
## lm(formula = temp ~ year + I(year^2) + I(year^3))
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.8557 -0.9646 -0.1552 1.0485 4.1538
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.959e+04 1.734e+04 2.283 0.0243 *
## year -6.159e+01 2.694e+01 -2.286 0.0241 *
## I(year^2) 3.197e-02 1.395e-02 2.291 0.0238 *
## I(year^3) -5.527e-06 2.407e-06 -2.296 0.0236 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.443 on 111 degrees of freedom
## Multiple R-squared: 0.1298, Adjusted R-squared: 0.1063
## F-statistic: 5.518 on 3 and 111 DF, p-value: 0.001436
plot(mod)
The plots look the best with the last model. This shows us that this is the best model for our data.
Are residuals correlated through time? If yes, then you have auto(self)correlation.
Positive autocorrelation: positive residual is likely to be followed by another positive residual or negative residual is likely to be followed by another negative residual.
Negative autocorrelation: positive residual is likely to be followed by a negative residual or negative residual is likely to be followed by a positive residual.
The error at time t only relates to the error at time t-1 and the error at time t+1.
The first way to look at this is to look at the residuals over time. You can plot that.
The other way is the Durbin Watson statistic. The test is located in the lmtest library so you have to call that first every time.
library(lmtest)
## Warning: package 'lmtest' was built under R version 3.4.4
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.4.4
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
dwtest(mod)
##
## Durbin-Watson test
##
## data: mod
## DW = 1.7171, p-value = 0.03464
## alternative hypothesis: true autocorrelation is greater than 0
dwtest(mod, alternative = "less")
##
## Durbin-Watson test
##
## data: mod
## DW = 1.7171, p-value = 0.9654
## alternative hypothesis: true autocorrelation is less than 0
dwtest(mod, alternative = "two.sided")
##
## Durbin-Watson test
##
## data: mod
## DW = 1.7171, p-value = 0.06928
## alternative hypothesis: true autocorrelation is not 0
The first output shows that there is positive autocorrelation because the p-value is small. We know it is positive because it says “true autocorrelation is greater than 0”.
The second output shows that there is no negative autocorrelation because the p-value is too big. We know it is negative because it says “true autocorrelation is less than 0”.
The last output shows that there is autocorrelation because the p-value is small. We know that this is positive or negative because it says “true autocorrelation is not 0”.