From e4c43f84c86d339c49d134abab95d030b6959b8a Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:49:55 -0800 Subject: [PATCH 01/10] move epiprocess to imports --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1994f2cc..8c47a1d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,6 @@ URL: https://github.com/cmu-delphi/epipredict/, BugReports: https://github.com/cmu-delphi/epipredict/issues/ Depends: epidatasets, - epiprocess (>= 0.10.4), parsnip (>= 1.0.0), R (>= 3.5.0) Imports: @@ -33,6 +32,7 @@ Imports: cli, distributional, dplyr, + epiprocess (>= 0.10.4), generics, ggplot2, glue, From b90b9be6ceed77485a9ed06ecd49ab0e9eafdc75 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:51:39 -0800 Subject: [PATCH 02/10] adjust reexports to include epiprocess necessaries, also frequently used dplyr, tibble, tidyr --- R/reexports-tidymodels.R | 27 ------------------ R/reexports.R | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 27 deletions(-) delete mode 100644 R/reexports-tidymodels.R create mode 100644 R/reexports.R diff --git a/R/reexports-tidymodels.R b/R/reexports-tidymodels.R deleted file mode 100644 index 3b28ac5c..00000000 --- a/R/reexports-tidymodels.R +++ /dev/null @@ -1,27 +0,0 @@ -#' @importFrom generics fit -#' @export -generics::fit - -#' @importFrom generics forecast -#' @export -generics::forecast - -#' @importFrom recipes prep -#' @export -recipes::prep - -#' @importFrom recipes bake -#' @export -recipes::bake - -#' @importFrom recipes rand_id -#' @export -recipes::rand_id - -#' @importFrom tibble tibble -#' @export -tibble::tibble - -#' @importFrom generics tidy -#' @export -generics::tidy diff --git a/R/reexports.R b/R/reexports.R new file mode 100644 index 00000000..02a377f7 --- /dev/null +++ b/R/reexports.R @@ -0,0 +1,59 @@ +#' @importFrom dplyr filter +#' @export +dplyr::filter + +#' @importFrom dplyr mutate +#' @export +dplyr::mutate + +#' @importFrom dplyr rename +#' @export +dplyr::rename + +#' @importFrom dplyr select +#' @export +dplyr::select + +#' @importFrom epiprocess as_epi_df +#' @export +epiprocess::as_epi_df + +#' @importFrom epiprocess key_colnames +#' @export +epiprocess::key_colnames + +#' @importFrom generics fit +#' @export +generics::fit + +#' @importFrom generics forecast +#' @export +generics::forecast + +#' @importFrom generics tidy +#' @export +generics::tidy + +#' @importFrom recipes prep +#' @export +recipes::prep + +#' @importFrom recipes bake +#' @export +recipes::bake + +#' @importFrom recipes rand_id +#' @export +recipes::rand_id + +#' @importFrom tibble as_tibble +#' @export +tibble::as_tibble + +#' @importFrom tibble tibble +#' @export +tibble::tibble + +#' @importFrom tidyr unnest +#' @export +tidyr::unnest From 6efe75949daa847f011dc4624505892aaf6571e9 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:54:01 -0800 Subject: [PATCH 03/10] remove unnecessary library calls from examples --- R/arx_classifier.R | 2 -- R/arx_forecaster.R | 1 - R/autoplot.R | 1 - R/cdc_baseline_forecaster.R | 38 ++++++++++++++--------------- R/epi_recipe.R | 19 ++++----------- R/frosting.R | 3 --- R/layer_add_forecast_date.R | 1 - R/layer_add_target_date.R | 1 - R/layer_cdc_flatline_quantiles.R | 41 ++++++++++++++++---------------- R/layer_naomit.R | 1 - R/layer_point_from_distn.R | 1 - R/layer_population_scaling.R | 3 +-- R/layer_predict.R | 1 - R/layer_quantile_distn.R | 1 - R/layer_residual_quantiles.R | 1 - R/layer_threshold_preds.R | 2 +- R/layers.R | 1 - R/make_grf_quantiles.R | 1 - R/make_smooth_quantile_reg.R | 3 +-- R/model-methods.R | 1 - R/pivot_quantiles.R | 2 -- R/step_epi_slide.R | 1 - R/step_growth_rate.R | 1 - R/step_population_scaling.R | 3 +-- R/tidy.R | 3 +-- 25 files changed, 48 insertions(+), 85 deletions(-) diff --git a/R/arx_classifier.R b/R/arx_classifier.R index 1c4f35ef..d1aa292d 100644 --- a/R/arx_classifier.R +++ b/R/arx_classifier.R @@ -26,7 +26,6 @@ #' @seealso [arx_class_epi_workflow()], [arx_class_args_list()] #' #' @examples -#' library(dplyr) #' tiny_geos <- c("as", "mp", "vi", "gu", "pr") #' jhu <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-11-01"), !(geo_value %in% tiny_geos)) @@ -107,7 +106,6 @@ arx_classifier <- function( #' @export #' @seealso [arx_classifier()] [arx_class_args_list()] #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-11-01")) #' diff --git a/R/arx_forecaster.R b/R/arx_forecaster.R index 5397ca88..21b1e63e 100644 --- a/R/arx_forecaster.R +++ b/R/arx_forecaster.R @@ -95,7 +95,6 @@ arx_forecaster <- function( #' @seealso [arx_forecaster()] #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-12-01")) #' diff --git a/R/autoplot.R b/R/autoplot.R index c0e3c68d..8e902f53 100644 --- a/R/autoplot.R +++ b/R/autoplot.R @@ -28,7 +28,6 @@ ggplot2::autoplot #' #' @name autoplot-epipred #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-11-01")) #' diff --git a/R/cdc_baseline_forecaster.R b/R/cdc_baseline_forecaster.R index 44090bf7..a97eece8 100644 --- a/R/cdc_baseline_forecaster.R +++ b/R/cdc_baseline_forecaster.R @@ -23,6 +23,7 @@ #' #' @examples #' library(dplyr) +#' library(epiprocess) #' weekly_deaths <- covid_case_death_rates %>% #' select(geo_value, time_value, death_rate) %>% #' left_join(state_census %>% select(pop, abbr), by = c("geo_value" = "abbr")) %>% @@ -36,25 +37,24 @@ #' cdc <- cdc_baseline_forecaster(weekly_deaths, "deaths_7dsum") #' preds <- pivot_quantiles_wider(cdc$predictions, .pred_distn) #' -#' if (require(ggplot2)) { -#' forecast_date <- unique(preds$forecast_date) -#' four_states <- c("ca", "pa", "wa", "ny") -#' preds %>% -#' filter(geo_value %in% four_states) %>% -#' ggplot(aes(target_date)) + -#' geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + -#' geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + -#' geom_line(aes(y = .pred), color = "orange") + -#' geom_line( -#' data = weekly_deaths %>% filter(geo_value %in% four_states), -#' aes(x = time_value, y = deaths_7dsum) -#' ) + -#' scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + -#' labs(x = "Date", y = "Weekly deaths") + -#' facet_wrap(~geo_value, scales = "free_y") + -#' theme_bw() + -#' geom_vline(xintercept = forecast_date) -#' } +#' library(ggplot2) +#' forecast_date <- unique(preds$forecast_date) +#' four_states <- c("ca", "pa", "wa", "ny") +#' preds %>% +#' filter(geo_value %in% four_states) %>% +#' ggplot(aes(target_date)) + +#' geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + +#' geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + +#' geom_line(aes(y = .pred), color = "orange") + +#' geom_line( +#' data = weekly_deaths %>% filter(geo_value %in% four_states), +#' aes(x = time_value, y = deaths_7dsum) +#' ) + +#' scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + +#' labs(x = "Date", y = "Weekly deaths") + +#' facet_wrap(~geo_value, scales = "free_y") + +#' theme_bw() + +#' geom_vline(xintercept = forecast_date) cdc_baseline_forecaster <- function( epi_data, outcome, diff --git a/R/epi_recipe.R b/R/epi_recipe.R index d1d183cc..367cf78f 100644 --- a/R/epi_recipe.R +++ b/R/epi_recipe.R @@ -45,19 +45,14 @@ epi_recipe <- function(x, ...) { #' #' @export #' @examples -#' library(dplyr) -#' library(recipes) #' jhu <- covid_case_death_rates %>% -#' filter(time_value > "2021-08-01") %>% -#' arrange(geo_value, time_value) +#' filter(time_value > "2021-08-01") #' #' r <- epi_recipe(jhu) %>% #' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>% #' step_epi_ahead(death_rate, ahead = 7) %>% #' step_epi_lag(case_rate, lag = c(0, 7, 14)) %>% -#' step_naomit(all_predictors()) %>% -#' # below, `skip` means we don't do this at predict time -#' step_naomit(all_outcomes(), skip = TRUE) +#' step_epi_naomit(all_predictors()) #' #' r epi_recipe.epi_df <- function(x, @@ -271,19 +266,14 @@ is_epi_recipe <- function(x) { #' #' @export #' @examples -#' library(dplyr) -#' library(recipes) -#' #' jhu <- covid_case_death_rates %>% -#' filter(time_value > "2021-08-01") %>% -#' arrange(geo_value, time_value) +#' filter(time_value > "2021-08-01") #' #' r <- epi_recipe(jhu) %>% #' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>% #' step_epi_ahead(death_rate, ahead = 7) %>% #' step_epi_lag(case_rate, lag = c(0, 7, 14)) %>% -#' step_naomit(all_predictors()) %>% -#' step_naomit(all_outcomes(), skip = TRUE) +#' step_epi_naomit() #' #' workflow <- epi_workflow() %>% #' add_epi_recipe(r) @@ -355,7 +345,6 @@ update_epi_recipe <- function(x, recipe, ..., blueprint = default_epi_recipe_blu #' #' @export #' @examples -#' library(dplyr) #' library(workflows) #' #' jhu <- covid_case_death_rates %>% diff --git a/R/frosting.R b/R/frosting.R index ef32b4a3..cb0fa916 100644 --- a/R/frosting.R +++ b/R/frosting.R @@ -8,7 +8,6 @@ #' @export #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' r <- epi_recipe(jhu) %>% @@ -127,7 +126,6 @@ update_frosting <- function(x, frosting, ...) { #' #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' r <- epi_recipe(jhu) %>% @@ -262,7 +260,6 @@ new_frosting <- function() { #' @export #' #' @examples -#' library(dplyr) #' # Toy example to show that frosting can be created and added for postprocessing #' f <- frosting() #' wf <- epi_workflow() %>% add_frosting(f) diff --git a/R/layer_add_forecast_date.R b/R/layer_add_forecast_date.R index 78cbb79a..3e62bafb 100644 --- a/R/layer_add_forecast_date.R +++ b/R/layer_add_forecast_date.R @@ -21,7 +21,6 @@ #' #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' r <- epi_recipe(jhu) %>% diff --git a/R/layer_add_target_date.R b/R/layer_add_target_date.R index f8b6a06e..bd97862c 100644 --- a/R/layer_add_target_date.R +++ b/R/layer_add_target_date.R @@ -24,7 +24,6 @@ #' #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' r <- epi_recipe(jhu) %>% diff --git a/R/layer_cdc_flatline_quantiles.R b/R/layer_cdc_flatline_quantiles.R index 13938d83..5211170a 100644 --- a/R/layer_cdc_flatline_quantiles.R +++ b/R/layer_cdc_flatline_quantiles.R @@ -55,13 +55,13 @@ #' @export #' #' @examples -#' library(dplyr) +#' library(recipes) #' r <- epi_recipe(covid_case_death_rates) %>% #' # data is "daily", so we fit this to 1 ahead, the result will contain #' # 1 day ahead residuals #' step_epi_ahead(death_rate, ahead = 1L, skip = TRUE) %>% -#' recipes::update_role(death_rate, new_role = "predictor") %>% -#' recipes::add_role(time_value, geo_value, new_role = "predictor") +#' update_role(death_rate, new_role = "predictor") %>% +#' add_role(time_value, geo_value, new_role = "predictor") #' #' forecast_date <- max(covid_case_death_rates$time_value) #' @@ -82,24 +82,23 @@ #' pivot_quantiles_wider(.pred_distn) %>% #' mutate(target_date = forecast_date + ahead) #' -#' if (require("ggplot2")) { -#' four_states <- c("ca", "pa", "wa", "ny") -#' preds %>% -#' filter(geo_value %in% four_states) %>% -#' ggplot(aes(target_date)) + -#' geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + -#' geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + -#' geom_line(aes(y = .pred), color = "orange") + -#' geom_line( -#' data = covid_case_death_rates %>% filter(geo_value %in% four_states), -#' aes(x = time_value, y = death_rate) -#' ) + -#' scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + -#' labs(x = "Date", y = "Death rate") + -#' facet_wrap(~geo_value, scales = "free_y") + -#' theme_bw() + -#' geom_vline(xintercept = forecast_date) -#' } +#' library(ggplot2) +#' four_states <- c("ca", "pa", "wa", "ny") +#' preds %>% +#' filter(geo_value %in% four_states) %>% +#' ggplot(aes(target_date)) + +#' geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + +#' geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + +#' geom_line(aes(y = .pred), color = "orange") + +#' geom_line( +#' data = covid_case_death_rates %>% filter(geo_value %in% four_states), +#' aes(x = time_value, y = death_rate) +#' ) + +#' scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + +#' labs(x = "Date", y = "Death rate") + +#' facet_wrap(~geo_value, scales = "free_y") + +#' theme_bw() + +#' geom_vline(xintercept = forecast_date) layer_cdc_flatline_quantiles <- function( frosting, ..., diff --git a/R/layer_naomit.R b/R/layer_naomit.R index 2b111a6f..5071bcc5 100644 --- a/R/layer_naomit.R +++ b/R/layer_naomit.R @@ -11,7 +11,6 @@ #' @return an updated `frosting` postprocessor #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/layer_point_from_distn.R b/R/layer_point_from_distn.R index c433717b..5af43489 100644 --- a/R/layer_point_from_distn.R +++ b/R/layer_point_from_distn.R @@ -16,7 +16,6 @@ #' @export #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/layer_population_scaling.R b/R/layer_population_scaling.R index badd073f..a3183a0a 100644 --- a/R/layer_population_scaling.R +++ b/R/layer_population_scaling.R @@ -52,8 +52,7 @@ #' @return an updated `frosting` postprocessor #' @export #' @examples -#' library(dplyr) -#' jhu <- epidatasets::cases_deaths_subset %>% +#' jhu <- cases_deaths_subset %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ca", "ny")) %>% #' select(geo_value, time_value, cases) #' diff --git a/R/layer_predict.R b/R/layer_predict.R index b59be5f0..0f4c33e1 100644 --- a/R/layer_predict.R +++ b/R/layer_predict.R @@ -16,7 +16,6 @@ #' @export #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/layer_quantile_distn.R b/R/layer_quantile_distn.R index b39b58f4..2d991a20 100644 --- a/R/layer_quantile_distn.R +++ b/R/layer_quantile_distn.R @@ -22,7 +22,6 @@ #' @export #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/layer_residual_quantiles.R b/R/layer_residual_quantiles.R index 96ad8841..9a811e53 100644 --- a/R/layer_residual_quantiles.R +++ b/R/layer_residual_quantiles.R @@ -15,7 +15,6 @@ #' residual quantiles added to the prediction #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/layer_threshold_preds.R b/R/layer_threshold_preds.R index 7b8ca025..4bfb5252 100644 --- a/R/layer_threshold_preds.R +++ b/R/layer_threshold_preds.R @@ -22,9 +22,9 @@ #' @return an updated `frosting` postprocessor #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value < "2021-03-08", geo_value %in% c("ak", "ca", "ar")) +#' #' r <- epi_recipe(jhu) %>% #' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>% #' step_epi_ahead(death_rate, ahead = 7) %>% diff --git a/R/layers.R b/R/layers.R index 752f014c..01cb19db 100644 --- a/R/layers.R +++ b/R/layers.R @@ -41,7 +41,6 @@ layer <- function(subclass, ..., .prefix = "layer_") { #' in the layer, and the values are the new values to update the layer with. #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' r <- epi_recipe(jhu) %>% diff --git a/R/make_grf_quantiles.R b/R/make_grf_quantiles.R index fbd221d2..b1792adf 100644 --- a/R/make_grf_quantiles.R +++ b/R/make_grf_quantiles.R @@ -60,7 +60,6 @@ #' #' # -- a more complicated task #' -#' library(dplyr) #' dat <- covid_case_death_rates %>% #' filter(time_value > as.Date("2021-10-01")) #' rec <- epi_recipe(dat) %>% diff --git a/R/make_smooth_quantile_reg.R b/R/make_smooth_quantile_reg.R index f31ade3c..28576b95 100644 --- a/R/make_smooth_quantile_reg.R +++ b/R/make_smooth_quantile_reg.R @@ -31,11 +31,10 @@ #' y <- sin(x) + rnorm(length(x), sd = .1) #' fd <- x[length(x) - 20] #' XY <- smoothqr::lagmat(y[1:(length(y) - 20)], c(-20:20)) -#' XY <- tibble::as_tibble(XY) +#' XY <- as_tibble(XY) #' qr_spec <- smooth_quantile_reg(quantile_levels = c(.2, .5, .8), outcome_locations = 20:1) #' tt <- qr_spec %>% fit_xy(x = XY[, 21:41], y = XY[, 1:20]) #' -#' library(tidyr) #' library(dplyr) #' pl <- predict( #' object = tt, diff --git a/R/model-methods.R b/R/model-methods.R index a575bd59..dd08d3ef 100644 --- a/R/model-methods.R +++ b/R/model-methods.R @@ -32,7 +32,6 @@ #' #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' diff --git a/R/pivot_quantiles.R b/R/pivot_quantiles.R index 2a9e0d4e..d4ef3d57 100644 --- a/R/pivot_quantiles.R +++ b/R/pivot_quantiles.R @@ -6,8 +6,6 @@ #' @export #' #' @examples -#' library(dplyr) -#' library(tidyr) #' edf <- covid_case_death_rates[1:3, ] #' edf$q <- dist_quantiles(list(1:5, 2:4, 3:10), list(1:5 / 6, 2:4 / 5, 3:10 / 11)) #' diff --git a/R/step_epi_slide.R b/R/step_epi_slide.R index 274ce245..4cf0e7ac 100644 --- a/R/step_epi_slide.R +++ b/R/step_epi_slide.R @@ -36,7 +36,6 @@ #' #' @export #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-01-01"), geo_value %in% c("ca", "ny")) #' rec <- epi_recipe(jhu) %>% diff --git a/R/step_growth_rate.R b/R/step_growth_rate.R index 159c2439..5497c295 100644 --- a/R/step_growth_rate.R +++ b/R/step_growth_rate.R @@ -30,7 +30,6 @@ #' @importFrom epiprocess growth_rate #' @export #' @examples -#' library(dplyr) #' tiny_geos <- c("as", "mp", "vi", "gu", "pr") #' rates <- covid_case_death_rates %>% #' filter(time_value >= as.Date("2021-11-01"), !(geo_value %in% tiny_geos)) diff --git a/R/step_population_scaling.R b/R/step_population_scaling.R index 6047f5c4..bb60d039 100644 --- a/R/step_population_scaling.R +++ b/R/step_population_scaling.R @@ -49,8 +49,7 @@ #' @return Scales raw data by the population #' @export #' @examples -#' library(dplyr) -#' jhu <- epidatasets::cases_deaths_subset %>% +#' jhu <- cases_deaths_subset %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ca", "ny")) %>% #' select(geo_value, time_value, cases) #' diff --git a/R/tidy.R b/R/tidy.R index 47c6efa6..3969f9dd 100644 --- a/R/tidy.R +++ b/R/tidy.R @@ -26,7 +26,6 @@ #' `type` (the method, e.g. "predict", "naomit"), and a character column `id`. #' #' @examples -#' library(dplyr) #' jhu <- covid_case_death_rates %>% #' filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny")) #' @@ -37,7 +36,7 @@ #' #' wf <- epi_workflow(r, parsnip::linear_reg()) %>% fit(jhu) #' latest <- get_test_data(recipe = r, x = jhu) - +#' #' f <- frosting() %>% #' layer_predict() %>% #' layer_naomit(.pred) From 766e79b5123143caad4b506f108013902dd7cbd1 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:54:31 -0800 Subject: [PATCH 04/10] delete layer creator (dev only, unused) --- R/create-layer.R | 45 ------------------------------------------ inst/templates/layer.R | 44 ----------------------------------------- 2 files changed, 89 deletions(-) delete mode 100644 R/create-layer.R delete mode 100644 inst/templates/layer.R diff --git a/R/create-layer.R b/R/create-layer.R deleted file mode 100644 index 7179cb7e..00000000 --- a/R/create-layer.R +++ /dev/null @@ -1,45 +0,0 @@ -#' Create a new layer -#' -#' This function creates the skeleton for a new `frosting` layer. When called -#' inside a package, it will create an R script in the `R/` directory, -#' fill in the name of the layer, and open the file. -#' -#' @inheritParams usethis::use_test -#' -#' @importFrom rlang %||% -#' @noRd -#' @keywords internal -#' @examples -#' \dontrun{ -#' -#' # Note: running this will write `layer_strawberry.R` to -#' # the `R/` directory of your current project -#' create_layer("strawberry") -#' } -#' -create_layer <- function(name = NULL, open = rlang::is_interactive()) { - name <- name %||% usethis:::get_active_r_file(path = "R") - if (substr(name, 1, 5) == "layer") { - nn <- substring(name, 6) - if (substr(nn, 1, 1) == "_") nn <- substring(nn, 2) - cli_abort( - c('`name` should not begin with "layer" or "layer_".', - i = 'Did you mean to use `create_layer("{ nn }")`?' - ) - ) - } - layer_name <- name - name <- paste0("layer_", name) - name <- usethis:::slug(name, "R") - usethis:::check_file_name(name) - path <- fs::path("R", name) - if (!fs::file_exists(path)) { - usethis::use_template( - "layer.R", - save_as = path, - data = list(name = layer_name), open = FALSE, - package = "epipredict" - ) - } - usethis::edit_file(usethis::proj_path(path), open = open) -} diff --git a/inst/templates/layer.R b/inst/templates/layer.R deleted file mode 100644 index 59556db5..00000000 --- a/inst/templates/layer.R +++ /dev/null @@ -1,44 +0,0 @@ -layer_{{ name }} <- function(frosting, # mandatory - ..., - args, # add as many as you need - more_args, - id = rand_id("{{{ name }}}")) { - - # validate any additional arguments here - - # if you don't need ... then uncomment the line below - ## rlang::check_dots_empty() - add_layer( - frosting, - layer_{{{ name }}}_new( - terms = dplyr::enquos(...), # remove if ... should be empty - args, - id = id - ) - ) -} - -layer_{{{ name }}}_new <- function(terms, args, more_args, id) { - layer("{{{ name }}}", - terms = terms, - args = args, - more_args = more_args, - id = id) -} - -#' @export -slather.layer_{{{ name }}} <- - function(object, components, workflow, new_data, ...) { - rlang::check_dots_empty() - - # if layer_ used ... in tidyselect, we need to evaluate it now - exprs <- rlang::expr(c(!!!object$terms)) - pos <- tidyselect::eval_select(exprs, components$predictions) - col_names <- names(pos) - # now can select with `tidyselect::all_of(col_names)` - - # add additional necessary processing steps here - - # always return components - components - } From f411be67c4c0f20bd6e150d93add8e4ff31748a5 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:54:55 -0800 Subject: [PATCH 05/10] imports from epiprocess --- R/epipredict-package.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/epipredict-package.R b/R/epipredict-package.R index b4b9973b..9ee83258 100644 --- a/R/epipredict-package.R +++ b/R/epipredict-package.R @@ -9,11 +9,11 @@ #' @importFrom dplyr full_join relocate summarise everything #' @importFrom dplyr inner_join #' @importFrom dplyr summarize filter mutate select left_join rename ungroup +#' @importFrom epiprocess growth_rate growth_rate_params is_epi_df #' @importFrom magrittr extract2 #' @importFrom rlang := !! %||% as_function global_env set_names !!! caller_arg #' @importFrom rlang is_logical is_true inject enquo enquos expr sym arg_match #' @importFrom stats poly predict lm residuals quantile #' @importFrom tibble as_tibble -na_chr <- NA_character_ ## usethis namespace: end NULL From 58980f48d46715189a4dc69035d7dbcb19733d03 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:55:10 -0800 Subject: [PATCH 06/10] switch ::: to :: from epiprocess --- R/utils-latency.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils-latency.R b/R/utils-latency.R index d854cb9c..353d0fdd 100644 --- a/R/utils-latency.R +++ b/R/utils-latency.R @@ -199,7 +199,7 @@ pad_to_end <- function(x, groups, end_date, columns_to_complete = NULL) { if (is.null(columns_to_complete)) { columns_to_complete <- setdiff(names(x), key_colnames(x)) } - itval <- epiprocess:::guess_period(c(x$time_value, end_date), "time_value") + itval <- epiprocess::guess_period(c(x$time_value, end_date), "time_value") # get the time values we need to fill in completed_time_values <- x %>% group_by(across(all_of(groups))) %>% From 9e93bb8719405f062b5628306038cbe06007564a Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:55:46 -0800 Subject: [PATCH 07/10] adjust some vignette libraries --- vignettes/backtesting.Rmd | 9 +++++---- vignettes/update.Rmd | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/vignettes/backtesting.Rmd b/vignettes/backtesting.Rmd index 765a684f..70d18fa3 100644 --- a/vignettes/backtesting.Rmd +++ b/vignettes/backtesting.Rmd @@ -11,8 +11,9 @@ vignette: > source("_common.R") ``` -```{r pkgs, include=FALSE, echo=TRUE} +```{r pkgs, message=FALSE} library(epipredict) +library(epiprocess) library(epidatr) library(data.table) library(dplyr) @@ -66,7 +67,7 @@ vignette mentioned above). # Select the `percent_cli` column from the data archive doctor_visits <- archive_cases_dv_subset$DT %>% select(geo_value, time_value, version, percent_cli) %>% - tidyr::drop_na(percent_cli) %>% + drop_na(percent_cli) %>% as_epi_archive(compactify = TRUE) ``` @@ -166,7 +167,7 @@ forecasts %>% doctor_visits %>% epix_as_of(doctor_visits$versions_end) %>% group_by(geo_value) %>% - tidyr::fill(percent_cli), + fill(percent_cli), by = c("geo_value", "target_date" = "time_value") ) %>% select(geo_value, forecast_date, .pred, `0.05`, `0.95`, percent_cli) @@ -376,7 +377,7 @@ linear regression with those from using boosted regression trees. ```{r get-can-fc, warning = FALSE} aheads <- c(7, 14, 21, 28) -canada_archive <- epidatasets::can_prov_cases +canada_archive <- can_prov_cases canada_archive_faux <- epix_as_of(canada_archive, canada_archive$versions_end) %>% mutate(version = time_value) %>% as_epi_archive() diff --git a/vignettes/update.Rmd b/vignettes/update.Rmd index 3d97da04..02fe71be 100644 --- a/vignettes/update.Rmd +++ b/vignettes/update.Rmd @@ -14,6 +14,9 @@ source("_common.R") ```{r setup, message=FALSE} library(epipredict) library(recipes) +library(dplyr) +library(workflows) +library(parsnip) ``` In this vignette, we will state the main goal of the add/update/remove and @@ -68,7 +71,7 @@ South Carolina. ```{r} jhu <- covid_case_death_rates %>% - dplyr::filter(time_value >= as.Date("2021-11-01"), geo_value %in% c("ak", "ca", "ny", "sc")) + filter(time_value >= as.Date("2021-11-01"), geo_value %in% c("ak", "ca", "ny", "sc")) jhu ``` @@ -100,7 +103,7 @@ We may then go on to add the fitted linear model to our `epi_workflow`: ```{r} # Fit a linear model -wf <- epi_workflow(r, parsnip::linear_reg()) %>% fit(jhu) +wf <- epi_workflow(r, linear_reg()) %>% fit(jhu) wf ``` @@ -139,7 +142,7 @@ using `workflows::remove_recipe()` and then inspect the class of `wf`: ```{r} wf %>% class() # class before -workflows::remove_recipe(wf) %>% class() # class after removing recipe using workflows function +remove_recipe(wf) %>% class() # class after removing recipe using workflows function ``` We can observe that `wf` is no longer an `epi_workflow` and a `workflow`. It has @@ -167,7 +170,7 @@ fit as before: ```{r} # fit linear model -wf <- Update_model(wf, parsnip::linear_reg()) %>% fit(jhu) +wf <- Update_model(wf, linear_reg()) %>% fit(jhu) wf ``` @@ -243,8 +246,8 @@ number in the order of operations, which can be obtained by inspecting `r2` or the tidy summary of it: ```{r} -workflows::extract_preprocessor(wf) # step_epi_ahead is the third step in r2 -tidy(workflows::extract_preprocessor(wf)) # tidy tibble summary of r2 +extract_preprocessor(wf) # step_epi_ahead is the third step in r2 +tidy(extract_preprocessor(wf)) # tidy tibble summary of r2 wf <- wf %>% adjust_epi_recipe(which_step = 3, ahead = 14) ``` @@ -268,7 +271,7 @@ variable, we would specify the step number of 2 in `which_step`. ```{r} wf <- wf %>% adjust_epi_recipe(which_step = 2, lag = c(0, 1, 7, 14, 21)) -workflows::extract_preprocessor(wf) +extract_preprocessor(wf) ``` We could adjust a recipe directly in the same way as we adjust a recipe in a @@ -289,7 +292,7 @@ with a new `epi_recipe` that has undergone the adjustment ```{r} r2 <- adjust_epi_recipe(r2, which_step = 2, lag = 0:21) -workflows::extract_preprocessor(wf) +extract_preprocessor(wf) ``` ## Adjust a single layer of a `frosting` From cd2c583f2bacc98f961bdec6df3cf5cf9a63b2b4 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 11:56:10 -0800 Subject: [PATCH 08/10] redocument --- NAMESPACE | 12 +++++++++ man/Add_model.Rd | 1 - man/add_epi_recipe.Rd | 9 ++----- man/add_frosting.Rd | 1 - man/adjust_epi_recipe.Rd | 1 - man/adjust_frosting.Rd | 1 - man/arx_class_epi_workflow.Rd | 1 - man/arx_classifier.Rd | 1 - man/arx_fcast_epi_workflow.Rd | 1 - man/autoplot-epipred.Rd | 1 - man/cdc_baseline_forecaster.Rd | 38 +++++++++++++------------- man/epi_recipe.Rd | 9 ++----- man/frosting.Rd | 1 - man/grf_quantiles.Rd | 1 - man/layer_add_forecast_date.Rd | 1 - man/layer_add_target_date.Rd | 1 - man/layer_cdc_flatline_quantiles.Rd | 41 ++++++++++++++--------------- man/layer_naomit.Rd | 1 - man/layer_point_from_distn.Rd | 1 - man/layer_population_scaling.Rd | 3 +-- man/layer_predict.Rd | 1 - man/layer_quantile_distn.Rd | 1 - man/layer_residual_quantiles.Rd | 1 - man/layer_threshold.Rd | 2 +- man/nested_quantiles.Rd | 2 -- man/reexports.Rd | 20 +++++++++++--- man/smooth_quantile_reg.Rd | 3 +-- man/step_epi_slide.Rd | 1 - man/step_growth_rate.Rd | 1 - man/step_population_scaling.Rd | 3 +-- man/tidy.frosting.Rd | 2 +- man/update.layer.Rd | 1 - 32 files changed, 77 insertions(+), 87 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 3a1fed50..003cd48e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -144,6 +144,8 @@ export(arx_class_epi_workflow) export(arx_classifier) export(arx_fcast_epi_workflow) export(arx_forecaster) +export(as_epi_df) +export(as_tibble) export(autoplot) export(bake) export(cdc_baseline_args_list) @@ -160,6 +162,7 @@ export(extract_argument) export(extract_frosting) export(extract_layers) export(extrapolate_quantiles) +export(filter) export(fit) export(flatline) export(flatline_args_list) @@ -171,6 +174,7 @@ export(get_test_data) export(is_epi_recipe) export(is_epi_workflow) export(is_layer) +export(key_colnames) export(layer) export(layer_add_forecast_date) export(layer_add_target_date) @@ -184,6 +188,7 @@ export(layer_quantile_distn) export(layer_residual_quantiles) export(layer_threshold) export(layer_unnest) +export(mutate) export(nested_quantiles) export(new_default_epi_recipe_blueprint) export(new_epi_recipe_blueprint) @@ -195,6 +200,8 @@ export(rand_id) export(remove_epi_recipe) export(remove_frosting) export(remove_model) +export(rename) +export(select) export(slather) export(smooth_quantile_reg) export(step_adjust_latency) @@ -208,6 +215,7 @@ export(step_population_scaling) export(step_training_window) export(tibble) export(tidy) +export(unnest) export(update_epi_recipe) export(update_frosting) export(update_model) @@ -256,8 +264,12 @@ importFrom(dplyr,summarize) importFrom(dplyr,tibble) importFrom(dplyr,tribble) importFrom(dplyr,ungroup) +importFrom(epiprocess,as_epi_df) importFrom(epiprocess,epi_slide) importFrom(epiprocess,growth_rate) +importFrom(epiprocess,growth_rate_params) +importFrom(epiprocess,is_epi_df) +importFrom(epiprocess,key_colnames) importFrom(generics,augment) importFrom(generics,fit) importFrom(generics,forecast) diff --git a/man/Add_model.Rd b/man/Add_model.Rd index 641bd767..8f316532 100644 --- a/man/Add_model.Rd +++ b/man/Add_model.Rd @@ -71,7 +71,6 @@ aliases with the lower-case names. However, in the event that properly. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/add_epi_recipe.Rd b/man/add_epi_recipe.Rd index b7426752..0135cfd6 100644 --- a/man/add_epi_recipe.Rd +++ b/man/add_epi_recipe.Rd @@ -38,19 +38,14 @@ Add an \code{epi_recipe} to a workflow default blueprint to automatically handle \link[epiprocess:epi_df]{epiprocess::epi_df} data. } \examples{ -library(dplyr) -library(recipes) - jhu <- covid_case_death_rates \%>\% - filter(time_value > "2021-08-01") \%>\% - arrange(geo_value, time_value) + filter(time_value > "2021-08-01") r <- epi_recipe(jhu) \%>\% step_epi_lag(death_rate, lag = c(0, 7, 14)) \%>\% step_epi_ahead(death_rate, ahead = 7) \%>\% step_epi_lag(case_rate, lag = c(0, 7, 14)) \%>\% - step_naomit(all_predictors()) \%>\% - step_naomit(all_outcomes(), skip = TRUE) + step_epi_naomit() workflow <- epi_workflow() \%>\% add_epi_recipe(r) diff --git a/man/add_frosting.Rd b/man/add_frosting.Rd index 00b899b7..82574748 100644 --- a/man/add_frosting.Rd +++ b/man/add_frosting.Rd @@ -26,7 +26,6 @@ update_frosting(x, frosting, ...) Add frosting to a workflow } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) r <- epi_recipe(jhu) \%>\% diff --git a/man/adjust_epi_recipe.Rd b/man/adjust_epi_recipe.Rd index 0ed7148a..b1dc6c60 100644 --- a/man/adjust_epi_recipe.Rd +++ b/man/adjust_epi_recipe.Rd @@ -52,7 +52,6 @@ must be inputted as \code{...}. See the examples below for brief illustrations of the different types of updates. } \examples{ -library(dplyr) library(workflows) jhu <- covid_case_death_rates \%>\% diff --git a/man/adjust_frosting.Rd b/man/adjust_frosting.Rd index 3b855a9a..e6b33f3d 100644 --- a/man/adjust_frosting.Rd +++ b/man/adjust_frosting.Rd @@ -35,7 +35,6 @@ must be inputted as \code{...}. See the examples below for brief illustrations of the different types of updates. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) r <- epi_recipe(jhu) \%>\% diff --git a/man/arx_class_epi_workflow.Rd b/man/arx_class_epi_workflow.Rd index 7497fe95..9b048463 100644 --- a/man/arx_class_epi_workflow.Rd +++ b/man/arx_class_epi_workflow.Rd @@ -47,7 +47,6 @@ before fitting and predicting. Supplying a trainer to the function may alter the returned \code{epi_workflow} object but can be omitted. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-11-01")) diff --git a/man/arx_classifier.Rd b/man/arx_classifier.Rd index 22edc98e..d78700df 100644 --- a/man/arx_classifier.Rd +++ b/man/arx_classifier.Rd @@ -48,7 +48,6 @@ This is an autoregressive classification model for that it estimates a class at a particular target horizon. } \examples{ -library(dplyr) tiny_geos <- c("as", "mp", "vi", "gu", "pr") jhu <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-11-01"), !(geo_value \%in\% tiny_geos)) diff --git a/man/arx_fcast_epi_workflow.Rd b/man/arx_fcast_epi_workflow.Rd index c2e38218..0d208858 100644 --- a/man/arx_fcast_epi_workflow.Rd +++ b/man/arx_fcast_epi_workflow.Rd @@ -42,7 +42,6 @@ may alter the returned \code{epi_workflow} object (e.g., if you intend to use \code{\link[=quantile_reg]{quantile_reg()}}) but can be omitted. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-12-01")) diff --git a/man/autoplot-epipred.Rd b/man/autoplot-epipred.Rd index 066f5538..087f25db 100644 --- a/man/autoplot-epipred.Rd +++ b/man/autoplot-epipred.Rd @@ -70,7 +70,6 @@ will be shown as well. Unfit workflows will result in an error, (you can simply call \code{autoplot()} on the original \code{epi_df}). } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-11-01")) diff --git a/man/cdc_baseline_forecaster.Rd b/man/cdc_baseline_forecaster.Rd index e7cefda2..c201bd99 100644 --- a/man/cdc_baseline_forecaster.Rd +++ b/man/cdc_baseline_forecaster.Rd @@ -38,6 +38,7 @@ This forecaster is meant to produce exactly the CDC Baseline used for } \examples{ library(dplyr) +library(epiprocess) weekly_deaths <- covid_case_death_rates \%>\% select(geo_value, time_value, death_rate) \%>\% left_join(state_census \%>\% select(pop, abbr), by = c("geo_value" = "abbr")) \%>\% @@ -51,23 +52,22 @@ weekly_deaths <- covid_case_death_rates \%>\% cdc <- cdc_baseline_forecaster(weekly_deaths, "deaths_7dsum") preds <- pivot_quantiles_wider(cdc$predictions, .pred_distn) -if (require(ggplot2)) { - forecast_date <- unique(preds$forecast_date) - four_states <- c("ca", "pa", "wa", "ny") - preds \%>\% - filter(geo_value \%in\% four_states) \%>\% - ggplot(aes(target_date)) + - geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + - geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + - geom_line(aes(y = .pred), color = "orange") + - geom_line( - data = weekly_deaths \%>\% filter(geo_value \%in\% four_states), - aes(x = time_value, y = deaths_7dsum) - ) + - scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + - labs(x = "Date", y = "Weekly deaths") + - facet_wrap(~geo_value, scales = "free_y") + - theme_bw() + - geom_vline(xintercept = forecast_date) -} +library(ggplot2) +forecast_date <- unique(preds$forecast_date) +four_states <- c("ca", "pa", "wa", "ny") +preds \%>\% + filter(geo_value \%in\% four_states) \%>\% + ggplot(aes(target_date)) + + geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + + geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + + geom_line(aes(y = .pred), color = "orange") + + geom_line( + data = weekly_deaths \%>\% filter(geo_value \%in\% four_states), + aes(x = time_value, y = deaths_7dsum) + ) + + scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + + labs(x = "Date", y = "Weekly deaths") + + facet_wrap(~geo_value, scales = "free_y") + + theme_bw() + + geom_vline(xintercept = forecast_date) } diff --git a/man/epi_recipe.Rd b/man/epi_recipe.Rd index b93e4bcf..90f6ea45 100644 --- a/man/epi_recipe.Rd +++ b/man/epi_recipe.Rd @@ -75,19 +75,14 @@ around \code{\link[recipes:recipe]{recipes::recipe()}} to properly handle the ad columns present in an \code{epi_df} } \examples{ -library(dplyr) -library(recipes) jhu <- covid_case_death_rates \%>\% - filter(time_value > "2021-08-01") \%>\% - arrange(geo_value, time_value) + filter(time_value > "2021-08-01") r <- epi_recipe(jhu) \%>\% step_epi_lag(death_rate, lag = c(0, 7, 14)) \%>\% step_epi_ahead(death_rate, ahead = 7) \%>\% step_epi_lag(case_rate, lag = c(0, 7, 14)) \%>\% - step_naomit(all_predictors()) \%>\% - # below, `skip` means we don't do this at predict time - step_naomit(all_outcomes(), skip = TRUE) + step_epi_naomit(all_predictors()) r } diff --git a/man/frosting.Rd b/man/frosting.Rd index 8534bc6d..e36100ba 100644 --- a/man/frosting.Rd +++ b/man/frosting.Rd @@ -22,7 +22,6 @@ to hold steps for postprocessing predictions. The arguments are currently placeholders and must be NULL } \examples{ -library(dplyr) # Toy example to show that frosting can be created and added for postprocessing f <- frosting() wf <- epi_workflow() \%>\% add_frosting(f) diff --git a/man/grf_quantiles.Rd b/man/grf_quantiles.Rd index ad33ce11..2e4b8bcb 100644 --- a/man/grf_quantiles.Rd +++ b/man/grf_quantiles.Rd @@ -84,7 +84,6 @@ predict(out, new_data = tib[1:5, ]) \%>\% # -- a more complicated task -library(dplyr) dat <- covid_case_death_rates \%>\% filter(time_value > as.Date("2021-10-01")) rec <- epi_recipe(dat) \%>\% diff --git a/man/layer_add_forecast_date.Rd b/man/layer_add_forecast_date.Rd index cc92e9a7..14c7864b 100644 --- a/man/layer_add_forecast_date.Rd +++ b/man/layer_add_forecast_date.Rd @@ -38,7 +38,6 @@ less than the maximum \code{as_of} value (from the data used pre-processing, model fitting, and postprocessing), an appropriate warning will be thrown. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) r <- epi_recipe(jhu) \%>\% diff --git a/man/layer_add_target_date.Rd b/man/layer_add_target_date.Rd index 37416d24..c9f43c5f 100644 --- a/man/layer_add_target_date.Rd +++ b/man/layer_add_target_date.Rd @@ -43,7 +43,6 @@ every dataset used (prep, training, and prediction). } } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) r <- epi_recipe(jhu) \%>\% diff --git a/man/layer_cdc_flatline_quantiles.Rd b/man/layer_cdc_flatline_quantiles.Rd index 632fdb65..35a6c03e 100644 --- a/man/layer_cdc_flatline_quantiles.Rd +++ b/man/layer_cdc_flatline_quantiles.Rd @@ -84,13 +84,13 @@ the future. This version continues to use the same set of residuals, and adds them on to produce wider intervals as \code{ahead} increases. } \examples{ -library(dplyr) +library(recipes) r <- epi_recipe(covid_case_death_rates) \%>\% # data is "daily", so we fit this to 1 ahead, the result will contain # 1 day ahead residuals step_epi_ahead(death_rate, ahead = 1L, skip = TRUE) \%>\% - recipes::update_role(death_rate, new_role = "predictor") \%>\% - recipes::add_role(time_value, geo_value, new_role = "predictor") + update_role(death_rate, new_role = "predictor") \%>\% + add_role(time_value, geo_value, new_role = "predictor") forecast_date <- max(covid_case_death_rates$time_value) @@ -111,22 +111,21 @@ preds <- preds \%>\% pivot_quantiles_wider(.pred_distn) \%>\% mutate(target_date = forecast_date + ahead) -if (require("ggplot2")) { - four_states <- c("ca", "pa", "wa", "ny") - preds \%>\% - filter(geo_value \%in\% four_states) \%>\% - ggplot(aes(target_date)) + - geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + - geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + - geom_line(aes(y = .pred), color = "orange") + - geom_line( - data = covid_case_death_rates \%>\% filter(geo_value \%in\% four_states), - aes(x = time_value, y = death_rate) - ) + - scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + - labs(x = "Date", y = "Death rate") + - facet_wrap(~geo_value, scales = "free_y") + - theme_bw() + - geom_vline(xintercept = forecast_date) -} +library(ggplot2) +four_states <- c("ca", "pa", "wa", "ny") +preds \%>\% + filter(geo_value \%in\% four_states) \%>\% + ggplot(aes(target_date)) + + geom_ribbon(aes(ymin = `0.1`, ymax = `0.9`), fill = blues9[3]) + + geom_ribbon(aes(ymin = `0.25`, ymax = `0.75`), fill = blues9[6]) + + geom_line(aes(y = .pred), color = "orange") + + geom_line( + data = covid_case_death_rates \%>\% filter(geo_value \%in\% four_states), + aes(x = time_value, y = death_rate) + ) + + scale_x_date(limits = c(forecast_date - 90, forecast_date + 30)) + + labs(x = "Date", y = "Death rate") + + facet_wrap(~geo_value, scales = "free_y") + + theme_bw() + + geom_vline(xintercept = forecast_date) } diff --git a/man/layer_naomit.Rd b/man/layer_naomit.Rd index 06e09d4b..eea81959 100644 --- a/man/layer_naomit.Rd +++ b/man/layer_naomit.Rd @@ -24,7 +24,6 @@ an updated \code{frosting} postprocessor Omit \code{NA}s from predictions or other columns } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/layer_point_from_distn.Rd b/man/layer_point_from_distn.Rd index bde2323b..329f62d5 100644 --- a/man/layer_point_from_distn.Rd +++ b/man/layer_point_from_distn.Rd @@ -34,7 +34,6 @@ information, so one should usually call this AFTER \code{layer_quantile_distn()} or set the \code{name} argument to something specific. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/layer_population_scaling.Rd b/man/layer_population_scaling.Rd index 083dd6b4..1b4c3d7d 100644 --- a/man/layer_population_scaling.Rd +++ b/man/layer_population_scaling.Rd @@ -79,8 +79,7 @@ passed will \emph{multiply} the selected variables while the \code{rate_rescalin argument is a common \emph{divisor} of the selected variables. } \examples{ -library(dplyr) -jhu <- epidatasets::cases_deaths_subset \%>\% +jhu <- cases_deaths_subset \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ca", "ny")) \%>\% select(geo_value, time_value, cases) diff --git a/man/layer_predict.Rd b/man/layer_predict.Rd index db771b88..9a7f12a4 100644 --- a/man/layer_predict.Rd +++ b/man/layer_predict.Rd @@ -58,7 +58,6 @@ postprocessing. This would typically be the first layer in a \code{frosting} postprocessor. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/layer_quantile_distn.Rd b/man/layer_quantile_distn.Rd index ed4762fa..21c38b81 100644 --- a/man/layer_quantile_distn.Rd +++ b/man/layer_quantile_distn.Rd @@ -45,7 +45,6 @@ If these engines were used, then this layer will grab out estimated (or extrapolated) quantiles at the requested quantile values. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/layer_residual_quantiles.Rd b/man/layer_residual_quantiles.Rd index 4efea525..16e16cea 100644 --- a/man/layer_residual_quantiles.Rd +++ b/man/layer_residual_quantiles.Rd @@ -40,7 +40,6 @@ residual quantiles added to the prediction Creates predictions based on residual quantiles } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) diff --git a/man/layer_threshold.Rd b/man/layer_threshold.Rd index 702c5d71..875390ce 100644 --- a/man/layer_threshold.Rd +++ b/man/layer_threshold.Rd @@ -40,9 +40,9 @@ smaller than the lower threshold or higher than the upper threshold equal to the threshold values. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value < "2021-03-08", geo_value \%in\% c("ak", "ca", "ar")) + r <- epi_recipe(jhu) \%>\% step_epi_lag(death_rate, lag = c(0, 7, 14)) \%>\% step_epi_ahead(death_rate, ahead = 7) \%>\% diff --git a/man/nested_quantiles.Rd b/man/nested_quantiles.Rd index 0fa0fe8c..b1571450 100644 --- a/man/nested_quantiles.Rd +++ b/man/nested_quantiles.Rd @@ -16,8 +16,6 @@ a list-col Turn a vector of quantile distributions into a list-col } \examples{ -library(dplyr) -library(tidyr) edf <- covid_case_death_rates[1:3, ] edf$q <- dist_quantiles(list(1:5, 2:4, 3:10), list(1:5 / 6, 2:4 / 5, 3:10 / 11)) diff --git a/man/reexports.Rd b/man/reexports.Rd index f6849a53..f5d28dbc 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -1,16 +1,24 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/autoplot.R, R/reexports-tidymodels.R +% Please edit documentation in R/autoplot.R, R/reexports.R \docType{import} \name{reexports} \alias{reexports} \alias{autoplot} +\alias{filter} +\alias{mutate} +\alias{rename} +\alias{select} +\alias{as_epi_df} +\alias{key_colnames} \alias{fit} \alias{forecast} +\alias{tidy} \alias{prep} \alias{bake} \alias{rand_id} +\alias{as_tibble} \alias{tibble} -\alias{tidy} +\alias{unnest} \title{Objects exported from other packages} \keyword{internal} \description{ @@ -18,12 +26,18 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ + \item{dplyr}{\code{\link[dplyr]{filter}}, \code{\link[dplyr]{mutate}}, \code{\link[dplyr]{rename}}, \code{\link[dplyr]{select}}} + + \item{epiprocess}{\code{\link[epiprocess:epi_df]{as_epi_df}}, \code{\link[epiprocess]{key_colnames}}} + \item{generics}{\code{\link[generics]{fit}}, \code{\link[generics]{forecast}}, \code{\link[generics]{tidy}}} \item{ggplot2}{\code{\link[ggplot2]{autoplot}}} \item{recipes}{\code{\link[recipes]{bake}}, \code{\link[recipes]{prep}}, \code{\link[recipes]{rand_id}}} - \item{tibble}{\code{\link[tibble]{tibble}}} + \item{tibble}{\code{\link[tibble]{as_tibble}}, \code{\link[tibble]{tibble}}} + + \item{tidyr}{\code{\link[tidyr]{unnest}}} }} diff --git a/man/smooth_quantile_reg.Rd b/man/smooth_quantile_reg.Rd index 90b2c104..06a52ad5 100644 --- a/man/smooth_quantile_reg.Rd +++ b/man/smooth_quantile_reg.Rd @@ -50,11 +50,10 @@ x <- -99:99 / 100 * 2 * pi y <- sin(x) + rnorm(length(x), sd = .1) fd <- x[length(x) - 20] XY <- smoothqr::lagmat(y[1:(length(y) - 20)], c(-20:20)) -XY <- tibble::as_tibble(XY) +XY <- as_tibble(XY) qr_spec <- smooth_quantile_reg(quantile_levels = c(.2, .5, .8), outcome_locations = 20:1) tt <- qr_spec \%>\% fit_xy(x = XY[, 21:41], y = XY[, 1:20]) -library(tidyr) library(dplyr) pl <- predict( object = tt, diff --git a/man/step_epi_slide.Rd b/man/step_epi_slide.Rd index b8e4cedb..4e7ba8be 100644 --- a/man/step_epi_slide.Rd +++ b/man/step_epi_slide.Rd @@ -80,7 +80,6 @@ that will generate one or more new columns of derived data by "sliding" a computation along existing data. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-01-01"), geo_value \%in\% c("ca", "ny")) rec <- epi_recipe(jhu) \%>\% diff --git a/man/step_growth_rate.Rd b/man/step_growth_rate.Rd index 2c98e74e..e76da110 100644 --- a/man/step_growth_rate.Rd +++ b/man/step_growth_rate.Rd @@ -72,7 +72,6 @@ sequence of any existing operations. that will generate one or more new columns of derived data. } \examples{ -library(dplyr) tiny_geos <- c("as", "mp", "vi", "gu", "pr") rates <- covid_case_death_rates \%>\% filter(time_value >= as.Date("2021-11-01"), !(geo_value \%in\% tiny_geos)) diff --git a/man/step_population_scaling.Rd b/man/step_population_scaling.Rd index c41ec7fc..427e896e 100644 --- a/man/step_population_scaling.Rd +++ b/man/step_population_scaling.Rd @@ -92,8 +92,7 @@ passed will \emph{divide} the selected variables while the \code{rate_rescaling} argument is a common \emph{multiplier} of the selected variables. } \examples{ -library(dplyr) -jhu <- epidatasets::cases_deaths_subset \%>\% +jhu <- cases_deaths_subset \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ca", "ny")) \%>\% select(geo_value, time_value, cases) diff --git a/man/tidy.frosting.Rd b/man/tidy.frosting.Rd index 8152b144..3f9b0e37 100644 --- a/man/tidy.frosting.Rd +++ b/man/tidy.frosting.Rd @@ -37,7 +37,6 @@ method for the operation exists). Note that this is a modified version of the \code{tidy} method for a recipe. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) @@ -48,6 +47,7 @@ r <- epi_recipe(jhu) \%>\% wf <- epi_workflow(r, parsnip::linear_reg()) \%>\% fit(jhu) latest <- get_test_data(recipe = r, x = jhu) + f <- frosting() \%>\% layer_predict() \%>\% layer_naomit(.pred) diff --git a/man/update.layer.Rd b/man/update.layer.Rd index f151beea..878c9bb1 100644 --- a/man/update.layer.Rd +++ b/man/update.layer.Rd @@ -18,7 +18,6 @@ will replace the elements of the same name in the actual post-processing layer. Analogous to \code{update.step()} from the \code{recipes} package. } \examples{ -library(dplyr) jhu <- covid_case_death_rates \%>\% filter(time_value > "2021-11-01", geo_value \%in\% c("ak", "ca", "ny")) r <- epi_recipe(jhu) \%>\% From 1ce825f27681c8597d346c1e17be708541e66abc Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 12:38:53 -0800 Subject: [PATCH 09/10] pass checks and redocument --- NAMESPACE | 4 ++++ R/epi_recipe.R | 2 +- R/flusight_hub_formatter.R | 1 + R/make_smooth_quantile_reg.R | 1 - R/reexports.R | 8 ++++++++ man/epi_recipe.Rd | 2 +- man/flusight_hub_formatter.Rd | 1 + man/reexports.Rd | 4 +++- man/smooth_quantile_reg.Rd | 1 - 9 files changed, 19 insertions(+), 5 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 003cd48e..565239a6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -192,8 +192,10 @@ export(mutate) export(nested_quantiles) export(new_default_epi_recipe_blueprint) export(new_epi_recipe_blueprint) +export(pivot_longer) export(pivot_quantiles_longer) export(pivot_quantiles_wider) +export(pivot_wider) export(prep) export(quantile_reg) export(rand_id) @@ -328,6 +330,8 @@ importFrom(tidyr,crossing) importFrom(tidyr,drop_na) importFrom(tidyr,expand_grid) importFrom(tidyr,fill) +importFrom(tidyr,pivot_longer) +importFrom(tidyr,pivot_wider) importFrom(tidyr,unnest) importFrom(tidyselect,all_of) importFrom(utils,capture.output) diff --git a/R/epi_recipe.R b/R/epi_recipe.R index 367cf78f..dae445f5 100644 --- a/R/epi_recipe.R +++ b/R/epi_recipe.R @@ -52,7 +52,7 @@ epi_recipe <- function(x, ...) { #' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>% #' step_epi_ahead(death_rate, ahead = 7) %>% #' step_epi_lag(case_rate, lag = c(0, 7, 14)) %>% -#' step_epi_naomit(all_predictors()) +#' step_epi_naomit() #' #' r epi_recipe.epi_df <- function(x, diff --git a/R/flusight_hub_formatter.R b/R/flusight_hub_formatter.R index b3e31822..feac624b 100644 --- a/R/flusight_hub_formatter.R +++ b/R/flusight_hub_formatter.R @@ -55,6 +55,7 @@ abbr_to_location <- function(abbr) { #' #' @examples #' library(dplyr) +#' library(epiprocess) #' weekly_deaths <- covid_case_death_rates %>% #' filter( #' time_value >= as.Date("2021-09-01"), diff --git a/R/make_smooth_quantile_reg.R b/R/make_smooth_quantile_reg.R index 28576b95..b56a67b3 100644 --- a/R/make_smooth_quantile_reg.R +++ b/R/make_smooth_quantile_reg.R @@ -35,7 +35,6 @@ #' qr_spec <- smooth_quantile_reg(quantile_levels = c(.2, .5, .8), outcome_locations = 20:1) #' tt <- qr_spec %>% fit_xy(x = XY[, 21:41], y = XY[, 1:20]) #' -#' library(dplyr) #' pl <- predict( #' object = tt, #' new_data = XY[max(which(complete.cases(XY[, 21:41]))), 21:41] diff --git a/R/reexports.R b/R/reexports.R index 02a377f7..2cc7aa9f 100644 --- a/R/reexports.R +++ b/R/reexports.R @@ -54,6 +54,14 @@ tibble::as_tibble #' @export tibble::tibble +#' @importFrom tidyr pivot_longer +#' @export +tidyr::pivot_longer + +#' @importFrom tidyr pivot_wider +#' @export +tidyr::pivot_wider + #' @importFrom tidyr unnest #' @export tidyr::unnest diff --git a/man/epi_recipe.Rd b/man/epi_recipe.Rd index 90f6ea45..fefd162e 100644 --- a/man/epi_recipe.Rd +++ b/man/epi_recipe.Rd @@ -82,7 +82,7 @@ r <- epi_recipe(jhu) \%>\% step_epi_lag(death_rate, lag = c(0, 7, 14)) \%>\% step_epi_ahead(death_rate, ahead = 7) \%>\% step_epi_lag(case_rate, lag = c(0, 7, 14)) \%>\% - step_epi_naomit(all_predictors()) + step_epi_naomit() r } diff --git a/man/flusight_hub_formatter.Rd b/man/flusight_hub_formatter.Rd index f48d33d6..2c283d7b 100644 --- a/man/flusight_hub_formatter.Rd +++ b/man/flusight_hub_formatter.Rd @@ -42,6 +42,7 @@ format for this forecast task is \href{https://github.com/cdcepi/FluSight-foreca } \examples{ library(dplyr) +library(epiprocess) weekly_deaths <- covid_case_death_rates \%>\% filter( time_value >= as.Date("2021-09-01"), diff --git a/man/reexports.Rd b/man/reexports.Rd index f5d28dbc..aedd9e46 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -18,6 +18,8 @@ \alias{rand_id} \alias{as_tibble} \alias{tibble} +\alias{pivot_longer} +\alias{pivot_wider} \alias{unnest} \title{Objects exported from other packages} \keyword{internal} @@ -38,6 +40,6 @@ below to see their documentation. \item{tibble}{\code{\link[tibble]{as_tibble}}, \code{\link[tibble]{tibble}}} - \item{tidyr}{\code{\link[tidyr]{unnest}}} + \item{tidyr}{\code{\link[tidyr]{pivot_longer}}, \code{\link[tidyr]{pivot_wider}}, \code{\link[tidyr]{unnest}}} }} diff --git a/man/smooth_quantile_reg.Rd b/man/smooth_quantile_reg.Rd index 06a52ad5..eb715be6 100644 --- a/man/smooth_quantile_reg.Rd +++ b/man/smooth_quantile_reg.Rd @@ -54,7 +54,6 @@ XY <- as_tibble(XY) qr_spec <- smooth_quantile_reg(quantile_levels = c(.2, .5, .8), outcome_locations = 20:1) tt <- qr_spec \%>\% fit_xy(x = XY[, 21:41], y = XY[, 1:20]) -library(dplyr) pl <- predict( object = tt, new_data = XY[max(which(complete.cases(XY[, 21:41]))), 21:41] From 3903960bea4459694fefa557ae2feb51add08ef2 Mon Sep 17 00:00:00 2001 From: "Daniel J. McDonald" Date: Wed, 26 Feb 2025 12:42:29 -0800 Subject: [PATCH 10/10] add to news and bump version --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8c47a1d4..b8d83a6a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: epipredict Title: Basic epidemiology forecasting methods -Version: 0.1.10 +Version: 0.1.11 Authors@R: c( person("Daniel J.", "McDonald", , "daniel@stat.ubc.ca", role = c("aut", "cre")), person("Ryan", "Tibshirani", , "ryantibs@cmu.edu", role = "aut"), diff --git a/NEWS.md b/NEWS.md index 28f5bb99..014ea306 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat - Addresses upstream breaking changes from cmu-delphi/epiprocess#595 (`growth_rate()`). `step_growth_rate()` has lost its `additional_gr_args_list` argument and now has an `na_rm` argument. +- Moves `epiprocess` out of depends (#440). No internals have changed, but downstream + users may need to add `library(epiprocess)` to existing code. ## Improvements