---
title: "STAT 436 / 536 - Lecture 13"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.align= 'center')
knitr::opts_chunk$set(fig.height= 6)
knitr::opts_chunk$set(fig.width = 6)
library(tidyverse)
library(gridExtra)
library(readr)
library(ggfortify)
```

## ARMA Process

- Thus far we have fit either AR(p) processes
$$x_t = \alpha_1 x_{t-1} + \alpha_2 x_{t-2} + \dots + \alpha_p x_{t-p} + w_t$$
*or* MA(q) processes
$$x_t = w_t + \beta_1 w_{t-1} + \dots \beta_q w_{t-q}$$
\vfill
- However, it would be reasonable to want to fit both at the same time. This results an ARMA model.

\vfill
- ARMA models can be written using the polynomial expression with the characteristic equations:
\vfill
There are several key points about an ARMA(p,q) process:

a. The process is stationary when the absolute value of the roots of $\theta$ all exceed 1.
\vfill
b. The process is invertible when the absolute value of the roots of $\phi$ all exceed 1.
\vfill
c. An AR(p) model is
\vfill
d. Similarly, the MA(q) model is 
\vfill
e. When fitting to data, an ARMA model will often be more parameter efficient than a single AR or MA model.
\vfill
f. When $\theta$ and $\phi$ share a common factor, a stationary model can be simplified. For example, with
$$(1-\frac{1}{2}B)(1-\frac{1}{3}B)x_t = (1-\frac{1}{2}B)w_t$$
can be simplified to
$$(1-\frac{1}{3}B)x_t = w_t.$$
\newpage

### Simulation and Model Fitting

- The r function `arima.sim` allows simulation from ARMA processes.
```{r}
set.seed(10312018)
#install.packages('fpp')
library(fpp)
ar.1 <- arima.sim(n = 100, model = list(ar = .7))
ggtsdisplay(ar.1)
```
\vfill
```{r}
#install.packages('fpp')
library(fpp)
ma.1 <- arima.sim(n = 100, model = list(ma = .7))
ggtsdisplay(ma.1)
```

\vfill
- Consider fitting the `arma.1` series that is simulated from an AR(1) process using both an AR model and a MA model. How should we choose?
```{r, eval = F}
arima(ar.1, order = c(1,0,0))
arima(ar.1, order = c(0,0,1))
arima(ma.1, order = c(1,0,0))
arima(ma.1, order = c(0,0,1))
```
\vfill
- We have seen the predictive framework, 

```{r, eval = F}
auto.arima(ar.1, max.d = 0)
auto.arima(ma.1, max.d = 0)
```

