---
title: "STAT481/581: Introduction to Time Series Analysis"
author: "Some practical forecasting issues"
date: "OTexts.org/fpp3/"
toc: true
colortheme: monashwhite
output:
  binb::monash:
    fig_width: 7
    fig_height: 3.5
    includes:
      in_header: header.tex
editor_options: 
  chunk_output_type: console
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache=TRUE, warning=FALSE, message=FALSE)
library(tidyverse)
library(fable)
library(tsibble)
library(tsibbledata)
library(feasts)
library(lubridate)
```

# Models for different frequencies

## Models for different frequencies

### Models for annual data

 * ETS, ARIMA, Dynamic regression
\pause

### Models for quarterly data

 * ETS, ARIMA/SARIMA, Dynamic regression, Dynamic harmonic regression, STL+ETS, STL+ARIMA
\pause

### Models for monthly data

 * ETS, ARIMA/SARIMA, Dynamic regression, Dynamic harmonic regression, STL+ETS, STL+ARIMA

## Models for different frequencies

### Models for weekly data

 * ARIMA/SARIMA, Dynamic regression, Dynamic harmonic regression, STL+ETS, STL+ARIMA, TBATS
\pause

### Models for daily, hourly and other sub-daily data

 * ARIMA/SARIMA, Dynamic regression, Dynamic harmonic regression, STL+ETS, STL+ARIMA, TBATS


# Ensuring forecasts stay within limits

## Positive forecasts
\fontsize{9}{9}\sf

```{r, fig.height=3.8}
eggs <- as_tsibble(fma::eggs)
eggs %>%
  model(ETS(log(value) ~ error("A") + trend("A") + season("N"))) %>%
  forecast(h=50) %>%
  autoplot(eggs)
```


# Forecast combinations

## Forecast combinations

### Clemen (1989)
"The results have been virtually unanimous: combining multiple forecasts leads to increased forecast accuracy. \dots In many cases one can make dramatic performance improvements by simply averaging the forecasts."

## Forecast combinations
\fontsize{9}{10}\sf\vspace*{-0.2cm}

```{r}
aus_cafe <- aus_retail %>%
  filter(Industry == "Cafes, restaurants and catering services") %>%
  summarise(Turnover = sum(Turnover))
fc <- aus_cafe %>%
  filter(Month <= yearmonth("2013 Sep")) %>%
  model(
    ETS = ETS(Turnover),
    ARIMA = ARIMA(Turnover)
  ) %>%
  mutate(
    Combination = (ETS + ARIMA)/2
  ) %>%
  forecast(h = "5 years")
```


## Forecast combinations
\fontsize{10}{10}\sf\vspace*{-0.2cm}

```{r combineplot, echo=TRUE, fig.height=4.2}
fc %>% autoplot(aus_cafe, level = NULL) +
  labs(x = "Year", y = "$ billion",
       title = "Australian monthly expenditure on eating out")
```

## Forecast combinations
\fontsize{10}{15}\sf
```{r combineaccuracy, dependson="combine1"}
fc %>% accuracy(aus_cafe)
```

# Missing values

## Missing values
\fontsize{12}{13}\sf

**Functions which can handle missing values**

 * `ARIMA()`
 * `TSLM()`
 * `NNETAR()`
 * `VAR()`
 * `FASSTER()`

**Models which cannot handle missing values**

 * `ETS()`
 * `STL()`
 * `TBATS()`

\pause

### What to do?
 1. Model section of data after last missing value.
 2. Estimate missing values with `interpolate()`.

## Missing values
\fontsize{12}{12}\sf
```{r}
gold <- as_tsibble(forecast::gold)
gold %>% autoplot(value)
```

## Missing values
\fontsize{12}{12}\sf
```{r, fig.height=3}
gold_complete <- gold %>%
  model(ARIMA(value)) %>%
  interpolate(gold)
gold_complete %>%
  autoplot(value, colour = "red") +
  autolayer(gold, value)
```

# Outliers

## Outliers
\fontsize{11}{13}\sf

```{r, fig.height=3.4}
fit <- gold %>%
  model(ARIMA(value))
augment(fit) %>% 
  mutate(stdres = .resid/sd(.resid, na.rm=TRUE)) %>% 
  filter(abs(stdres) > 10)
```
