Skip to content

Commit e66e75f

Browse files
authored
Merge branch 'dev' into djm/plotting
2 parents 29673be + 98bdc85 commit e66e75f

26 files changed

+543
-145
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ Imports:
3131
cli,
3232
distributional,
3333
dplyr,
34-
fs,
3534
generics,
3635
ggplot2,
3736
glue,
3837
hardhat (>= 1.3.0),
39-
lifecycle,
4038
magrittr,
41-
methods,
4239
quantreg,
4340
recipes (>= 1.0.4),
4441
rlang,
@@ -47,20 +44,22 @@ Imports:
4744
tibble,
4845
tidyr,
4946
tidyselect,
50-
usethis,
47+
tsibble,
5148
vctrs,
5249
workflows (>= 1.0.0)
5350
Suggests:
5451
covidcast,
5552
data.table,
5653
epidatr (>= 1.0.0),
54+
fs,
5755
knitr,
5856
lubridate,
5957
poissonreg,
6058
ranger,
6159
RcppRoll,
6260
rmarkdown,
6361
testthat (>= 3.0.0),
62+
usethis,
6463
xgboost
6564
VignetteBuilder:
6665
knitr

NAMESPACE

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export(bake)
136136
export(cdc_baseline_args_list)
137137
export(cdc_baseline_forecaster)
138138
export(check_enough_train_data)
139-
export(create_layer)
140139
export(default_epi_recipe_blueprint)
141140
export(detect_layer)
142141
export(dist_quantiles)
@@ -223,9 +222,7 @@ importFrom(generics,fit)
223222
importFrom(ggplot2,autoplot)
224223
importFrom(hardhat,refresh_blueprint)
225224
importFrom(hardhat,run_mold)
226-
importFrom(lifecycle,deprecated)
227225
importFrom(magrittr,"%>%")
228-
importFrom(methods,is)
229226
importFrom(quantreg,rq)
230227
importFrom(recipes,bake)
231228
importFrom(recipes,prep)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
3434
- use `checkmate` for input validation
3535
- refactor quantile extrapolation (possibly creates different results)
3636
- add automatic plotting functionality `autoplot()`
37+
- force `target_date` + `forecast_date` handling to match the time_type of
38+
the epi_df. allows for annual and weekly data

R/bake.epi_recipe.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#' original columns in `new_data`.
1414
#' @importFrom rlang is_empty quos
1515
#' @importFrom tibble is_tibble as_tibble
16-
#' @importFrom methods is
1716
#' @rdname bake
1817
#' @export
1918
bake.epi_recipe <- function(object, new_data, ...) {

R/create-layer.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#' @inheritParams usethis::use_test
88
#'
99
#' @importFrom rlang %||%
10-
#' @export
11-
#'
10+
#' @noRd
11+
#' @keywords internal
1212
#' @examples
1313
#' \dontrun{
1414
#'

R/dist_quantiles.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ quantile_extrapolate <- function(x, tau_out, middle) {
233233
dplyr::arrange(q)
234234
}
235235
if (any(indl)) {
236-
qvals_out[indl] <- tail_extrapolate(tau_out[indl], head(qv, 2))
236+
qvals_out[indl] <- tail_extrapolate(tau_out[indl], utils::head(qv, 2))
237237
}
238238
if (any(indr)) {
239-
qvals_out[indr] <- tail_extrapolate(tau_out[indr], tail(qv, 2))
239+
qvals_out[indr] <- tail_extrapolate(tau_out[indr], utils::tail(qv, 2))
240240
}
241241
qvals_out
242242
}

R/epi_workflow.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ fit.epi_workflow <- function(object, data, ..., control = workflows::control_wor
251251
#' preds
252252
predict.epi_workflow <- function(object, new_data, ...) {
253253
if (!workflows::is_trained_workflow(object)) {
254-
rlang::abort(
255-
c("Can't predict on an untrained epi_workflow.",
256-
i = "Do you need to call `fit()`?"
257-
)
258-
)
254+
cli::cli_abort(c(
255+
"Can't predict on an untrained epi_workflow.",
256+
i = "Do you need to call `fit()`?"
257+
))
259258
}
260259
components <- list()
261260
components$mold <- workflows::extract_mold(object)

R/epipredict-package.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
## usethis namespace: start
22
#' @importFrom tibble tibble
3-
#' @importFrom rlang := !!
3+
#' @importFrom rlang := !! %||%
44
#' @importFrom stats poly predict lm residuals quantile
55
#' @importFrom cli cli_abort
6-
#' @importFrom lifecycle deprecated
76
#' @importFrom checkmate assert assert_character assert_int assert_scalar
87
#' assert_logical assert_numeric assert_number assert_integer
98
#' assert_integerish assert_date assert_function

R/flusight_hub_formatter.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ abbr_to_location <- function(abbr) {
5858
#' @examples
5959
#' if (require(dplyr)) {
6060
#' weekly_deaths <- case_death_rate_subset %>%
61+
#' filter(
62+
#' time_value >= as.Date("2021-09-01"),
63+
#' geo_value %in% c("ca", "ny", "dc", "ga", "vt")
64+
#' ) %>%
6165
#' select(geo_value, time_value, death_rate) %>%
6266
#' left_join(state_census %>% select(pop, abbr), by = c("geo_value" = "abbr")) %>%
6367
#' mutate(deaths = pmax(death_rate / 1e5 * pop * 7, 0)) %>%

R/layer_add_forecast_date.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
#' p3
6969
layer_add_forecast_date <-
7070
function(frosting, forecast_date = NULL, id = rand_id("add_forecast_date")) {
71+
arg_is_chr_scalar(id)
72+
arg_is_scalar(forecast_date, allow_null = TRUE)
73+
# can't validate the type of forecast_date until we know the time_type
7174
add_layer(
7275
frosting,
7376
layer_add_forecast_date_new(
@@ -78,8 +81,6 @@ layer_add_forecast_date <-
7881
}
7982

8083
layer_add_forecast_date_new <- function(forecast_date, id) {
81-
forecast_date <- arg_to_date(forecast_date, allow_null = TRUE)
82-
arg_is_chr_scalar(id)
8384
layer("add_forecast_date", forecast_date = forecast_date, id = id)
8485
}
8586

@@ -91,26 +92,25 @@ slather.layer_add_forecast_date <- function(object, components, workflow, new_da
9192
workflow$fit$meta$max_time_value,
9293
max(new_data$time_value)
9394
)
94-
object$forecast_date <- max_time_value
95+
forecast_date <- max_time_value
96+
} else {
97+
forecast_date <- object$forecast_date
9598
}
96-
as_of_pre <- attributes(workflows::extract_preprocessor(workflow)$template)$metadata$as_of
97-
as_of_fit <- workflow$fit$meta$as_of
98-
as_of_post <- attributes(new_data)$metadata$as_of
9999

100-
as_of_date <- as.Date(max(as_of_pre, as_of_fit, as_of_post))
101-
102-
if (object$forecast_date < as_of_date) {
103-
cli_warn(
104-
c("The forecast_date is less than the most ",
105-
"recent update date of the data: ",
106-
i = "forecast_date = {object$forecast_date} while data is from {as_of_date}."
107-
)
108-
)
109-
}
100+
expected_time_type <- attr(
101+
workflows::extract_preprocessor(workflow)$template, "metadata"
102+
)$time_type
103+
if (expected_time_type == "week") expected_time_type <- "day"
104+
validate_date(forecast_date, expected_time_type,
105+
call = expr(layer_add_forecast_date())
106+
)
107+
forecast_date <- coerce_time_type(forecast_date, expected_time_type)
108+
object$forecast_date <- forecast_date
110109
components$predictions <- dplyr::bind_cols(
111110
components$predictions,
112-
forecast_date = as.Date(object$forecast_date)
111+
forecast_date = forecast_date
113112
)
113+
114114
components
115115
}
116116

R/layer_add_target_date.R

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
#' p3
6464
layer_add_target_date <-
6565
function(frosting, target_date = NULL, id = rand_id("add_target_date")) {
66-
target_date <- arg_to_date(target_date, allow_null = TRUE)
6766
arg_is_chr_scalar(id)
67+
arg_is_scalar(target_date, allow_null = TRUE)
68+
# can't validate the type of target_date until we know the time_type
6869
add_layer(
6970
frosting,
7071
layer_add_target_date_new(
@@ -84,35 +85,39 @@ slather.layer_add_target_date <- function(object, components, workflow, new_data
8485
the_recipe <- workflows::extract_recipe(workflow)
8586
the_frosting <- extract_frosting(workflow)
8687

88+
expected_time_type <- attr(
89+
workflows::extract_preprocessor(workflow)$template, "metadata"
90+
)$time_type
91+
if (expected_time_type == "week") expected_time_type <- "day"
92+
8793
if (!is.null(object$target_date)) {
88-
target_date <- as.Date(object$target_date)
89-
} else { # null target date case
90-
if (detect_layer(the_frosting, "layer_add_forecast_date") &&
91-
!is.null(extract_argument(
92-
the_frosting,
93-
"layer_add_forecast_date", "forecast_date"
94+
target_date <- object$target_date
95+
validate_date(target_date, expected_time_type,
96+
call = expr(layer_add_target_date())
97+
)
98+
target_date <- coerce_time_type(target_date, expected_time_type)
99+
} else if (
100+
detect_layer(the_frosting, "layer_add_forecast_date") &&
101+
!is.null(forecast_date <- extract_argument(
102+
the_frosting, "layer_add_forecast_date", "forecast_date"
94103
))) {
95-
forecast_date <- extract_argument(
96-
the_frosting,
97-
"layer_add_forecast_date", "forecast_date"
98-
)
99-
100-
ahead <- extract_argument(the_recipe, "step_epi_ahead", "ahead")
101-
102-
target_date <- forecast_date + ahead
103-
} else {
104-
max_time_value <- max(
105-
workflows::extract_preprocessor(workflow)$max_time_value,
106-
workflow$fit$meta$max_time_value,
107-
max(new_data$time_value)
108-
)
109-
110-
ahead <- extract_argument(the_recipe, "step_epi_ahead", "ahead")
111-
112-
target_date <- max_time_value + ahead
113-
}
104+
validate_date(forecast_date, expected_time_type,
105+
call = expr(layer_add_forecast_date())
106+
)
107+
forecast_date <- coerce_time_type(forecast_date, expected_time_type)
108+
ahead <- extract_argument(the_recipe, "step_epi_ahead", "ahead")
109+
target_date <- forecast_date + ahead
110+
} else {
111+
max_time_value <- max(
112+
workflows::extract_preprocessor(workflow)$max_time_value,
113+
workflow$fit$meta$max_time_value,
114+
max(new_data$time_value)
115+
)
116+
ahead <- extract_argument(the_recipe, "step_epi_ahead", "ahead")
117+
target_date <- max_time_value + ahead
114118
}
115119

120+
object$target_date <- target_date
116121
components$predictions <- dplyr::bind_cols(components$predictions,
117122
target_date = target_date
118123
)

R/layer_population_scaling.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ slather.layer_population_scaling <-
144144
length(object$df_pop_col) == 1
145145
)
146146

147+
if (is.null(object$by)) {
148+
object$by <- intersect(
149+
kill_time_value(epi_keys(components$predictions)),
150+
colnames(dplyr::select(object$df, !object$df_pop_col))
151+
)
152+
}
147153
try_join <- try(
148154
dplyr::left_join(components$predictions, object$df,
149155
by = object$by
@@ -157,8 +163,8 @@ slather.layer_population_scaling <-
157163
))
158164
}
159165

160-
object$df <- object$df %>%
161-
dplyr::mutate(dplyr::across(tidyselect::where(is.character), tolower))
166+
# object$df <- object$df %>%
167+
# dplyr::mutate(dplyr::across(tidyselect::where(is.character), tolower))
162168
pop_col <- rlang::sym(object$df_pop_col)
163169
exprs <- rlang::expr(c(!!!object$terms))
164170
pos <- tidyselect::eval_select(exprs, components$predictions)

R/pivot_quantiles.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ pivot_quantiles_wider <- function(.data, ...) {
143143
}
144144

145145
pivot_quantiles <- function(.data, ...) {
146-
lifecycle::deprecate_stop("0.0.6", "pivot_quantiles()", "pivot_quantiles_wider()")
146+
msg <- c(
147+
"{.fn pivot_quantiles} was deprecated in {.pkg epipredict} 0.0.6",
148+
i = "Please use {.fn pivot_quantiles_wider} instead."
149+
)
150+
deprecate_stop(msg)
147151
}
148152

149153
validate_pivot_quantiles <- function(.data, ...) {

0 commit comments

Comments
 (0)