-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchapter_7_lesson_2.qmd
640 lines (459 loc) · 17.6 KB
/
chapter_7_lesson_2.qmd
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
---
title: "Seasonal ARIMA Models"
subtitle: "Chapter 7: Lesson 2"
format: html
editor: source
sidebar: false
---
```{r}
#| include: false
source("common_functions.R")
```
```{=html}
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
```
## Learning Outcomes
{{< include outcomes/_chapter_7_lesson_2_outcomes.qmd >}}
## Preparation
- Read Section 7.3
## Learning Journal Exchange (10 min)
- Review another student's journal
- What would you add to your learning journal after reading another student's?
- What would you recommend the other student add to their learning journal?
- Sign the Learning Journal review sheet for your peer
### Definition of Seasonal ARIMA (SARIMA) Models
We can add a seasonal component to an ARIMA model. We call this model a Seasonal ARIMA model, or a SARIMA model.
We use differencing at a lag equal to the number of seasons, $s$. This allows us to remove additional seasonal effects that carry over from one cycle to another.
Recall that if we use a difference of lag 1 to remove a linear trend, we introduce a moving average term. The same thing happens when we introduce a difference with lag $s$.
For the lag $s$ values, we can apply an autoregressive (AR) component at lag $s$ with $P$ parameters, an integrated (I) component with parameter $D$, and a moving average (MA) component with $Q$ terms.
This yields the full SARIMA model.
::: {.callout-note icon=false title="Definition of SARIMA Models"}
A Seasonal ARIMA model, or SARIMA model with $s$ seasons can be expressed as:
$$
\Theta_P \left( \mathbf{B}^s \right) \theta_p \left( \mathbf{B} \right) \left( 1 - \mathbf{B}^s \right)^D \left( 1 - \mathbf{B} \right)^d x_t = \Phi_Q \left( \mathbf{B}^s \right) \phi_q \left( \mathbf{B} \right) w_t
$$
where
- $p$, $d$, and $q$ are the parameters for the $ARIMA(p, d, q)$ model applied to the time series and the differences are taken with a lag of 1,
- $s$ is the number of seasons, and
- $P$, $D$, and $Q$ are the parameters for the $ARIMA(P, D, Q)$ model applied to the time series where the differences are taken across a lag of $s$.
$\Theta_P \left( \mathbf{B}^s \right)$, $\theta_p \left( \mathbf{B} \right)$, $\Phi_Q \left( \mathbf{B}^s \right)$, and $\phi_q \left( \mathbf{B} \right)$ are polynomials of degree $P$, $p$, $Q$, and $q$, respectively.
We denote this model as $ARIMA(p,d,q)(P,D,Q)_s$ or $ARIMA(p,d,q)(P,D,Q)[s]$.
:::
Looking closely at this model, we can see it is the combination of the ARIMA model
$$
\theta_p \left( \mathbf{B} \right) \left( 1 - \mathbf{B} \right)^d x_t = \phi_q \left( \mathbf{B} \right) w_t
$$
and the same model, after applying a difference across $s$ seasons
$$
\Theta_P \left( \mathbf{B}^s \right) \left( 1 - \mathbf{B}^s \right)^D x_t = \Phi_Q \left( \mathbf{B}^s \right) w_t
$$
### Special Cases of a Seasonal ARIMA Model
#### Stationary SARIMA Models
::: {.callout-note icon=false title="Stationarity of SARIMA Models"}
The SARIMA model is, in general, non-stationary. However, there is a special case in which it is stationary. An $ARIMA(p,d,q)(P,D,Q)_s$ will be stationary if the following conditions are satisfied:
1. If $D = d = 0$, and
2. The roots of the characteristic equation
$$
\Theta_P \left( \mathbf{B}^s \right) \theta_p \left( \mathbf{B} \right) = 0
$$
are all greter than 1 in absolute value.
:::
#### A Simple AR Model with a Seasonal Period of $s$ Units
$ARIMA(0,0,0)(1,0,0)_{s}$ is given as
$$
x_t = \alpha x_{t-s} + w_t
$$
This model would be appropriate for data where there are $s$ seasons and the value in the season exactly one cycle prior affects the current value. This model is stationary when $\left| \alpha^{1/s} \right| > 1$.
#### Stochastic Trends with Seasonal Influences
Many time series with stochastic trends also have seasonal influences. We can extend the previous model as:
$$
x_t = x_{t-1} + \alpha x_{t-s} - \alpha x_{t-(s+1)} + w_t
$$
This is equivalent to
$$
\underbrace{\left( 1 - \alpha \mathbf{B}^{12} \right)}_{\Theta_1 \left( \mathbf{B}^{12} \right)} \left( 1 - \mathbf{B} \right) x_t = w_t
$$
which is $ARIMA(0,1,0)(1,0,0)_s$.
Note that we could have written this model as:
$$
\nabla x_t = \alpha \nabla x_{t-s} + w_t
$$
This makes it clear that under this model, the *change* at time $t$ depends on the change at the corresponding time in the previous cycle.
Note that $\left( 1 - \mathbf{B} \right)$ is a factor in the characteristic polynomial, so this model will be non-stationary.
#### Simple Quarterly Seasonal Moving Average Model (with no Trend)
We can write a simple quarterly seasonal moving average model as
$$
x_t = \underbrace{w_t - \beta w_{t-4}}_{\left( 1 - \beta \mathbf{B}^{4} \right) w_t}
$$
This is an $ARIMA(0,0,0)(0,0,1)_4$ model.
#### Quarterly Seasonal Moving Average Model with a Stocastic Trend
We can add a stochastic trend to the previous model by including first-order differences:
$$
x_t = x_{t-1} + w_t - \beta w_{t-4}
$$
This is an $ARIMA(0,1,0)(0,0,1)_4$ model.
#### Quarterly Seasonal Moving Average Model where the Seasonal Terms Contain a Stocastic Trend
We can allow the seasonal components to include a stochastic trend. We apply differencing at the seasonal period. This yields the model
$$
x_t = x_{t-4} + w_t - \beta w_{t-4}
$$
This is an $ARIMA(0,0,0)(0,1,1)_4$ process.
::: {.callout-warning icon=false title="Effects of Differencing"}
Recall that lag $s$ differencing will remove a linear trend, however if there is a linear trend, differencing at lag 1 will introduce an AR process in the residuals. If a linear model is appropriate in a, say, quarterly series with additive seasonals, then the model could be
$$
x_t = a + bt + s_{[t]} + w_t
$$
where $[t]$ is the modulus operator, or $[t]$ is the remainder when $t$ is divided by $4$. Another way to view this is to note that for a quarterly time series, $[t] = [t-4]$.
If we apply first-order differencing at lag 4, we get
\begin{align*}
\left( 1 - \mathbf{B}^{4} \right) x_t
&= x_t - x_{t-4} \\
&= \left(a + bt + s_{[t]} + w_t \right) - \left(a + b(t-4) + s_{[t-4]} + w_{t-4} \right) \\
&= \left(a + bt + s_{[t]} + w_t \right) - \left(a + b(t-4) + s_{[t]} + w_{t-4} \right) \\
&= 4b + w_t - w_{t-4}
\end{align*}
This is an $ARIMA(0,0,0)(0,1,1)_4$ process with constant term of $4b$.
If we apply first-order differencing at lag 1 and then do the differencing at lag 4, we get the following process:
\begin{align*}
\left( 1 - \mathbf{B}^{4} \right) \left( 1 - \mathbf{B} \right) x_t
&= \left( 1 - \mathbf{B}^{4} \right) \left( x_t - \mathbf{B}x_t \right) \\
&= \left( 1 - \mathbf{B}^{4} \right) \left[ \left( a + bt + s_{[t]} + w_t \right) - \mathbf{B} \left( a + bt + s_{[t]} + w_t \right) \right] \\
&= \left( 1 - \mathbf{B}^{4} \right) \left[ \left( a + bt + s_{[t]} + w_t \right) - \left( a + b(t-1) + s_{[t-1]} + w_{t-1} \right) \right] \\
&= \left( 1 - \mathbf{B}^{4} \right) \left[ \left( s_{[t]} + w_t \right) - \left( -b + s_{[t-1]} + w_{t-1} \right) \right] \\
&= \left( 1 - \mathbf{B}^{4} \right) \left( b + s_{[t]} - s_{[t-1]} + w_t - w_{t-1} \right) \\
&= \left( b + s_{[t]} - s_{[t-1]} + w_t - w_{t-1} \right) - \mathbf{B}^{4} \left( b + s_{[t]} - s_{[t-1]} + w_t - w_{t-1} \right) \\
&= \left( b + s_{[t]} - s_{[t-1]} + w_t - w_{t-1} \right) - \left( b + s_{[t-4]} - s_{[t-5]} + w_{t-4} - w_{t-5} \right) \\
&= \left( s_{[t]} - s_{[t-1]} + w_t - w_{t-1} \right) - \left( s_{[t]} - s_{[t-1]} + w_{t-4} - w_{t-5} \right) \\
&= w_t - w_{t-1} - w_{t-4} + w_{t-5}
\end{align*}
This represents an $ARIMA(0,1,1)(0,1,1)_4$ process without a constant term.
:::
## Class Activity: Seasonal ARIMA Models - Retail: General Merchandise Stores (10 min)
```{r}
#| label: fig-PaintWallpaperRetailSideBySidePlot
#| fig-cap: "Time plots of the retail sales in General Merchandise stores; time plot of the timt series (left) and the natural logarithm of the time series (right)"
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
#| fig-height: 3.5
# Read in retail sales data for "Full-Service Restaurants"
retail_ts <- rio::import("https://byuistats.github.io/timeseries/data/retail_by_business_type.parquet") |>
filter(naics == 452) |>
mutate( month = yearmonth(as_date(month)) ) |>
na.omit() |>
as_tsibble(index = month)
retail_plot_raw <- retail_ts |>
autoplot(.vars = sales_millions) +
labs(
x = "Month",
y = "sales_millions",
title = "Sales (in millions)",
subtitle = "General Merchandise Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
retail_plot_log <- retail_ts |>
autoplot(.vars = log(sales_millions)) +
labs(
x = "Month",
y = "log(sales_millions)",
title = "Log of Sales (in millions)",
subtitle = "General Merchandise Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
retail_plot_raw | retail_plot_log
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Based on the figure above, should the logarithm be applied to the time series? Justify your answer.
:::
Here are a few models we could fit to this time series.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| label: get_model
#| warning: false
#| output: false
retail_ts |>
model(
ar_model = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 0) +
PDQ(1, 0, 0), approximation = TRUE),
ma_model = ARIMA(sales_millions ~ 1 +
pdq(0, 1, 1) +
PDQ(0, 0, 1), approximation = TRUE),
arima_102012 = ARIMA(sales_millions ~ 1 +
pdq(1, 0, 2) +
PDQ(0, 1, 2), approximation = TRUE),
arima_102102 = ARIMA(sales_millions ~ 1 +
pdq(1, 0, 2) +
PDQ(1, 0, 2), approximation = TRUE),
arima_112112 = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 2) +
PDQ(1, 1, 2), approximation = TRUE),
arima_012012 = ARIMA(sales_millions ~ 1 +
pdq(0, 1, 2) +
PDQ(0, 1, 2), approximation = TRUE),
arima_111111 = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 1) +
PDQ(1, 1, 1), approximation = TRUE),
) |>
glance()
```
```{r}
#| echo: false
#| warning: false
retail_ts |>
model(
ar_model = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 0) +
PDQ(1, 0, 0), approximation = TRUE),
ma_model = ARIMA(sales_millions ~ 1 +
pdq(0, 1, 1) +
PDQ(0, 0, 1), approximation = TRUE),
arima_102012 = ARIMA(sales_millions ~ 1 +
pdq(1, 0, 2) +
PDQ(0, 1, 2), approximation = TRUE),
arima_112112 = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 2) +
PDQ(1, 1, 2), approximation = TRUE),
arima_012012 = ARIMA(sales_millions ~ 1 +
pdq(0, 1, 2) +
PDQ(0, 1, 2), approximation = TRUE),
arima_111111 = ARIMA(sales_millions ~ 1 +
pdq(1, 1, 1) +
PDQ(1, 1, 1), approximation = TRUE),
) |>
glance() |>
select(-ar_roots, -ma_roots) |>
display_table()
```
Here is the model automatically selected by R.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
best_fit_retail <- retail_ts |>
model(
ar_model = ARIMA(sales_millions ~ 1 +
pdq(0:2, 0:2, 0:2) +
PDQ(0:2, 0:2, 0:2), approximation = TRUE))
best_fit_retail
```
This is the acf and pacf of the residuals from the automatically selected model.
```{r}
#| label: fig-retailACFandPACFofResiduals
#| fig-cap: "Correlogram (left) and partial correlogram (right) of the residuals from the best-fitting model"
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
acf_plot <- best_fit_retail |>
residuals() |>
feasts::ACF() |>
autoplot()
pacf_plot <- best_fit_retail |>
residuals() |>
feasts::PACF() |>
autoplot()
acf_plot | pacf_plot
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- What do you notice in the acf and pacf plots?
:::
```{r}
#| echo: false
#| warning: false
#| output: false
#| include: false
best_fit_retail |>
glance()
```
Here are the coefficients from the automatically-selected model.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
#| output: false
coefficients(best_fit_retail)
```
```{r}
#| echo: false
coefficients(best_fit_retail) |>
display_table()
```
We can forecast future values of this time series using our model.
```{r}
#| label: fig-retailforecast
#| fig-cap: "Five-year forecast of the time series based on the best-fitting model"
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
best_fit_retail |>
forecast(h = "60 months") |>
autoplot(retail_ts) +
labs(
x = "Month",
y = "Total U.S. Sales in Millions",
title = "Forecasted Sales (in millions)",
subtitle = "General Merchandise Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
```{r}
#| echo: false
#| include: false
#| eval: false
############################################
#### This does not work with these data ####
############################################
# Very slow, but you can see all fits.
get_arima <- function(p, d, q, P, D, Q, data = retail_ts) {
model(data, arima = ARIMA(sales_millions ~ 1 +
pdq(p, d, q) + PDQ(P, D, Q), approximation = TRUE))
}
get_aic <- function(fit) {
gfit <- glance(fit)
if (nrow(gfit) != 0) {
pull(gfit, AIC)
} else {
NA
}
}
all_possible <- expand_grid(
p = 0:2, d = 0:2, q = 0:2,
P = 0:2, D = 0:2, Q = 0:2)
all_possible <- all_possible |>
mutate(
fit = purrr::pmap(all_possible, get_arima),
aic = purrr::map_dbl(fit, ~get_aic(.x))
)
filter(all_possible, aic == min(aic, na.rm = TRUE))
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- How well does the forecast generated by this model appear to work?
:::
## Small-Group Activity: Seasonal ARIMA Models - Retail: Shoe Stores (20 min)
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Repeat the analysis above for the retail sales for shoe stores (NAICS code 4482).
- Create time plots of the data and the logarithm of the data.
- Is it appropriate to take the logarithm of the time series? (Use the appropriate time series for the following.)
- Find the best-fitting SARIMA model for the time series.
- Determine the model coefficients.
- Use the acf and pacf plots of the residuals to assess whether this model adequately models the time series.
- Use your model to forecast the value of the time series over the next five years.
- How well does the forecast generated by this model appear to work?
- Did the downward COVID spike seriously affect the applicability of this model?
:::
## Homework Preview (5 min)
- Review upcoming homework assignment
- Clarify questions
::: {.callout-note icon=false}
## Download Homework
<a href="https://byuistats.github.io/timeseries/homework/homework_7_2.qmd" download="homework_7_2.qmd"> homework_7_2.qmd </a>
:::
<a href="javascript:showhide('Solutions1')"
style="font-size:.8em;">Small-Group Activity: Seasonal ARIMA Models - Retail: Shoe Stores</a>
::: {#Solutions1 style="display:none;"}
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
# Read in retail sales data for "Full-Service Restaurants"
retail_ts <- rio::import("https://byuistats.github.io/timeseries/data/retail_by_business_type.parquet") |>
filter(naics == 4482) |>
mutate( month = yearmonth(as_date(month)) ) |>
na.omit() |>
as_tsibble(index = month)
retail_plot_raw <- retail_ts |>
autoplot(.vars = sales_millions) +
labs(
x = "Month",
y = "sales_millions",
title = "Sales (in millions)",
subtitle = "Shoe Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
retail_plot_log <- retail_ts |>
autoplot(.vars = log(sales_millions)) +
labs(
x = "Month",
y = "log(sales_millions)",
title = "Log of Sales (in millions)",
subtitle = "Shoe Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
retail_plot_raw | retail_plot_log
best_fit_retail <- retail_ts |>
model(
ar_model = ARIMA(sales_millions ~ 1 +
pdq(0:2, 0:2, 0:2) +
PDQ(0:2, 0:2, 0:2), approximation = TRUE))
best_fit_retail
acf_plot <- best_fit_retail |>
residuals() |>
feasts::ACF() |>
autoplot()
pacf_plot <- best_fit_retail |>
residuals() |>
feasts::PACF() |>
autoplot()
acf_plot | pacf_plot
best_fit_retail |>
glance()
coefficients(best_fit_retail)
best_fit_retail |>
forecast(h = "60 months") |>
autoplot(retail_ts) +
labs(
x = "Month",
y = "Total U.S. Sales in Millions",
title = "Forecasted Sales (in millions)",
subtitle = "Shoe Stores"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
:::