View source: R/layer_population_scaling.R
layer_population_scaling | R Documentation |
layer_population_scaling
creates a specification of a frosting layer
that will "undo" per-capita scaling. Typical usage would
load a dataset that contains state-level population, and use it to convert
predictions made from a rate-scale model to raw scale by multiplying by
the population.
Although, it is worth noting that there is nothing special about "population".
The function can be used to scale by any variable. Population is the
standard use case in the epidemiology forecasting scenario. Any value
passed will multiply the selected variables while the rate_rescaling
argument is a common divisor of the selected variables.
layer_population_scaling(
frosting,
...,
df,
by = NULL,
df_pop_col,
rate_rescaling = 1,
create_new = TRUE,
suffix = "_scaled",
id = rand_id("population_scaling")
)
frosting |
a |
... |
One or more selector functions to scale variables
for this step. See |
df |
a data frame that contains the population data to be used for inverting the existing scaling. |
by |
A (possibly named) character vector of variables to join by. If If columns in the training/testing data and To join by different variables on the See |
df_pop_col |
the name of the column in the data frame |
rate_rescaling |
Sometimes rates are "per 100K" or "per 1M" rather than
"per person". Adjustments can be made here. For example, if the original
rate is "per 100K", then set |
create_new |
TRUE to create a new column and keep the original column
in the |
suffix |
a character. The suffix added to the column name if
|
id |
a random id string |
an updated frosting
postprocessor
jhu <- cases_deaths_subset %>%
filter(time_value > "2021-11-01", geo_value %in% c("ca", "ny")) %>%
select(geo_value, time_value, cases)
pop_data <- data.frame(states = c("ca", "ny"), value = c(20000, 30000))
r <- epi_recipe(jhu) %>%
step_population_scaling(
df = pop_data,
df_pop_col = "value",
by = c("geo_value" = "states"),
cases, suffix = "_scaled"
) %>%
step_epi_lag(cases_scaled, lag = c(0, 7, 14)) %>%
step_epi_ahead(cases_scaled, ahead = 7, role = "outcome") %>%
step_epi_naomit()
f <- frosting() %>%
layer_predict() %>%
layer_threshold(.pred) %>%
layer_naomit(.pred) %>%
layer_population_scaling(.pred,
df = pop_data,
by = c("geo_value" = "states"),
df_pop_col = "value"
)
wf <- epi_workflow(r, linear_reg()) %>%
fit(jhu) %>%
add_frosting(f)
forecast(wf)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.