|
1 |
| -test_that("scaled_pop", { |
2 |
| - library(epipredict) |
3 |
| - jhu <- case_death_rate_subset %>% |
4 |
| - dplyr::filter(time_value >= as.Date("2021-12-01")) |
5 |
| - # the as_of for this is wildly far in the future |
6 |
| - attributes(jhu)$metadata$as_of <- max(jhu$time_value) + 3 |
7 |
| - expect_warning(res <- scaled_pop(jhu, "case_rate", c("death_rate"), -2L)) |
8 |
| - expect_equal( |
9 |
| - names(res), |
10 |
| - c("geo_value", "forecast_date", "target_end_date", "quantile", "value") |
11 |
| - ) |
12 |
| - expect_true(all( |
13 |
| - res$target_end_date == |
14 |
| - as.Date("2022-01-01") |
15 |
| - )) |
16 |
| - # confirm scaling produces different results |
17 |
| - expect_warning(res_unscaled <- scaled_pop(jhu, |
18 |
| - "case_rate", |
19 |
| - c("death_rate"), |
20 |
| - -2L, |
21 |
| - pop_scaling = FALSE |
22 |
| - )) |
23 |
| - expect_false(res_unscaled %>% |
24 |
| - full_join(res, |
25 |
| - by = join_by(geo_value, forecast_date, target_end_date, quantile), |
26 |
| - suffix = c(".unscaled", ".scaled") |
27 |
| - ) %>% |
28 |
| - mutate(equal = value.unscaled == value.scaled) %>% |
29 |
| - summarize(all(equal)) %>% pull(`all(equal)`)) |
30 |
| - # confirming that it produces exactly the same result as arx_forecaster |
31 |
| - # test case where extra_sources is "empty" |
32 |
| - expect_warning(scaled_pop( |
33 |
| - jhu, |
34 |
| - "case_rate", |
35 |
| - c(""), |
36 |
| - 1L |
37 |
| - )) |
38 |
| - # test case where the epi_df is empty |
39 |
| - null_jhu <- jhu %>% filter(time_value < as.Date("0009-01-01")) |
40 |
| - expect_no_error(null_res <- scaled_pop(null_jhu, "case_rate", c("death_rate"))) |
41 |
| - null_res <- scaled_pop(null_jhu, "case_rate", c("death_rate")) |
42 |
| - expect_identical(names(null_res), names(res)) |
43 |
| - expect_equal(nrow(null_res), 0) |
44 |
| - expect_identical(null_res, tibble(geo_value = character(), forecast_date = Date(), target_end_date = Date(), quantile = numeric(), value = numeric())) |
45 |
| -}) |
| 1 | +# TODO better way to do this than copypasta |
| 2 | +forecasters <- list( |
| 3 | + c("scaled_pop", scaled_pop), |
| 4 | + c("flatline_fc", flatline_fc) |
| 5 | +) |
| 6 | +forecaster <- c("flatline", flatline_fc) |
| 7 | +for (forecaster in forecasters) { |
| 8 | + test_that(forecaster[[1]], { |
| 9 | + jhu <- case_death_rate_subset %>% |
| 10 | + dplyr::filter(time_value >= as.Date("2021-12-01")) |
| 11 | + # the as_of for this is wildly far in the future |
| 12 | + attributes(jhu)$metadata$as_of <- max(jhu$time_value) + 3 |
| 13 | + res <- forecaster[[2]](jhu, "case_rate", c("death_rate"), -2L) |
| 14 | + expect_equal( |
| 15 | + names(res), |
| 16 | + c("geo_value", "forecast_date", "target_end_date", "quantile", "value") |
| 17 | + ) |
| 18 | + expect_true(all( |
| 19 | + res$target_end_date == |
| 20 | + as.Date("2022-01-01") |
| 21 | + )) |
| 22 | + # any forecaster specific tests |
| 23 | + if (forecaster[[1]] == "scaled_pop") { |
| 24 | + # confirm scaling produces different results |
| 25 | + res_unscaled <- forecaster[[2]](jhu, |
| 26 | + "case_rate", |
| 27 | + c("death_rate"), |
| 28 | + -2L, |
| 29 | + pop_scaling = FALSE |
| 30 | + ) |
| 31 | + expect_false(res_unscaled %>% |
| 32 | + full_join(res, |
| 33 | + by = join_by(geo_value, forecast_date, target_end_date, quantile), |
| 34 | + suffix = c(".unscaled", ".scaled") |
| 35 | + ) %>% |
| 36 | + mutate(equal = value.unscaled == value.scaled) %>% |
| 37 | + summarize(all(equal)) %>% pull(`all(equal)`)) |
| 38 | + } |
| 39 | + # TODO confirming that it produces exactly the same result as arx_forecaster |
| 40 | + # test case where extra_sources is "empty" |
| 41 | + forecaster[[2]]( |
| 42 | + jhu, |
| 43 | + "case_rate", |
| 44 | + c(""), |
| 45 | + 1L |
| 46 | + ) |
| 47 | + # test case where the epi_df is empty |
| 48 | + null_jhu <- jhu %>% filter(time_value < as.Date("0009-01-01")) |
| 49 | + expect_no_error(null_res <- forecaster[[2]](null_jhu, "case_rate", c("death_rate"))) |
| 50 | + null_res <- forecaster[[2]](null_jhu, "case_rate", c("death_rate")) |
| 51 | + expect_identical(names(null_res), names(res)) |
| 52 | + expect_equal(nrow(null_res), 0) |
| 53 | + expect_identical(null_res, tibble(geo_value = character(), forecast_date = Date(), target_end_date = Date(), quantile = numeric(), value = numeric())) |
| 54 | + }) |
| 55 | +} |
0 commit comments