Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion R/layer_residual_quantiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -129,6 +130,7 @@ slather.layer_residual_quantiles <-
r <- bind_cols(key_cols, select(r, .resid)) %>%
group_by(!!!rlang::syms(common))
}
grps <- common
}
}

Expand All @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-layer_residual_quantiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down