forked from TBrost/BYUI-Timeseries-Drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_1_lesson_5.qmd
1139 lines (843 loc) · 30.2 KB
/
chapter_1_lesson_5.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: "Multiplicative Models"
subtitle: "Chapter 1: Lesson 5"
format: html
editor: source
sidebar: false
---
```{r}
#| include: false
source("common_functions.R")
pacman::p_load(data.table) # for transpose
```
```{=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_1_lesson_5_outcomes.qmd >}}
## Preparation
- Review Sections 1.5.1-1.5.3
## 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
## Class Activity: Comparing Models in the Textbook Versus R (2 min)
Both the textbook and R use the same model in the additive case:
$$
x_t = m_t + s_t + z_t
$$
However, there is a discrepancy in the definitions for the mulitplicative models. The textbook defines the multiplicative model as
$$
x_t = m_t \cdot s_t + z_t
$$ but R defines the multiplicative model as
$$
x_t = m_t \cdot s_t \cdot z_t
$$ You can investigate R's definition by executing this command in RStudio.
```{r}
#| eval: false
?classical_decomposition
```
## Class Activity: Exploring Simulated Time Series Data (10 min)
So far, you have learned how to estimate a trend using aggregated data (i.e., an annual average) or a moving average. We will compute the seasonal effect and use this to get the random component.
### Additive Model
The code hidden below simulates 10 years of a monthly time series with a linear trend and seasonal variation based on an additive model. Because the data are simulated, we know exactly which functions were used to create it, and we can observe what happens when we decompose this function.
#### Table 1: Simulated Data (Additive Model)
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
# Set random seed for reproducibility
set.seed(20)
# Set parameters & initialize vectors
num_years <- 10
n <- 12 * num_years
sigma <- .75
a <- 0.05
b <- 1
c <- 0.5
trend <- seasonal <- x_t <- rep(0,n)
time_seq <- seq(1,n)
# Generate correlated error terms
w <- rnorm(n + 4, 0, 1)
z = w + lead(w,1) + lead(w,2) + lead(w,3) + lead(w,4)
z = head(z, n) / 2
# Get date
year_seq <- lubridate::year(today()) - num_years + (time_seq - 1) %/% 12
month_seq <- (time_seq - 1) %% 12 + 1
date_seq <- ymd(paste0(year_seq,"-",month_seq,"-01"))
# Get data
for (t in 1:n) {
trend[t] <- a * t + 10
seasonal[t] <- b * sin(t / 12 * 2 * pi * 1) + c * cos(t / 12 * 2 * pi * 3)
x_t[t] <- trend[t] + seasonal[t] + z[t]
}
x_df <- data.frame(x_t = x_t, trend = trend, seasonal = seasonal)
start_year <- lubridate::year(today()) - num_years
start_date <- lubridate::ymd(paste0(start_year,"-01-01"))
# start_date <- lubridate::ymd("1958-01-01")
date_seq <- seq(start_date,
start_date + months(nrow(x_df)-1),
by = "1 months")
x_df_ts <- x_df |>
mutate(
date = date_seq,
month = tsibble::yearmonth(date)
) |>
select(date, month, trend, seasonal, x_t) |>
as_tsibble(index = month)
```
```{r}
#| echo: false
x_df_ts |>
rename(
Date = date,
Month = month,
"Trend, $$m_t$$" = trend,
"Seasonal, $$s_t$$" = seasonal,
"Data, $$x_t$$" = x_t
) |>
display_partial_table(6,2)
```
The code above has generated simulated data, where the trend is linear with equation
$$
m_t = \frac{t}{20}
$$
and the seasonal effect follows the function
$$
s_t = \sin \left( \frac{t\pi}{6} \right) + \frac{1}{2}\cos\left(\frac{t \pi}{18} \right)
$$
Letting $t$ represent the month number across 10 years, we simulate a time series. Click on the tabs below to compare the actual construction of the time series (using the components generated in the code above) to the decomposition in R.
::: panel-tabset
#### Actual Construction (Additive)
Here is a plot of the components of the simulated data.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
trend_plot <- ggplot(x_df_ts, aes(x=month, y=trend)) +
geom_line() +
labs(
title="Plot of Trend",
x="Month",
y="Trend"
) +
theme(plot.title = element_text(hjust = 0.5))
seasonal_plot <- ggplot(x_df_ts, aes(x=month, y=seasonal)) +
geom_line() +
labs(
title="Plot of Seasonal Effect",
x="Month",
y="Seasonal"
) +
theme(plot.title = element_text(hjust = 0.5))
error_plot <- ggplot(x_df_ts, aes(x = month, y = x_t - trend - seasonal)) +
geom_line() +
labs(
title="Plot of Random Error Term",
x="Month",
y="Random"
) +
theme(plot.title = element_text(hjust = 0.5))
x_plot <- ggplot(x_df_ts, aes(x=month, y=x_t)) +
geom_line() +
labs(
title="Plot of Simulated Time Series",
x="Month",
y="$$x_t$$"
) +
theme(plot.title = element_text(hjust = 0.5))
x_plot <- x_plot + labs(title = "True (Simulated) Values", x = NULL)
trend_plot <- trend_plot + labs(title = NULL, x = NULL)
seasonal_plot <- seasonal_plot + labs(title = NULL, x = NULL)
error_plot <- error_plot + labs(title = NULL)
x_plot / trend_plot / seasonal_plot / error_plot
```
#### Decomposition (Additive)
Now, we use R to decompose the time series $\{x_t\}$.
```{r}
#| warning: false
#| code-fold: true
#| code-summary: "Show the code"
x_decompose <- x_df_ts |>
model(feasts::classical_decomposition(x_t,
type = "add")) |>
components()
autoplot(x_decompose)
```
```{r}
#| include: false
i <- c(0:n)
# a <- 0
x_t <- a*i + b*sin(i/12*2*pi) + c*cos(i/12*2*pi*3)
df <- data.frame(i, x_t)
ggplot(df, aes(x=i, y=x_t)) +
geom_line() +
labs(title="Plot of Function",
x="i",
y="$$x_t$$")
```
:::
<!-- End of panel-tabset -->
::: {.callout-tip icon="false" title="Check Your Understanding"}
- How does the (estimated) decomposition compare to the theoretical values above?
- How well is the trend estimated?
- How well is the seasonal effect estimated?
- How well is the random effect estimated?
- Make changes to the simulated data and observe the effect on the plots
:::
### Multiplicative Model
We now simulate data and apply R's multiplicative model. This implies that the error term, $z_t$, has a mean of 1, rather than 0.
#### Table 2: Simulated Data (Multiplicative Model)
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# Set random seed for reproducibility
set.seed(123)
# Set parameters & initialize vectors
num_years <- 10
n <- 12 * num_years
sigma <- .75
a <- 0.03
b <- 1
c <- 0.5
trend <- seasonal <- x_t <- rep(0,n)
time_seq <- seq(1,n)
# Generate correlated error terms
w <- rnorm(n + 4, 0.2, 0.1) # Changed to a mean of 1 and sd of 0.03
z = w + lead(w,1) + lead(w,2) + lead(w,3) + lead(w,4)
z = head(z, n)
# Get date
year_seq <- lubridate::year(today()) - num_years + (time_seq - 1) %/% 12
month_seq <- (time_seq - 1) %% 12 + 1
date_seq <- ymd(paste0(year_seq,"-",month_seq,"-01"))
# Get data
for (t in 1:n) {
trend[t] <- exp(a * t)
seasonal[t] <- exp( b * sin(t / 12 * 2 * pi * 1) + c * cos(t / 12 * 2 * pi * 3) + 1 )
x_t[t] <- trend[t] * seasonal[t] * z[t] # Note R's definition of the mult. model
}
x_df <- data.frame(x_t = x_t, trend = trend, seasonal = seasonal)
start_year <- lubridate::year(today()) - num_years
start_date <- lubridate::ymd(paste0(start_year,"-01-01"))
# start_date <- lubridate::ymd("1958-01-01")
date_seq <- seq(start_date,
start_date + months(nrow(x_df)-1),
by = "1 months")
x_df_ts <- x_df |>
mutate(
date = date_seq,
month = tsibble::yearmonth(date)
) |>
select(date, month, trend, seasonal, x_t) |>
as_tsibble(index = month)
```
```{r}
#| echo: false
x_df_ts |>
rename(
Date = date,
Month = month,
"Trend, $$m_t$$" = trend,
"Seasonal, $$s_t$$" = seasonal,
"Data, $$x_t$$" = x_t
) |>
display_partial_table(6,2)
```
The code above simulated data, where the trend is exponential with equation
$$
m_t = e^{0.03 t}
$$
and the seasonal effect follows the function
$$
s_t = \sin \left( \frac{t\pi}{6} \right) + \frac{1}{2}\cos\left(\frac{t \pi}{18} \right) + 1
$$
Letting $t$ represent the month number across 10 years, we simulate a time series with multiplicative effects. Click on the tabs below to compare the actual construction of the time series (using the components generated in the code above) to the decomposition in R.
::: panel-tabset
#### Actual Construction (Multiplicative)
Here is a plot of the components of the simulated data.
```{r}
#| warning: false
#| code-fold: true
#| code-summary: "Show the code"
trend_plot <- ggplot(x_df_ts, aes(x=month, y=trend)) +
geom_line() +
labs(
title="Plot of Trend",
x="Month",
y="Trend"
) +
theme(plot.title = element_text(hjust = 0.5))
seasonal_plot <- ggplot(x_df_ts, aes(x=month, y=seasonal)) +
geom_line() +
labs(
title="Plot of Seasonal Effect",
x="Month",
y="Seasonal"
) +
theme(plot.title = element_text(hjust = 0.5))
error_plot <- ggplot(x_df_ts, aes(x = month, y = x_t / trend / seasonal)) +
geom_line() +
labs(
title="Plot of Random Error Term",
x="Month",
y="Random"
) +
theme(plot.title = element_text(hjust = 0.5))
x_plot <- ggplot(x_df_ts, aes(x=month, y=x_t)) +
geom_line() +
labs(
title="Plot of Simulated Time Series",
x="Month",
y="x_t"
) +
theme(plot.title = element_text(hjust = 0.5))
x_plot <- x_plot + labs(title = "True (Simulated) Values", x = NULL)
trend_plot <- trend_plot + labs(title = NULL, x = NULL)
seasonal_plot <- seasonal_plot + labs(title = NULL, x = NULL)
error_plot <- error_plot + labs(title = NULL)
x_plot / trend_plot / seasonal_plot / error_plot
```
#### Decomposition (Multiplicative)
Now, we use R to decompose the time series $\{x_t\}$.
```{r}
#| warning: false
#| code-fold: true
#| code-summary: "Show the code"
x_decompose <- x_df_ts |>
model(feasts::classical_decomposition(x_t,
type = "mult")) |>
components()
autoplot(x_decompose)
```
```{r}
#| include: false
i <- c(0:n)
# a <- 0
x_t <- a*i + b*sin(i/12*2*pi) + c*cos(i/12*2*pi*3)
df <- data.frame(i, x_t)
ggplot(df, aes(x=i, y=x_t)) +
geom_line() +
labs(title="Plot of Function",
x="i",
y="$$x_t$$")
```
:::
<!-- End of panel-tabset -->
::: {.callout-tip icon="false" title="Check Your Understanding"}
- How does the (estimated) decomposition compare to the theoretical values above?
- How well is the trend estimated?
- How well is the seasonal effect estimated?
- How well is the random effect estimated?
- Make changes to the simulated data and observe the effect on the plots
:::
### Which Model Should I Use: Additive or Multiplicative?
Compare the following two time series.
```{r}
#| echo: false
# Read and clean rexburg weather data
rexburg_day <- rio::import("https://byuistats.github.io/timeseries/data/rexburg_weather.csv") |>
mutate(date_seq = dates) |>
mutate(
dates = date_seq,
year = lubridate::year(date_seq),
month = lubridate::month(date_seq),
value = rexburg_airport_high
) |>
dplyr::select(-date_seq, -imputed) |>
tibble()
rexburg_daily_ts <- rexburg_day |>
mutate(index = dates) |>
as_tsibble(index = index)
rexburg_annual_ts <- summarise(index_by(rexburg_daily_ts, year), value = mean(value))
rexburg_weather_plot <- autoplot(rexburg_daily_ts, .vars = value) +
labs(
x = "Date",
y = "High Temp (F)",
title = "Daily High Temperature in Rexburg, Idaho"
) +
theme(plot.title = element_text(hjust = 0.5))
# mp <- autoplot(rexburg_daily_ts, .vars = value) +
# labs(y = "high temperature")
# yp <- autoplot(rexburg_annual_ts) +
# labs(y = "high temperature")
# # +
# # scale_x_continuous(breaks = seq(1900, 2010, by = 2))
# mp / yp
# S&P 500
replaceCommas<-function(x){
x<-as.numeric(gsub("\\,", "", x))
}
sp500_dat <- rio::import("https://byuistats.github.io/timeseries/data/sp500.csv") |>
mutate(dates = mdy(Date))
sp500_day <- sp500_dat |>
mutate(date_seq = dates) |>
mutate(
dates = date_seq,
year = lubridate::year(date_seq),
month = lubridate::month(date_seq),
value = replaceCommas(Close)
) |>
dplyr::select(-date_seq) |>
tibble()
sp500_ts <- sp500_day |>
mutate(index = dates) |>
as_tsibble(index = index)
# sp500_annual_ts <- summarise(index_by(sp500_ts, year), value = mean(value))
#
# temp <- sp500_ts |> filter(month == 7 & day(dates) == 1) |>
# select(Date, year)
#
# temp2 <- sp500_annual_ts |>
# right_join(temp)
sp500_plot <- autoplot(sp500_ts, .vars = value) +
labs(
x = "Date",
y = "Closing Price",
title = "Daily Closing Price of the S&P 500 Stock Index"
) +
theme(plot.title = element_text(hjust = 0.5))
rexburg_weather_plot / sp500_plot
```
::: {.callout-tip icon="false" title="Check Your Understanding"}
Complete a table like the following in your Learning Journal to compare characteristics of these two time series. Be sure to include a sketch of the respective time plots.
| | Rexburg Temperature | S&P 500 Closing Price |
|------------------------------------------|---------------------|-----------------------|
| Deterministic or stochastic trend? | | |
| Is there a seasonal effect? | | |
| Is there evidence of cycles? | | |
| Does the variation get bigger over time? | | |
| Additive or multiplicative model? | | |
:::
<!-- ## POSSIBLY: DEMONSTRATE THAT TWO UNRELATED TIME SERIES WILL BE CORRELATED IF THEY BOTH CONTAIN A TREND---------- -->
<!-- Compare Rexburg weather and Basel, Switzerland Weather. (Better yet, Melbourne, Austrailia) -->
## Small Group Activity: Apple's Quarterly Revenue (30 min)
The code below imports and plots the quarterly revenue for Apple Inc. (in billions of U.S. dollars).
```{r}
#| code-fold: true
#| code-summary: "Show the code"
apple_ts <- rio::import("https://byuistats.github.io/timeseries/data/apple_revenue.csv") |>
mutate(
dates = mdy(date),
year = lubridate::year(dates),
quarter = lubridate::quarter(dates),
value = revenue_billions
) |>
dplyr::select(dates, year, quarter, value) |>
arrange(dates) |>
mutate(index = tsibble::yearquarter(dates)) |>
as_tsibble(index = index) |>
dplyr::select(index, dates, year, quarter, value) |>
rename(revenue = value) # rename value to emphasize data context
apple_ts |>
autoplot(.vars = revenue) +
labs(
x = "Quarter",
y = "Apple Revenue, Billions $US",
title = "Apple's Quarterly Revenue, Billions of U.S. Dollars"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- What do you notice?
- Does it seem like there is a trend in the time series?
- Is there evidence of a seasonal effect? If so, during which quarter(s) are the revenues particularly high? particularly low?
- Can you attribute a reason for this behavior?
- Is an additive or multiplicative model more appropriate? Why?
:::
We want to find the seasonally adjusted series for a multiplicative model. This is a multi-step process.
### Centered Moving Average
First, we compute the centered moving average, $\hat m_t$.
```{r}
#| include: false
# computes the 4-quarter centered moving average (m_hat)
apple_ts <- apple_ts |>
mutate(
m_hat = (
(1/2) * lag(revenue, 2)
+ lag(revenue, 1)
+ revenue
+ lead(revenue, 1)
+ (1/2) * lead(revenue, 2)
) / 4
)
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
- Write a mutate statement that will compute the 4-quarter moving average for the variable "revenue" in the tsibble apple_ts. You can use the mutate statement from [Chapter 1 Lesson 4](https://byuistats.github.io/timeseries/chapter_1_lesson_4.html#centered-moving-average) as a starting point.
```{r}
#| eval: false
# computes the 4-quarter centered moving average (m_hat)
apple_ts <- apple_ts |>
mutate(
m_hat =
# Your code goes here
)
```
:::
```{r}
#| echo: false
# Defines example quarter to illustrate computation of m_hat
sample_year <- 2007
sample_qtr <- 1
row_of_sample_m_hat <- apple_ts |>
as.data.frame() |>
mutate(row_num = row_number()) |>
filter(year == sample_year & quarter == sample_qtr) |>
dplyr::select(row_num) |>
pull()
sample_quarter <- apple_ts |>
as.data.frame() |>
mutate(
quarter_text = case_when(
quarter == 1 ~ "first",
quarter == 2 ~ "second",
quarter == 3 ~ "third",
quarter == 4 ~ "fourth",
TRUE ~ "--ERROR--"
)
) |>
filter(row_number() == row_of_sample_m_hat) |>
dplyr::select(quarter_text) |>
pull()
```
To emphasize the computation of the centered moving average, the observed data values that were used to find $\hat m_t$ for the `r sample_quarter` quarter of `r sample_year` are shown in <span style="color:#009E73;">green</span> in the table below.
### Estimated Quarterly Multiplicative Effect
The centered moving average, $\hat m_t$, is then used to compute the quarterly multiplictive effect, $\hat s_t$:
$$
\hat s_t = \dfrac{ x_t }{ \hat m_t }
$$
#### Table 3: Computation of the Centered Moving Average, $\hat m_t$, and the Estimated Quarterly Multiplicative Effect, $\hat s_t$
```{r}
#| echo: false
apple_shat_ts <- apple_ts |>
dplyr::select(index, year, quarter, revenue, m_hat) |>
mutate(s_hat = revenue / m_hat)
apple_shat_ts |>
round_df(3) |>
mutate(
m_hat = as.character(m_hat),
s_hat = as.character(s_hat),
) |>
mutate(
m_hat = ifelse(row_number() %in% c(4,6,8), "______", m_hat),
s_hat = ifelse(row_number() <= 7, "______", s_hat),
revenue = as.character(revenue)
# adjusted_s_hat = ifelse(row_number() <= 12, "", adjusted_s_hat),
# seasonally_adjusted_x = ifelse(row_number() <= 12, "", seasonally_adjusted_x)
) |>
convert_df_to_char(3) |>
dplyr::select(index, revenue, m_hat, s_hat) |>
rename(
quarter = index,
"Revenue $$x_t$$" = revenue,
"$$ \\hat m_t $$" = m_hat,
"$$ \\hat s_t $$" = s_hat
) |>
color_specific_cell(row_num = row_of_sample_m_hat, col_num = 3, color = "#009E73") |>
color_specific_cell(row_num = row_of_sample_m_hat-2, col_num = 2, color = "#009E73") |>
color_specific_cell(row_num = row_of_sample_m_hat-1, col_num = 2, color = "#009E73") |>
color_specific_cell(row_num = row_of_sample_m_hat, col_num = 2, color = "#009E73") |>
color_specific_cell(row_num = row_of_sample_m_hat+1, col_num = 2, color = "#009E73") |>
color_specific_cell(row_num = row_of_sample_m_hat+2, col_num = 2, color = "#009E73") |>
display_partial_table(14, 5)
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- Working with your assigned partner, fill in the missing values of $\hat m_t$ in the table above.
- Then, find the missing values of $\hat s_t$.
:::
### Seasonally Adjusted Factors
Next, we need to compute the mean (across years) of $\hat s_t$ by quarter. To help us calculate this, it can be convenient to organize the values of $\hat s_t$ in a table, where the rows give the year and the columns give the quarter.
The overall mean of these means will be reasonably close to, but not exactly one.
We adjust these values by dividing the quarterly means by the overall mean.
#### Table 4: Computation of the Seasonally Adjusted Factors, $\bar s_t$
```{r}
#| echo: false
#| warning: false
# Compute s_hat
apple_shat_ts2 <- apple_shat_ts |>
data.frame() |>
mutate(quarter = paste0("Q",quarter)) |>
round_df(3) # Round df to make the computations simpler
wider_df <- apple_shat_ts2 |>
dplyr::select(year, quarter, s_hat) |>
pivot_wider(values_from = "s_hat", names_from = "quarter")
wider_df2 <- wider_df %>%
round_df(3)
wider_df3 <- wider_df2 |>
# Hide bar_s_t values for November and December
# mutate(Q1 = ifelse(row_number() == 1, "______", Q1)) |>
# mutate(Q2 = ifelse(row_number() == 1, "______", Q2)) |>
mutate(Q3 = ifelse(row_number() == 1, "______", Q3)) |>
mutate(Q4 = ifelse(row_number() == 1, "______", Q4)) |>
mutate(Q1 = ifelse(row_number() == 2, "______", Q1)) |>
mutate(Q2 = ifelse(row_number() == 2, "______", Q2)) |>
mutate(Q3 = ifelse(row_number() == 2, "______", Q3)) |>
rename(Year = year)
column_sum <- wider_df3 |>
pivot_longer(cols = c("Q1", "Q2", "Q3", "Q4"), values_to = "revenue", names_to = "quarter") |>
mutate(revenue = as.numeric(revenue)) |>
group_by(quarter) |>
summarise(sum = sum(revenue, na.rm = TRUE))
wider_df3 |>
color_specific_cell(1, 4, "#0072B2") |>
color_specific_cell(1, 5, "#0072B2") |>
color_specific_cell(2, 2, "#0072B2") |>
color_specific_cell(2, 3, "#0072B2") |>
color_specific_cell(2, 4, "#0072B2") |>
rbind(c("Mean", rep("______",4))) |>
color_last_row2("#0072B2") |>
rbind(c("$$ \\bar s_t $$", rep("$$~$$ ______",4))) |>
color_last_row2("#0072B2") |>
display_table()
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- The table above gives the values of $\hat s_t$. Fill in the missing values in the first two rows of the table above. (Note you already computed these.)
- The second-to-last row (labeled "Mean") gives the values of $\bar s_t$. Find the mean of the $\hat s_t$ values for each quarter. Call these means $\bar {\hat s_t}$. To simplify your computations, the sum of the values visible in the table above is summarized here:
```{r}
#| echo: false
column_sum |>
mutate(Quarter = "Partial Sum") |>
pivot_wider(values_from = "sum", names_from = "quarter") |>
display_table()
```
- Compute the mean of the $\bar {\hat s_t}$ values. Call this number $\bar {\bar {\hat s_t}}$ (This number should be relatively close to 1.)
- Divide each of the $\bar {\hat s_t}$ values by $\bar {\bar {\hat s_t}}$ to get $\bar s_t$, the seasonally adjusted factor for quarter $t$. (Note that the mean of the $\bar s_t$ values will be 1.)
$$ \bar s_t = \frac{ \left( \bar {\hat s_t} \right) }{ \left( \bar {\bar {\hat s_t}} \right) } $$
:::
### Random Component and the Seasonally Adjusted Time Series
Using R's definition of the multiplicative model, we calculate the random component by dividing the values in the time series by the product of the trend and the seasonally adjusted factor:
$$
\text{random component} = \dfrac{ x_t }{ \hat m_t \cdot \bar s_t }
$$
The seasonally adjusted series is computed by dividing the respective observed values by $\bar s_t$:
$$
\text{seasonally adjusted series} = \dfrac{ x_t }{ \bar s_t }
$$
Use these equations to calculate the values missing from the table below. The adjusted seasonal effect $\bar s_t$ (s_bar) was computed in the last row of the previous table.
#### Table 5: Computation of the Random Component and the Seasonally Adjusted Time Series
```{r}
#| echo: false
#| warning: false
num_blank_rows <- 7
num_addl_rows <- 3
# Compute s_hat
apple_ts2 <- apple_ts |>
mutate(s_hat = revenue / m_hat)
# Compute the unadjusted_s_bar and s_bar
adj_s_bar_df <- apple_ts2 |>
data.frame() |>
group_by(quarter) |>
summarize(unadjusted_s_bar = mean(s_hat, na.rm = TRUE)) |>
mutate(s_bar_bar = mean(unadjusted_s_bar)) |>
mutate(s_bar = unadjusted_s_bar / s_bar_bar) |>
dplyr::select(quarter, s_bar, s_bar_bar)
# Get seasonally adjusted time series
adjusted_ts <- apple_ts2 |>
left_join(adj_s_bar_df, by = join_by(quarter)) |>
mutate(random = revenue / ( m_hat * s_bar) ) |>
mutate(seasonally_adjusted_x = revenue / s_bar) |>
dplyr::select(-s_bar_bar) |>
as.data.frame() |>
dplyr::select(index, revenue, m_hat, s_hat, s_bar, random, seasonally_adjusted_x)
additional_rows_from_adjusted_ts <- head(adjusted_ts,num_blank_rows + num_addl_rows) |>
tail(num_addl_rows) |>
convert_df_to_char(3)
adjusted_ts |>
as_tibble() |>
round_df(3) |>
filter(row_number() <= num_blank_rows) |>
mutate(
s_hat = "______",
s_bar = "______",
random = "______",
seasonally_adjusted_x = "______"
) |>
convert_df_to_char() |>
bind_rows(additional_rows_from_adjusted_ts) |>
dplyr::select(index, revenue, m_hat, s_hat, s_bar, random, seasonally_adjusted_x) |>
rename(
Quarter = index,
"Revenue $$x_t$$" = revenue,
"$$ \\hat m_t $$" = m_hat,
"$$ \\hat s_t $$" = s_hat,
"$$ \\bar s_t $$" = s_bar,
Random = random,
"Seasonally Adjusted $$x_t$$" = seasonally_adjusted_x
) |>
display_partial_table(num_blank_rows + num_addl_rows, 0)
```
## Class Activity: Computing the Multiplicative Decomposition in R (3 min)
The R code below calculates the decomposition, including the seasonally adjusted time series, beginning with the tsibble `apple_ts`.
#### Table 6: First Few Rows of the Decomposition of the Apple Revenue Time Series
```{r}
#| code-fold: true
#| code-summary: "Show the code"
apple_decompose <- apple_ts |>
model(feasts::classical_decomposition(revenue,
type = "mult")) |>
components()
apple_decompose |>
head(8) |>
display_table()
```
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
autoplot(apple_decompose)
```
The figure below illustrates the original time series (in black), the centered moving average $\hat m_t$ (in blue), and the seasonally adjusted series (in red).
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: false
apple_decompose |>
ggplot() +
geom_line(data = apple_decompose, aes(x = index, y = revenue), color = "black") +
geom_line(data = apple_decompose, aes(x = index, y = season_adjust), color = "#D55E00") +
geom_line(data = apple_decompose, aes(x = index, y = trend), color = "#0072B2") +
labs(
x = "Quarter",
y = "Quarterly Revenue, Billions",
title = "Apple Inc. Quarterly Revenue (in Billions of U.S. Dollars)"
) +
theme(plot.title = element_text(hjust = 0.5))
```
::: {.callout-tip icon=false title="Check Your Understanding"}
- Do you observe a trend in the time series?
- What does this trend suggest?
- In what quarter does Apple tend to have the greatest revenue?
- What would explain this phenomenon?
:::
## Homework Preview (5 min)
- Review upcoming homework assignment
- Clarify questions
## Homework
::: {.callout-note icon="false"}
## Download Assignment
<!-- ## need to update href link to correct files when we get them -->
<a href="https://byuistats.github.io/timeseries/homework/homework_1_5.qmd" download="homework_1_5.qmd"> homework_1_5.qmd </a>
<a href="https://github.com/TBrost/BYUI-Timeseries-Drafts/raw/master/handouts/chapter_1_5_handout.xlsx" download="chapter_1_5_handout.xlsx"> Tables-Handout-Excel </a>
:::