forked from robjhyndman/ETC3550Slides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-practical.Rmd
196 lines (151 loc) · 4.78 KB
/
12-practical.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
title: "ETC3550: Applied forecasting for business and economics"
author: "Ch12. Some practical forecasting issues"
date: "OTexts.org/fpp2/"
fontsize: 14pt
output:
beamer_presentation:
fig_width: 7
fig_height: 3.5
highlight: tango
theme: metropolis
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache=TRUE, warning=FALSE, message=FALSE)
library(fpp2)
options(digits=3,width=50)
```
# Models for different frequencies
## Models for different frequencies
\vspace*{-0.2cm}
### 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{12}{12}\sf
```{r}
eggs %>%
ets(model="AAN", damped=FALSE, lambda=0) %>%
forecast(h=50, biasadj=TRUE) %>%
autoplot()
```
# 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{10}{10}\sf\vspace*{-0.2cm}
```r
train <- window(auscafe, end=c(2012,9))
h <- length(auscafe) - length(train)
ETS <- forecast(ets(train), h=h)
ARIMA <- forecast(auto.arima(train, lambda=0, biasadj=TRUE),
h=h)
STL <- stlf(train, lambda=0, h=h, biasadj=TRUE)
NNAR <- forecast(nnetar(train), h=h)
TBATS <- forecast(tbats(train, biasadj=TRUE), h=h)
Combination <- (ETS[["mean"]] + ARIMA[["mean"]] +
STL[["mean"]] + NNAR[["mean"]] + TBATS[["mean"]])/5
autoplot(auscafe) +
autolayer(ETS, series="ETS", PI=FALSE) +
autolayer(ARIMA, series="ARIMA", PI=FALSE) +
autolayer(STL, series="STL", PI=FALSE) +
autolayer(NNAR, series="NNAR", PI=FALSE) +
autolayer(TBATS, series="TBATS", PI=FALSE) +
autolayer(Combination, series="Combination") +
xlab("Year") + ylab("$ billion") +
ggtitle("Australian monthly expenditure on eating out")
```
## Forecast combinations
\fontsize{10}{10}\sf\vspace*{-0.2cm}
```{r combine1, message=FALSE, warning=FALSE, echo=FALSE}
train <- window(auscafe, end=c(2012,9))
h <- length(auscafe) - length(train)
ETS <- forecast(ets(train), h=h)
ARIMA <- forecast(auto.arima(train, lambda=0, biasadj=TRUE),
h=h)
STL <- stlf(train, lambda=0, h=h, biasadj=TRUE)
NNAR <- forecast(nnetar(train), h=h)
TBATS <- forecast(tbats(train, biasadj=TRUE), h=h)
Combination <- (ETS[["mean"]] + ARIMA[["mean"]] +
STL[["mean"]] + NNAR[["mean"]] + TBATS[["mean"]])/5
```
```{r combineplot, dependson="combine1", echo=FALSE, fig.height=4.8}
autoplot(auscafe) +
autolayer(ETS, series="ETS", PI=FALSE) +
autolayer(ARIMA, series="ARIMA", PI=FALSE) +
autolayer(STL, series="STL", PI=FALSE) +
autolayer(NNAR, series="NNAR", PI=FALSE) +
autolayer(TBATS, series="TBATS", PI=FALSE) +
autolayer(Combination, series="Combination") +
xlab("Year") + ylab("$ billion") +
ggtitle("Australian monthly expenditure on eating out")
```
## Forecast combinations
\fontsize{11}{15}\sf
```{r combineaccuracy, dependson="combine1"}
c(ETS = accuracy(ETS, auscafe)["Test set","RMSE"],
ARIMA = accuracy(ARIMA, auscafe)["Test set","RMSE"],
`STL-ETS` = accuracy(STL, auscafe)["Test set","RMSE"],
NNAR = accuracy(NNAR, auscafe)["Test set","RMSE"],
TBATS = accuracy(TBATS, auscafe)["Test set","RMSE"],
Combination =
accuracy(Combination, auscafe)["Test set","RMSE"])
```
# Missing values
## Missing values
\fontsize{14}{15}\sf
**Functions which can handle missing values**
* `auto.arima()`, `Arima()`
* `tslm()`
* `nnetar()`
**Models which cannot handle missing values**
* `ets()`
* `stl()`
* `stlf()`
* `tbats()`
\pause
### What to do?
1. Model section of data after last missing value.
2. Estimate missing values with `na.interp()`.
## Missing values
\fontsize{12}{12}\sf
```{r}
autoplot(gold)
```
## Missing values
\fontsize{12}{12}\sf
```{r, fig.height=3}
gold %>% na.interp() %>%
autoplot(series="Interpolated") +
autolayer(gold, series="Original") +
scale_color_manual(
values=c(`Interpolated`="red",`Original`="gray"))
```
# Outliers
## Outliers
```{r, fig.height=3.4}
autoplot(gold)
```
## Outliers
```{r, fig.height=3.4}
tsoutliers(gold)
```
## Outliers
```{r, fig.height=3.4}
gold %>% tsclean() %>% autoplot()
```