Skip to content

Commit abcdd0b

Browse files
authored
Merge pull request #429 from cmu-delphi/distn_quantileNA
fix for dist_quantile all `NA` s
2 parents 2811d2f + 964d800 commit abcdd0b

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
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.5
3+
Version: 0.1.6
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

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
2323
## Bug fixes
2424
- Shifting no columns results in no error for either `step_epi_ahead` and `step_epi_lag`
2525
- Quantiles produced by `grf` were sometimes out of order.
26+
- dist_quantiles can have all `NA` values without causing unrelated errors
2627

2728
# epipredict 0.1
2829

R/dist_quantiles.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ is_dist_quantiles <- function(x) {
128128
median.dist_quantiles <- function(x, na.rm = FALSE, ..., middle = c("cubic", "linear")) {
129129
quantile_levels <- field(x, "quantile_levels")
130130
values <- field(x, "values")
131+
# we have exactly that quantile
131132
if (0.5 %in% quantile_levels) {
132133
return(values[match(0.5, quantile_levels)])
133134
}
134-
if (length(quantile_levels) < 2 || min(quantile_levels) > 0.5 || max(quantile_levels) < 0.5) {
135+
# if there's only 1 quantile_level (and it isn't 0.5), or the smallest quantile is larger than 0.5 or the largest smaller than 0.5, or if every value is NA, return NA
136+
if (length(quantile_levels) < 2 || min(quantile_levels) > 0.5 || max(quantile_levels) < 0.5 || all(is.na(values))) {
135137
return(NA)
136138
}
137139
if (length(quantile_levels) < 3 || min(quantile_levels) > .25 || max(quantile_levels) < .75) {
@@ -161,6 +163,9 @@ quantile_extrapolate <- function(x, tau_out, middle) {
161163
tau <- field(x, "quantile_levels")
162164
qvals <- field(x, "values")
163165
nas <- is.na(qvals)
166+
if (all(nas)) {
167+
return(rep(NA, times = length(tau_out)))
168+
}
164169
qvals_out <- rep(NA, length(tau_out))
165170
qvals <- qvals[!nas]
166171
tau <- tau[!nas]

man/step_adjust_latency.Rd

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

tests/testthat/test-dist_quantiles.R

+11
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,14 @@ test_that("arithmetic works on quantiles", {
110110
expect_snapshot(error = TRUE, sum(dstn))
111111
expect_snapshot(error = TRUE, suppressWarnings(dstn + distributional::dist_normal()))
112112
})
113+
114+
test_that("quantile.dist_quantile works for NA vectors", {
115+
distn <- dist_quantiles(
116+
list(c(NA, NA)),
117+
list(1:2 / 3)
118+
)
119+
expect_true(is.na(quantile(distn, p = 0.5)))
120+
expect_true(is.na(median(distn)))
121+
expect_true(is.na(mean(distn)))
122+
expect_equal(format(distn), "quantiles(NA)[2]")
123+
})

vignettes/backtesting.Rmd

+7-8
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ canada_archive_faux <- epix_as_of(canada_archive, canada_archive$versions_end) %
385385
smooth_cases <- function(epi_df) {
386386
epi_df %>%
387387
group_by(geo_value) %>%
388-
epi_slide_mean("case_rate", .window_size = 7, na.rm = TRUE) %>%
389-
rename(cr_7dav = slide_value_case_rate)
388+
epi_slide_mean("case_rate", .window_size = 7, na.rm = TRUE, .suffix = "_{.n}dav")
390389
}
391390
forecast_dates <- seq.Date(
392391
from = min(canada_archive$DT$version),
@@ -401,8 +400,8 @@ canada_forecasts <- bind_rows(
401400
~ forecast_k_week_ahead(
402401
canada_archive_faux,
403402
ahead = .x,
404-
outcome = "cr_7dav",
405-
predictors = "cr_7dav",
403+
outcome = "case_rate_7dav",
404+
predictors = "case_rate_7dav",
406405
forecast_dates = forecast_dates,
407406
process_data = smooth_cases
408407
) %>% mutate(version_aware = FALSE)
@@ -412,8 +411,8 @@ canada_forecasts <- bind_rows(
412411
~ forecast_k_week_ahead(
413412
canada_archive,
414413
ahead = .x,
415-
outcome = "cr_7dav",
416-
predictors = "cr_7dav",
414+
outcome = "case_rate_7dav",
415+
predictors = "case_rate_7dav",
417416
forecast_dates = forecast_dates,
418417
process_data = smooth_cases
419418
) %>% mutate(version_aware = TRUE)
@@ -435,15 +434,15 @@ case_rate_data <- bind_rows(
435434
~ canada_archive %>%
436435
epix_as_of(.x) %>%
437436
smooth_cases() %>%
438-
mutate(case_rate = cr_7dav, version = .x)
437+
mutate(case_rate = case_rate_7dav, version = .x)
439438
) %>%
440439
bind_rows() %>%
441440
mutate(version_aware = TRUE),
442441
# Latest data for the version-unaware forecasts
443442
canada_archive %>%
444443
epix_as_of(doctor_visits$versions_end) %>%
445444
smooth_cases() %>%
446-
mutate(case_rate = cr_7dav, version_aware = FALSE)
445+
mutate(case_rate = case_rate_7dav, version_aware = FALSE)
447446
) %>%
448447
filter(geo_value == geo_choose)
449448

0 commit comments

Comments
 (0)