---
title: "Time Series Patterns"
description: |
  Vocabulary for describing visual structure in time series.
author:
  - name: Kris Sankaran
    affiliation: UW Madison
date: 03-02-2021
output:
  distill::distill_article:
    self_contained: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(cache = FALSE, message = FALSE, warning = FALSE, echo = TRUE)
```

_[Reading](https://otexts.com/fpp3/tspatterns.html), [Recording](https://mediaspace.wisc.edu/media/Week%206%20%5B2%5D%20Time%20Series%20Patterns/1_i43mqixm), [Rmarkdown](https://raw.githubusercontent.com/krisrs1128/stat479/master/_posts/2021-02-23-week6-2/week6-2.Rmd)_

```{r}
library("dplyr")
library("tsibble")
library("tsibbledata")
library("fpp2")
library("ggplot2")
theme_set(theme_minimal())
```

1. There are a few structures that are worth keeping an eye out for whenever you
plot a single time series. We’ll review the main vocabulary in these notes.

2. Vocabulary

	* Trend: A long-run increase or decrease in a time series.
	* Seasonal: A pattern that recurs over a fixed and known period.
	* Cyclic: A rising and falling pattern that does not occur over a fixed or known period.

A few series with different combinations of these patterns are shown below.

```{r}
ggplot(as_tsibble(qauselec)) +
  geom_line(aes(x = index, y = value)) +
  labs(title = "Australian Quarterly Electricity Production")

ggplot(as_tsibble(hsales)) +
  geom_line(aes(x = index, y = value)) +
  labs(title = "Housing Sales")

ggplot(as_tsibble(ustreas)) +
  geom_line(aes(x = index, y = value)) +
  labs(title = "US Treasury Bill Contracts")

ggplot(as_tsibble(diff(goog))) +
  geom_line(aes(x = index, y = value)) +
  labs(title = "Google Stock Prices")
```
	
3. A series can display combinations of these patterns at once. Further, the
same data can exhibit different patterns depending on the scale at which it is
viewed. For example, though a dataset might seem seasonal at short time scales,
a long-term trend might appear after zooming out. This is visible in the
electricity production plot above.

4. Finally, it's worth keeping in mind that real-world structure can be much more complicated than any of these patterns. For example, the plot below shows the number of passengers on flights from Melbourne to Sydney between 1987 and 1992. You can see a period when no flights were made and a trial in 1992 where economy seats were switched to business seats.

```{r}
melbourne_sydney <- ansett %>%
  filter(Airports == "MEL-SYD") # challenge: facet by Airports, instead of filtering

ggplot(melbourne_sydney) +
  geom_line(aes(x = Week, y = Passengers, col = Class))
```