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

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

```{r}
plot.ts( cbind(cmort, part, tempr))
pairs( cbind( cmort, part, tempr ))
```


```{r}
trend = time(cmort)
temp = tempr - mean(tempr)
temp2 = temp^2

fit01 = lm( cmort ~ trend + temp + temp2 + part )
summary(fit01)
r = resid( fit01 )
plot(r, type="l")
acf2(r)
```

The residuals are correlated.  Based on ACF and PACF, it looks like AR(2) might be a good model.  But, we'll try a few other alternatives as well.

```{r}
(fit00 = arima( cmort, order=c(0,0,0), xreg=trend+temp+temp2+part))
(fit10 = arima( cmort, order=c(1,0,0), xreg=trend+temp+temp2+part))
(fit01 = arima( cmort, order=c(0,0,1), xreg=trend+temp+temp2+part))
(fit20 = arima( cmort, order=c(2,0,0), xreg=trend+temp+temp2+part))
(fit11 = arima( cmort, order=c(1,0,1), xreg=trend+temp+temp2+part))
(fit02 = arima( cmort, order=c(0,0,2), xreg=trend+temp+temp2+part))
(fit30 = arima( cmort, order=c(3,0,0), xreg=trend+temp+temp2+part))
(fit21 = arima( cmort, order=c(2,0,1), xreg=trend+temp+temp2+part))
(fit12 = arima( cmort, order=c(1,0,2), xreg=trend+temp+temp2+part))
(fit03 = arima( cmort, order=c(0,0,3), xreg=trend+temp+temp2+part))
```


```{r}
AIC(fit00,fit10,fit01,fit20,fit11,fit02,fit30,fit21,fit12,fit03)
BIC(fit00,fit10,fit01,fit20,fit11,fit02,fit30,fit21,fit12,fit03)
```

AIC and BIC both suggest AR(2) as the preferred model.
Let's look at some diagnostics...  (The sarima function provides diagnostics automatically,
so let's just use that.)

```{r, fig.height=8, fig.width=8}
sarima(cmort, 2, 0, 0, xreg=trend+temp+temp2+part, details=FALSE )
```

Looks good.  Residuals are approximately normal.  No significant autocorrelation.













```



