-
Notifications
You must be signed in to change notification settings - Fork 3
/
chapter_2_lesson_2.qmd
768 lines (597 loc) · 21.3 KB
/
chapter_2_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
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
---
title: "Autocorrelation Concepts"
subtitle: "Chapter 2: 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>
```
```{r}
#| include: false
get_data_for_cov_table <- function(offset = 1) {
# set random number generator seed
set.seed(997)
# set parameters
n <- 10
rho <- 0.99
mu <- 0
sigma <- 3
# build population correlation matrix
tmp.r <- matrix(rho, n, n)
tmp.r <- tmp.r^abs(row(tmp.r)-col(tmp.r))
# simulate correlated normal random data
x1 <- round(mvrnorm(1, rep(mu,n), sigma^2 * tmp.r),1)
# build a data frame
df <- data.frame(t = 1:length(x1),
x = x1,
y = lead(x1, offset)) %>%
mutate(
xx = x - mean(x),
xx2 = xx^2,
yy = y - mean(x),
# yy2 = yy^2,
xy = xx * yy
) %>%
dplyr::select(t, x, y, xx, xx2, yy, xy)
return(df)
}
make_cov_table_df <- function(df, offset=1, decimals_1st_order = 5, decimals_2nd_order = 5) {
# Color vector
oi_colors <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#F5C710", "#CC79A7", "#999999")
df_summary <- df %>%
summarize(
x = sum(x),
y = sum(y, na.rm = TRUE),
xx = sum(xx),
yy = sum(yy, na.rm = TRUE),
xx2 = sum(xx2),
# yy2 = sum(yy2, na.rm = TRUE),
xy = sum(xy, na.rm = TRUE)
) %>%
# round_df(3) %>%
mutate(t = paste0("sum")) %>%
mutate(
t = paste0("sum"),
x = round_as_text(x, 1),
y = round_as_text(y, 1),
xx = round_as_text(xx, decimals_1st_order),
xx2 = round_as_text(xx2, decimals_2nd_order),
yy = round_as_text(yy, decimals_1st_order),
# yy2 = round_as_text(yy2, decimals_2nd_order),
xy = round_as_text(xy, decimals_2nd_order)
) %>%
dplyr::select(t, x, y, xx, xx2, yy, xy)
out <- df %>%
mutate(
t = as.character(t),
x = round_as_text(x, 1),
y = round_as_text(y, 1),
xx = round_as_text(xx, decimals_1st_order),
xx2 = round_as_text(xx2, decimals_2nd_order),
yy = round_as_text(yy, decimals_1st_order),
# yy2 = round_as_text(yy2, decimals_2nd_order),
xy = round_as_text(xy, decimals_2nd_order)
) %>%
mutate(
x = cell_spec(x,
color = case_when(
is.na(x) ~ "#999999",
TRUE ~ oi_colors[( row_number() + 0 ) %% 9 + 1]
)
),
y = cell_spec(y,
color = case_when(
is.na(y) ~ "#999999",
TRUE ~ oi_colors[( row_number() + offset ) %% 9 + 1]
)
)
) %>%
mutate(
# x = ifelse(row_number() > nrow(.) - offset, paste0("[",x,"]"), x),
y = ifelse(row_number() > nrow(.) - offset, NA, y),
) %>%
replace(., is.na(.), "—") %>%
bind_rows(df_summary) %>%
rename(
"x_t" = x,
"x_{t+k}" = y,
# paste0("x_{t+", offset, "}") = y,
"x_t-mean(x)" = xx,
"(x_t-mean(x))^2" = xx2,
"x_{t+k}-mean(x)" = yy,
# "(x_{t+k}-mean(x))^2" = yy2,
"(x-mean(x))(x_{t+k}-mean(x))" = xy
)
return(out)
}
# Eliminates the values in columns 3 onward for a specified row_num
make_blank_row_in_cov_table <- function(df, row_num) {
for (col_num in 3:ncol(df)) {
df[row_num, col_num] = ""
}
return(df)
}
# Compute summary values
compute_summaries <- function(df, digits = 4) {
df %>%
summarize(
n = n(),
mean_x = mean(x),
mean_y = mean(y, na.rm = TRUE),
ss_x = sum(xx2),
# ss_y = sum(yy2, na.rm = TRUE),
ss_xy = sum(xy, na.rm = TRUE),
c_0 = sum(xx2) / nrow(.),
c_k = sum(xy, na.rm = TRUE) / nrow(.),
r_k = c_k / c_0
) %>%
round_df(digits)
}
```
## Learning Outcomes
{{< include outcomes/_chapter_2_lesson_2_outcomes.qmd >}}
## Preparation
- Read Sections 2.2.5
## Learning Journal Exchange (10 min)
- Review another student's journal
- What would you add to your learning journal after reading your partner's?
- What would you recommend your partner add to their learning journal?
- Sign the Learning Journal review sheet for your peer
## Hands-on Exercise -- Exploring Sample Autocorrelation (40 min)
<!-- Compute sample acvf and acf -->
### Comparison of Independent and Autocorrelated Error Terms
In the previous lesson, we computed the sample covariance and sample correlation coefficient between two independent variables. When working with time series, the observations are not independent. There is often a relationship between sequential observations. We will compute the autocovariance function and autocorrelation function for a time series. Note: the prefix "auto" comes from a Greek root meaning "self."
The figure below illustrates the difference between a series of data, where the residuals are independent compared to a series with autocorrelated data.
```{r}
#| echo: false
# Generate data
set.seed(101)
x <- 1:100*0.5 + arima.sim(n=100-1, list(ar=0.1, ma=0.2, order=c(1,1,1)))
e <- rnorm(100, 0, 2.5)
y <- 1:100*0.5 + e
# Create data frame
df <- data.frame(
time = 1:100,
Autocorrelated = x,
Independent = y
)
# Plot
ggplot(df, aes(x = time)) +
geom_line(aes(y = Independent, color = "Independent")) +
geom_point(aes(y = Independent, color = "Independent")) +
geom_line(aes(y = Autocorrelated, color = "Autocorrelated")) +
geom_point(aes(y = Autocorrelated, color = "Autocorrelated")) +
scale_color_okabeito(
palette = "full",
reverse = FALSE,
order = c(2,4),
aesthetics = "color"
) +
labs(title="Comparison of Independent and Autocorrelated Error Terms",
x="Time",
y="Values",
color="Series") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- The variances of the residuals for these two series are approximately equal. What characteristics distinguish the two series in the figure above?
:::
### Autocovariance and Autocorrelation
We will use the following data to explore the concepts of autovariance and autocorrelation.
```{r}
#| echo: false
offset_value <- 1
cov_df <- get_data_for_cov_table(offset = offset_value)
# Obtain the number of data values.
n <- nrow(cov_df)
cov_df %>%
dplyr::select(t, x) %>%
rename("$$ x_t $$"= x) %>%
display_table()
```
You can use this R command to read in the observations.
```{r}
#| echo: false
cat("x <- c(", paste(cov_df$x, collapse = ", "),")")
```
```{r}
#| echo: false
ggplot(cov_df, aes(x = t, y = x)) +
geom_line(color = "#0072B2") +
geom_point(color = "#0072B2") +
# scale_color_okabeito(
# palette = "full",
# reverse = TRUE,
# order = c(1,7,8,5,6,3,4,2,9),
# aesthetics = "color"
# ) +
ylim(0,ceiling(max(cov_df$x))) +
labs(title = "Sample Time Series",
x = "Time",
y = "x") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
```
We will use the sample mean of these data repeatedly. The value of $\bar x$ is:
$$
\bar x
= \frac{1}{n} \sum\limits_{t=1}^{n} x_t
= \frac{1}{`r n`} \cdot `r sum(cov_df$x) %>% round(4)`
= `r mean(cov_df$x)`
$$
We will be finding the autocovariance and correlation of a time series with itself. First, we start with a lag of 1. With a lag of 1 the corresponding values of the time series that are being compared are shifted by one time unit. Then, we will consider any integer lag: lag $k$.
### Lag $k$ Sample Autocovariance Function (acvf), $c_k$
The **lag** $k$ sample autocovariance function, acvf, denoted $c_k$, is defined as
$$
c_k = \frac{1}{n} \sum\limits_{t=1}^{n-k}(x_t-\bar x)(x_{t+k}-\bar x)
$$
We denote the lag by the letter $k$, where $k \ge 0$. This is the number of values the data set is shifted to compute the autocovariance.
::: {.callout-tip icon=false title="Check Your Understanding"}
- Explain the equation for $c_k$ to your partner.
- What is the equation for $c_0$, the value of the autocovariance function with lag $k=0$?
- This expression is very similar to a definition we have encountered previously. What is it?
:::
#### Lag $k=1$ Sample Autocovariance Function, $c_1$
We will now find the autocovariance between the values in a time series ($x = x_t$) and the same values, shifted by one unit of time ($y = x_{t+1}$).
```{r}
#| echo: false
offset_value <- 1
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
rename(
"$$ x_t $$" = x_t,
"$$ x_{t+1} $$" = `x_{t+k}`,
"$$ x_t-\\bar x $$" = `x_t-mean(x)`,
"$$ (x_t-\\bar x)^2 $$" = `(x_t-mean(x))^2`,
"$$ x_{t+1}-\\bar x$$"= `x_{t+k}-mean(x)`,
"$$ (x-\\bar x)(x_{t+1}-\\bar x) $$"= `(x-mean(x))(x_{t+k}-mean(x))`
) %>%
display_table()
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- Working with your assigned partner, compute each of the values in row 1 by hand. Recall that $\bar x = `r mean(cov_df$x)`$.
- With your partner, add up the values in the last column to verify that the sum is `r sum(cov_df$xy, na.rm = TRUE) %>% round(10)`.
:::
The scatterplot below illustrates the relationship between the observed data ($x_t$) and the next observation ($x_{t+1}$).
```{r}
#| echo: false
ggplot(cov_df %>% na.omit(), aes(x = x, y = y)) +
geom_point(size = 2, colour= "#0072B2") +
xlab(expression(x[t])) +
ylab(expression(x[t+1])) +
theme_bw() +
ggtitle(paste0("Data pairs with a lag of k=",offset_value)) +
theme(plot.title = element_text(hjust = 0.5))
```
In this example, the second variable is $x_{t+1}$, where $t>1$. the autocovariance of $x_t$ and $x_{t+1}$ is:
$$
c_1
= \frac{1}{n} \sum\limits_{t=1}^{n-1}(x_t-\bar x)(x_{t+1}-\bar x)
= \frac{1}{`r n`} \sum\limits_{t=1}^{`r n - offset_value`}(x_t-\bar x)(x_{t+1}-\bar x)
= \frac{1}{`r n`} \cdot `r summarized$ss_xy`
= `r summarized$c_k`
$$
This is the (auto)covariance of $x$ with itself, but with a lag of 1 time unit. This is the value of the **lag** $k=1$ autocovariance function, acvf_1.
::: {.callout-tip icon=false title="Check Your Understanding"}
- What does the lag 1 autocovariance measure?
:::
### Lag $k$ Sample Autocorrelation Function (acf), $r_k$
The **sample autocorrelation function, acf**, denoted $r_k$, where $k$ is the lag, is defined as
$$
r_k
= \frac{c_k}{c_0}
= \frac{ \frac{1}{n} \sum\limits_{t=1}^{n-k}(x_t-\bar x)(x_{t+k}-\bar x) }{ \frac{1}{n} \sum\limits_{t=1}^{n}(x_t-\bar x)^2 }
= \frac{ \sum\limits_{t=1}^{n-k}(x_t-\bar x)(x_{t+k}-\bar x) }{ \sum\limits_{t=1}^{n}(x_t-\bar x)^2 }
$$
Note that $c_0$ is the variance of $x$, but computed by dividing by $n$, instead of $n-1$.
::: {.callout-tip icon=false title="Check Your Understanding"}
- Interpret the components of the numerator and the denominator of the expression for $r_k$ to your partner.
:::
#### Lag $k=1$ Sample Autocorrelation Function, $r_1$
We can compute the **lag 1 autocorrelation** or the **autocorrelation of** $x$ with lag 1 as the quotient $r_1 = \frac{c_1}{c_0}$. We have already determined that
$c_1 = `r summarized$c_k %>% round(4)`$. We now compute $c_0$:
$$
c_0 = \frac{1}{n} \sum\limits_{t=1}^{n-0} (x_t-\bar x)(x_{t+0}-\bar x)
= \frac{1}{n} \sum\limits_{t=1}^{n} (x_t-\bar x)^2
= \frac{1}{`r n`} \cdot `r summarized$ss_x %>% round(4)`
= `r summarized$c_0 %>% round(4)`
$$
We use $c_0$ and $c_1$ to compute $r_1$. Here are two ways we can compute this value:
\begin{align*}
r_1
&= \frac{c_1}{c_0}
=
\frac{ \frac{1}{n} \sum\limits_{t=1}^{`r n - offset_value`}(x_t-\bar x)(x_{t+1}-\bar x)
}{
\frac{1}{n} \sum\limits_{t=1}^{`r n`}(x_t-\bar x)^2
}
=
\frac{
\frac{1}{`r summarized$n`} \cdot `r summarized$ss_xy`
}{
\frac{1}{`r summarized$n`} \cdot `r summarized$ss_x`
}
= \frac{`r summarized$c_k`}{`r summarized$c_0`}
= `r summarized$r_k %>% round(4)`
\\
&=
\frac{ \sum\limits_{t=1}^{`r n - offset_value`}(x_t-\bar x)(x_{t+1}-\bar x)
}{
\sum\limits_{t=1}^{`r n`}(x_t-\bar x)^2
}
= \frac{`r summarized$ss_xy`}{`r summarized$ss_x`}
= `r summarized$r_k %>% round(4)`
\end{align*}
- What does the lag 1 autocorrelation, $c_1$, measure?
```{r}
#| echo: false
# Initialize table
auto_cov_cor_table <- data.frame(lag = integer(),
autocovariance = double(),
autocorrelation = double())
```
```{r}
#| echo: false
# Add rows to the table
auto_cov_cor_table <- auto_cov_cor_table %>%
bind_rows(c(
lag = offset_value,
autocovariance = summarized$c_k,
autocorrelation = summarized$r_k
)
)
# # Display the table, except row 2
# auto_cov_cor_table %>%
# mutate( ####################################### Suppresses row 2
# autocovariance = ifelse(row_number() == 2,"", autocovariance),
# autocorrelation = ifelse(row_number() == 2,"", autocorrelation)
# ) %>%
# knitr::kable(format = "html", align='ccc', escape = FALSE, width = NA) %>%
# kable_styling(full_width = FALSE, "striped")
```
#### Lag $k = 2$
::: {.callout-tip icon=false title="Check Your Understanding"}
- Working with your assigned partner, fill in the blanks in the following table. Use the results to compute $c_2$ and $r_2$.
<a href="https://github.com/TBrost/BYUI-Timeseries-Drafts/raw/master/handouts/chapter_2_2_handout.xlsx" download="chapter_2_2_handout.xlsx"> Tables-Handout-Excel </a>
:::
```{r}
#| echo: false
offset_value <- 2
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
rename(
"$$ x_t $$" = x_t,
"$$ x_{t+k} $$" = `x_{t+k}`,
"$$ x_t-\\bar x $$" = `x_t-mean(x)`,
"$$ (x_t-\\bar x)^2 $$" = `(x_t-mean(x))^2`,
"$$ x_{t+k}-\\bar x$$"= `x_{t+k}-mean(x)`,
"$$ (x-\\bar x)(x_{t+k}-\\bar x) $$"= `(x-mean(x))(x_{t+k}-mean(x))`
) %>%
############ Let students fill in these rows ##########
make_blank_row_in_cov_table(4) %>%
make_blank_row_in_cov_table(5) %>%
make_blank_row_in_cov_table(6) %>%
make_blank_row_in_cov_table(11) %>%
display_table()
```
The figure below illustrates the relationship between $x_t$ and $x_{t+2}$.
```{r}
#| echo: false
ggplot(cov_df %>% na.omit(), aes(x = x, y = y)) +
geom_point(size = 2, colour= "#0072B2") +
xlab(expression(x[t])) +
ylab(expression(x[t+2])) +
theme_bw() +
ggtitle(paste0("Data pairs with a lag of k=",offset_value)) +
theme(plot.title = element_text(hjust = 0.5))
```
```{r}
#| echo: false
# Add rows to the table
auto_cov_cor_table <- auto_cov_cor_table %>%
bind_rows(c(
lag = offset_value,
autocovariance = summarized$c_k,
autocorrelation = summarized$r_k
)
)
```
#### Lag $k = 3$
```{r}
#| echo: false
offset_value <- 3
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
rename(
"$$ x_t $$" = x_t,
"$$ x_{t+k} $$" = `x_{t+k}`,
"$$ x_t-\\bar x $$" = `x_t-mean(x)`,
"$$ (x_t-\\bar x)^2 $$" = `(x_t-mean(x))^2`,
"$$ x_{t+k}-\\bar x$$"= `x_{t+k}-mean(x)`,
"$$ (x-\\bar x)(x_{t+k}-\\bar x) $$"= `(x-mean(x))(x_{t+k}-mean(x))`
) %>%
display_table()
```
The figure below illustrates the correlations between $x_t$ and $x_{t+3}$. Note that $c_3 = \dfrac{`r summarized$ss_xy`}{`r summarized$n`} = `r summarized$c_k`$ and $r_3 = \dfrac{`r summarized$c_k`}{`r summarized$c_0`} = `r summarized$r_k`$.
::: {.callout-tip icon=false title="Check Your Understanding"}
- Does the value of $r_3 = `r summarized$r_k`$ seem reasonable, given the pattern in this plot?
:::
```{r}
#| echo: false
ggplot(cov_df %>% na.omit(), aes(x = x, y = y)) +
geom_point(size = 2, colour= "#0072B2") +
xlab(expression(x[t])) +
ylab(expression(x[t+3])) +
theme_bw() +
ggtitle(paste0("Data pairs with a lag of k=",offset_value)) +
theme(plot.title = element_text(hjust = 0.5))
```
```{r}
#| echo: false
# Add rows to the table
auto_cov_cor_table <- auto_cov_cor_table %>%
bind_rows(c(
lag = offset_value,
autocovariance = summarized$c_k,
autocorrelation = summarized$r_k
)
)
```
#### Lag $k = 4$
::: {.callout-tip icon=false title="Check Your Understanding"}
- Compute $c_4$ and $r_4$ using R (but not automated functions), Excel, or hand calculations.
:::
```{r}
#| echo: false
offset_value <- 4
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
rename(
"$$ x_t $$" = x_t,
"$$ x_{t+k} $$" = `x_{t+k}`,
"$$ x_t-\\bar x $$" = `x_t-mean(x)`,
"$$ (x_t-\\bar x)^2 $$" = `(x_t-mean(x))^2`,
"$$ x_{t+k}-\\bar x$$"= `x_{t+k}-mean(x)`,
"$$ (x-\\bar x)(x_{t+k}-\\bar x) $$"= `(x-mean(x))(x_{t+k}-mean(x))`
) %>%
make_blank_row_in_cov_table(1) %>%
make_blank_row_in_cov_table(2) %>%
make_blank_row_in_cov_table(3) %>%
make_blank_row_in_cov_table(4) %>%
make_blank_row_in_cov_table(5) %>%
make_blank_row_in_cov_table(6) %>%
make_blank_row_in_cov_table(7) %>%
make_blank_row_in_cov_table(8) %>%
make_blank_row_in_cov_table(9) %>%
make_blank_row_in_cov_table(10) %>%
make_blank_row_in_cov_table(11) %>%
display_table()
```
The figure below illustrates the correlations between $x_t$ and $x_{t+4}$.
::: {.callout-tip icon=false title="Check Your Understanding"}
- Does the value of $r_4$ you computed seem reasonable, given the pattern in this plot?
:::
```{r}
#| echo: false
ggplot(cov_df %>% na.omit(), aes(x = x, y = y)) +
geom_point(size = 2, colour= "#0072B2") +
xlab(expression(x[t])) +
ylab(expression(x[t+4])) +
theme_bw() +
ggtitle(paste0("Data pairs with a lag of k=",offset_value)) +
theme(plot.title = element_text(hjust = 0.5))
```
## Class Activity: Using R to compute the acvf and acf (5 min)
We will continue to use the following sample data.
```{r}
#| echo: false
# simulate correlated normal random data
x1 <- get_toy_data()
df <- data.frame(x = x1)
cat(" x <- c(",paste(x1, collapse = ", "),") \n df <- data.frame(x = x)")
```
### acvf
This code gives the values of the acvf.
```{r}
acf(df$x, plot=FALSE, type = "covariance")
```
### acf
We can obtain the acf by changing the argument for the paramter `type` to `"correlation"`.
```{r}
acf(df$x, plot=FALSE, type = "correlation")
```
## Homework Preview (5 min)
- Review upcoming homework assignment
- Clarify questions
## Homework
::: {.callout-note icon=false}
## Download Homework
<a href="https://byuistats.github.io/timeseries/homework/homework_2_2.qmd" download="homework_2_2.qmd"> homework_2_2.qmd </a>
:::
<a href="javascript:showhide('Solutions')"
style="font-size:.8em;">Class Activity: k=2</a>
::: {#Solutions style="display:none;"}
<a href="https://github.com/TBrost/BYUI-Timeseries-Drafts/raw/master/handouts/chapter_2_2_handout_key.xlsx" download="chapter_2_2_handout_key.xlsx"> Tables-Handout-Excel-key </a>
Solutions to Class Activity: $k=2$
```{r}
#| echo: false
offset_value <- 2
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
display_table()
```
\begin{align*}
c_2
&= \frac{1}{n} \sum\limits_{t=1}^{n-1}(x_t-\bar x)(x_{t+2}-\bar x)
= \frac{1}{`r n`} \cdot `r summarized$ss_xy`
= `r summarized$c_k |> round(3)` \\
r_2
&= \frac{c_2}{c_0}
= \frac{`r summarized$c_k |> round(3)`}{`r summarized$c_0 |> round(3)`}
= `r summarized$r_k |> round(3)`
\end{align*}
:::
<a href="javascript:showhide('Solutions_k4')"
style="font-size:.8em;">Class Activity: k=4</a>
::: {#Solutions_k4 style="display:none;"}
Solutions to Class Activity: $k=4$
```{r}
#| echo: false
offset_value <- 4
cov_df <- get_data_for_cov_table(offset = offset_value)
cov_table <- cov_df %>%
make_cov_table_df(offset = offset_value)#, decimals_1st_order = 1, decimals_2nd_order = 2)
summarized <- compute_summaries(cov_df)
cov_table %>%
display_table()
```
\begin{align*}
c_4
&= \frac{1}{n} \sum\limits_{t=1}^{n-1}(x_t-\bar x)(x_{t+4}-\bar x)
= \frac{1}{`r n`} \cdot `r summarized$ss_xy`
= `r summarized$c_k |> round(3)` \\
r_4
&= \frac{c_4}{c_0} = \frac{`r summarized$c_k |> round(3)`}{`r summarized$c_0 |> round(3)`}
= `r summarized$r_k |> round(3)`
\end{align*}
:::