44# ' @param location_abbr Character string indicating abbreviation of state
55# ' @param forecast_date Character string or date indicating the date of
66# ' forecast in YYYY-MM-DD
7- # ' @param forecast_horizon Integer indicating the number of days to forecast . Default is `28`.
7+ # ' @param forecast_horizon Forecast horizon . Default is `28`.
88# ' @param filepath_name Name of directory to save the raw input wastewater data.
99# ' @param right_trunc Boolean indicating whether to use the real-time, right
1010# ' truncated data or the final corrected data. Default is `FALSE` indicating
1616# ' @importFrom readr read_csv
1717# ' @importFrom lubridate ymd days
1818get_hosp_for_eval <- function (location_name ,
19- location_abbr ,
20- forecast_date ,
21- forecast_horizon = 28 ,
22- filepath_name = file.path(" input" , " data" , " hosp" )) {
23- if (file.exists(file.path(filepath_name , " RKI_hosp_adj.csv" ))) {
24- RKI_hosp_adj <- read_csv(file.path(filepath_name , " RKI_hosp_adj.csv" ))
25- } else {
26- RKI_hosp_adj <- read_csv(" https://raw.githubusercontent.com/robert-koch-institut/COVID-19-Hospitalisierungen_in_Deutschland/refs/heads/main/Aktuell_Deutschland_adjustierte-COVID-19-Hospitalisierungen.csv" ) # nolint
27- dir_create(filepath_name )
28- write_csv(RKI_hosp_adj , file.path(filepath_name , " RKI_hosp_adj.csv" ))
29- # load in initial values
30- }
31- # Add a month to get out of the holiday period.
32- date_to_get_init_vals <- min(RKI_hosp_adj $ Datum ) + days(30 )
33- init_vals <- get_initial_values(
34- start_date_RKI_data = date_to_get_init_vals
35- ) | >
36- filter(age_group == " 00+" ) | >
37- mutate(location = stringr :: str_sub(location , - 2 )) | >
38- filter(location == location_abbr ) | >
39- arrange(- desc(date )) | >
40- pull(value )
19+ location_abbr ,
20+ forecast_date ,
21+ forecast_horizon = 28 ,
22+ filepath_name =
23+ file.path(" input" , " data" , " hosp" )) {
24+ if (file.exists(file.path(filepath_name , " RKI_hosp_adj.csv" ))) {
25+ RKI_hosp_adj <- read_csv(file.path(filepath_name , " RKI_hosp_adj.csv" ))
26+ } else {
27+ RKI_hosp_adj <- read_csv(" https://raw.githubusercontent.com/robert-koch-institut/COVID-19-Hospitalisierungen_in_Deutschland/refs/heads/main/Aktuell_Deutschland_adjustierte-COVID-19-Hospitalisierungen.csv" ) # nolint
28+ dir_create(filepath_name )
29+ write_csv(RKI_hosp_adj , file.path(filepath_name , " RKI_hosp_adj.csv" ))
30+ # load in initial values
31+ }
32+ # Add a month to get out of the holiday period.
33+ date_to_get_init_vals <- min(RKI_hosp_adj $ Datum ) + days(30 )
34+ init_vals <- get_initial_values(
35+ start_date_RKI_data = date_to_get_init_vals
36+ ) | >
37+ filter(age_group == " 00+" ) | >
38+ mutate(location = stringr :: str_sub(location , - 2 )) | >
39+ filter(location == location_abbr ) | >
40+ arrange(- desc(date )) | >
41+ pull(value )
4142
42- hosp_clean <- RKI_hosp_adj | >
43- rename(
44- date = Datum ,
45- state = Bundesland ,
46- age_group = Altersgruppe ,
47- # This is the initial reported hospital admissions (right-truncated)
48- init_hosp_7d_count = `fixierte_7T_Hospitalisierung_Faelle` ,
49- # This is the updated, eventual reported admissions for that date
50- updated_hosp_7d_count = `aktualisierte_7T_Hospitalisierung_Faelle` ,
51- state_pop = `Bevoelkerung`
52- ) | >
53- arrange(- desc(date )) | >
54- filter(
55- state == location_name ,
56- date > = date_to_get_init_vals
57- ) | >
58- # Replace with once we have initial values
59- mutate(daily_hosp_admits = convert_rolling_sum_to_inc(
60- rolling_sums = updated_hosp_7d_count ,
61- k = 7 ,
62- initial_values = init_vals
63- )) | >
64- filter(
65- date < = ymd(forecast_date ) + days(forecast_horizon ),
66- state == location_name
67- ) | >
68- select(
69- date , daily_hosp_admits , state_pop , init_hosp_7d_count ,
70- updated_hosp_7d_count
71- )
43+ hosp_clean <- RKI_hosp_adj | >
44+ rename(
45+ date = Datum ,
46+ state = Bundesland ,
47+ age_group = Altersgruppe ,
48+ # This is the initial reported hospital admissions (right-truncated)
49+ init_hosp_7d_count = `fixierte_7T_Hospitalisierung_Faelle` ,
50+ # This is the updated, eventual reported admissions for that date
51+ updated_hosp_7d_count = `aktualisierte_7T_Hospitalisierung_Faelle` ,
52+ state_pop = `Bevoelkerung`
53+ ) | >
54+ arrange(- desc(date )) | >
55+ filter(
56+ state == location_name ,
57+ date > = date_to_get_init_vals
58+ ) | >
59+ # Replace with once we have initial values
60+ mutate(daily_hosp_admits = convert_rolling_sum_to_inc(
61+ rolling_sums = updated_hosp_7d_count ,
62+ k = 7 ,
63+ initial_values = init_vals
64+ )) | >
65+ filter(
66+ date < = ymd(forecast_date ) + days(forecast_horizon ),
67+ state == location_name
68+ ) | >
69+ select(
70+ date , daily_hosp_admits , state_pop , init_hosp_7d_count ,
71+ updated_hosp_7d_count
72+ )
7273 return (hosp_clean )
7374}
7475
@@ -89,15 +90,17 @@ get_hosp_for_fit <- function(hosp_data_eval,
8990 calibration_period = 100 ,
9091 lag = 3 ) {
9192 if (isFALSE(right_trunc )) {
92- hosp_for_fit <- hosp_data_eval | >
93- filter(
94- date > = ymd(forecast_date ) - days(calibration_period ),
95- date < = ymd(forecast_date ) - days(lag )
96- ) } else {
97- # Insert function to use git history to get the data as of the forecast date
98- hosp_for_fit <- NULL
99- }
93+ hosp_for_fit <- hosp_data_eval | >
94+ filter(
95+ date > = ymd(forecast_date ) - days(calibration_period ),
96+ date < = ymd(forecast_date ) - days(lag )
97+ )
98+ } else {
99+ # Insert function to use git history to get the data as of the forecast date
100+ hosp_for_fit <- NULL
101+ }
100102 return (hosp_for_fit )
103+ }
101104
102105# ' Get initial values of daily admissions
103106# '
@@ -113,7 +116,7 @@ get_hosp_for_fit <- function(hosp_data_eval,
113116# ' triangle from the German Nowcast Hub. Default is from the data at
114117# ' https://raw.githubusercontent.com/KITmetricslab/hospitalization-nowcast-hub/refs/heads/main/data-truth/COVID-19/COVID-19_hospitalizations_preprocessed.csv #nolint
115118# ' @autoglobal
116- # ' @inheritParams get_hosp_data
119+ # ' @inheritParams get_hosp_for_eval
117120# ' @importFrom glue glue
118121# ' @importFrom dplyr select filter mutate arrange desc
119122# ' @importFrom fs dir_create
0 commit comments