From 6ed660f26ece57528661235fd3037db2ce943b93 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Fri, 27 Nov 2026 02:57:20 -0800 Subject: [PATCH] fix: compute layer_residual_quantiles() by_key quantiles within each key --- DESCRIPTION | 2 +- NEWS.md | 4 ++ R/layer_residual_quantiles.R | 11 +++++- .../testthat/test-layer_residual_quantiles.R | 38 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5c5695cf..7ad7ff87 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: epipredict Title: Basic epidemiology forecasting methods -Version: 0.2.8 +Version: 0.2.9 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 a723b0eb..7b5e41b0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,10 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicate PR's. +# epipredict 0.2.9 + +- `layer_residual_quantiles(by_key = )` now computes residual quantiles within each key and matches them to predictions by key. Before, `flatline_forecaster(quantile_by_key = "geo_value")` pooled residuals across all locations, and grouped quantiles errored when a key had no prediction. + # epipredict 0.2.8 - Point the README contact link to the Delphi Contact Us page. diff --git a/R/layer_residual_quantiles.R b/R/layer_residual_quantiles.R index 26ec9a18..71127337 100644 --- a/R/layer_residual_quantiles.R +++ b/R/layer_residual_quantiles.R @@ -103,6 +103,7 @@ slather.layer_residual_quantiles <- r <- grab_residuals(the_fit, components) ## Handle any grouping requests + grps <- character(0L) if (length(object$by_key) > 0L) { key_cols <- bind_cols( geo_value = components$mold$extras$roles$geo_value, @@ -120,7 +121,7 @@ slather.layer_residual_quantiles <- r <- r %>% select(any_of(c(common, ".resid"))) common_in_r <- common[common %in% names(r)] if (length(common_in_r) == length(common)) { - r <- left_join(key_cols, r, by = common_in_r) + r <- group_by(r, !!!rlang::syms(common)) } else { cli_warn(paste( "Some grouping keys are not in data.frame returned by the", @@ -129,6 +130,7 @@ slather.layer_residual_quantiles <- r <- bind_cols(key_cols, select(r, .resid)) %>% group_by(!!!rlang::syms(common)) } + grps <- common } } @@ -146,6 +148,13 @@ slather.layer_residual_quantiles <- } estimate <- components$predictions$.pred + if (length(grps) > 0L) { + # match each prediction to the residual quantiles of its own key + r <- left_join( + select(components$predictions, all_of(grps)), r, + by = grps, relationship = "many-to-one" + ) + } res <- tibble(.pred_distn = r$dstn + estimate) res <- check_name( res, diff --git a/tests/testthat/test-layer_residual_quantiles.R b/tests/testthat/test-layer_residual_quantiles.R index 2421b8a1..02b36901 100644 --- a/tests/testthat/test-layer_residual_quantiles.R +++ b/tests/testthat/test-layer_residual_quantiles.R @@ -87,6 +87,44 @@ test_that("Grouping by keys is supported", { expect_false(all(pivot2$width == pivot2$width[1])) }) +test_that("by_key uses each key's own residuals with flatline", { + out <- flatline_forecaster( + jhu, "death_rate", + args_list = flatline_args_list( + quantile_levels = c(0.1, 0.9), nonneg = FALSE, + quantile_by_key = "geo_value" + ) + ) + res <- residuals(workflows::extract_fit_engine(out$epi_workflow)) + expected <- res %>% + dplyr::group_by(geo_value) %>% + dplyr::summarise(width = diff(quantile( + c(.resid, -.resid), c(0.1, 0.9), + na.rm = TRUE, names = FALSE + ))) + widths <- out$predictions %>% + pivot_quantiles_wider(.pred_distn) %>% + dplyr::transmute(geo_value, width = `0.9` - `0.1`) + expect_equal( + widths$width, + expected$width[match(widths$geo_value, expected$geo_value)] + ) +}) + +test_that("by_key matches residual quantiles to predictions by key", { + # ak has no forecast because its latest predictors are missing + jhu_lag <- jhu %>% + dplyr::filter(!(geo_value == "ak" & time_value > max(time_value) - 3)) + f <- frosting() %>% + layer_predict() %>% + layer_naomit(.pred) %>% + layer_residual_quantiles(by_key = "geo_value") + wf_lag <- epi_workflow(r, parsnip::linear_reg(), f) %>% fit(jhu_lag) + expect_warning(p <- forecast(wf_lag), "grouping keys") + expect_equal(p$geo_value, c("ca", "ny")) + expect_false(anyNA(as.matrix(p$.pred_distn))) +}) + test_that("Canned forecasters work with / without", { meta <- attr(jhu, "metadata") meta$as_of <- max(jhu$time_value)