Skip to content

Commit 4da9b23

Browse files
authored
Merge pull request #436 from cmu-delphi/climatological
Climatological
2 parents a805c2a + 03d50b2 commit 4da9b23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2926
-181
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ inst/doc
1010
.Rprofile
1111
renv.lock
1212
renv/
13+
.Renviron

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Imports:
3838
glue,
3939
hardhat (>= 1.3.0),
4040
lifecycle,
41+
lubridate,
4142
magrittr,
4243
recipes (>= 1.0.4),
4344
rlang (>= 1.1.0),
@@ -54,7 +55,6 @@ Suggests:
5455
fs,
5556
grf,
5657
knitr,
57-
lubridate,
5858
poissonreg,
5959
purrr,
6060
quantreg,

NAMESPACE

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ S3method(autoplot,epi_workflow)
2020
S3method(bake,check_enough_train_data)
2121
S3method(bake,epi_recipe)
2222
S3method(bake,step_adjust_latency)
23+
S3method(bake,step_climate)
2324
S3method(bake,step_epi_ahead)
2425
S3method(bake,step_epi_lag)
2526
S3method(bake,step_epi_slide)
@@ -59,6 +60,7 @@ S3method(predict,flatline)
5960
S3method(prep,check_enough_train_data)
6061
S3method(prep,epi_recipe)
6162
S3method(prep,step_adjust_latency)
63+
S3method(prep,step_climate)
6264
S3method(prep,step_epi_ahead)
6365
S3method(prep,step_epi_lag)
6466
S3method(prep,step_epi_slide)
@@ -72,6 +74,7 @@ S3method(print,arx_fcast)
7274
S3method(print,canned_epipred)
7375
S3method(print,cdc_baseline_fcast)
7476
S3method(print,check_enough_train_data)
77+
S3method(print,climate_fcast)
7578
S3method(print,epi_recipe)
7679
S3method(print,epi_workflow)
7780
S3method(print,flat_fcast)
@@ -89,6 +92,7 @@ S3method(print,layer_residual_quantiles)
8992
S3method(print,layer_threshold)
9093
S3method(print,layer_unnest)
9194
S3method(print,step_adjust_latency)
95+
S3method(print,step_climate)
9296
S3method(print,step_epi_ahead)
9397
S3method(print,step_epi_lag)
9498
S3method(print,step_epi_slide)
@@ -152,6 +156,8 @@ export(cdc_baseline_args_list)
152156
export(cdc_baseline_forecaster)
153157
export(check_enough_train_data)
154158
export(clean_f_name)
159+
export(climate_args_list)
160+
export(climatological_forecaster)
155161
export(default_epi_recipe_blueprint)
156162
export(detect_layer)
157163
export(dist_quantiles)
@@ -207,6 +213,7 @@ export(select)
207213
export(slather)
208214
export(smooth_quantile_reg)
209215
export(step_adjust_latency)
216+
export(step_climate)
210217
export(step_epi_ahead)
211218
export(step_epi_lag)
212219
export(step_epi_naomit)
@@ -286,6 +293,10 @@ importFrom(glue,glue)
286293
importFrom(hardhat,extract_recipe)
287294
importFrom(hardhat,refresh_blueprint)
288295
importFrom(hardhat,run_mold)
296+
importFrom(lubridate,"%m-%")
297+
importFrom(lubridate,leap_year)
298+
importFrom(lubridate,month)
299+
importFrom(lubridate,yday)
289300
importFrom(magrittr,"%>%")
290301
importFrom(magrittr,extract2)
291302
importFrom(recipes,bake)

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
1111
`data(<dataset name>)`, but can be accessed with
1212
`data(<dataset name>, package = "epidatasets")`, `epidatasets::<dataset name>`
1313
or, after loading the package, the name of the dataset alone (#382).
14+
- `step_adjust_latency()` no longer allows empty column selection.
1415
- Addresses upstream breaking changes from cmu-delphi/epiprocess#595 (`growth_rate()`).
1516
`step_growth_rate()` has lost its `additional_gr_args_list` argument and now
1617
has an `na_rm` argument.
@@ -25,8 +26,11 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
2526
- Fix `quantile_reg()` producing error when asked to output just median-level predictions.
2627
- (temporary) ahead negative is allowed for `step_epi_ahead` until we have `step_epi_shift`
2728
- Add `reference_date` as an argument to `epi_recipe()`
29+
- Add `step_climate()` to create "climate" predictor in forecast workflows
30+
- Add `climatological_forecaster()` to automatically create climate baselines
2831

2932
## Bug fixes
33+
3034
- Shifting no columns results in no error for either `step_epi_ahead` and `step_epi_lag`
3135
- Quantiles produced by `grf` were sometimes out of order.
3236
- dist_quantiles can have all `NA` values without causing unrelated errors

R/arx_forecaster.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ arx_forecaster <- function(
9292
#'
9393
#' @return An unfitted `epi_workflow`.
9494
#' @export
95-
#' @seealso [arx_forecaster()]
95+
#' @seealso [arx_forecaster()], [arx_args_list()]
9696
#'
9797
#' @examples
9898
#' jhu <- covid_case_death_rates %>%
@@ -282,6 +282,7 @@ arx_fcast_epi_workflow <- function(
282282
#'
283283
#' @return A list containing updated parameter choices with class `arx_flist`.
284284
#' @export
285+
#' @seealso [arx_forecaster()]
285286
#'
286287
#' @examples
287288
#' arx_args_list()

R/autoplot.R

+5-4
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,13 @@ autoplot.epi_workflow <- function(
119119
shift <- -as.numeric(old_name_y[2])
120120
new_name_y <- paste(old_name_y[-c(1:2)], collapse = "_")
121121
edf <- rename(edf, !!new_name_y := !!names(y))
122+
} else {
123+
new_name_y <- names(y)
124+
shift <- 0
122125
}
123126

124-
if (!is.null(shift)) {
125-
edf <- mutate(edf, time_value = time_value + shift)
126-
}
127-
other_keys <- setdiff(key_colnames(object), c("geo_value", "time_value"))
127+
edf <- mutate(edf, time_value = time_value + shift)
128+
other_keys <- key_colnames(object, exclude = c("geo_value", "time_value"))
128129
edf <- as_epi_df(edf,
129130
as_of = object$fit$meta$as_of,
130131
other_keys = other_keys

R/canned-epipred.R

+32-30
Original file line numberDiff line numberDiff line change
@@ -112,37 +112,39 @@ print.canned_epipred <- function(x, name, ...) {
112112
"At forecast date{?s}: {.val {fds}},",
113113
"For target date{?s}: {.val {tds}},"
114114
))
115-
fit_recipe <- extract_recipe(x$epi_workflow)
116-
if (detect_step(fit_recipe, "adjust_latency")) {
117-
is_adj_latency <- map_lgl(fit_recipe$steps, function(x) inherits(x, "step_adjust_latency"))
118-
latency_step <- fit_recipe$steps[is_adj_latency][[1]]
119-
# all steps after adjust_latency
120-
later_steps <- fit_recipe$steps[-(1:which(is_adj_latency))]
121-
if (latency_step$method == "extend_ahead") {
122-
step_names <- "step_epi_ahead"
123-
type_str <- "Aheads"
124-
} else if (latency_step$method == "extend_lags") {
125-
step_names <- "step_epi_lag"
126-
type_str <- "Lags"
127-
} else {
128-
step_names <- ""
129-
type_str <- "columns locf"
130-
}
131-
later_steps[[1]]$columns
132-
valid_columns <- later_steps %>%
133-
keep(function(x) inherits(x, step_names)) %>%
134-
purrr::map("columns") %>%
135-
reduce(c)
136-
latency_per_base_col <- latency_step$latency_table %>%
137-
filter(col_name %in% valid_columns) %>%
138-
mutate(latency = abs(latency))
139-
if (latency_step$method != "locf" && nrow(latency_per_base_col) > 1) {
140-
intro_text <- glue::glue("{type_str} adjusted per column: ")
141-
} else if (latency_step$method != "locf") {
142-
intro_text <- glue::glue("{type_str} adjusted for ")
115+
if ("actions" %in% names(x$pre) && "recipe" %in% names(x$pre$actions)) {
116+
fit_recipe <- extract_recipe(x$epi_workflow)
117+
if (detect_step(fit_recipe, "adjust_latency")) {
118+
is_adj_latency <- map_lgl(fit_recipe$steps, function(x) inherits(x, "step_adjust_latency"))
119+
latency_step <- fit_recipe$steps[is_adj_latency][[1]]
120+
# all steps after adjust_latency
121+
later_steps <- fit_recipe$steps[-(1:which(is_adj_latency))]
122+
if (latency_step$method == "extend_ahead") {
123+
step_names <- "step_epi_ahead"
124+
type_str <- "Aheads"
125+
} else if (latency_step$method == "extend_lags") {
126+
step_names <- "step_epi_lag"
127+
type_str <- "Lags"
128+
} else {
129+
step_names <- ""
130+
type_str <- "columns locf"
131+
}
132+
later_steps[[1]]$columns
133+
valid_columns <- later_steps %>%
134+
keep(function(x) inherits(x, step_names)) %>%
135+
purrr::map("columns") %>%
136+
reduce(c)
137+
latency_per_base_col <- latency_step$latency_table %>%
138+
filter(col_name %in% valid_columns) %>%
139+
mutate(latency = abs(latency))
140+
if (latency_step$method != "locf" && nrow(latency_per_base_col) > 1) {
141+
intro_text <- glue::glue("{type_str} adjusted per column: ")
142+
} else if (latency_step$method != "locf") {
143+
intro_text <- glue::glue("{type_str} adjusted for ")
144+
}
145+
latency_info <- paste0(intro_text, paste(apply(latency_per_base_col, 1, paste0, collapse = "="), collapse = ", "))
146+
cli::cli_ul(latency_info)
143147
}
144-
latency_info <- paste0(intro_text, paste(apply(latency_per_base_col, 1, paste0, collapse = "="), collapse = ", "))
145-
cli::cli_ul(latency_info)
146148
}
147149
cli::cli_text("")
148150
}

0 commit comments

Comments
 (0)