-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split prior infections and growth calculation into separate function (#…
…888) * Split prior infections and growth calculation into separate function * Add tests * Move function to create.R * Rename function * Rename test script and function * Add NEWS item
- Loading branch information
1 parent
88584d6
commit 13dec33
Showing
4 changed files
with
115 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
test_that("estimate_early_dynamics works", { | ||
cases <- EpiNow2::example_confirmed[1:30] | ||
prior_estimates <- estimate_early_dynamics(cases$confirm, 7) | ||
# Check dimensions | ||
expect_identical( | ||
names(prior_estimates), | ||
c("prior_infections", "prior_growth") | ||
) | ||
expect_identical(length(prior_estimates), 2L) | ||
# Check values | ||
expect_identical( | ||
round(prior_estimates$prior_infections, 2), | ||
4.53 | ||
) | ||
expect_identical( | ||
round(prior_estimates$prior_growth, 2), | ||
0.35 | ||
) | ||
}) | ||
|
||
test_that("estimate_early_dynamics handles NA values correctly", { | ||
cases <- c(10, 20, NA, 40, 50, NA, 70) | ||
prior_estimates <- estimate_early_dynamics(cases, 7) | ||
expect_equal( | ||
prior_estimates$prior_infections, | ||
log(mean(c(10, 20, 40, 50, 70), na.rm = TRUE)) | ||
) | ||
expect_true(!is.na(prior_estimates$prior_growth)) | ||
}) | ||
|
||
test_that("estimate_early_dynamics handles exponential growth", { | ||
cases <- 2^(c(0:6)) # Exponential growth | ||
prior_estimates <- estimate_early_dynamics(cases, 7) | ||
expect_equal(prior_estimates$prior_infections, log(mean(cases[1:7]))) | ||
expect_true(prior_estimates$prior_growth > 0) # Growth should be positive | ||
}) | ||
|
||
test_that("estimate_early_dynamics handles exponential decline", { | ||
cases <- rev(2^(c(0:6))) # Exponential decline | ||
prior_estimates <- estimate_early_dynamics(cases, 7) | ||
expect_equal(prior_estimates$prior_infections, log(mean(cases[1:7]))) | ||
expect_true(prior_estimates$prior_growth < 0) # Growth should be negative | ||
}) | ||
|
||
test_that("estimate_early_dynamics correctly handles seeding time less than 2", { | ||
cases <- c(5, 10, 20) # Less than 7 days of data | ||
prior_estimates <- estimate_early_dynamics(cases, 1) | ||
expect_equal(prior_estimates$prior_growth, 0) # Growth should be 0 if seeding time is <= 1 | ||
}) |