Skip to content

Commit 396f33f

Browse files
sbfnk-botsbfnk
andauthored
Fix date-dimension mismatch in forecast_infections (#1324)
* Fix date-dimension mismatch in forecast_infections Move summary() call before args modification to ensure dates are calculated from the original fit dimensions rather than extended dimensions. This fixes the error when extending R trajectory beyond the original observation period. Co-Authored-By: Sebastian Funk <sebastian.funk@lshtm.ac.uk> * Address review feedback - Fix NEWS.md to start with action verb "Fixed" - Add unit test for extended R trajectory scenario Co-Authored-By: Sebastian Funk <sebastian.funk@lshtm.ac.uk> --------- Co-authored-by: sbfnk-bot <sbfnk-bot@users.noreply.github.com> Co-authored-by: Sebastian Funk <sebastian.funk@lshtm.ac.uk>
1 parent 8454b07 commit 396f33f

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# EpiNow2 (development version)
22

3+
## Bug fixes
4+
5+
- Fixed a bug in `forecast_infections()` where the summary call to extract dates was using modified args instead of the original fit dimensions, causing a date-dimension mismatch when extending the R trajectory beyond the original observation period.
6+
37
## Breaking changes
48

59
- Removed deprecated arguments that have been erroring since v1.7.0/v1.8.0:

R/simulate_infections.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ forecast_infections <- function(estimates,
398398
draws$R <- R_draws
399399
}
400400

401+
# Extract R dates from original fit before modifying args
402+
summarised <- summary(estimates, type = "parameters")
403+
401404
# redefine time if Rt != data$t
402405
est_time <- estimates$args$t
403406
horizon <- estimates$args$horizon
@@ -422,7 +425,6 @@ forecast_infections <- function(estimates,
422425
}
423426

424427
# define dates of interest
425-
summarised <- summary(estimates, type = "parameters")
426428
dates <- seq(
427429
min(na.omit(unique(summarised[variable == "R"]$date))) - days(shift),
428430
by = "day", length.out = dim(draws$R)[2] + shift

tests/testthat/test-forecast-infections.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,23 @@ test_that("get_predictions produces expected output with format = 'summary'", {
173173
expect_true("mean" %in% names(preds))
174174
expect_false("confirm" %in% names(preds))
175175
})
176+
177+
test_that("forecast_infections works with R extended beyond original fit", {
178+
skip_integration()
179+
fixtures <- get_test_fixtures()
180+
# Get the original R length from the fit
181+
original_R_length <- nrow(
182+
summary(fixtures$estimate_infections, type = "parameters", param = "R")
183+
)
184+
# Create an R vector longer than the original fit (extend by 10 days)
185+
extended_R <- c(rep(NA_real_, original_R_length), rep(0.8, 10))
186+
# This would fail before the fix due to date-dimension mismatch
187+
sims <- forecast_infections(
188+
fixtures$estimate_infections, R = extended_R, samples = 10
189+
)
190+
expect_equal(names(sims), c("samples", "summarised", "observations"))
191+
expect_true(nrow(sims$samples) > 0)
192+
# Verify get_samples works (this was part of the failing call chain)
193+
samples <- get_samples(sims)
194+
expect_s3_class(samples, "data.table")
195+
})

0 commit comments

Comments
 (0)