## Chapter 9: Time Series Models 

## Sec 9.1: Time Series -- Some Basic Ideas 
## ss 9.1.1: Preliminary graphical explorations 
## Plot depth measurements: ts object LakeHuron (datasets) 
plot(LakeHuron, ylab="depth (in feet)", xlab = "Time (in years)") 

plot of chunk unnamed-chunk-1

opar <- par(mfrow=c(1,3), pty="s")
lag.plot(LakeHuron, lags=3, do.lines=FALSE) 

plot of chunk unnamed-chunk-1

par(opar)

## ss 9.1.2: The autocorrelation and partial autocorrelation function 
acf(LakeHuron) 
## pacf(LakeHuron) gives the plot of partial autocorelations 

## ss 9.1.3: Autoregressive (AR) models 
##                         The { AR(1)} model 
## Footnote Code
## Yule-Walker autocorrelation estimate  
LH.yw <- ar(x = LakeHuron, order.max = 1, method = "yw") 
                          # autocorrelation estimate 
                          # order.max = 1 for the AR(1) model 
LH.yw$ar                  # autocorrelation estimate of alpha 
## [1] 0.8319
## Maximum likelihood estimate 
LH.mle <- ar(x = LakeHuron, order.max = 1, method = "mle") 
LH.mle$ar                 # maximum likelihood estimate of alpha 
## [1] 0.8375
LH.mle$x.mean             # estimated series mean 
## [1] 579.1
LH.mle$var.pred           # estimated innovation variance 
## [1] 0.5093
##                   The general { AR(}p{ )} model 
ar(LakeHuron, method="mle")  
## 
## Call:
## ar(x = LakeHuron, method = "mle")
## 
## Coefficients:
##      1       2  
##  1.044  -0.250  
## 
## Order selected 2  sigma^2 estimated as  0.479
## ss 9.1.4: *~Autoregressive moving average (ARMA) models -- theory 
## Footnote Code
## series.diff <- diff(series) 

## ss 9.1.5: Automatic model selection? 
library(forecast) 
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: timeDate
## This is forecast 5.5

plot of chunk unnamed-chunk-1

auto.arima(LakeHuron) 
## Series: LakeHuron 
## ARIMA(0,1,4) with drift         
## 
## Coefficients:
##         ma1     ma2     ma3     ma4   drift
##       0.058  -0.316  -0.303  -0.235  -0.021
## s.e.  0.103   0.106   0.110   0.130   0.017
## 
## sigma^2 estimated as 0.481:  log likelihood=-101.1
## AIC=214.2   AICc=215.1   BIC=229.6
## Check that model removes most of the correlation structure 
acf(resid(arima(LakeHuron, order=c(p=1, d=1, q=2)))) 

plot of chunk unnamed-chunk-1

## The following achieves the same effect, for these data 
acf(resid(auto.arima(LakeHuron))) 

plot of chunk unnamed-chunk-1

## ss 9.1.6: A time series forecast 
LH.arima <- auto.arima(LakeHuron) 
fcast <- forecast(LH.arima) 
plot(fcast) 

plot of chunk unnamed-chunk-1

##                    Use of simulation as a check 
oldpar <- par(mfrow=c(2,2), mar=c(3.1,4.6,2.6, 1.1)) 
for(i in 1:4){ 
  ma3 <- 0.125*i 
  simts <- arima.sim(model=list(order=c(0,0,3), ma=c(0,0,ma3)), n=98) 
  acf(simts, main="", xlab="") 
  mtext(side=3, line=0.5, paste("ma3 =", ma3), adj=0) 
}