|
| 1 | +--- |
| 2 | +title: "Untitled" |
| 3 | +author: "DJM" |
| 4 | +date: '2022-06-06' |
| 5 | +output: html_document |
| 6 | +--- |
| 7 | + |
| 8 | +```{r setup, include=FALSE} |
| 9 | +knitr::opts_chunk$set(echo = TRUE) |
| 10 | +library(tidyverse) |
| 11 | +library(tidymodels) |
| 12 | +library(epiprocess) |
| 13 | +library(epipredict) |
| 14 | +
|
| 15 | +``` |
| 16 | + |
| 17 | +```{r small-data} |
| 18 | +jhu <- jhu_csse_daily_subset %>% |
| 19 | + filter(time_value > "2021-08-01") %>% |
| 20 | + select(geo_value:death_rate_7d_av) %>% |
| 21 | + rename(case_rate = case_rate_7d_av, death_rate = death_rate_7d_av) |
| 22 | +
|
| 23 | +jhu_latest <- jhu %>% |
| 24 | + filter(!is.na(case_rate), !is.na(death_rate)) %>% |
| 25 | + group_by(geo_value) %>% |
| 26 | + slice_tail(n = 15) %>% # have lags 0,...,14, so need 15 for a complete case |
| 27 | + ungroup() |
| 28 | +``` |
| 29 | + |
| 30 | +The recipe encodes how to process training/testing data. S3 object. |
| 31 | + |
| 32 | +```{r recipe} |
| 33 | +r <- epi_recipe(jhu) %>% |
| 34 | + step_epi_lag(death_rate, lag = c(0, 7, 14)) %>% |
| 35 | + step_epi_ahead(death_rate, ahead = 7) %>% |
| 36 | + step_epi_lag(case_rate, lag = c(0, 7, 14)) %>% |
| 37 | + step_naomit(all_predictors()) %>% |
| 38 | + step_naomit(all_outcomes(), skip = TRUE) |
| 39 | +``` |
| 40 | + |
| 41 | +The workflow combines a recipe and a model specification. Fit, estimates |
| 42 | +the model, adds the resulting object to the workflow. |
| 43 | + |
| 44 | +```{r workflow} |
| 45 | +wf <- epi_workflow(r, linear_reg()) %>% |
| 46 | + fit(jhu) |
| 47 | +
|
| 48 | +wf |
| 49 | +``` |
| 50 | + |
| 51 | +The workflow also has slots for post-processing. (Currently unimplemented.) |
| 52 | + |
| 53 | +```{r workflow2} |
| 54 | +names(wf) # 3 lists and a flag |
| 55 | +``` |
| 56 | + |
| 57 | +Predict gives a new `epi_df` |
| 58 | + |
| 59 | +```{r predict} |
| 60 | +pp <- predict(wf, new_data = jhu_latest) |
| 61 | +pp |
| 62 | +``` |
| 63 | + |
| 64 | +Can add a `forecast_date` (should be a post processing step) |
| 65 | + |
| 66 | +```{r predict2} |
| 67 | +predict(wf, new_data = jhu_latest, forecast_date = "2021-12-31") %>% |
| 68 | + filter(!is.na(.pred)) |
| 69 | +``` |
| 70 | + |
| 71 | + |
0 commit comments