-
Notifications
You must be signed in to change notification settings - Fork 3
/
_sample_code.qmd
342 lines (225 loc) · 5.3 KB
/
_sample_code.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
---
title: "Sample Code"
format: html
editor: source
sidebar: false
---
```{r}
#| include: false
source("common_functions.R")
```
<!-- Check Your Understanding -->
::: {.callout-tip icon=false title="Check Your Understanding"}
TheInitialPromptGoesHere
- Question1
- Question2
:::
<!-- Folded code: -->
```{r}
#| code-fold: true
#| code-summary: "Show the code"
code_goes_here <- 0
```
<!-- Tables -->
```{r}
#| label: tbl-LabelGoesHere
#| tbl-cap: "TitleGoesHere"
# make figure
```
<!-- Figures -->
```{r}
#| label: fig-LabelGoesHere
#| fig-cap: "TitleGoesHere"
# make figure
```
::: panel-tabset
#### first_tab
Stuff
#### second_tab
Stuff
#### third_tab
Stuff
:::
::: {.callout-note}
Text goes here.
:::
::: {.callout-warning}
Text goes here.
:::
::: {.callout-important}
Text goes here.
:::
::: {.callout-tip}
Text goes here. Color used for Check your understanding.
:::
::: {.callout-caution}
Text goes here.
:::
::: {.callout-note icon=false title="TitleGoesHere"}
This is a callout for a note with a title
:::
::: {.callout-tip appearance="minimal"}
This creates a callout box with no title
:::
::: {.callout-note}
Here is a note.
:::
<!-- Solutions at the bottom of the page -->
<a href="javascript:showhide('Solutions')"
style="font-size:.8em;">Class Activity</a>
::: {#Solutions style="display:none;"}
Solutions to Class Activity
```{r}
#| echo: false
stuff <- "here"
```
:::
# Fill in missing data
tidyr::fill()
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
Stuff in first column
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
Stuff in second column
:::
:::
<!-- End of two columns -->
# Three column format
::: columns
::: {.column width="30%"}
Stuff in first column
:::
::: {.column width="5%"}
<!-- empty column to create gap -->
:::
::: {.column width="30%"}
Stuff in second column
:::
::: {.column width="5%"}
<!-- empty column to create gap -->
:::
::: {.column width="30%"}
Stuff in third column
:::
:::
# Generating R code for user
```{r}
#| echo: false
x <- c(1:5)
y <- x^2
df <- data.frame(x = x, y = y)
cat("x <- c(", paste(df$x, collapse = ", "),")\n")
cat("y <- c(", paste(df$y, collapse = ", "),")")
```
```{r}
#| echo: false
df <- data.frame(x = c(1:16)) |> mutate(y = x^2)
# build a string that gives the data
xy_string <- rep("",6)
xy_string[1] <- "sample_df <- data.frame("
xy_string[2] <- paste0(" \t x = c(", paste(df$x |> head(8), collapse = ", "), ", ")
xy_string[3] <- paste0(" \t \t ", paste(df$x |> tail(8), collapse = ", "), ")," )
xy_string[4] <- paste0(" \t y = c(", paste(df$y |> head(8), collapse = ", "), ", ")
xy_string[5] <- paste0(" \t \t ", paste(df$y |> tail(8), collapse = ", "), ")" )
xy_string[6] <- ")"
xystr <- paste(xy_string, " \n ", collapse="")
#| echo: false
cat(xystr)
```
## Plots
```{r}
#| eval: false
ts |>
autoplot(.vars = random) +
geom_line(data = ts, aes(x = index, y = a_t), linewidth = 1) +
labs(
x = "Time",
y = "Random",
title = paste0("Random Component and EWMA (α = ", alpha, ")")
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
```
```{r}
#| eval: false
ts |>
autoplot(.vars = random) +
geom_line(data = ts, aes(x = index, y = a_t), color = "#D55E00", linewidth = 1) +
labs(
x = "Time",
y = "Random",
title = paste0("Random Component and EWMA (α = ", alpha, ")"),
subtitle = "(All Dates)"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
<!-- Formatting dates -->
```{r}
# Formatting dates
usd1_ts <- rio::import("data/exchange_rates.parquet") |>
filter(currency == "USD.CAD") |>
mutate(diff = rate - lag(rate)) |>
as_tsibble(index = date) |>
na.omit()
usd1_ts %>%
autoplot(.vars = rate) +
labs(
title = "Converstion Rate for 1 USD to Canadian Dollars",
subtitle =
paste0(
format(min(usd1_ts$date), "%d %b %Y"),
" - ",
format(max(usd1_ts$date), "%d %b %Y")
),
x = "Date",
y = "Conversion Rate",
) +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
```{r}
#| echo: false
#| results: asis
# LaTeX math mode from a string
set.seed(6)
n <- 8
d_operator <- data.frame(t = c(1:n), x = sample(1:15, n, replace = FALSE)) |>
mutate(diff = t - n)
cat( paste( paste0("$x_{t", ifelse(d_operator$t==n,"",d_operator$t-n), "} = ", d_operator$x, "$"), collapse = ",$~$ " ) )
cat( paste("$ x_t =", n, "$") )
# DO I need the cat()?
```
```{r}
# Create a time series based on the current year
# See 3-3 and 4-5
n_months <- 36
start_date <- my(paste(1, floor(year(now())-n_months/12)))
date_seq <- seq(start_date,
start_date + months(n_months - 1),
by = "1 months")
```
### Operators
Backward Shift Operator: $\mathbf{B}$
Difference operator: $\nabla$
### Ellipses
⋮
### Hyperlinks to pages
<!-- Bookmark -->
<a id="IDvalue">ThingToLinkTo</a>
<!-- Reference -->
<a href="https://byuistats.github.io/timeseries/chapter_4_lesson_2.html#DifferenceOperator">Chapter 4 Lesson 2</a>
<!-- Linear Interpolation and Imputation of Missing Values using the zoo package -->
<!-- See Lesson 5-3 -->