Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
#####
# R
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3.9017
rev: v0.4.3.9019
hooks:
- id: lintr
#####
Expand Down
2 changes: 1 addition & 1 deletion hewr/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(process_model_samples,pyrenew)
S3method(process_model_samples,timeseries)
export(augment_timeseries_draws_w_obs)
export(epiweekly_samples_from_daily)
export(format_timeseries_output)
export(generate_exp_growth_pois)
Expand All @@ -21,6 +22,5 @@ export(process_loc_forecast)
export(process_model_samples)
export(prop_from_timeseries)
export(read_and_combine_data)
export(to_tidy_draws_timeseries)
importFrom(rlang,":=")
importFrom(rlang,.data)
24 changes: 11 additions & 13 deletions hewr/R/process_loc_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ load_and_aggregate_ts <- function(
dplyr::mutate(
data = purrr::pmap(
list(.data$samples, .data$observed),
function(samples, observed, epiweekly) {
to_tidy_draws_timeseries(
function(samples, observed) {
augment_timeseries_draws_w_obs(
tidy_forecast = samples,
observed = observed
)
Expand Down Expand Up @@ -239,24 +239,22 @@ read_and_combine_data <- function(model_dir) {
#' @param value_colname Name of the column in
#' `tidy_forecast` for the sampled values.
#' Default `".value"`.
#' @param epiweekly Is the timeseries epiweekly (as opposed
#' to daily)? Boolean, default `FALSE` (i.e. daily timeseries).
#' @export
to_tidy_draws_timeseries <- function(
augment_timeseries_draws_w_obs <- function(
tidy_forecast,
observed,
date_colname = "date",
sample_id_colname = ".draw",
value_colname = ".value",
epiweekly = FALSE
value_colname = ".value"
) {
first_forecast_date <- min(tidy_forecast[[date_colname]])
resolution <- unique(tidy_forecast$resolution)
day_count <- ifelse(resolution == "epiweekly", 7, 1)
checkmate::assert_scalar(resolution)
step_size_days <- dplyr::if_else(resolution == "epiweekly", 7, 1)
n_draws <- max(tidy_forecast[[sample_id_colname]])

target_variables <- unique(tidy_forecast$.variable)
transformed_obs <- observed |>

obs_as_samples <- observed |>
dplyr::filter(
.data[[date_colname]] < !!first_forecast_date,
.data$.variable %in% target_variables
Expand All @@ -265,13 +263,13 @@ to_tidy_draws_timeseries <- function(
dplyr::mutate(resolution = !!resolution)

stopifnot(
max(as.Date(transformed_obs[[date_colname]])) +
lubridate::ddays(day_count) ==
max(as.Date(obs_as_samples[[date_colname]])) +
lubridate::ddays(step_size_days) ==
first_forecast_date
)

dplyr::bind_rows(
transformed_obs,
obs_as_samples,
tidy_forecast
) |>
dplyr::select(!!sample_id_colname, tidyselect::everything())
Expand Down

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

4 changes: 2 additions & 2 deletions hewr/man/process_loc_forecast.Rd

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

4 changes: 2 additions & 2 deletions hewr/tests/testthat/test_process_loc_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ example_eval_dat <- tibble::tibble(
) |>
dplyr::mutate(.value = rpois(dplyr::n(), 100))

test_that("to_tidy_draws_timeseries() works as expected", {
test_that("augment_timeseries_draws_w_obs() works as expected", {
forecast <- tibble::tibble(
date = as.Date(c("2024-12-21", "2024-12-22")),
resolution = "daily",
Expand All @@ -67,7 +67,7 @@ test_that("to_tidy_draws_timeseries() works as expected", {
.variable = rep("other_ed_visits", 4L),
.value = c(11037, 12898, 15172, 17716),
)
result <- to_tidy_draws_timeseries(
result <- augment_timeseries_draws_w_obs(
forecast,
obs
)
Expand Down
4 changes: 2 additions & 2 deletions hewr/tests/testthat/test_timeseries_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ test_that("epiweekly_samples_from_daily aggregates correctly", {
expect_true(all(result$.variable == "observed_ed_visits"))
})

test_that("to_tidy_draws_timeseries combines forecast and observed", {
test_that("augment_timeseries_draws_w_obs combines forecast and observed", {
# Create minimal forecast data - 3 forecast dates x 2 draws = 6 rows
tidy_forecast <- tibble::tibble(
date = rep(as.Date("2024-01-08") + 0:2, each = 2),
Expand All @@ -160,7 +160,7 @@ test_that("to_tidy_draws_timeseries combines forecast and observed", {
.value = 10:16
)

result <- to_tidy_draws_timeseries(
result <- augment_timeseries_draws_w_obs(
tidy_forecast = tidy_forecast,
observed = observed
)
Expand Down