-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchapter_1_lesson_2.qmd
310 lines (231 loc) · 10.1 KB
/
chapter_1_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
---
title: "Plots Trends, and Seasonal Variation"
subtitle: "Chapter 1: Lesson 2"
format: html
editor: source
sidebar: false
#filters:
# - shinylive
---
```{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_1_lesson_2_outcomes.qmd >}}
## Preparation
- Read Sections 1.1-1.4
## Learning Journal Exchange (15 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
## Vocabulary and Nomenclature Matching Activity (5 min)
::: {.callout-tip icon="false" title="Check Your Understanding"}
Working with a partner, match the definitions on the left with the terms on the right.
<!-- Code Source: https://bookdown.org/yihui/rmarkdown-cookbook/multi-column.html -->
#### Vocabulary Matching
::: {style="display: flex;"}
<div>
1. A figure with time on the horizontal axis and the value of a random variable on the vertical axis
2. A systematic change in a time series that does not appear to be periodic
3. Repeated pattern within each year (or any other fixed time period)
4. Repeated pattern that does not correspond to some fixed natural period
5. Observations in which values are related to lagged observations of the same variable
6. Random trend that does not follow a discernible or predictable pattern
7. Can be modeled with mathematical functions, facilitating the long-term prediction of the behavior
</div>
<div>
A. Cycle
B. Correlated (Serially Dependent) Data
C. Deterministic Trend
D. Seasonal Variation
E. Stochastic Trend
F. Time Plot
G. Trend
</div>
:::
:::
## Comparison of Deterministic and Stochastic Time Series (10 min)
### Stochastic Time Series
The following app illustrates a few realizations of a stochastic time series.
```{=html}
<iframe id="stochastic" src="https://posit.byui.edu/content/728a7ddc-97d2-44ee-9977-fc648102b986" style="border: none; width: 100%; height: 500px" frameborder="0"></iframe>
```
- If a stochastic time series displays an upward trend, can we conclude that trend will continue in the same direction? Why or why not?
### Deterministic Time Series
The figure below illustrates realizations of a deterministic time series. The data fluctuate around a sine curve.
```{=html}
<iframe id="deterministic" src="https://posit.byui.edu/content/4d25268b-d562-455c-ac47-5a9871770e7c" style="border: none; width: 100%; height: 550px" frameborder="0"></iframe>
```
<!-- <a href="javascript:showhide('Meme')" -->
<!-- style="font-size:.8em;">Meme</a> -->
<!-- ::: {#Meme style="display:none;"} -->
<!-- ![](images/MobiusMeme.png) -->
<!-- ::: -->
## Class Activity: Importing Data and Creating a tsibble Object (5 min)
Recall the Google Trends data for the term "chocolate" from the last lesson. The cleaned data are available in the file <a href="data/chocolate.csv" download>chocolate.csv</a>. Here are the first few rows of the csv:
![https://byuistats.github.io/timeseries/data/chocolate.csv](images/chocolate_csv_image.png)
### Import the Data
Use the code below to import the chocolate data and convert it into a time series (tsibble) object. You can click on the clipboard icon in the upper right-hand corner of the box below to copy the code.
```{r}
# load packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load("tsibble", "fable",
"feasts", "tsibbledata",
"fable.prophet", "tidyverse",
"patchwork", "rio")
# read in the data from a csv
chocolate_month <- rio::import("https://byuistats.github.io/timeseries/data/chocolate.csv")
# define the first date in the time series
start_date <- lubridate::ymd("2004-01-01")
# create a sequence of dates, one month apart, starting with start_date
date_seq <- seq(start_date,
start_date + months(nrow(chocolate_month)-1),
by = "1 months")
# create a tibble including variables dates, year, month, value
chocolate_tibble <- tibble(
dates = date_seq,
year = lubridate::year(date_seq), # gets the year part of the date
month = lubridate::month(date_seq), # gets the month
value = pull(chocolate_month, chocolate) # gets the value of the ts
)
# create a tsibble where the index variable is the year/month
chocolate_month_ts <- chocolate_tibble |>
mutate(index = tsibble::yearmonth(dates)) |>
as_tsibble(index = index)
# generate the ts plot
choc_plot <- autoplot(chocolate_month_ts, .vars = value) +
labs(
x = "Month",
y = "Searches",
title = "Relative Number of Google Searches for 'Chocolate'"
) +
theme(plot.title = element_text(hjust = 0.5))
choc_plot
```
### Explore R commands summarizing time series data
::: {.callout-tip icon="false" title="Check Your Understanding"}
- What does each of the following R commands give us? <!-- - class(chocolate_month_ts) -->
- head(chocolate_month_ts, 1)
- tail(chocolate_month_ts, 1)
- guess_frequency(chocolate_month_ts$index)
:::
## Estimating the Trend: Annual Aggregation (10 min)
To help visualize what is happening with a time series, we can simply aggregate the data in the time series to the annual level by computing the mean of the observations in a given year. This can make it easier to spot a trend.
For the chocolate data, when we average the data for each year, we get:
```{r}
#| code-fold: true
#| code-summary: "Aggregation"
chocolate_annual_ts <- summarise(
index_by(chocolate_month_ts, year),
value = mean(value)
)
#chocolate_annual_ts
```
```{r}
#| code-fold: true
#| code-summary: "Table"
chocolate_annual_ts |>
as.data.frame() |>
concat_partial_table(nrow_head = 6, nrow_tail= 3, decimals = 4) |>
display_table()
```
The first plot is the time series plot of the raw data, and the second plot is a time series plot of the annual means.
```{r}
#| code-fold: true
#| code-summary: "Show the code"
# monthly plot
mp <- autoplot(chocolate_month_ts, .vars = value) +
labs(
x = "Month",
y = "Searches",
title = "Relative Number of Google Searches for 'Chocolate'"
) +
theme(plot.title = element_text(hjust = 0.5))
# yearly plot
yp <- autoplot(chocolate_annual_ts, .vars = value) +
labs(
x = "Year",
y = "Searches",
title = "Mean Annual Google Searches for 'Chocolate'"
) +
scale_x_continuous(breaks = seq(2004, max(chocolate_month_ts$year), by = 2)) +
theme(plot.title = element_text(hjust = 0.5))
mp / yp
```
If you want to superimpose these plots, it would make sense to align the mean value for the year with the middle of the year. Here is a plot superimposing the annual mean aligned with July 1 (in blue) on the values of the time series (in black).
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| warning: true
chocolate_annual_ts <- summarise(
index_by(chocolate_month_ts, year),
value = mean(value)
) |>
mutate(index = tsibble::yearmonth( mdy(paste0("7/1/",year)) )) |>
as_tsibble(index = index)
# combined plot
autoplot(chocolate_month_ts, .vars = value) +
geom_line(data = chocolate_annual_ts,
aes(x = index, y = value),
color = "#56B4E9") +
labs(
x = "Month",
y = "Searches",
title = "Relative Number of Google Searches for 'Chocolate'"
) +
theme(plot.title = element_text(hjust = 0.5))
```
::: {.callout-tip icon="false" title="Check Your Understanding"}
- What do the annually-aggregated data tell us about the trend?
- What do you observe in the trend?
- What do you suspect is causing this trend?
:::
## 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_2.qmd" download="homework_1_2.qmd"> homework_1_2.qmd </a>
:::
<a href="javascript:showhide('Solutions')"
style="font-size:.8em;">Matching</a>
::: {#Solutions style="display:none;"}
::: {.callout-tip icon="false" title="Check Your Understanding"}
Matching Solutions
#### Vocabulary Matching
| | |
|-------------------------------------------------------------------------------------------------------|-----------------------------------------|
| 1\. A figure with time on the horizontal axis and the value of a random variable on the vertical axis | F. Time Plot |
| 2\. A systematic change in a time series that does not appear to be periodic | G. Trend |
| 3\. Repeated pattern within each year (or any other fixed time period) | D. Seasonal Variation |
| 4\. Repeated pattern that does not correspond to some fixed natural period | A. Cycle |
| 5\. Observations in which values are related to lagged observations of the same variable | B. Correlated (Serially Dependent) Data |
| 6\. Random trend that does not follow a discernible or predictable pattern | E. Stochastic Trend |
| 7\. Can be modeled with mathematical functions, facilitating the long-term prediction of the behavior | C. Deterministic Trend |
:::
:::