-
Notifications
You must be signed in to change notification settings - Fork 2
/
Forecasting 3-7 - ARMA Seasonal Models.Rmd
294 lines (199 loc) · 6.46 KB
/
Forecasting 3-7 - ARMA Seasonal Models.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
---
output:
xaringan::moon_reader:
css: "my-theme.css"
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
---
layout: true
.hheader[<a href="index.html">`r fontawesome::fa("home", fill = "steelblue")`</a>]
---
```{r setup, include=FALSE, message=FALSE}
options(htmltools.dir.version = FALSE, servr.daemon = TRUE)
knitr::opts_chunk$set(fig.height=5, fig.align="center")
library(huxtable)
```
class: center, middle, inverse
# Seasonal ARIMA Models
.futnote[Eli Holmes, UW SAFS]
.citation[[email protected]]
---
```{r load_data, echo=FALSE, message=FALSE, warning=FALSE}
library(ggplot2)
library(gridExtra)
library(reshape2)
library(tseries)
library(forecast)
```
## Seasonality
Load the chinook salmon data set
```{r}
load("chinook.RData")
head(chinook)
```
---
The data are monthly and start in January 1990. To make this into a ts object do
```{r}
chinookts <- ts(chinook$log.metric.tons, start=c(1990,1),
frequency=12)
```
`start` is the year and month and frequency is the number of months in the year.
Use `?ts` to see more examples of how to set up ts objects.
---
## Plot seasonal data
```{r}
plot(chinookts)
```
---
## Seasonal ARIMA model
Seasonally differenced data:
$$z_t = x_t - x_{t+s} - m$$
Basic structure of a seasonal AR model
$z_t$ = AR(p) + AR(season) + AR(p+season)
Example AR(1) non-seasonal part + AR(1) seasonal part
$$z_t = \phi_1 z_{t-1} + \Phi_1 z_{t-12} - \phi_1\Phi_1 z_{t-13}$$
---
## Notation
ARIMA (p,d,q)(ps,ds,qs)S
ARIMA (1,0,0)(1,1,0)[12]
Notice we are modeling $x$ this year in Jan (say) as a function of $x$ in Jan last year.
---
## Seasonal models
Let's imagine that we can describe our data as a combination of the mean trend, a seasonal term, and error.
$$x_t = \mu t+ s_t + w_t$$
Let's imagine that the seasonal term is just a constant based on month and doesn't change with time.
$$s_t = f(month)$$
---
We want to remove the $s_t$ with differencing so that we can model $e_t$. We can solve for $x_{t+1}$ by using $x_{t-s}$ where $s$ is the seasonal length (e.g. 12 if season is yearly).
When we take the first seasonal difference, we get
$$\Delta_s x_t = \mu(t-(t-s)) + s_t - s_{t-s} + w_t - w_{t-s} = \mu s + w_t - w_{t-s}$$
The $s_t-s_{t-s}$ disappears because $s_t = s_{t-s}$ when the seasonal effect is just a function of the month. Depending on what $m_t$ is, we might be done or we might have to do a first difference. Notice that the error term is a moving average in the seasonal part.
---
```{r, echo=FALSE}
set.seed(123)
yr=10
s=12
sd=sqrt(.1)
mu=0.05
st=sin(pi*(1:(s*yr))/(s/2))
mt=mu*(1:(s*yr))
et=rnorm(s*yr,0,sd)
xt=mt+st+et
xt=ts(xt,start=1,frequency=12)
plot(xt)
```
```{r}
plot(diff(xt,lag=12))
```
---
We can recover the model parameters with `Arima()`. Note the drift term is returned at $\mu$ not $\mu s$.
```{r}
forecast::Arima(xt, seasonal=c(0,1,1), include.drift=TRUE)
```
---
`auto.arima()` identifies a ARIMA(0,0,0)(0,1,2)[12].
```{r}
forecast::auto.arima(xt, stepwise=FALSE)
```
---
## Seasonal model with changing season
Let's imagine that our seasonality is increasing over time.
$$s_t = \beta \times year \times f(month)\times$$
When we take the first seasonal difference, we get
$$\Delta_s x_t = \mu(t-(t-s)) + \beta f(month)\times (year - (year-1)) + w_t - w_{t-s} \\ = \mu s + \beta f(month) + w_t - w_{t-s}$$
---
We need to take another seasonal difference to get rid of the $f(month)$ which is not a constant; it is different for different months as it is our seasonality.
$$\Delta^2_{s} x_t = w_t - w_{t-s} - w_{t-s} + w_{t-2s}=w_t - 2w_{t-s} + w_{t-2s}$$
So our ARIMA model should be ARIMA(0,0,0)(0,2,2)
---
```{r, echo=FALSE}
set.seed(123)
yr=10
s=12
sd=sqrt(.1)
mu=0.05
beta=.2
st=(1+beta*rep(1:yr, each=s))*sin(pi*(1:(s*yr))/(s/2))
st=beta*rep(1:yr, each=s)*sin(pi*(1:(s*yr))/(s/2))
mt=mu*(1:(s*yr))
et=rnorm(s*yr,0,sd)
xt=mt+st+et
xt=ts(xt,start=1,frequency=12)
plot(xt)
```
---
But we can recover the model with `Arima()`. Note the drift term is returned at $\mu$ not $\mu s$.
```{r}
forecast::Arima(xt, seasonal=c(0,2,2))
```
---
`auto.arima()` again has problems and returns many Infs; turn on `trace=TRUE` to see the problem.
```{r}
forecast::auto.arima(xt, stepwise=FALSE)
```
---
## Seasonal model with changing season #2
Let's imagine that our seasonality increases and then decreases.
$$s_t = (a y^2-b y+h) f(month)$$
```{r, echo=FALSE}
set.seed(100)
yr=10
s=12
sd=sqrt(.1)
mu=0.05
beta=.1*(rep(1:yr, each=s)^2-2*5*rep(1:yr, each=s)+30)
st=beta*sin(pi*(1:(s*yr))/(s/2))
mt=mu*(1:(s*yr))
et=rnorm(s*yr,0,sd)
xt=mt+st+et
xt=ts(xt,start=1,frequency=12)
plot(xt)
```
---
Then we need to take 3 seasonal differences to get rid of the seasonality. The first will get rid of the $h f(month)$, the next will get rid of $by$ (year) terms and $y^2$ terms, the third will get rid of extra $y$ terms introduced by the 2nd difference. The seasonal differences will get rid of the linear trend also.
$$\Delta^3_{s} x_t = w_t - 2w_{t-s} + w_{t-2s}-w_{t-s}+2w_{t-2s}-w_{t-3s}=w_t - 3w_{t-s} + 3w_{t-2s}-w_{t-3s}$$
So our ARIMA model should be ARIMA(0,0,0)(0,3,3).
---
## `auto.arima()` for seasonal ts
`auto.arima()` will recognize that our data has season and fit a seasonal ARIMA model to our data by default. We will define the training data up to 1998 and use 1999 as the test data.
```{r}
traindat <- window(chinookts, c(1990,10), c(1998,12))
testdat <- window(chinookts, c(1999,1), c(1999,12))
fit <- forecast::auto.arima(traindat)
fit
```
---
## Forecast using seasonal model
```{r}
fr <- forecast::forecast(fit, h=12)
plot(fr)
points(testdat)
```
---
## Missing values
Missing values are ok when fitting a seasonal ARIMA model
```{r echo=FALSE}
plot(fr)
```
---
## Summary
Basic steps for identifying a seasonal model. **forecast** automates most of this.
* Check that you have specified your season correctly in your ts object.
* Plot your data. Look for trend, seasonality and random walks.
---
## Summary
* Use differencing to remove season and trend.
* Season and no trend. Take a difference of lag = season
* No seasonality but a trend. Try a first difference
* Both. Do both types of differences
* Neither. No differencing.
* Random walk. First difference.
* Parametric looking curve. Tranform.
---
## Summary
* Examine the ACF and PACF of the differenced data.
* Look for patterns (spikes) at seasonal lags
* Estimate likely models and compare with model selection criteria (or cross-validation). Use `TRACE=TRUE`
* Do residual checks.