Skip to content

Commit

Permalink
Truncate PMFs longer than the data
Browse files Browse the repository at this point in the history
To the length of the data and throw a warning rather than an error.
This change allows us to run on old right-truncation estimates.
  • Loading branch information
zsusswein committed Oct 2, 2024
1 parent 0e65fb2 commit 37ee7b7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 11 additions & 2 deletions R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,24 @@ format_right_truncation <- function(pmf, data) {
# silently removed if length of the PMF was longer than the data,
# effectively eliminating the right-truncation correction

cli::cli_abort(
trunc_idx <- nrow(data) - 1
cli::cli_warn(
c(
"Removing right-truncation PMF elements after {.val {trunc_idx}}",
"Right truncation PMF longer than the data",
"PMF length: {.val {length(pmf)}}",
"Data length: {.val {nrow(data)}}",
"PMF can only be up to length as the data"
"PMF can only be up to the length of the data"
),
class = "right_trunc_too_long"
)
suppressWarnings({
EpiNow2::trunc_opts(
dist = EpiNow2::dist_spec(
pmf = pmf[seq_len(trunc_idx)]
)
)
})
} else {
suppressWarnings({
EpiNow2::trunc_opts(
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/fit_model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Right truncation longer than data throws error

Removing right-truncation PMF elements after 1
Right truncation PMF longer than the data
PMF length: 3
Data length: 2
PMF can only be up to the length of the data

5 changes: 2 additions & 3 deletions tests/testthat/test-fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,11 @@ test_that("Right truncation longer than data throws error", {
data <- data.frame(x = c(1, 2))
right_truncation_pmf <- c(0.1, 0.2, 0.7)

expect_error(
expect_snapshot_warning(
format_right_truncation(
right_truncation_pmf,
data
),
class = "right_trunc_too_long"
)
)
})

Expand Down

0 comments on commit 37ee7b7

Please sign in to comment.