Skip to content

Commit

Permalink
make weekly data from generate simulated data vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitejohnson committed Dec 16, 2024
1 parent 5caa395 commit 448719a
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ Imports:
scales,
ggplot2,
posterior,
checkmate
checkmate,
zoo
Remotes:
stan-dev/cmdstanr
VignetteBuilder:
Expand Down
15 changes: 14 additions & 1 deletion R/generate_simulated_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ generate_simulated_data <- function(r_in_weeks = # nolint
nt = 9,
forecast_horizon = 28,
sim_start_date = lubridate::ymd(
"2023-09-01"
"2023-09-03"
),
hosp_wday_effect = c(
0.95, 1.01, 1.02,
Expand Down Expand Up @@ -512,6 +512,11 @@ generate_simulated_data <- function(r_in_weeks = # nolint
date_df = date_df
)

weekly_hosp_data <- make_weekly_data(
hosp_data = hosp_data,
day_of_week_to_sum = "Saturday"
)

hosp_data_eval <- format_hosp_data(
pred_obs_hosp = pred_obs_hosp,
dur_obs = (ot + ht),
Expand All @@ -522,6 +527,12 @@ generate_simulated_data <- function(r_in_weeks = # nolint
"daily_hosp_admits_for_eval" = "daily_hosp_admits"
)

weekly_hosp_data_eval <- make_weekly_data(
hosp_data = hosp_data_eval,
count_col_name = "daily_hosp_admits_for_eval",
day_of_week_to_sum = "Saturday"
)

# Make a subpopulation level hospital admissions data
# For now this will only be used for evaluation, eventually, can add
# feature to use this in calibration
Expand Down Expand Up @@ -571,6 +582,8 @@ generate_simulated_data <- function(r_in_weeks = # nolint
ww_data_eval = ww_data_eval,
hosp_data = hosp_data,
hosp_data_eval = hosp_data_eval,
weekly_hosp_data = weekly_hosp_data,
weekly_hosp_data_eval = weekly_hosp_data_eval,
subpop_hosp_data = subpop_hosp_data,
subpop_hosp_data_eval = subpop_hosp_data_eval,
true_global_rt = true_rt
Expand Down
38 changes: 38 additions & 0 deletions R/make_weekly_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Make daily data into weekly data
#' This is an internal function used to generate simulated weekly data
#'
#' @param hosp_data A tibble containing daily hospital admissions data,
#' expects the following columns: "date", and whatever is specified in
#' `count_col_name`
#' @param count_col_name A character string indicating the name of the column
#' with the daily counts, default is `"daily_hosp_admits"`
#' @param day_of_week_to_sum A character string indicating what day of the
#' week to assign the past 7 days of hospital admissions to. Must be full
#' weekday name with first letter in uppercase. Default is Saturday
#'
#' @return A dataframe with weekly hospital admissions data, assigned to
#' the day of the week to sum
make_weekly_data <- function(hosp_data,
count_col_name = "daily_hosp_admits",
day_of_week_to_sum = "Saturday") {
hosp_data_w_wday <- hosp_data |>
dplyr::mutate(
day_of_week = lubridate::wday(.data$date,
week_start = 1,
label = TRUE,
abbr = FALSE
),
day_of_week_numeric = lubridate::wday(.data$date),
weekly_hosp_admits = zoo::rollsum(.data[[count_col_name]],
k = 7,
na.pad = TRUE,
align = "right"
)
) |>
dplyr::filter(day_of_week == {{ day_of_week_to_sum }})

weekly_hosp_data <- hosp_data_w_wday |>
dplyr::select(date, weekly_hosp_admits, state_pop)

return(weekly_hosp_data)
}
4 changes: 4 additions & 0 deletions data-raw/vignette_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ ww_data_eval <- simulated_data$ww_data_eval
hosp_data <- hosp_data_from_sim |>
dplyr::mutate("location" = "example state")
hosp_data_eval <- simulated_data$hosp_data_eval
weekly_hosp_data <- simulated_data$weekly_hosp_data
weekly_hosp_data_eval <- simulated_data$weekly_hosp_data_eval
subpop_hosp_data <- simulated_data$subpop_hosp_data
subpop_hosp_data_eval <- simulated_data$subpop_hosp_data_eval
true_global_rt <- simulated_data$true_global_rt

usethis::use_data(hosp_data, overwrite = TRUE)
usethis::use_data(hosp_data_eval, overwrite = TRUE)
usethis::use_data(weekly_hosp_data, overwrite = TRUE)
usethis::use_data(weekly_hosp_data_eval, overwrite = TRUE)
usethis::use_data(ww_data, overwrite = TRUE)
usethis::use_data(ww_data_eval, overwrite = TRUE)
usethis::use_data(subpop_hosp_data, overwrite = TRUE)
Expand Down
Binary file modified data/hosp_data.rda
Binary file not shown.
Binary file modified data/hosp_data_eval.rda
Binary file not shown.
Binary file modified data/subpop_hosp_data.rda
Binary file not shown.
Binary file modified data/subpop_hosp_data_eval.rda
Binary file not shown.
Binary file modified data/true_global_rt.rda
Binary file not shown.
Binary file added data/weekly_hosp_data.rda
Binary file not shown.
Binary file added data/weekly_hosp_data_eval.rda
Binary file not shown.
Binary file modified data/ww_data.rda
Binary file not shown.
Binary file modified data/ww_data_eval.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/generate_simulated_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/make_weekly_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scratch/sim_data_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lab <- c(1, 2, 3, 3, 3)
ot <- 90
nt <- 9
forecast_horizon <- 28
sim_start_date <- lubridate::ymd("2023-09-01")
sim_start_date <- lubridate::ymd("2023-09-03")
hosp_wday_effect <- c(
0.95, 1.01, 1.02,
1.02, 1.01, 1,
Expand Down

0 comments on commit 448719a

Please sign in to comment.