Skip to content

Commit 64a820d

Browse files
authored
Merge pull request #254 from cmu-delphi/v0.0.6-cleanup
V0.0.6 cleanup
2 parents 8c72690 + 9206afa commit 64a820d

4 files changed

+60
-54
lines changed

R/cdc_baseline_forecaster.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ cdc_baseline_forecaster <- function(
161161
#' cdc_baseline_args_list(quantile_levels = c(.1, .3, .7, .9), n_training = 120)
162162
cdc_baseline_args_list <- function(
163163
data_frequency = "1 week",
164-
aheads = 1:4,
164+
aheads = 1:5,
165165
n_training = Inf,
166166
forecast_date = NULL,
167167
quantile_levels = c(.01, .025, 1:19 / 20, .975, .99),
168-
nsims = 1e3L,
168+
nsims = 1e5L,
169169
symmetrize = TRUE,
170170
nonneg = TRUE,
171171
quantile_by_key = "geo_value",

R/flusight_hub_formatter.R

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
abbr_to_fips <- function(abbr) {
2-
fi <- dplyr::left_join(
3-
tibble::tibble(abbr = tolower(abbr)),
4-
state_census,
5-
by = "abbr"
6-
) %>%
7-
dplyr::mutate(fips = as.character(fips), fips = case_when(
8-
fips == "0" ~ "US",
9-
nchar(fips) < 2L ~ paste0("0", fips),
10-
TRUE ~ fips
11-
)) %>%
12-
pull(.data$fips)
13-
names(fi) <- NULL
14-
fi
1+
location_to_abbr <- function(location) {
2+
dictionary <-
3+
state_census %>%
4+
dplyr::mutate(fips = sprintf("%02d", fips)) %>%
5+
dplyr::transmute(
6+
location = dplyr::case_match(fips, "00" ~ "US", .default = fips),
7+
abbr
8+
)
9+
dictionary$abbr[match(location, dictionary$location)]
1510
}
1611

12+
abbr_to_location <- function(abbr) {
13+
dictionary <-
14+
state_census %>%
15+
dplyr::mutate(fips = sprintf("%02d", fips)) %>%
16+
dplyr::transmute(
17+
location = dplyr::case_match(fips, "00" ~ "US", .default = fips),
18+
abbr
19+
)
20+
dictionary$location[match(abbr, dictionary$abbr)]
21+
}
22+
23+
24+
25+
1726
#' Format predictions for submission to FluSight forecast Hub
1827
#'
1928
#' This function converts predictions from any of the included forecasters into
@@ -47,22 +56,23 @@ abbr_to_fips <- function(abbr) {
4756
#' @export
4857
#'
4958
#' @examples
50-
#' library(dplyr)
51-
#' weekly_deaths <- case_death_rate_subset %>%
52-
#' select(geo_value, time_value, death_rate) %>%
53-
#' left_join(state_census %>% select(pop, abbr), by = c("geo_value" = "abbr")) %>%
54-
#' mutate(deaths = pmax(death_rate / 1e5 * pop * 7, 0)) %>%
55-
#' select(-pop, -death_rate) %>%
56-
#' group_by(geo_value) %>%
57-
#' epi_slide(~ sum(.$deaths), before = 6, new_col_name = "deaths") %>%
58-
#' ungroup() %>%
59-
#' filter(weekdays(time_value) == "Saturday")
59+
#' if (require(dplyr)) {
60+
#' weekly_deaths <- case_death_rate_subset %>%
61+
#' select(geo_value, time_value, death_rate) %>%
62+
#' left_join(state_census %>% select(pop, abbr), by = c("geo_value" = "abbr")) %>%
63+
#' mutate(deaths = pmax(death_rate / 1e5 * pop * 7, 0)) %>%
64+
#' select(-pop, -death_rate) %>%
65+
#' group_by(geo_value) %>%
66+
#' epi_slide(~ sum(.$deaths), before = 6, new_col_name = "deaths") %>%
67+
#' ungroup() %>%
68+
#' filter(weekdays(time_value) == "Saturday")
6069
#'
61-
#' cdc <- cdc_baseline_forecaster(weekly_deaths, "deaths")
62-
#' flusight_hub_formatter(cdc)
63-
#' flusight_hub_formatter(cdc, target = "wk inc covid deaths")
64-
#' flusight_hub_formatter(cdc, target = paste(horizon, "wk inc covid deaths"))
65-
#' flusight_hub_formatter(cdc, target = "wk inc covid deaths", output_type = "quantile")
70+
#' cdc <- cdc_baseline_forecaster(weekly_deaths, "deaths")
71+
#' flusight_hub_formatter(cdc)
72+
#' flusight_hub_formatter(cdc, target = "wk inc covid deaths")
73+
#' flusight_hub_formatter(cdc, target = paste(horizon, "wk inc covid deaths"))
74+
#' flusight_hub_formatter(cdc, target = "wk inc covid deaths", output_type = "quantile")
75+
#' }
6676
flusight_hub_formatter <- function(
6777
object, ...,
6878
.fcast_period = c("daily", "weekly")) {
@@ -93,11 +103,6 @@ flusight_hub_formatter.data.frame <- function(
93103
object <- object %>%
94104
# combine the predictions and the distribution
95105
dplyr::mutate(.pred_distn = nested_quantiles(.pred_distn)) %>%
96-
dplyr::rowwise() %>%
97-
dplyr::mutate(
98-
.pred_distn = list(add_row(.pred_distn, values = .pred, quantile_levels = NA)),
99-
.pred = NULL
100-
) %>%
101106
tidyr::unnest(.pred_distn) %>%
102107
# now we create the correct column names
103108
dplyr::rename(
@@ -106,7 +111,7 @@ flusight_hub_formatter.data.frame <- function(
106111
reference_date = forecast_date
107112
) %>%
108113
# convert to fips codes, and add any constant cols passed in ...
109-
dplyr::mutate(location = abbr_to_fips(tolower(geo_value)), geo_value = NULL)
114+
dplyr::mutate(location = abbr_to_location(tolower(geo_value)), geo_value = NULL)
110115

111116
# create target_end_date / horizon, depending on what is available
112117
pp <- ifelse(match.arg(.fcast_period) == "daily", 1L, 7L)

man/cdc_baseline_args_list.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/flusight_hub_formatter.Rd

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)