---
title: "exercise_3_05"
author: "Garland Durham"
date: "February 10, 2017"
output: html_document
---

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



```{r}
plot(gnp)
plot(log(gnp))
z=diff(log(gnp))
plot(z)
acf(z)
pacf(z)
```

Looks like maybe MA(2) or AR(1) or something.  Let's try fitting a few.

```{r}

(fit01 = arima( z, order=c(0,0,1) )) 
(fit02 = arima( z, order=c(0,0,2) )) 
(fit03 = arima( z, order=c(0,0,3) )) 

(fit10 = arima( z, order=c(1,0,0) )) 
(fit11 = arima( z, order=c(1,0,1) )) 
(fit12 = arima( z, order=c(1,0,2) )) 
(fit13 = arima( z, order=c(1,0,3) )) 

(fit20 = arima( z, order=c(2,0,0) ))
(fit21 = arima( z, order=c(2,0,1) )) 
(fit22 = arima( z, order=c(2,0,2) )) 
(fit23 = arima( z, order=c(2,0,3) )) 

(fit30 = arima( z, order=c(3,0,0) ))
(fit31 = arima( z, order=c(3,0,1) )) 
(fit32 = arima( z, order=c(3,0,2) )) 
(fit33 = arima( z, order=c(3,0,3) )) 

AIC( fit01, fit02, fit03, fit10, fit11, fit12, fit13, fit20, fit21, fit22, fit23, fit30, fit31, fit32, fit33)
BIC( fit01, fit02, fit03, fit10, fit11, fit12, fit13, fit20, fit21, fit22, fit23, fit30, fit31, fit32, fit33)
```

AIC prefers ARMA(3,2), BIC prefers AR((1).  This is not unusual.  AIC tends to overfit.

Let's look at some diagnostics for the AR(1).

```{r}
r = resid(fit10)
lags = 10
acf(r)
Box.test( r, 10, fitdf=2 )
qqnorm(r)
qqline(r)
jarque.bera.test(r)
```

There is no evidence of seasonality.  The AR(2) does a good job of eliminating autocorrelation, but the residuals don't look very normal.  We fail to reject the null hypothesis that the residuals are white noise based on the Box-Pierce test, but we reject that they are normall distributed based on the Jarque-Bera test.


















