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 )]
15
10
}
16
11
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
+
17
26
# ' Format predictions for submission to FluSight forecast Hub
18
27
# '
19
28
# ' This function converts predictions from any of the included forecasters into
@@ -47,22 +56,23 @@ abbr_to_fips <- function(abbr) {
47
56
# ' @export
48
57
# '
49
58
# ' @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")
60
69
# '
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
+ # ' }
66
76
flusight_hub_formatter <- function (
67
77
object , ... ,
68
78
.fcast_period = c(" daily" , " weekly" )) {
@@ -93,11 +103,6 @@ flusight_hub_formatter.data.frame <- function(
93
103
object <- object %> %
94
104
# combine the predictions and the distribution
95
105
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
- ) %> %
101
106
tidyr :: unnest(.pred_distn ) %> %
102
107
# now we create the correct column names
103
108
dplyr :: rename(
@@ -106,7 +111,7 @@ flusight_hub_formatter.data.frame <- function(
106
111
reference_date = forecast_date
107
112
) %> %
108
113
# 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 )
110
115
111
116
# create target_end_date / horizon, depending on what is available
112
117
pp <- ifelse(match.arg(.fcast_period ) == " daily" , 1L , 7L )
0 commit comments