forked from TBrost/BYUI-Timeseries-Drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_4_lesson_4.qmd
1595 lines (1171 loc) · 44.5 KB
/
chapter_4_lesson_4.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Fitted AR Models"
subtitle: "Chapter 4: Lesson 4"
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_4_lesson_4_outcomes.qmd >}}
## Preparation
- Read Sections 4.6-4.7
## 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: Fitting a Simulated $AR(1)$ Model with Zero Mean (5 min)
We will demonstrate how AR models are fitted via simulation. We will fit two different $AR(1)$ models and an $AR(2)$ model. The advantage of using simulation is that we know how the time series was constructed. So, we know the model that was used and the actual values of the parameters in that model. We can then see how close our estimated parameter values are to the true values.
### Simulate an $AR(1)$ Time Series
In this simulation, we first simulate data from the $AR(1)$ model
$$
x_t = 0.75 ~ x_{t-1} + w_t
$$
where $w_t$ is a white noise process with variance 1.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
set.seed(123)
n_rep <- 1000
alpha1 <- 0.75
dat_ts <- tibble(w = rnorm(n_rep)) |>
mutate(
index = 1:n(),
x = purrr::accumulate2(
lag(w), w,
\(acc, nxt, w) alpha1 * acc + w,
.init = 0)[-1]) |>
tsibble::as_tsibble(index = index)
dat_ts |>
autoplot(.vars = x) +
labs(
x = "Time",
y = "Simulated Time Series",
title = "Simulated Values from an AR(1) Process"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
```
The R command `mean(dat_ts$x)` gives the mean of the $x_t$ values as `r round(mean(dat_ts$x),3)`.
### Fit an $AR(1)$ Model with Zero Mean
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# Fit the AR(1) model
fit_ar <- dat_ts |>
model(AR(x ~ order(1)))
tidy(fit_ar)
```
The estimate of the parameter $\alpha_1$ (i.e. the fitted value of the parameter $\alpha_1$) is $\hat \alpha_1 = `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)`$.
When R fits an AR model, the mean of the time series is subtracted from the data before the parameter values are estimated.
If R detects that the mean of the time series is not significantly different from zero, it is omitted from the output.
Because the mean is subtracted from the time series before the parameter values are estimated, R is using the model
$$
z_t = \alpha_1 ~ z_{t-1} + w_t
$$
where $z_t = x_t - \mu$ and $\mu$ is the mean of the time series.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Answer the following questions with your partner.
- Use the expression for $z_t$ above to solve for $x_t$ in terms of $x_{t-1}$, $\mu$, $\alpha_1$, and $w_t$.
- What does your model reduce to when $\mu = 0$?
- Explain to your partner why this correctly models a time series with mean $\mu$.
:::
We replace the parameter $\mu$ with its estimator $\hat \mu = \bar x$. We also replace $\alpha_1$ with the fitted value from the output $\hat \alpha_1$. This gives us the fitted model:
$$
\hat x_t = \bar x + \hat \alpha_1 ~ (x_{t-1} - \bar x)
$$
The fitted model can be expressed as:
\begin{align*}
\hat x_t
&= `r round(mean(dat_ts$x),3)` + `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` \left( x_{t-1} - `r round(mean(dat_ts$x ),3)` \right) \\
&= `r round(mean(dat_ts$x),3)` - `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` ~ (`r round(mean(dat_ts$x ),3)`) + `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` ~ \left( x_{t-1} \right) \\
&= `r ( mean(dat_ts$x) - tidy(fit_ar) |> select(estimate) |> pull() * mean(dat_ts$x ) ) |> round(3)` + `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` ~ x_{t-1}
\end{align*}
Even though R does not report the parameter for the mean of the process, $\hat \mu = `r ( mean(dat_ts$x) - tidy(fit_ar) |> select(estimate) |> pull() * mean(dat_ts$x ) ) |> round(3)`$, it is not significantly different from zero. One could argue that we should not use a model that contains the mean and instead focus on a simple fitted model that has only one parameter:
$$
\hat x_t = `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` ~ x_{t-1}
$$
### Confidence Interval for the Model Parameter
The P-value given above tests the hypothesis that $\alpha_1=0$. This is not helpful in this context. We are interested in the plausible values for $\alpha_1$, not whether or not it is different from zero. For this reason, we consider a confidence interval and disregard the P-value.
We can compute an approximate 95% confidence interval for $\alpha_1$ as:
$$
\left(
\hat \alpha_1 - 2 \cdot SE_{\hat \alpha_1}
, ~
\hat \alpha_1 + 2 \cdot SE_{\hat \alpha_1}
\right)
$$
where $\hat \alpha_1$ is our parameter estimate and $SE_{\hat \alpha_1}$ is the standard error of the estimate. Both of these values are given in the R output.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
ci_summary <- tidy(fit_ar) |>
mutate(
lower = estimate - 2 * std.error,
upper = estimate + 2 * std.error
)
```
So, our 95% confidence interval for $\alpha_1$ is:
$$
\left(
`r ci_summary |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r ci_summary |> select(std.error) |> pull() |> round(3)`
, ~
`r ci_summary |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r ci_summary |> select(std.error) |> pull() |> round(3)`
\right)
$$
or
$$
\left(
`r ((ci_summary |> select(estimate) |> pull()) - 2 * (ci_summary |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((ci_summary |> select(estimate) |> pull()) + 2 * (ci_summary |> select(std.error) |> pull())) |> round(3)`
\right)
$$
Note that the confidence interval contains $\alpha_1 = `r alpha1`$, the value of the parameter we used in our simulation. The process of estimating the parameter worked well. In practice, we will not know the value of $\alpha_1$, but the confidence interval gives us a reasonable estimate of the value.
### Residuals
For an $AR(1)$ model where the mean of the time series is not statistically significantly different from 0, the residuals are computed as
\begin{align*}
r_t
&= x_t - \hat x_t \\
&= x_t - \left[ `r tidy(fit_ar) |> select(estimate) |> pull() |> round(3)` ~ x_{t-1} \right]
\end{align*}
```{r}
#| include: false
#| eval: false
# Computing the residuals manually
dat_ts |>
# Zero mean model
mutate(resid0 = x - ( (tidy(fit_ar) |> select(estimate) |> pull()) * lag(x) ) ) |>
# Non-zero mean model
mutate(resid1 = x - (mean(x) + (tidy(fit_ar) |> select(estimate) |> pull()) * (lag(x) - mean(x)) ) )
```
We can easily obtain these residual values in R:
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| include: false
fit_ar |> residuals()
```
The variance of the residuals is $`r fit_ar |> residuals() |> as_tibble() |> select(.resid) |> pull() |> var(na.rm = TRUE) |> round(3)`$.
This is very close to the actual value used in the simulation: $\sigma^2 = 1$.
<!-- Start of the next section -->
<!-- These parameters are used in the simulation below -->
```{r}
#| echo: false
alpha0 <- 50
alpha1 <- 0.75
sigma_sqr <- 5
```
## Class Activity: Fitting a Simulated $AR(1)$ Model with Non-Zero Mean (5 min)
### Simulate an $AR(1)$ Time Series
It is easy to conceive situations where the mean of an AR model, $\mu$, is not zero.
The model we have been fitting is
$$
x_t = \mu + \alpha_1 ~ \left( x_{t-1} - \mu \right) + w_t
$$
where $\mu$ and $\alpha_1$ are constants, and $w_t$ is a white noise process with variance $\sigma^2$.
This model can be simplified by combining like terms.
\begin{align*}
x_t
&= \mu + \alpha_1 ~ \left( x_{t-1} - \mu \right) + w_t \\
&= \underbrace{\mu - \alpha_1 ~ (\mu)}_{\alpha_0} + \alpha_1 ~ \left( x_{t-1} \right) + w_t \\
&= \alpha_0 + \alpha_1 ~ \left( x_{t-1} \right) + w_t
\end{align*}
Suppose the mean of the $AR(1)$ process is $\mu = `r alpha0`$. We will set $\alpha_1 = `r alpha1`$, and $\sigma^2 = `r sigma_sqr`$ for this simulation.
After specifying these numbers, the model becomes:
\begin{align*}
x_t
&= `r alpha0` + `r alpha1` ~ ( x_{t-1} - `r alpha0` ) + w_t \\
&= `r alpha0` - `r alpha1` ~ ( `r alpha0` ) + `r alpha1` ~ x_{t-1} + w_t \\
&= `r alpha0 - alpha1 * alpha0` + `r alpha1` ~ x_{t-1} + w_t
\end{align*}
where $w_t$ is a white noise process with variance $\sigma^2 = `r sigma_sqr`$.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
set.seed(123)
n_rep <- 1000
alpha1 <- 0.75
sigma_sqr <- 5
dat_ts <- tibble(w = rnorm(n = n_rep, sd = sqrt(sigma_sqr))) |>
mutate(
index = 1:n(),
x = purrr::accumulate2(
lag(w), w,
\(acc, nxt, w) alpha1 * acc + w,
.init = 0)[-1]) |>
mutate(x = x + alpha0) |>
tsibble::as_tsibble(index = index)
dat_ts |>
autoplot(.vars = x) +
labs(
x = "Time",
y = "Simulated Time Series",
title = "Simulated Values from an AR(1) Process"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
```
The R command `mean(dat_ts$x)` gives the mean of the $x_t$ values as `r round(mean(dat_ts$x),3)`.
### Fit an $AR(1)$ Model with Non-Zero Mean
We now use R to fit an $AR(1)$ model to the time series data.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# Fit the AR(1) model
fit_ar <- dat_ts |>
model(AR(x ~ order(1)))
tidy(fit_ar)
```
The estimate of the parameter for the constant (mean) term $\alpha_0$ is $\hat \alpha_0 = `r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)`$.
The estimate of the parameter $\alpha_1$ (i.e. the fitted value of the parameter $\alpha_1$) is $\hat \alpha_1 = `r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)`$.
<!-- Recall that the mean of the time series is subtracted from the data before the parameter values are estimated. To estimate the time series, start with the mean and then add the coefficient $\hat \alpha_1$ multiplied by the difference between $x_{t-1}$ and the mean. -->
Fitting the model
$$
x_t = \alpha_0 + \alpha_1 ~ x_{t-1} + w_t
$$
we get
\begin{align*}
\hat x_t
&= \hat \alpha_0 + \hat \alpha_1 ~ x_{t-1} \\
&= `r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` +
`r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)`
~ x_{t-1}
\end{align*}
### Confidence Intervals for the Model Parameters
We can compute approximate 95% confidence intervals for $\alpha_0$ and $\alpha_1$:
$$
\left(
\hat \alpha_i - 2 \cdot SE_{\hat \alpha_i}
, ~
\hat \alpha_i + 2 \cdot SE_{\hat \alpha_i}
\right)
$$
where $\hat \alpha_i$ is our estimate of parameter $i \in \{0,1\}$, and $SE_{\hat \alpha_i}$ is the standard error of the respective estimates.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
ci_summary <- tidy(fit_ar) |>
mutate(
lower = estimate - 2 * std.error,
upper = estimate + 2 * std.error
)
```
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
95% Confidence Interval for $\alpha_0$:
$$
\left(
\hat \alpha_0 - 2 \cdot SE_{\hat \alpha_0}
, ~
\hat \alpha_0 + 2 \cdot SE_{\hat \alpha_0}
\right)
$$
$$
\left(
`r ci_summary |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r ci_summary |> filter(str_detect(term, "const")) |> select(std.error) |> pull() |> round(3)`
, ~
`r ci_summary |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r ci_summary |> filter(str_detect(term, "const")) |> select(std.error) |> pull() |> round(3)`
\right)
$$
$$
\left(
`r ((ci_summary |> filter(str_detect(term, "const")) |> select(estimate) |> pull()) - 2 * (ci_summary |> filter(str_detect(term, "const")) |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((ci_summary |> filter(str_detect(term, "const")) |> select(estimate) |> pull()) + 2 * (ci_summary |> filter(str_detect(term, "const")) |> select(std.error) |> pull())) |> round(3)`
\right)
$$
The confidence interval for $\alpha_0$ contains
$$\alpha_0 = \mu - \alpha_1 ~ (\mu) = `r alpha0 * (1-alpha1)`$$
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
95% Confidence Interval for $\alpha_1$:
$$
\left(
\hat \alpha_1 - 2 \cdot SE_{\hat \alpha_1}
, ~
\hat \alpha_1 + 2 \cdot SE_{\hat \alpha_1}
\right)
$$
$$
\left(
`r ci_summary |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r ci_summary |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull() |> round(3)`
, ~
`r ci_summary |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r ci_summary |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull() |> round(3)`
\right)
$$
$$
\left(
`r ((ci_summary |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull()) - 2 * (ci_summary |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((ci_summary |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull()) + 2 * (ci_summary |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull())) |> round(3)`
\right)
$$
The confidence interval for $\alpha_1$ contains
$$\alpha_1 = `r alpha1`$$
:::
:::
<!-- End of two columns -->
Both intervals captured the true value used in the simulation. The process of estimating the parameter worked well. In practice, we will not know the value of $\alpha_1$, but the confidence interval gives us a reasonable estimate of the value.
About 95% of the time, the confidence interval will capture the true parameter value.
### Residuals
The residuals in this model are computed as
\begin{align*}
r_t
&= x_t - \hat x_t \\
&= x_t -
\left[
`r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` +
`r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)`
~ x_{t-1}
\right]
\end{align*}
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| include: false
fit_ar |> residuals()
```
The variance of the residuals is $`r fit_ar |> residuals() |> as_tibble() |> select(.resid) |> pull() |> var(na.rm = TRUE) |> round(3)`$, which is near the actual parameter value: $\sigma^2 = `r sigma_sqr`$.
## Class Activity: Fitting a Simulated $AR(2)$ Model (10 min)
### Simulate an $AR(2)$ Time Series
```{r}
#| echo: false
# Set parameters
alpha0 <- 20
alpha1 <- 0.5
alpha2 <- 0.4
sigma_sqr <- 9
```
In this section, we will simulate data from the following $AR(2)$ process:
$$
x_t = `r alpha0 * (1 - alpha1 - alpha2)` + `r alpha1` ~ x_{t-1} + `r alpha2` ~ x_{t-2} + w_t
$$
where $w_t$ is a discrete white noise process with variance $\sigma^2 = `r sigma_sqr`$.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Use the $AR(2)$ process above to answer the following questions.
- Is this $AR(2)$ process stationary? (Hint: The characteristic polynomial only includes terms that involve $x_t$.)
- Rewrite the model in the form
$$
x_t = \mu + \alpha_1 ~ ( x_{t-1} - \mu) + \alpha_2 ~ ( x_{t-2} - \mu) + w_t
$$
Identify the value of each of the coefficients ($\mu$, $\alpha_1$, and $\alpha_2$).
<!-- Solution Solution Solution Solution Solution Solution -->
<!-- \begin{align*} -->
<!-- x_t -->
<!-- &= `r alpha0` + `r alpha1` ~ ( x_{t-1} - `r alpha0`) + `r alpha2` ~ ( x_{t-2} - `r alpha0`) + w_t \\ -->
<!-- &= `r alpha0 - alpha1 * alpha0 - alpha2 * alpha0` + `r alpha1` ~ x_{t-1} + `r alpha2` ~ x_{t-2} + w_t -->
<!-- \end{align*} -->
- What is the mean of this $AR(2)$ process?
:::
Here is a time plot of the simulated time series.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
set.seed(123)
n_rep <- 1000
alpha0 <- 20
alpha1 <- 0.5
alpha2 <- 0.4
sigma_sqr <- 9
dat_ts <- tibble(w = rnorm(n = n_rep, sd = sqrt(sigma_sqr))) |>
mutate(
index = 1:n(),
x = 0
) |>
tsibble::as_tsibble(index = index)
# Simulate x values
dat_ts$x[1] <- alpha0 + dat_ts$w[1]
dat_ts$x[2] <- alpha0 + alpha1 * ( dat_ts$x[1] - alpha0 ) + dat_ts$w[2]
for (i in 3:nrow(dat_ts)) {
dat_ts$x[i] <- alpha0 +
alpha1 * ( dat_ts$x[i-1] - alpha0 ) +
alpha2 * ( dat_ts$x[i-2] - alpha0 ) +
dat_ts$w[i]
}
dat_ts |>
autoplot(.vars = x) +
labs(
x = "Time",
y = "Simulated Time Series",
title = paste("Simulated Values from an AR(2) Process with Mean", alpha0)
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
```
### Fit an $AR(2)$ Model
We fit an $AR(2)$ model to these simulated values.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# Fit the AR(2) model
fit_ar <- dat_ts |>
model(AR(x ~ order(2)))
tidy(fit_ar)
```
The estimates of the parameter values are:
$\hat \alpha_0 = `r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)`$,
$\hat \alpha_1 = `r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)`$,
and
$\hat \alpha_2 = `r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull() |> round(3)`$.
This means that our fitted model can be expressed as:
\begin{align*}
\hat x_t
&=
\hat \alpha_0
+ \hat \alpha_1 ~ x_{t-1}
+ \hat \alpha_2 ~ x_{t-2}
\\
&=
`r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)`
+
`r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)`
~ x_{t-1}
+
`r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull() |> round(3)`
~ x_{t-2}
\end{align*}
### Confidence Interval for the Model Parameters
We can compute an approximate 95% confidence interval for $\alpha_i$ as:
$$
\left(
\hat \alpha_i - 2 \cdot SE_{\hat \alpha_i}
, ~
\hat \alpha_i + 2 \cdot SE_{\hat \alpha_i}
\right)
$$
where $\hat \alpha_i$ is our estimate of the $i^{th}$ parameter and $SE_{\hat \alpha_i}$ is the standard error of the respective estimate. These values are given in the R output from the code below.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
ci_summary <- tidy(fit_ar) |>
mutate(
lower = estimate - 2 * std.error,
upper = estimate + 2 * std.error
)
```
<!-- Beginning of three columns -->
::: columns
::: {.column width="30%"}
95% confidence interval for $\alpha_0$:
$$
\left(
\hat \alpha_0 - 2 \cdot SE_{\hat \alpha_0}
, ~
\hat \alpha_0 + 2 \cdot SE_{\hat \alpha_0}
\right)
$$
$$
\left(
`r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(std.error) |> pull() |> round(3)`
,
\right.
~~~~~~~~~~~~~~~~~~~
$$
$$
~~~~~~~~~~~~~~~~~~~
\left.
`r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(std.error) |> pull() |> round(3)`
\right)
$$
$$
\left(
`r ((tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull()) - 2 * (tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(estimate) |> pull()) + 2 * (tidy(fit_ar) |> filter(str_detect(term, "const")) |> select(std.error) |> pull())) |> round(3)`
\right)
$$
This confidence interval contains $\alpha_0 = `r alpha0 * (1 - alpha1 - alpha2)`$.
:::
::: {.column width="5%"}
<!-- empty column to create gap -->
:::
::: {.column width="30%"}
95% confidence interval for $\alpha_1$:
$$
\left(
\hat \alpha_1 - 2 \cdot SE_{\hat \alpha_1}
, ~
\hat \alpha_1 + 2 \cdot SE_{\hat \alpha_1}
\right)
$$
$$
\left(
`r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull() |> round(3)`
,
\right.
~~~~~~~~~~~~~~~~~~~
$$
$$
~~~~~~~~~~~~~~~~~~~
\left.
`r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull() |> round(3)`
\right)
$$
$$
\left(
`r ((tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull()) - 2 * (tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(estimate) |> pull()) + 2 * (tidy(fit_ar) |> filter(str_detect(term, "ar1")) |> select(std.error) |> pull())) |> round(3)`
\right)
$$
This confidence interval contains $\alpha_1 = `r alpha1`$.
:::
::: {.column width="5%"}
<!-- empty column to create gap -->
:::
::: {.column width="30%"}
95% confidence interval for $\alpha_2$:
$$
\left(
\hat \alpha_2 - 2 \cdot SE_{\hat \alpha_2}
, ~
\hat \alpha_2 + 2 \cdot SE_{\hat \alpha_2}
\right)
$$
$$
\left(
`r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull() |> round(3)` - 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(std.error) |> pull() |> round(3)`
,
\right.
~~~~~~~~~~~~~~~~~~~
$$
$$
~~~~~~~~~~~~~~~~~~~
\left.
`r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull() |> round(3)` + 2 \cdot `r tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(std.error) |> pull() |> round(3)`
\right)
$$
$$
\left(
`r ((tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull()) - 2 * (tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(std.error) |> pull())) |> round(3)`
, ~
`r ((tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(estimate) |> pull()) + 2 * (tidy(fit_ar) |> filter(str_detect(term, "ar2")) |> select(std.error) |> pull())) |> round(3)`
\right)
$$
This confidence interval contains $\alpha_2 = `r alpha2`$.
:::
:::
<!-- End of three columns -->
All three confidence intervals contain the true parameter values we used for the simulation.
### Residuals
We can compute the residuals in the same manner as we did for the other models.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Working with a partner, do the following
- Write the expression used to compute the residuals.
- Find the residuals of this sequence using your expression.
- Here are the first few residuals. Compare these to the values you computed.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
fit_ar |>
residuals()
```
- Explain why there are no residuals for times $t=1$ and $t=2$.
:::
The variance of the residuals is `r fit_ar |> residuals() |> as_tibble() |> select(.resid) |> na.omit() |> pull() |> var() |> round(3)`. This is close to `r sigma_sqr`, the parameter used in the simulation.
## Small-Group Activity: Global Warming (20 min)
<a id="GlobalWarming">The</a> time plot below illustrates the change in global
surface temperature compared to the long-term average
observed from 1951 to 1980. (Source: NASA/GISS.)
```{r}
#| code-fold: true
#| code-summary: "Show the code"
temps_ts <- rio::import("https://byuistats.github.io/timeseries/data/global_temparature.csv") |>
as_tsibble(index = year)
temps_ts |> autoplot(.vars = change) +
labs(
x = "Year",
y = "Temperature Change (Celsius)",
title = paste0("Change in Mean Annual Global Temperature (", min(temps_ts$year), "-", max(temps_ts$year), ")")
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
```
### Using the PACF to Choose $p$ for an $AR(p)$ Process
In the [previous lesson](https://byuistats.github.io/timeseries/chapter_4_lesson_3.html#pacfTable), we noted that the partial correlogram can be used to assess the number of parameters in an AR model.
Here is a partial correlogram for the change in the mean annual global temperature.
<!-- pacf(stock_ts$value, plot=TRUE, lag.max = 25) -->
```{r}
#| code-fold: true
#| code-summary: "Show the code"
pacf(temps_ts$change)
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Working with your partner, do the following:
- We will apply an $AR(p)$ model. What value of $p$ is suggested by the pacf?
- Using the value of $p$ you identified, fit an $AR(p)$ model to the global temperature data. State the fitted $AR(p)$ model in the form
$$\hat x_t = \cdots$$
- Obtain 95% confidence intervals for each of the parameters. Which are significantly different from zero?
- Give the first three residual values (skipping the NAs).
- What is the estimate of $\sigma^2$?
- Make a correlogram for the residuals. Does it appear that your model has fully explained the variation in the temperatures?
:::
### Fitting Models (Dynamic Number of Parameters)
You may have concluded that $p=3$ might be insufficient for modeling these data.
We now explore a technique that allows R to choose $p$ based on the significance of the parameters.
If we specify `order(1:9)` in the model statement, R returns the largest $AR(p)$ model (up to $p=9$) for which the parameter $\alpha_p$ is significant.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
global_ar <- temps_ts |>
model(AR(change ~ order(1:9)))
tidy(global_ar)
```
R returned an
$AR(`r tidy(global_ar) |> as_tibble() |> dplyr::select(term) |> tail(1) |> right(1)`)$
model for this time series.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
Working with your partner, do the following:
- State the fitted $AR(p)$ model in the form
$$\hat x_t = \cdots$$
- Obtain 95% confidence intervals for each of the parameters. Which are significantly different from zero?
- Give the first three residual values (skipping the NAs).
- What is the estimate of $\sigma^2$?
- Make a correlogram for the residuals. Does it appear that your model has fully explained the variation in the temperatures? Justify your answer.
:::
### Stationarity of the $AR(p)$ Model
With the exception of a lone seemingly spurious autocorrelation, there are no significant values of the acf of the residuals in the
$AR(`r tidy(global_ar) |> as_tibble() |> dplyr::select(term) |> tail(1) |> right(1)`)$
model. This suggests that the model accounts for the variation in the time series.
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Write the characteristic equation for the $AR(`r tidy(global_ar) |> as_tibble() |> dplyr::select(term) |> tail(1) |> right(1)`)$
model you developed.
- Click on the link below to obtain a more precise version of the characteristic equation, then solve the characteristic equation by any method.
<a href="javascript:showhide('CharacteristicEquation')"
style="font-size:.8em;">Characteristic Equation</a>
::: {#CharacteristicEquation style="display:none;"}
```{r}
#| code-fold: true
#| code-summary: "Show the code"
alphas <- global_ar |> coefficients() |> tail(-1) |> dplyr::select(estimate) |> pull()
cat(
"0 = 1",
"- (", alphas[1], ") * x",
"- (", alphas[2], ") * x^2",
"- (", alphas[3], ") * x^3",
"\n ",
"- (", alphas[4], ") * x^4",
"- (", alphas[5], ") * x^5",
"- (", alphas[6], ") * x^6"
)
```
:::
- Is our $AR(`r tidy(global_ar) |> as_tibble() |> dplyr::select(term) |> tail(1) |> right(1)`)$
model stationary?
:::
## Class Activity: Forecasting with an $AR(p)$ Model (5 min)
We now use the model to forecast the mean temperature difference for the next 50 years.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
temps_forecast <- global_ar |> forecast(h = "50 years")
temps_forecast |>
autoplot(temps_ts, level = 95) +
geom_line(aes(y = .fitted, color = "Fitted"),
data = augment(global_ar)) +
scale_color_discrete(name = "") +
labs(
x = "Year",
y = "Temperature Change (Celsius)",
title = paste0("Change in Mean Annual Global Temperature (", min(temps_ts$year), "-", max(temps_ts$year), ")"),
subtitle = paste0("50-Year Forecast Based on our AR(", tidy(global_ar) |> as_tibble() |> dplyr::select(term) |> tail(1) |> right(1), ") Model")
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
<!-- Check your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Does this forecast seem appropriate for the data? Why or why not?
:::
### Class Activity: Comparison to the Results in Section 4.6.3 of the Book (5 min)
In Sections 1.4.5 and 4.6.3 of the textbook, the authors present a similar dataset on the mean annual temperatures on Earth through 2005. Here is a time plot of their data:
```{r}
#| code-fold: true
#| code-summary: "Show the code"
global_ts <- tibble(x = scan("data/global.dat")) |>
mutate(
date = seq(
ymd("1856-01-01"),
by = "1 months",
length.out = n()),
year = year(date),
year_month = tsibble::yearmonth(date)
) |>
summarise(x = mean(x), .by = year) |>
as_tsibble(index = year)
global_ts |> autoplot(.vars = x) +
labs(
x = "Year",
y = "Temperature Change (Celsius)",
title = paste0("Change in Mean Annual Global Temperature (", min(global_ts$year), "-", max(global_ts$year), ")"),
subtitle = "Data from Textbook Sections 1.4.5 and 4.6.3"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),