Skip to content

Commit 7a103fa

Browse files
authored
Merge pull request #435 from cmu-delphi/canned_median_quantile
Default quantiles match autoplot, always include `0.5`
2 parents 18d1725 + 10b5b61 commit 7a103fa

30 files changed

+273
-185
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: epipredict
22
Title: Basic epidemiology forecasting methods
3-
Version: 0.1.7
3+
Version: 0.1.8
44
Authors@R: c(
55
person("Daniel J.", "McDonald", , "[email protected]", role = c("aut", "cre")),
66
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
2525
- Shifting no columns results in no error for either `step_epi_ahead` and `step_epi_lag`
2626
- Quantiles produced by `grf` were sometimes out of order.
2727
- dist_quantiles can have all `NA` values without causing unrelated errors
28+
- adjust default quantiles throughout so that they match.
29+
- force `layer_residual_quantiles()` to always include `0.5`.
2830

2931
# epipredict 0.1
3032

R/arx_forecaster.R

+6-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ arx_fcast_epi_workflow <- function(
200200
} else {
201201
quantile_levels <- sort(compare_quantile_args(
202202
args_list$quantile_levels,
203-
rlang::eval_tidy(trainer$eng_args$quantiles) %||% c(.1, .5, .9),
203+
rlang::eval_tidy(trainer$eng_args$quantiles) %||%
204+
c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
204205
"grf"
205206
))
206207
trainer$eng_args$quantiles <- rlang::enquo(quantile_levels)
@@ -253,8 +254,8 @@ arx_fcast_epi_workflow <- function(
253254
#' the last day of data. For example, if the last day of data was 3 days ago,
254255
#' the ahead becomes `ahead+3`.
255256
#' - `"extend_lags"`: increase the lags so they're relative to the actual
256-
#' forecast date. For example, if the lags are `c(0,7,14)` and the last day of
257-
#' data was 3 days ago, the lags become `c(3,10,17)`.
257+
#' forecast date. For example, if the lags are `c(0, 7, 14)` and the last day of
258+
#' data was 3 days ago, the lags become `c(3, 10, 17)`.
258259
#' @param warn_latency by default, `step_adjust_latency` warns the user if the
259260
#' latency is large. If this is `FALSE`, that warning is turned off.
260261
#' @param quantile_levels Vector or `NULL`. A vector of probabilities to produce
@@ -295,7 +296,7 @@ arx_args_list <- function(
295296
target_date = NULL,
296297
adjust_latency = c("none", "extend_ahead", "extend_lags", "locf"),
297298
warn_latency = TRUE,
298-
quantile_levels = c(0.05, 0.95),
299+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
299300
symmetrize = TRUE,
300301
nonneg = TRUE,
301302
quantile_by_key = character(0L),
@@ -362,7 +363,7 @@ compare_quantile_args <- function(alist, tlist, train_method = c("qr", "grf")) {
362363
default_alist <- eval(formals(arx_args_list)$quantile_levels)
363364
default_tlist <- switch(train_method,
364365
"qr" = eval(formals(quantile_reg)$quantile_levels),
365-
"grf" = c(.1, .5, .9)
366+
"grf" = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95)
366367
)
367368
if (setequal(alist, default_alist)) {
368369
if (setequal(tlist, default_tlist)) {

R/autoplot.R

+11-12
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ ggplot2::autoplot
3939
#' step_epi_naomit()
4040
#'
4141
#' f <- frosting() %>%
42-
#' layer_residual_quantiles(
43-
#' quantile_levels = c(.025, .1, .25, .75, .9, .975)
44-
#' ) %>%
42+
#' layer_residual_quantiles() %>%
4543
#' layer_threshold(starts_with(".pred")) %>%
4644
#' layer_add_target_date()
4745
#'
@@ -85,7 +83,7 @@ NULL
8583
#' @rdname autoplot-epipred
8684
autoplot.epi_workflow <- function(
8785
object, predictions = NULL,
88-
.levels = c(.5, .8, .95), ...,
86+
.levels = c(.5, .8, .9), ...,
8987
.color_by = c("all_keys", "geo_value", "other_keys", ".response", "all", "none"),
9088
.facet_by = c(".response", "other_keys", "all_keys", "geo_value", "all", "none"),
9189
.base_color = "dodgerblue4",
@@ -183,7 +181,7 @@ autoplot.epi_workflow <- function(
183181
}
184182

185183
if (".pred" %in% names(predictions)) {
186-
ntarget_dates <- n_distinct(predictions$time_value)
184+
ntarget_dates <- dplyr::n_distinct(predictions$time_value)
187185
if (ntarget_dates > 1L) {
188186
bp <- bp +
189187
geom_line(
@@ -231,24 +229,25 @@ starts_with_impl <- function(x, vars) {
231229

232230
plot_bands <- function(
233231
base_plot, predictions,
234-
levels = c(.5, .8, .95),
232+
levels = c(.5, .8, .9),
235233
fill = "blue4",
236234
alpha = 0.6,
237235
linewidth = 0.05) {
238236
innames <- names(predictions)
239-
n <- length(levels)
240-
alpha <- alpha / (n - 1)
241-
l <- (1 - levels) / 2
242-
l <- c(rev(l), 1 - l)
237+
n_levels <- length(levels)
238+
alpha <- alpha / (n_levels - 1)
239+
# generate the corresponding level that is 1 - level
240+
levels <- (1 - levels) / 2
241+
levels <- c(rev(levels), 1 - levels)
243242

244243
ntarget_dates <- dplyr::n_distinct(predictions$time_value)
245244

246245
predictions <- predictions %>%
247-
mutate(.pred_distn = dist_quantiles(quantile(.pred_distn, l), l)) %>%
246+
mutate(.pred_distn = dist_quantiles(quantile(.pred_distn, levels), levels)) %>%
248247
pivot_quantiles_wider(.pred_distn)
249248
qnames <- setdiff(names(predictions), innames)
250249

251-
for (i in 1:n) {
250+
for (i in 1:n_levels) {
252251
bottom <- qnames[i]
253252
top <- rev(qnames)[i]
254253
if (i == 1) {

R/extract.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @examples
1414
#' f <- frosting() %>%
1515
#' layer_predict() %>%
16-
#' layer_residual_quantiles(quantile_levels = c(0.0275, 0.975), symmetrize = FALSE) %>%
16+
#' layer_residual_quantiles(symmetrize = FALSE) %>%
1717
#' layer_naomit(.pred)
1818
#'
1919
#' extract_argument(f, "layer_residual_quantiles", "symmetrize")

R/flatline_forecaster.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ flatline_args_list <- function(
108108
n_training = Inf,
109109
forecast_date = NULL,
110110
target_date = NULL,
111-
quantile_levels = c(0.05, 0.95),
111+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
112112
symmetrize = TRUE,
113113
nonneg = TRUE,
114114
quantile_by_key = character(0L),

R/layer_quantile_distn.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#' p
4545
layer_quantile_distn <- function(frosting,
4646
...,
47-
quantile_levels = c(.25, .75),
47+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
4848
truncate = c(-Inf, Inf),
4949
name = ".pred_distn",
5050
id = rand_id("quantile_distn")) {

R/layer_residual_quantiles.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#' @param frosting a `frosting` postprocessor
44
#' @param ... Unused, include for consistency with other layers.
55
#' @param quantile_levels numeric vector of probabilities with values in (0,1)
6-
#' referring to the desired quantile.
6+
#' referring to the desired quantile. Note that 0.5 will always be included
7+
#' even if left out by the user.
78
#' @param symmetrize logical. If `TRUE` then interval will be symmetric.
89
#' @param by_key A character vector of keys to group the residuals by before
910
#' calculating quantiles. The default, `c()` performs no grouping.
@@ -28,7 +29,7 @@
2829
#' f <- frosting() %>%
2930
#' layer_predict() %>%
3031
#' layer_residual_quantiles(
31-
#' quantile_levels = c(0.0275, 0.975),
32+
#' quantile_levels = c(0.025, 0.975),
3233
#' symmetrize = FALSE
3334
#' ) %>%
3435
#' layer_naomit(.pred)
@@ -48,7 +49,7 @@
4849
#' p2 <- forecast(wf2)
4950
layer_residual_quantiles <- function(
5051
frosting, ...,
51-
quantile_levels = c(0.05, 0.95),
52+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
5253
symmetrize = TRUE,
5354
by_key = character(0L),
5455
name = ".pred_distn",
@@ -59,6 +60,7 @@ layer_residual_quantiles <- function(
5960
arg_is_chr(by_key, allow_empty = TRUE)
6061
arg_is_probabilities(quantile_levels)
6162
arg_is_lgl(symmetrize)
63+
quantile_levels <- sort(unique(c(0.5, quantile_levels)))
6264
add_layer(
6365
frosting,
6466
layer_residual_quantiles_new(

R/make_grf_quantiles.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ make_grf_quantiles <- function() {
141141
data = c(x = "X", y = "Y"),
142142
func = c(pkg = "grf", fun = "quantile_forest"),
143143
defaults = list(
144-
quantiles = c(0.1, 0.5, 0.9),
144+
quantiles = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
145145
num.threads = 1L,
146146
seed = rlang::expr(stats::runif(1, 0, .Machine$integer.max))
147147
)

R/make_quantile_reg.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @param engine Character string naming the fitting function. Currently, only
1313
#' "rq" and "grf" are supported.
1414
#' @param quantile_levels A scalar or vector of values in (0, 1) to determine which
15-
#' quantiles to estimate (default is 0.5).
15+
#' quantiles to estimate (default is the set 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95).
1616
#' @param method A fitting method used by [quantreg::rq()]. See the
1717
#' documentation for a list of options.
1818
#'
@@ -27,7 +27,9 @@
2727
#' rq_spec <- quantile_reg(quantile_levels = c(.2, .8)) %>% set_engine("rq")
2828
#' ff <- rq_spec %>% fit(y ~ ., data = tib)
2929
#' predict(ff, new_data = tib)
30-
quantile_reg <- function(mode = "regression", engine = "rq", quantile_levels = 0.5, method = "br") {
30+
quantile_reg <- function(mode = "regression", engine = "rq",
31+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
32+
method = "br") {
3133
# Check for correct mode
3234
if (mode != "regression") {
3335
cli_abort("`mode` must be 'regression'")

R/make_smooth_quantile_reg.R

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
#' the [tidymodels](https://www.tidymodels.org/) framework. Currently, the
66
#' only supported engine is [smoothqr::smooth_qr()].
77
#'
8-
#' @param mode A single character string for the type of model.
9-
#' The only possible value for this model is "regression".
10-
#' @param engine Character string naming the fitting function. Currently, only
11-
#' "smooth_qr" is supported.
12-
#' @param quantile_levels A scalar or vector of values in (0, 1) to determine which
13-
#' quantiles to estimate (default is 0.5).
8+
#' @inheritParams quantile_reg
149
#' @param outcome_locations Defaults to the vector `1:ncol(y)` but if the
1510
#' responses are observed at a different spacing (or appear in a different
1611
#' order), that information should be used here. This
@@ -76,7 +71,7 @@ smooth_quantile_reg <- function(
7671
mode = "regression",
7772
engine = "smoothqr",
7873
outcome_locations = NULL,
79-
quantile_levels = 0.5,
74+
quantile_levels = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95),
8075
degree = 3L) {
8176
# Check for correct mode
8277
if (mode != "regression") cli_abort("`mode` must be 'regression'")

man/arx_args_list.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/arx_class_args_list.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/autoplot-epipred.Rd

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extract_argument.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/flatline_args_list.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/grf_quantiles.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_quantile_distn.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_residual_quantiles.Rd

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/quantile_reg.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/smooth_quantile_reg.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)