Skip to content

Commit d2eae48

Browse files
committed
much renaming to quantile_values
1 parent d242eed commit d2eae48

17 files changed

+151
-138
lines changed

R/arx_forecaster.R

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#' out <- arx_forecaster(jhu, "death_rate",
3434
#' c("case_rate", "death_rate"),
3535
#' trainer = quantile_reg(),
36-
#' args_list = arx_args_list(levels = 1:9 / 10)
36+
#' args_list = arx_args_list(quantile_values = 1:9 / 10)
3737
#' )
3838
arx_forecaster <- function(epi_data,
3939
outcome,
@@ -99,7 +99,7 @@ arx_forecaster <- function(epi_data,
9999
#' arx_fcast_epi_workflow(jhu, "death_rate",
100100
#' c("case_rate", "death_rate"),
101101
#' trainer = quantile_reg(),
102-
#' args_list = arx_args_list(levels = 1:9 / 10)
102+
#' args_list = arx_args_list(quantile_values = 1:9 / 10)
103103
#' )
104104
arx_fcast_epi_workflow <- function(
105105
epi_data,
@@ -134,18 +134,20 @@ arx_fcast_epi_workflow <- function(
134134
# --- postprocessor
135135
f <- frosting() %>% layer_predict() # %>% layer_naomit()
136136
if (inherits(trainer, "quantile_reg")) {
137-
# add all levels to the forecaster and update postprocessor
138-
tau <- sort(compare_quantile_args(
139-
args_list$levels,
140-
rlang::eval_tidy(trainer$args$tau)
137+
# add all quantile_values to the forecaster and update postprocessor
138+
quantile_values <- sort(compare_quantile_args(
139+
args_list$quantile_values,
140+
rlang::eval_tidy(trainer$args$quantile_values)
141141
))
142-
args_list$levels <- tau
143-
trainer$args$tau <- rlang::enquo(tau)
144-
f <- layer_quantile_distn(f, levels = tau) %>% layer_point_from_distn()
142+
args_list$quantile_values <- quantile_values
143+
trainer$args$quantile_values <- rlang::enquo(quantile_values)
144+
f <- layer_quantile_distn(f, quantile_values = quantile_values) %>%
145+
layer_point_from_distn()
145146
} else {
146147
f <- layer_residual_quantiles(
147148
f,
148-
probs = args_list$levels, symmetrize = args_list$symmetrize,
149+
quantile_values = args_list$quantile_values,
150+
symmetrize = args_list$symmetrize,
149151
by_key = args_list$quantile_by_key
150152
)
151153
}
@@ -173,7 +175,7 @@ arx_fcast_epi_workflow <- function(
173175
#' The default `NULL` will attempt to determine this automatically.
174176
#' @param target_date Date. The date for which the forecast is intended.
175177
#' The default `NULL` will attempt to determine this automatically.
176-
#' @param levels Vector or `NULL`. A vector of probabilities to produce
178+
#' @param quantile_values Vector or `NULL`. A vector of probabilities to produce
177179
#' prediction intervals. These are created by computing the quantiles of
178180
#' training residuals. A `NULL` value will result in point forecasts only.
179181
#' @param symmetrize Logical. The default `TRUE` calculates
@@ -197,6 +199,7 @@ arx_fcast_epi_workflow <- function(
197199
#' create a prediction. For this reason, setting `nafill_buffer < min(lags)`
198200
#' will be treated as _additional_ allowed recent data rather than the
199201
#' total amount of recent data to examine.
202+
#' @param ... Space to handle future expansions (unused).
200203
#'
201204
#'
202205
#' @return A list containing updated parameter choices with class `arx_flist`.
@@ -205,18 +208,19 @@ arx_fcast_epi_workflow <- function(
205208
#' @examples
206209
#' arx_args_list()
207210
#' arx_args_list(symmetrize = FALSE)
208-
#' arx_args_list(levels = c(.1, .3, .7, .9), n_training = 120)
211+
#' arx_args_list(quantile_values = c(.1, .3, .7, .9), n_training = 120)
209212
arx_args_list <- function(
210213
lags = c(0L, 7L, 14L),
211214
ahead = 7L,
212215
n_training = Inf,
213216
forecast_date = NULL,
214217
target_date = NULL,
215-
levels = c(0.05, 0.95),
218+
quantile_values = c(0.05, 0.95),
216219
symmetrize = TRUE,
217220
nonneg = TRUE,
218221
quantile_by_key = character(0L),
219-
nafill_buffer = Inf) {
222+
nafill_buffer = Inf,
223+
...) {
220224
# error checking if lags is a list
221225
.lags <- lags
222226
if (is.list(lags)) lags <- unlist(lags)
@@ -227,7 +231,7 @@ arx_args_list <- function(
227231
arg_is_date(forecast_date, target_date, allow_null = TRUE)
228232
arg_is_nonneg_int(ahead, lags)
229233
arg_is_lgl(symmetrize, nonneg)
230-
arg_is_probabilities(levels, allow_null = TRUE)
234+
arg_is_probabilities(quantile_values, allow_null = TRUE)
231235
arg_is_pos(n_training)
232236
if (is.finite(n_training)) arg_is_pos_int(n_training)
233237
if (is.finite(nafill_buffer)) arg_is_pos_int(nafill_buffer, allow_null = TRUE)
@@ -238,7 +242,7 @@ arx_args_list <- function(
238242
lags = .lags,
239243
ahead,
240244
n_training,
241-
levels,
245+
quantile_values,
242246
forecast_date,
243247
target_date,
244248
symmetrize,
@@ -259,8 +263,8 @@ print.arx_fcast <- function(x, ...) {
259263
}
260264

261265
compare_quantile_args <- function(alist, tlist) {
262-
default_alist <- eval(formals(arx_args_list)$levels)
263-
default_tlist <- eval(formals(quantile_reg)$tau)
266+
default_alist <- eval(formals(arx_args_list)$quantile_values)
267+
default_tlist <- eval(formals(quantile_reg)$quantile_values)
264268
if (setequal(alist, default_alist)) {
265269
if (setequal(tlist, default_tlist)) {
266270
return(sort(unique(union(alist, tlist))))

R/canned-epipred.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ validate_forecaster_inputs <- function(epi_data, outcome, predictors) {
88
arg_is_chr(predictors)
99
arg_is_chr_scalar(outcome)
1010
if (!outcome %in% names(epi_data)) {
11-
cli::cli_abort("{outcome} was not found in the training data.")
11+
cli::cli_abort("{.var {outcome}} was not found in the training data.")
1212
}
1313
check <- hardhat::check_column_names(epi_data, predictors)
1414
if (!check$ok) {
1515
cli::cli_abort(c(
1616
"At least one predictor was not found in the training data.",
17-
"!" = "The following required columns are missing: {check$missing_names}."
17+
"!" = "The following required columns are missing: {.val {check$missing_names}}."
1818
))
1919
}
2020
invisible(TRUE)
@@ -41,8 +41,8 @@ arx_lags_validator <- function(predictors, lags) {
4141
predictors_miss <- setdiff(predictors, names(lags))
4242
cli::cli_abort(c(
4343
"If lags is a named list, then all predictors must be present.",
44-
i = "The predictors are '{predictors}'.",
45-
i = "So lags is missing '{predictors_miss}'."
44+
i = "The predictors are {.var {predictors}}.",
45+
i = "So lags is missing {.var {predictors_miss}}'."
4646
))
4747
}
4848
}

R/epipredict-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#' @importFrom tibble tibble
22
#' @importFrom rlang := !!
33
#' @importFrom stats poly predict lm residuals quantile
4+
#' @importFrom cli cli_abort
45
#' @import epiprocess parsnip
56
NULL

R/extract.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @examples
1414
#' f <- frosting() %>%
1515
#' layer_predict() %>%
16-
#' layer_residual_quantiles(probs = c(0.0275, 0.975), symmetrize = FALSE) %>%
16+
#' layer_residual_quantiles(quantile_values = c(0.0275, 0.975), symmetrize = FALSE) %>%
1717
#' layer_naomit(.pred)
1818
#'
1919
#' extract_argument(f, "layer_residual_quantiles", "symmetrize")
@@ -27,10 +27,10 @@ extract_argument.layer <- function(x, name, arg, ...) {
2727
arg_is_chr_scalar(name, arg)
2828
in_layer_name <- class(x)[1]
2929
if (name != in_layer_name) {
30-
cli_stop("Requested {name} not found. This is a(n) {in_layer_name}.")
30+
cli_abort("Requested {.val {name}} not found. This is a(n) {.cls {in_layer_name}}.")
3131
}
3232
if (!arg %in% names(x)) {
33-
cli_stop("Requested argument {arg} not found in {name}.")
33+
cli_abort("Requested argument {.val {arg}} not found in {.val {name}}.")
3434
}
3535
x[[arg]]
3636
}
@@ -41,10 +41,10 @@ extract_argument.step <- function(x, name, arg, ...) {
4141
arg_is_chr_scalar(name, arg)
4242
in_step_name <- class(x)[1]
4343
if (name != in_step_name) {
44-
cli_stop("Requested {name} not found. This is a {in_step_name}.")
44+
cli_abort("Requested {.val {name}} not found. This is a {.cls {in_step_name}}.")
4545
}
4646
if (!arg %in% names(x)) {
47-
cli_stop("Requested argument {arg} not found in {name}.")
47+
cli_abort("Requested argument {.val {arg}} not found in {.val {name}}.")
4848
}
4949
x[[arg]]
5050
}
@@ -55,7 +55,7 @@ extract_argument.recipe <- function(x, name, arg, ...) {
5555
step_names <- map_chr(x$steps, ~ class(.x)[1])
5656
has_step <- name %in% step_names
5757
if (!has_step) {
58-
cli_stop("recipe object does not contain a {name}.")
58+
cli_abort("recipe object does not contain a {.val {name}}.")
5959
}
6060
step_locations <- which(name == step_names)
6161
out <- map(x$steps[step_locations], extract_argument, name = name, arg = arg)
@@ -69,7 +69,7 @@ extract_argument.frosting <- function(x, name, arg, ...) {
6969
layer_names <- map_chr(x$layers, ~ class(.x)[1])
7070
has_layer <- name %in% layer_names
7171
if (!has_layer) {
72-
cli_stop("frosting object does not contain a {name} layer.")
72+
cli_abort("frosting object does not contain a {.val {name}}.")
7373
}
7474
layer_locations <- which(name == layer_names)
7575
out <- map(x$layers[layer_locations], extract_argument, name = name, arg = arg)
@@ -83,15 +83,15 @@ extract_argument.epi_workflow <- function(x, name, arg, ...) {
8383
type <- sub("_.*", "", name)
8484
if (type %in% c("check", "step")) {
8585
if (!workflows:::has_preprocessor_recipe(x)) {
86-
cli_stop("The workflow must have a recipe preprocessor.")
86+
cli_abort("The workflow must have a recipe preprocessor.")
8787
}
8888
out <- extract_argument(x$pre$actions$recipe$recipe, name, arg)
8989
}
9090
if (type %in% "layer") {
9191
out <- extract_argument(extract_frosting(x), name, arg)
9292
}
9393
if (!type %in% c("check", "step", "layer")) {
94-
cli_stop("{name} must begin with one of step, check, or layer")
94+
cli_abort("{.val {name}} must begin with one of step, check, or layer")
9595
}
9696
return(out)
9797
}

R/flatline_forecaster.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ flatline_forecaster <- function(
5858
f <- frosting() %>%
5959
layer_predict() %>%
6060
layer_residual_quantiles(
61-
probs = args_list$levels,
61+
quantile_values = args_list$quantile_values,
6262
symmetrize = args_list$symmetrize,
6363
by_key = args_list$quantile_by_key
6464
) %>%
@@ -101,7 +101,7 @@ flatline_forecaster <- function(
101101
#' @examples
102102
#' flatline_args_list()
103103
#' flatline_args_list(symmetrize = FALSE)
104-
#' flatline_args_list(levels = c(.1, .3, .7, .9), n_training = 120)
104+
#' flatline_args_list(quantile_values = c(.1, .3, .7, .9), n_training = 120)
105105
flatline_args_list <- function(
106106
ahead = 7L,
107107
n_training = Inf,
@@ -111,14 +111,15 @@ flatline_args_list <- function(
111111
symmetrize = TRUE,
112112
nonneg = TRUE,
113113
quantile_by_key = character(0L),
114-
nafill_buffer = Inf) {
114+
nafill_buffer = Inf,
115+
...) {
115116
arg_is_scalar(ahead, n_training)
116117
arg_is_chr(quantile_by_key, allow_empty = TRUE)
117118
arg_is_scalar(forecast_date, target_date, allow_null = TRUE)
118119
arg_is_date(forecast_date, target_date, allow_null = TRUE)
119120
arg_is_nonneg_int(ahead)
120121
arg_is_lgl(symmetrize, nonneg)
121-
arg_is_probabilities(levels, allow_null = TRUE)
122+
arg_is_probabilities(quantile_values, allow_null = TRUE)
122123
arg_is_pos(n_training)
123124
if (is.finite(n_training)) arg_is_pos_int(n_training)
124125
if (is.finite(nafill_buffer)) arg_is_pos_int(nafill_buffer, allow_null = TRUE)
@@ -129,7 +130,7 @@ flatline_args_list <- function(
129130
n_training,
130131
forecast_date,
131132
target_date,
132-
levels,
133+
quantile_values,
133134
symmetrize,
134135
nonneg,
135136
quantile_by_key,

R/layer_point_from_distn.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#' step_epi_ahead(death_rate, ahead = 7) %>%
2525
#' step_epi_naomit()
2626
#'
27-
#' wf <- epi_workflow(r, quantile_reg(tau = c(.25, .5, .75))) %>% fit(jhu)
27+
#' wf <- epi_workflow(r, quantile_reg(quantile_values = c(.25, .5, .75))) %>% fit(jhu)
2828
#'
2929
#' latest <- get_test_data(recipe = r, x = jhu)
3030
#'

R/layer_quantile_distn.R

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#' This function calculates quantiles when the prediction was _distributional_.
44
#' Currently, the only distributional engine is `quantile_reg()`.
55
#' If this engine is used, then this layer will grab out estimated (or extrapolated)
6-
#' quantiles at the requested levels.
6+
#' quantiles at the requested quantile values.
77
#'
88
#' @param frosting a `frosting` postprocessor
99
#' @param ... Unused, include for consistency with other layers.
10-
#' @param levels a vector of probabilities (quantiles) to extract
10+
#' @param quantile_values a vector of probabilities to extract
1111
#' @param truncate Do we truncate the distribution to an interval
1212
#' @param name character. The name for the output column.
1313
#' @param id a random id string
@@ -25,7 +25,8 @@
2525
#' step_epi_ahead(death_rate, ahead = 7) %>%
2626
#' step_epi_naomit()
2727
#'
28-
#' wf <- epi_workflow(r, quantile_reg(tau = c(.25, .5, .75))) %>% fit(jhu)
28+
#' wf <- epi_workflow(r, quantile_reg(quantile_values = c(.25, .5, .75))) %>%
29+
#' fit(jhu)
2930
#'
3031
#' latest <- get_test_data(recipe = r, x = jhu)
3132
#'
@@ -39,31 +40,31 @@
3940
#' p
4041
layer_quantile_distn <- function(frosting,
4142
...,
42-
levels = c(.25, .75),
43+
quantile_values = c(.25, .75),
4344
truncate = c(-Inf, Inf),
4445
name = ".pred_distn",
4546
id = rand_id("quantile_distn")) {
4647
rlang::check_dots_empty()
4748
arg_is_chr_scalar(name, id)
48-
arg_is_probabilities(levels)
49+
arg_is_probabilities(quantile_values)
4950
stopifnot(
5051
length(truncate) == 2L, is.numeric(truncate), truncate[1] < truncate[2]
5152
)
5253

5354
add_layer(
5455
frosting,
5556
layer_quantile_distn_new(
56-
levels = levels,
57+
quantile_values = quantile_values,
5758
truncate = truncate,
5859
name = name,
5960
id = id
6061
)
6162
)
6263
}
6364

64-
layer_quantile_distn_new <- function(levels, truncate, name, id) {
65+
layer_quantile_distn_new <- function(quantile_values, truncate, name, id) {
6566
layer("quantile_distn",
66-
levels = levels,
67+
quantile_values = quantile_values,
6768
truncate = truncate,
6869
name = name,
6970
id = id
@@ -75,14 +76,15 @@ slather.layer_quantile_distn <-
7576
function(object, components, workflow, new_data, ...) {
7677
dstn <- components$predictions$.pred
7778
if (!inherits(dstn, "distribution")) {
78-
rlang::abort(
79-
c(
80-
"`layer_quantile_distn` requires distributional predictions.",
81-
"These are of class {class(dstn)}."
82-
)
83-
)
79+
cli_abort(c(
80+
"`layer_quantile_distn()` requires distributional predictions.",
81+
"These are of class {.cls {class(dstn)}}."
82+
))
8483
}
85-
dstn <- dist_quantiles(quantile(dstn, object$levels), object["levels"])
84+
dstn <- dist_quantiles(
85+
quantile(dstn, object$quantile_values),
86+
object$quantile_values
87+
)
8688

8789
truncate <- object$truncate
8890
if (!all(is.infinite(truncate))) {
@@ -100,9 +102,9 @@ print.layer_quantile_distn <- function(
100102
title <- "Creating predictive quantiles"
101103
td <- "<calculated>"
102104
td <- rlang::enquos(td)
103-
ext <- x$levels
105+
ext <- x$quantile_values
104106
print_layer(td,
105-
title = title, width = width, conjunction = "levels",
107+
title = title, width = width, conjunction = "quantile_values",
106108
extra_text = ext
107109
)
108110
}

0 commit comments

Comments
 (0)