---
title: "STAT 436 / 536 - Lecture 3"
date: September 5, 2018
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)
```

### Time Series Decomposition - Visually

```{r, echo = T, fig.align='center', fig.width=6, fig.height=4}
library(datasets)
library(ggplot2)
library(ggfortify)
data(AirPassengers)
autoplot(AirPassengers) + ylim(0,650) +  
  labs(title="Monthly Airline Passenger Count", y="Number of Passengers(thousands)", x= 'Year') 
```

```{r, echo = T, fig.align='center', fig.width=6, fig.height=4}
autoplot(aggregate(AirPassengers)) + ylim(0,6000) +  
  labs(title="Annual Airline Passenger Count", y="Number of Passengers(thousands)", x= 'Year') 
```

```{r, echo = T, fig.align='center', fig.width=6, fig.height=4}
boxplot(AirPassengers ~ cycle(AirPassengers), ylim = c(0,700), xlab = 'Month',
        ylab = "Number of Passengers(thousands)", main ="Monthly Airline Passenger Count")

```
\newpage

#### Exercise: Mauna Loa $CO_2$ Observations
Using the Mauna Loa dataset, create three figures: 1.) the complete time series, 2.) annual decomposition, and 3.) monthly series. Think carefully about the appropriate scale for the y-axis of these plots.

```{r, echo =T}
data(co2)
```

\newpage

### Time Series Decomposition - Modeling and Notation
#### Time Series Mathematical Notation
- Let $\{x_t\} = \{x_1, \dots, x_T\}$ be a time series of length $T$. \vfill
- A time series is a  \vfill
- The hat notation will often be used for prediction, \vfill
- For those of you familiar with Bayesian notation,  \vfill

#### Time Series Models

- We have seen how to visually decompose a time series into an annual trend and a seasonal pattern. So this additive model could be written as:
\vfill
\vfill
- What is missing from the previous model? \vfill
- **Question:** how should we think about estimating a trend?
    - \vfill
    - \vfill
  \newpage
- **Question**: Now how about the seasonal effect?
  -  \vfill
  -  \vfill
  - \vfill
  -   \vfill
- With a ts object in R, the `decompose()` function will automatically do this procedure.
```{r, eval = F}
decompose(AirPassengers)
```
\vfill
  