---
title: "Exercise 3.9"
author: "Garland Durham"
date: "March 15, 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(astsa)
require(tseries)
require(forecast)
```

```{r}
x = AirPassengers
plot(x)
plot(log(x))
y = log(x)

```

Let's work with y = log(x).  

First, note that a simple ARIMA(0,1,0) model implies the forecast for next month is this months value.  Not too good...
```{r fig.height=8, fig.width=8}
fit01=arima(y, order=c(0, 1, 0), seasonal=list(order=c(0, 0, 0), period=12) )
f01  =fitted( fit01 )
ts.plot( cbind(y, f01), col=c(1,2))
```

Now let's try ARIMA(0,1,0)x12.  For this one the forecast is last year's value.  This would actually be quite good
if we could include a constant to get year-on-year growth...
```{r}
fit02=arima(y, order=c(0, 0, 0), seasonal=list(order=c(0, 1, 0), period=12) )
f02  =fitted( fit02 )
ts.plot( cbind(y, f02), col=c(1,2))
```

ARIMA(0,1,0)x(0,1,0)x12 gets the year-on-year growth and is a very good model!  But, we are missing some 
autocorrelation in the residuals at 1 month and 1 year.
```{r}
fit03=arima(y, order=c(0, 1, 0), seasonal=list(order=c(0, 1, 0), period=12) )
f03  =fitted( fit03 )
ts.plot( cbind(y, f03), col=c(1,2))
acf(resid(fit03))
```

So, let's try adding some MA terms to capture that:
```{r, fig.height=8, fig.width=8}
fit04=arima(y, order=c(0, 1, 1), seasonal=list(order=c(0, 1, 0), period=12) )
f04  =fitted( fit04 )
fit05=arima(y, order=c(0, 1, 0), seasonal=list(order=c(0, 1, 1), period=12) )
f05  =fitted( fit05 )
fit06=arima(y, order=c(0, 1, 1), seasonal=list(order=c(0, 1, 1), period=12) )
f06  =fitted( fit06 )
ts.plot( cbind(y, f03, f04, f05, f06), col=1:5)
```

Visually, these all look pretty similar.  Let's look at AIC/BIC
 
```{r}
AIC(fit03,fit04,fit05,fit06)
BIC(fit03,fit04,fit05,fit06)
```

ARIMA(0,1,1)x(0,1,1)x12 definitely wins.  Let's check the diagnostics.
```{r, fig.height=8, fig.width=8}
sarima( y, 0,1,1, 0,1,1, S=12, details=FALSE)
```

NICE!   








