@@ -196,7 +196,8 @@ In the following sections, we will go over preprocessing the data in the
196
196
` epi_recipe ` framework, and fitting a model and making predictions within the
197
197
` epipredict ` framework and using the package's canned forecasters.
198
198
199
- # A simple example
199
+ # A Simple AR(3) Model Example
200
+
200
201
## Preprocessing
201
202
202
203
As a simple example, let's work with the ` num_graduates ` column for now. We will
@@ -212,7 +213,8 @@ employ_small <- employ %>%
212
213
mutate(
213
214
num_graduates_prop = num_graduates / sum(num_graduates),
214
215
med_income_2y_prop = med_income_2y / sum(med_income_2y),
215
- med_income_5y_prop = med_income_5y / sum(med_income_5y)) %>%
216
+ med_income_5y_prop = med_income_5y / sum(med_income_5y)
217
+ ) %>%
216
218
ungroup()
217
219
head(employ_small)
218
220
```
@@ -269,7 +271,9 @@ and `ahead` columns.
269
271
270
272
``` {r view-preprocessed, include=T}
271
273
# Display a sample of the preprocessed data
272
- baked_sample <- r %>% prep() %>% bake(new_data = employ_small) %>%
274
+ baked_sample <- r %>%
275
+ prep() %>%
276
+ bake(new_data = employ_small) %>%
273
277
sample_n(5)
274
278
baked_sample
275
279
```
@@ -325,25 +329,27 @@ employ_small_with_preds <- augment(wf_linreg, latest)
325
329
employ_small_with_preds %>% head()
326
330
```
327
331
328
- ## AR(3) Model Diagnostics
332
+ ## Model Diagnostics
329
333
330
334
First, we'll plot the residuals (that is, $y_ {t} - \hat{y}_ {t}$) against the
331
335
fitted values ($\hat{y}_ {t}$).
332
336
333
337
``` {r lienarreg-resid-plot, include=T, warning=F}
334
- employ_small_with_preds <- employ_small_with_preds %>%
335
- mutate(resid = num_graduates_prop - .pred)
336
-
338
+ employ_small_with_preds <- employ_small_with_preds %>%
339
+ mutate(resid = num_graduates_prop - .pred)
340
+
337
341
p1 <- employ_small_with_preds %>%
338
- ggplot(aes(x = .pred, y = resid)) +
342
+ ggplot(aes(x = .pred, y = resid)) +
339
343
geom_point(size = 1.5, alpha = .8) +
340
- geom_smooth(method = "loess", color = "red", linetype = "dashed", size = .7) +
344
+ geom_smooth(method = "loess", color = "red", linetype = "dashed", size = .7) +
341
345
xlab("Fitted values") +
342
346
ylab("Residuals") +
343
347
ggtitle("Plot of Fitted Values vs. Residuals in AR(3) Model")
344
348
345
- p2 <- employ_small_with_preds %>%
346
- ggplot(aes(sample = resid)) + stat_qq(alpha = .6) + stat_qq_line() +
349
+ p2 <- employ_small_with_preds %>%
350
+ ggplot(aes(sample = resid)) +
351
+ stat_qq(alpha = .6) +
352
+ stat_qq_line() +
347
353
ggtitle("Q-Q Plot of Residuals")
348
354
349
355
grid.arrange(p1, p2, ncol = 2)
@@ -378,7 +384,9 @@ where $y_i$ is the 2-year median income at time $i$.
378
384
``` {r flatline, include=T, warning=F}
379
385
out_fl <- flatline_forecaster(employ_small, "med_income_2y",
380
386
args_list = flatline_args_list(
381
- ahead = 1L, forecast_date = as.Date("2015-01-01")))
387
+ ahead = 1L, forecast_date = as.Date("2015-01-01")
388
+ )
389
+ )
382
390
383
391
augment(out_fl$epi_workflow, employ_small) %>% head()
384
392
```
@@ -394,11 +402,13 @@ where $y_i$ is as before, and $z_i$ is the 5-year median income at time $i$.
394
402
395
403
``` {r arx-lr, include=T, warning=F}
396
404
arx_args <- arx_args_list(
397
- lags = c(0L, 1L), ahead = 1L, forecast_date = as.Date("2015-01-01"))
405
+ lags = c(0L, 1L), ahead = 1L, forecast_date = as.Date("2015-01-01")
406
+ )
398
407
399
408
out_arx_lr <- arx_forecaster(employ_small, "med_income_2y",
400
409
c("med_income_2y", "med_income_5y"),
401
- args_list = arx_args)
410
+ args_list = arx_args
411
+ )
402
412
403
413
out_arx_lr$predictions %>% head()
404
414
```
@@ -410,7 +420,8 @@ as expected. Below we use a boosted tree model instead of a linear regression.
410
420
out_arx_rf <- arx_forecaster(
411
421
employ_small, "med_income_2y", c("med_income_2y", "med_income_5y"),
412
422
trainer = parsnip::boost_tree(mode = "regression", trees = 20),
413
- args_list = arx_args)
423
+ args_list = arx_args
424
+ )
414
425
415
426
out_arx_rf$predictions %>% head()
416
427
```
0 commit comments