1414# ' @param quantiles_to_save Vector of numerics indicating the quantiles
1515# ' @param ind_filepath Character string of the file path to save the outputs
1616# ' from each model run
17+ # ' @param save_draws Boolean indicating whether or not to save the draws,
18+ # ' default is FALSE.
1719# '
1820# ' @returns Data.frame of the quantiles alongside the evaluation data.
1921# ' @autoglobal
@@ -34,9 +36,18 @@ fit_wwinference_wrapper <- function(
3436 calibration_time = 90 ,
3537 forecast_horizon = 28 ,
3638 quantiles_to_save = c(0.025 , 0.1 , 0.25 , 0.5 , 0.75 , 0.9 , 0.975 ),
37- ind_filepath = file.path(" output" )) {
39+ ind_filepath = file.path(" output" ),
40+ save_draws = FALSE ) {
3841 loc <- unique(count_data $ state )
42+ if ((nrow(ww_data ) == 0 || is.null(ww_data )) &&
43+ isTRUE(model_spec $ include_ww )) {
44+ model_spec $ include_ww <- FALSE
45+ flag_missing_ww <- TRUE
46+ } else {
47+ flag_missing_ww <- FALSE
48+ }
3949 include_ww <- model_spec $ include_ww
50+ hosp_data_real_time <- unique(count_data $ hosp_data_real_time )
4051 ww_fit_obj <- wwinference(
4152 ww_data = ww_data ,
4253 count_data = count_data ,
@@ -54,32 +65,37 @@ fit_wwinference_wrapper <- function(
5465 # Save plots
5566 full_fp <- file.path(ind_filepath , this_forecast_date , loc )
5667 if (! file.exists(file.path(full_fp ))) {
57- dir_create(full_fp , recursive = TRUE , showWarnings = FALSE )
68+ dir_create(full_fp , recurse = TRUE )
5869 }
5970 fig_fp <- file.path(full_fp , " figs" )
6071 if (! file.exists(file.path(fig_fp ))) {
61- dir_create(fig_fp , recursive = TRUE , showWarnings = FALSE )
72+ dir_create(fig_fp , recurse = TRUE )
6273 }
6374
6475 plot_hosp_draws <- get_plot_forecasted_counts(
6576 draws = hosp_draws ,
6677 forecast_date = this_forecast_date
67- ) + ggtitle(glue(" {loc}, wastewater: {include_ww}" ))
78+ ) + ggtitle(glue(" {loc}, wastewater: {include_ww}, hosp data real-time: {hosp_data_real_time} " )) # nolint
6879
6980 ggsave(
7081 plot = plot_hosp_draws ,
7182 filename = file.path(
7283 fig_fp ,
73- glue(" hosp_draws_ww_{include_ww}.png" )
84+ glue(" hosp_draws_ww_{include_ww}_rt_{hosp_data_real_time} .png" )
7485 )
7586 )
7687 ww_draws <- if (! is.null(ww_fit_obj $ raw_input_data $ input_ww_data )) {
7788 get_draws(ww_fit_obj , what = " predicted_ww" )$ predicted_ww
7889 } else {
7990 NULL
8091 }
92+ data_fp <- file.path(full_fp , " data" )
93+ if (! file.exists(file.path(data_fp ))) {
94+ dir_create(data_fp , recurse = TRUE )
95+ }
8196
8297 if (! is.null(ww_draws )) {
98+ # Plot
8399 plot_ww_draws <- get_plot_ww_conc(
84100 draws = ww_draws ,
85101 forecast_date = this_forecast_date
@@ -91,6 +107,43 @@ fit_wwinference_wrapper <- function(
91107 " ww_draws.png"
92108 )
93109 )
110+ ww_data_obs <- select(
111+ ww_data ,
112+ date , site , lab ,
113+ log_genome_copies_per_ml , below_lod ,
114+ log_lod , flag_as_ww_outlier
115+ )
116+ ww_metadata <- ww_data | >
117+ select(
118+ site , lab , site_pop ,
119+ location_name , location_abbr ,
120+ forecast_date , lab_site_name
121+ ) | >
122+ distinct()
123+
124+ # Get and save quantiles
125+ ww_quantiles <- ww_draws | >
126+ trajectories_to_quantiles(
127+ quantiles = quantiles_to_save ,
128+ timepoint_cols = " date" ,
129+ value_col = " pred_value" ,
130+ quantile_value_name = " predicted" ,
131+ quantile_level_name = " quantile_level" ,
132+ id_cols = c(" site" , " lab" )
133+ ) | >
134+ left_join(ww_data_obs ,
135+ by = c(" date" , " site" , " lab" )
136+ ) | >
137+ left_join(ww_metadata ,
138+ by = c(" site" , " lab" )
139+ )
140+ write_csv(
141+ ww_quantiles ,
142+ file.path(
143+ data_fp ,
144+ " ww_quantiles.csv"
145+ )
146+ )
94147 }
95148
96149 draws_w_data <- get_model_draws_w_data(
@@ -100,18 +153,18 @@ fit_wwinference_wrapper <- function(
100153 model = " wwinference" ,
101154 forecast_date = this_forecast_date ,
102155 location = loc ,
156+ hosp_data_real_time = hosp_data_real_time ,
103157 eval_data = hosp_data_eval
104158 )
105- data_fp <- file.path(full_fp , " data" )
106- if (! file.exists(file.path(data_fp ))) {
107- dir_create(data_fp , recursive = TRUE , showWarnings = FALSE )
159+
160+ if (isTRUE(save_draws )) {
161+ arrow :: write_parquet(
162+ draws_w_data ,
163+ file.path(data_fp , glue :: glue(
164+ " hosp_draws_ww_{include_ww}_rt_{hosp_data_real_time}.parquet"
165+ ))
166+ )
108167 }
109- write_csv(
110- draws_w_data ,
111- file.path(data_fp , glue :: glue(
112- " hosp_draws_ww_{include_ww}.csv"
113- ))
114- )
115168 # Make a plot here with calibration and evaluation data and save it.
116169 get_plot_draws_w_calib_data(
117170 draws_w_data ,
@@ -124,7 +177,8 @@ fit_wwinference_wrapper <- function(
124177 offset = 1 ,
125178 quantiles = TRUE ,
126179 probs = quantiles_to_save
127- )
180+ ) | >
181+ mutate(flag_missing_ww = flag_missing_ww )
128182
129183 write_csv(
130184 hosp_quantiles ,
0 commit comments