Skip to contents

Postprocessing step to add the target date

Usage

layer_add_target_date(
  frosting,
  target_date = NULL,
  id = rand_id("add_target_date")
)

Arguments

frosting

a frosting postprocessor

target_date

The target date to add as a column to the epi_df. If there's a forecast date specified in a layer, then it is the forecast date plus ahead (from step_epi_ahead in the epi_recipe). Otherwise, it is the maximum time_value (from the data used in pre-processing, fitting the model, and postprocessing) plus ahead, where ahead has been specified in preprocessing. The user may override these by specifying a target date of their own (of the form "yyyy-mm-dd").

id

a random id string

Value

an updated frosting postprocessor

Details

By default, this function assumes that a value for ahead has been specified in a preprocessing step (most likely in step_epi_ahead). Then, ahead is added to the maximum time_value in the test data to get the target date.

Examples

library(dplyr)
jhu <- case_death_rate_subset %>%
  filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny"))
r <- epi_recipe(jhu) %>%
  step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
  step_epi_ahead(death_rate, ahead = 7) %>%
  step_epi_naomit()

wf <- epi_workflow(r, linear_reg()) %>% fit(jhu)

# Use ahead + forecast date
f <- frosting() %>%
  layer_predict() %>%
  layer_add_forecast_date(forecast_date = as.Date("2022-05-31")) %>%
  layer_add_target_date() %>%
  layer_naomit(.pred)
wf1 <- wf %>% add_frosting(f)

p <- forecast(wf1)
p
#> An `epi_df` object, 0 x 5 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * as_of     = 2022-05-31 19:08:25.791826
#> 
#> # A tibble: 0 × 5
#> # ℹ 5 variables: geo_value <chr>, time_value <date>, .pred <dbl>,
#> #   forecast_date <date>, target_date <date>

# Use ahead + max time value from pre, fit, post
# which is the same if include `layer_add_forecast_date()`
f2 <- frosting() %>%
  layer_predict() %>%
  layer_add_target_date() %>%
  layer_naomit(.pred)
wf2 <- wf %>% add_frosting(f2)

p2 <- forecast(wf2)
p2
#> An `epi_df` object, 3 x 4 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * as_of     = 2022-05-31 19:08:25.791826
#> 
#> # A tibble: 3 × 4
#>   geo_value time_value .pred target_date
#> * <chr>     <date>     <dbl> <date>     
#> 1 ak        2021-12-31 0.245 2022-01-07 
#> 2 ca        2021-12-31 0.313 2022-01-07 
#> 3 ny        2021-12-31 0.295 2022-01-07 

# Specify own target date
f3 <- frosting() %>%
  layer_predict() %>%
  layer_add_target_date(target_date = "2022-01-08") %>%
  layer_naomit(.pred)
wf3 <- wf %>% add_frosting(f3)

p3 <- forecast(wf3)
p3
#> An `epi_df` object, 3 x 4 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * as_of     = 2022-05-31 19:08:25.791826
#> 
#> # A tibble: 3 × 4
#>   geo_value time_value .pred target_date
#> * <chr>     <date>     <dbl> <date>     
#> 1 ak        2021-12-31 0.245 2022-01-08 
#> 2 ca        2021-12-31 0.313 2022-01-08 
#> 3 ny        2021-12-31 0.295 2022-01-08