-
Notifications
You must be signed in to change notification settings - Fork 3
/
chapter_7_lesson_1.qmd
800 lines (534 loc) · 20.8 KB
/
chapter_7_lesson_1.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
---
title: "Introduction to Non-stationary Models and Differencing"
subtitle: "Chapter 7: Lesson 1"
format: html
editor: source
sidebar: false
---
<!-- # -->
<!-- # -->
<!-- # -->
<!-- This could be two lessons. -->
<!-- We did not have enough time to do the -->
<!-- Check Your Understanding activities. -->
<!-- # -->
<!-- # -->
<!-- # -->
```{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_1_outcomes.qmd >}}
## Preparation
- Read Sections 7.1-7.2
- Read [Prof. Frenzel's Blog Post](https://prof-frenzel.medium.com/kb-time-series-analysis-part-4-autoregressive-models-ed824838bd4c)
## 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
## Class Activity: Non-seasonal ARIMA Models (15 min)
### Effect of Differencing
In [Chapter 4 Lesson 2](https://byuistats.github.io/timeseries/chapter_4_lesson_2.html#McDonalds),
we found that if we compute the first difference of the price of McDonald's stock from July 2020 through December 2023, the differences can be modeled as white noise.
Sometimes differencing can remove trends.
Consider the case of a random walk and a linear trend with white noise errors
#### Random Walk
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Consider the random walk
$$
x_t = x_{t-1} + w_t
$$
where $\{w_t\}$ is a white noise process.
- What is the model for the first differences of this time series?
<!-- White noise -->
:::
#### Linear Trend with White Noise Errors
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Consider a time series with a linear trend and white noise errors.
$$
x_t = a + bt + w_t
$$
where $\{w_t\}$ is a white noise process.
- What is the model for the first differences of this time series?
<!-- $\nabla x_t = x_t - x_{t-1} = w_t$, which is white noise. -->
- What is the model obtained by subtracting $a+bt$ from this series?
<!-- $\nabla x_t = x_t - x_{t-1} = b + w_t - w_{t-1}$, which is an $MA(1)$ process -->
- What are some potential concerns of using differencing to eliminate a deterministic trend?
<!-- It can lead to an unnecessarily complicated model. -->
:::
### Fitting an ARIMA Model when the Difference Model has a Non-Zero Mean
(See the last sentence in the first paragraph on page 138.)
### Differencing a Time Series or the Logarithm of a Time Series
If the difference of a time series demonstrates an increasing trend, taking the logarithm before differencing can eliminate the increasing variation in the differences. As an example, consider the Australian electricity production series given in the book.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| output: false
pacman::p_load("tsibble", "fable", "feasts",
"tsibbledata", "fable.prophet",
"tidyverse", "patchwork")
cbe <- read_table("https://byuistats.github.io/timeseries/data/cbe.dat") |>
select(elec) |>
mutate(
date = seq(
ymd("1958-01-01"),
by = "1 months",
length.out = n()),
year_month = tsibble::yearmonth(date)) |>
as_tsibble(index = year_month)
cbe |>
mutate(
`Diff series` = elec - lag(elec),
`Diff log-series` = log(elec) - lag(log(elec))) |>
pivot_longer(
cols = all_of(c("elec", "Diff series", "Diff log-series"))) |>
mutate(name = factor(name, levels =c("elec","Diff series", "Diff log-series"))) |>
ggplot(aes(x = date, y = value)) +
geom_line() +
facet_wrap(~name, ncol = 1, scales = "free", strip.position = "left") +
labs(x = "Time", y = "") +
scale_x_date(breaks = "5 years", date_labels = "%Y") +
theme_bw()
```
```{r}
#| label: fig-BooksFigure7Dot1
#| fig-cap: "Plot of Australian electricity production, first differences, and first differences of the logarithm of the series"
#| echo: false
cbe |>
mutate(
`Diff series` = elec - lag(elec),
`Diff log-series` = log(elec) - lag(log(elec))) |>
pivot_longer(
cols = all_of(c("elec", "Diff series", "Diff log-series"))) |>
mutate(name = factor(name, levels =c("elec","Diff series", "Diff log-series"))) |>
ggplot(aes(x = date, y = value)) +
geom_line() +
facet_wrap(~name, ncol = 1, scales = "free", strip.position = "left") +
labs(x = "Time", y = "") +
scale_x_date(breaks = "5 years", date_labels = "%Y") +
theme_bw()
```
### Integrated Time Series of Order d
::: {.callout-note icon=false title="Definition of an Integrated Series of Order $d$, $I(d)$"}
We say that a time series is **integrated of order d** if the $d^{th}$ difference of $\{x_t\}$ is a white noise process $\{w_t\}$. Expressed differently, we write this as ${\nabla^d x_t = w_t}$. We denote an integrated time series of order $d$ as $I(d)$.
:::
Recall that $\nabla^d \equiv \left( 1 - \mathbf{B} \right)^d$.
So, either of the following can be used to indicate an integrated time series of order $d$:
\begin{align*}
\nabla^d x_t &= w_t \\
~\\
\left( 1 - \mathbf{B} \right)^d x_t &= w_t
\end{align*}
#### Special Case: $I(1)$
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- What model is given by the special case $I(1)$?
<!-- Random walk -->
:::
### Second-Order Differencing and Lagged Differences
A linear trend can be removed by first-order differencing. A curved trend can sometimes be eliminated by second order differencing.
In some cases, a lagged difference is more appropriate. For example, if you have monthly data and need to remove additive seasonal effects, you may want to take a difference with a lag of 12. This subtracts sequential January observations from each other. This models the year-over-year growth.
Notice that taking a lag 12 difference
$$
\left( 1 - \mathbf{B}^{12} \right) x_t = x_t - x_{t-12}
$$
is very different from taking the twelfth differences
\begin{align*}
\nabla^{12} x_t
&= \left( 1 - \mathbf{B} \right)^{12} x_t \\
&= x_t - 12 x_{t-1} + 66 x_{t-2} - 220 x_{t-3} + 495 x_{t-4} - 792 x_{t-5} + 924 x_{t-6} \\
& ~~~~~~~~~~~~~~~~~~~ - 792 x_{t-7} + 495 x_{t-8} - 220 x_{t-9} + 66 x_{t-10} - 12 x_{t-11} + x_{t-12}
\end{align*}
### ARIMA Process
#### ARIMA
::: {.callout-note icon=false title="Definition of an ARIMA Process"}
A time series is said to follow an **$ARIMA(p,d,q)$ process** if the $d^{th}$ differences of the time series follow an $ARMA(p,q)$ process.
:::
Suppose we let $y_t = \left( 1 - \mathbf{B} \right)^d x_t$. The series $\{y_t\}$ follows an $ARMA(p,q)$ process if
$\theta_p \left(\mathbf{B} \right) y_t = \phi_q \left(\mathbf{B} \right)w_t$.
Substituting, we find that $\{x_t\}$ follows an $ARIMA(p,d,q)$ process if
$$
\theta_p \left(\mathbf{B} \right) \left( 1 - \mathbf{B} \right)^d x_t = \phi_q \left(\mathbf{B} \right) w_t
$$
where $\theta_p \left(\mathbf{B} \right)$ and $\phi_q \left(\mathbf{B} \right)$ are polynomials of orders $p$ and $q$, respectively.
#### Special Case: $IMA(d,q)$ Process
::: {.callout-note icon=false title="Definition of an IMA Process"}
A time series $\{x_t\}$ follows an **$IMA(d,q)$ process** if it can be expressed as:
$$
\left( 1 - \mathbf{B} \right)^d x_t = \phi_q \left(\mathbf{B} \right) w_t
$$
Note that $IMA(d,q) \equiv ARIMA(0,d,q)$.
:::
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Solve for $x_t$ in an $IMA(1,1)$ process.
<!-- $x_t = x_{t-1} + w_t + \beta w_{t-1}$ -->
:::
#### Special Case: $ARI(p,d)$ Process
::: {.callout-note icon=false title="Definition of an ARI Process"}
A time series $\{x_t\}$ follows an **$ARI(p,d)$ process** if it can be expressed as:
$$
\theta_p \left(\mathbf{B} \right) \left( 1 - \mathbf{B} \right)^d x_t = w_t
$$
Note that $ARI(p,d) \equiv ARIMA(p,d,0)$.
:::
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Solve for $x_t$ in an $ARI(1,1)$ process.
<!-- $x_t = \alpha x_{t-1} + x_{t-1} - \alpha x_{t-2} + w_t $ -->
:::
### Simulating an ARIMA Process
We can simulate data from the ARIMA process
$$
x_t = 0.5 x_{t-1} + x_{t-1} - 0.5 x_{t-2} + w_t + 0.3 w_{t-1}
$$
using the following R code.
```{r}
set.seed(1)
n <- 10000
x <- rnorm(n)
w <- rnorm(n)
for (i in 3:n) {
x[i] <- 0.5 * x[i - 1] + x[i - 1] - 0.5 * x[i - 2] + w[i] + 0.3 * w[i - 1]
}
arima(x, order = c(1, 1, 1))
```
This is an ARIMA(1,1,1) process with parameters $\alpha = 0.5$ and $\beta = 0.3$.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Modify the code above to simulate from an $ARIMA(2,1,2)$ process with parameters $\alpha_1 = 0.5$, $\alpha_2 = 0.2$, $\beta_1 = 0.4$, and $\beta_2 = 0.1$.
<!-- $$(1 - \alpha_1 B - \alpha_2 B^2) (1-B) x_t = (1 + \beta_1 B + \beta_2 B^2) w_t$$ -->
:::
## Class Activity: Fitting an ARIMA Process - Exchange Rates (10 min)
The data file [exchange_rates.parquet](https://byuistats.github.io/timeseries/data/exchange_rates.parquet) gives the exchange rates for foreign currencies. The daily-observed values in the time series are the amount in the foreign currency equivalent to one U. S. dollar. We will consider the exchange rates to convert one dollar into Euros.
```{r}
#| label: tbl-exchangeRatesTS
#| tbl-cap: "Select values of the time series representing the exchange rate to convert US$1 into Euros"
#| code-fold: true
#| code-summary: "Show the code"
exchange_ts <- rio::import("data/exchange_rates.parquet") |>
filter(currency == "USD.EUR") |>
as_tsibble(index = date) |>
na.omit()
exchange_ts |>
display_partial_table(6,3)
```
```{r}
#| label: fig-exchangeRateTimePlot
#| fig-cap: "Time plot of the exchange rate to convert US$1 into Euros"
#| code-fold: true
#| code-summary: "Show the code"
exchange_ts |>
autoplot(.vars = rate) + labs(title = exchange_ts$currency[1])
```
```{r}
#| label: fig-exchangeRateACFpacf
#| fig-cap: "Correlogram and partial correlogram for the time series representing the exchange rate to convert US$1 into Euros"
#| code-fold: true
#| warning: false
#| code-summary: "Show the code"
acf_plot <- exchange_ts |> select(rate) |> ACF() |> autoplot(var = .resid)
pacf_plot <- exchange_ts |> select(rate) |> PACF() |> autoplot(var = .resid)
acf_plot | pacf_plot
```
```{r}
# Fit the ARIMA Model
exchange_model <- exchange_ts |>
model(
auto = ARIMA(rate ~ 1 + pdq(0:2,0:1,0:2) + PDQ(0, 0, 0)),
a000 = ARIMA(rate ~ 1 + pdq(0,0,0) + PDQ(0, 0, 0)),
a001 = ARIMA(rate ~ 1 + pdq(0,0,1) + PDQ(0, 0, 0)),
a002 = ARIMA(rate ~ 1 + pdq(0,0,2) + PDQ(0, 0, 0)),
a100 = ARIMA(rate ~ 1 + pdq(1,0,0) + PDQ(0, 0, 0)),
a101 = ARIMA(rate ~ 1 + pdq(1,0,1) + PDQ(0, 0, 0)),
a102 = ARIMA(rate ~ 1 + pdq(1,0,2) + PDQ(0, 0, 0)),
a200 = ARIMA(rate ~ 1 + pdq(2,0,0) + PDQ(0, 0, 0)),
a201 = ARIMA(rate ~ 1 + pdq(2,0,1) + PDQ(0, 0, 0)),
a202 = ARIMA(rate ~ 1 + pdq(2,0,2) + PDQ(0, 0, 0)),
a011 = ARIMA(rate ~ 1 + pdq(0,1,1) + PDQ(0, 0, 0)),
a012 = ARIMA(rate ~ 1 + pdq(0,1,2) + PDQ(0, 0, 0)),
a110 = ARIMA(rate ~ 1 + pdq(1,1,0) + PDQ(0, 0, 0)),
a111 = ARIMA(rate ~ 1 + pdq(1,1,1) + PDQ(0, 0, 0)),
a112 = ARIMA(rate ~ 1 + pdq(1,1,2) + PDQ(0, 0, 0)),
a210 = ARIMA(rate ~ 1 + pdq(2,1,0) + PDQ(0, 0, 0)),
a211 = ARIMA(rate ~ 1 + pdq(2,1,1) + PDQ(0, 0, 0)),
a212 = ARIMA(rate ~ 1 + pdq(2,1,2) + PDQ(0, 0, 0))
)
```
Here is one way to determine which model is selected by the "auto" process.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
exchange_model |>
select(auto)
```
We now examine all the fitted models to determine the value of the residual mean squared error (`sigma2`), log-likelihood, AIC, AICc, and BIC. For the log-likelihood, larger values are preferable. For all other measures, smaller values are preferred.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| output: false
exchange_model |>
glance()
```
```{r}
#| label: tbl-exchangeRateSeveralFittedModels
#| tbl-cap: "Values used in the model selection process for the time series representing the exchange rate to convert US$1 into Euros"
#| echo: false
exchange_model |>
glance() |>
display_arima_models()
```
Suppose we choose to apply the "auto" model, which is $ARIMA(1,1,1)$.
The model parameters are summarized here:
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| output: false
exchange_model |>
select(auto) |>
coefficients()
```
```{r}
#| echo: false
exchange_model |>
select(auto) |>
coefficients() |>
display_table()
```
The following plots give the acf and pacf of the residuals from this model.
```{r}
#| label: fig-exchangeRateResidACFpacf
#| fig-cap: "Correlogram and Partial Correlogram for the residuals from the ARIMA(1,1,1) model for the daily exchange rates to convert US$1 into Euros"
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
model_resid <- exchange_model |>
select(auto) |>
residuals()
acf_plot <- model_resid |> ACF() |> autoplot(var = .resid)
pacf_plot <- model_resid |> PACF() |> autoplot(var = .resid)
acf_plot | pacf_plot
```
Here is a histogram of the residuals from our model.
```{r}
#| label: fig-exchangeRateResidHistogram
#| fig-cap: "Histogram of the residuals from the ARIMA(1,1,1) model for the daily exchange rates to convert US$1 into Euros"
#| code-fold: true
#| code-summary: "Show the code"
model_resid |>
mutate(density = dnorm(.resid, mean(model_resid$.resid), sd(model_resid$.resid))) |>
ggplot(aes(x = .resid)) +
geom_histogram(aes(y = after_stat(density)),
color = "white", fill = "#56B4E9", binwidth = 0.001) +
geom_line(aes(x = .resid, y = density)) +
theme_bw() +
labs(
x = "Values",
y = "Frequency",
title = "Histogram of Residuals"
) +
theme(
plot.title = element_text(hjust = 0.5)
)
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Write the fitted model:
$$
x_t = ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$$
- Does the model provide an appropriate fit for the data?
:::
Here is a forecast for the next 7 days based on our model.
```{r}
final_model <- exchange_model |>
select(auto)
temps_forecast <- final_model |>
select(auto) |>
forecast(h = "7 days")
temps_forecast |>
autoplot(exchange_ts, level = 95) +
geom_line(aes(y = .fitted, color = "Fitted"),
data = augment(final_model)) +
scale_color_discrete(name = "") +
labs(
x = paste0(
"Date (",
format(ymd(min(exchange_ts$date)), "%d %b %Y"),
" - ",
format(ymd(max(exchange_ts$date)), "%d %b %Y"),
")"
),
y = "Exchange Rate",
title = "Exchange Rate for Converting US$1 to Euros",
subtitle = "7-Day Forecast Based on our AR(1,1,1) Model"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
## Small-Group Activity: Fitting an ARIMA Process - Microsoft Stock Prices (20 min)
A time series given the daily closing price for Microsoft (MSFT) stock is given below. To handle the gaps in the data, we define a new variable, `t`, which gives the observation number.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# Set symbol and date range
symbol <- "MSFT" # Abercrombie & Fitch stock trading symbol
date_start <- "2020-01-01"
date_end <- "2024-03-28"
# Fetch stock prices
df_stock <- tq_get(symbol, from = date_start, to = date_end, get = "stock.prices")
# Transform data into tsibble
stock_ts <- df_stock |>
mutate(
dates = date,
value = close
) |>
dplyr::select(dates, value) |>
as_tibble() |>
arrange(dates) |>
mutate(t = 1:n()) |>
as_tsibble(index = t, key = NULL)
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Using the daily closing prices of Microsoft stock, do the following.
- Make a time plot of the data.
- Create a correlogram and partial correlogram of the stock prices.
- Fit candidate $ARIMA(p,d,q)$ models to the data.
- Choose the "best" model, and justify your selection.
- Generate a correlagram and partial correlogram of the residuals from your chosen model.
- Make a histogram of the residuals from your model.
- Did your your model account for the the time series?
- Predict the value 60 trading days in the future.
*Note:* The "time" index is just an integer sequence in the `stock_ts` tsibble. So, apply the `forecast()` function as `forecast(h = 60)`, rather than `forecast(h = "60 days")`.
:::
```{r}
#| include: false
#| echo: false
#| eval: false
# Generate time series plot using plot_ly
plot_ly(stock_ts, x = ~dates, y = ~value, type = 'scatter', mode = 'lines') |>
layout(
xaxis = list(title = "Date"),
yaxis = list(title = "Value"),
title = paste0("Time Plot of ", symbol, " Daily Closing Price (", format(ymd(date_start), "%d %b %Y"), " - ", format(ymd(date_end), "%d %b %Y"),")")
)
stock_model <- stock_ts |>
model(
auto = ARIMA(value ~ 1 + pdq(0:2,0:1,0:2) + PDQ(0, 0, 0)),
a000 = ARIMA(value ~ 1 + pdq(0,0,0) + PDQ(0, 0, 0)),
a001 = ARIMA(value ~ 1 + pdq(0,0,1) + PDQ(0, 0, 0)),
a002 = ARIMA(value ~ 1 + pdq(0,0,2) + PDQ(0, 0, 0)),
a100 = ARIMA(value ~ 1 + pdq(1,0,0) + PDQ(0, 0, 0)),
a101 = ARIMA(value ~ 1 + pdq(1,0,1) + PDQ(0, 0, 0)),
a102 = ARIMA(value ~ 1 + pdq(1,0,2) + PDQ(0, 0, 0)),
a200 = ARIMA(value ~ 1 + pdq(2,0,0) + PDQ(0, 0, 0)),
a201 = ARIMA(value ~ 1 + pdq(2,0,1) + PDQ(0, 0, 0)),
a202 = ARIMA(value ~ 1 + pdq(2,0,2) + PDQ(0, 0, 0)),
a011 = ARIMA(value ~ 1 + pdq(0,1,1) + PDQ(0, 0, 0)),
a012 = ARIMA(value ~ 1 + pdq(0,1,2) + PDQ(0, 0, 0)),
a110 = ARIMA(value ~ 1 + pdq(1,1,0) + PDQ(0, 0, 0)),
a111 = ARIMA(value ~ 1 + pdq(1,1,1) + PDQ(0, 0, 0)),
a112 = ARIMA(value ~ 1 + pdq(1,1,2) + PDQ(0, 0, 0)),
a210 = ARIMA(value ~ 1 + pdq(2,1,0) + PDQ(0, 0, 0)),
a211 = ARIMA(value ~ 1 + pdq(2,1,1) + PDQ(0, 0, 0)),
a212 = ARIMA(value ~ 1 + pdq(2,1,2) + PDQ(0, 0, 0))
)
stock_model |>
glance()
stock_model |>
select(auto)
#########
stock_model |>
glance() |>
display_arima_models()
####
model_resid <- stock_model |>
select(auto) |>
residuals()
acf(model_resid$.resid, main = "ACF of Residuals from ARIMA Model")
pacf(model_resid$.resid, main = "ACF of Residuals from ARIMA Model")
model_resid |>
mutate(density = dnorm(.resid, mean(model_resid$.resid), sd(model_resid$.resid))) |>
ggplot(aes(x = .resid)) +
geom_histogram(aes(y = after_stat(density)),
color = "white", fill = "#56B4E9", binwidth = 2) +
geom_line(aes(x = .resid, y = density)) +
theme_bw() +
labs(
x = "Values",
y = "Frequency",
title = "Histogram of Residuals"
) +
theme(
plot.title = element_text(hjust = 0.5)
)
######
stock_ts$diff = stock_ts$value - lag(stock_ts$value)
stock_ts |>
na.omit() |>
autoplot(.vars = diff) +
labs(
title = paste("Time Plot of Differences in Daily", symbol, "Stock Prices"),
subtitle =
paste0(
format(ymd(date_start), "%d %b %Y"),
" - ",
format(ymd(date_end), "%d %b %Y")
),
x = "Date",
y = "Difference",
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
## 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_1.qmd" download="homework_7_1.qmd"> homework_7_1.qmd </a>
:::
<a href="javascript:showhide('Solutions1')"
style="font-size:.8em;">Class Activity</a>
::: {#Solutions1 style="display:none;"}
:::
<a href="javascript:showhide('Solutions2')"
style="font-size:.8em;">Class Activity</a>
::: {#Solutions2 style="display:none;"}
:::
<a href="javascript:showhide('Solutions3')"
style="font-size:.8em;">Class Activity</a>
::: {#Solutions3 style="display:none;"}
:::