-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching to broadcasting random walk (#747)
* setup broadcasting for rw * make wider changes to make broadcasting approach easier * update interface and test code * add unit tests for Rt_opts * write unit tests for create_rt_data * Update NEWS.md * revert setup changes * update docs * catch initialisation issue * Update NEWS.md Co-authored-by: James Azam <[email protected]> --------- Co-authored-by: James Azam <[email protected]>
- Loading branch information
1 parent
8e0bf2a
commit 39cdaff
Showing
9 changed files
with
227 additions
and
36 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
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.
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,88 @@ | ||
test_that("create_rt_data returns expected default values", { | ||
result <- create_rt_data() | ||
|
||
expect_type(result, "list") | ||
expect_equal(result$r_mean, 1) | ||
expect_equal(result$r_sd, 1) | ||
expect_equal(result$estimate_r, 1) | ||
expect_equal(result$bp_n, 0) | ||
expect_equal(result$breakpoints, numeric(0)) | ||
expect_equal(result$future_fixed, 1) | ||
expect_equal(result$fixed_from, 0) | ||
expect_equal(result$pop, 0) | ||
expect_equal(result$stationary, 0) | ||
expect_equal(result$future_time, 0) | ||
}) | ||
|
||
test_that("create_rt_data handles NULL rt input correctly", { | ||
result <- create_rt_data(rt = NULL) | ||
|
||
expect_equal(result$estimate_r, 0) | ||
expect_equal(result$future_fixed, 0) | ||
expect_equal(result$stationary, 1) | ||
}) | ||
|
||
test_that("create_rt_data handles custom rt_opts correctly", { | ||
custom_rt <- rt_opts( | ||
prior = list(mean = 2, sd = 0.5), | ||
use_rt = FALSE, | ||
rw = 0, | ||
use_breakpoints = FALSE, | ||
future = "project", | ||
gp_on = "R0", | ||
pop = 1000000 | ||
) | ||
|
||
result <- create_rt_data(rt = custom_rt, horizon = 7) | ||
|
||
expect_equal(result$r_mean, 2) | ||
expect_equal(result$r_sd, 0.5) | ||
expect_equal(result$estimate_r, 0) | ||
expect_equal(result$pop, 1000000) | ||
expect_equal(result$stationary, 1) | ||
expect_equal(result$future_time, 7) | ||
}) | ||
|
||
test_that("create_rt_data handles breakpoints correctly", { | ||
result <- create_rt_data(rt_opts(use_breakpoints = TRUE), | ||
breakpoints = c(1, 0, 1, 0, 1)) | ||
|
||
expect_equal(result$bp_n, 3) | ||
expect_equal(result$breakpoints, c(2, 2, 3, 3, 4)) | ||
}) | ||
|
||
test_that("create_rt_data handles random walk correctly", { | ||
result <- create_rt_data(rt_opts(rw = 2), | ||
breakpoints = rep(1, 10)) | ||
|
||
expect_equal(result$bp_n, 5) | ||
expect_equal(result$breakpoints, c(1, 2, 2, 3, 3, 4, 4, 5, 5, 6)) | ||
}) | ||
|
||
test_that("create_rt_data throws error for invalid inputs", { | ||
expect_error(create_rt_data(rt_opts(rw = 2)), | ||
"breakpoints must be supplied when using random walk") | ||
}) | ||
|
||
test_that("create_rt_data handles future projections correctly", { | ||
result <- create_rt_data(rt_opts(future = "project"), horizon = 7) | ||
|
||
expect_equal(result$future_fixed, 0) | ||
expect_equal(result$fixed_from, 0) | ||
expect_equal(result$future_time, 7) | ||
}) | ||
|
||
test_that("create_rt_data handles zero sum breakpoints", { | ||
result <- create_rt_data(rt_opts(use_breakpoints = TRUE), | ||
breakpoints = rep(0, 5)) | ||
|
||
expect_equal(result$bp_n, 0) | ||
}) | ||
|
||
test_that("create_rt_data adjusts breakpoints for horizon", { | ||
result <- create_rt_data(rt_opts(rw = 2, future = "latest"), | ||
breakpoints = rep(1, 10), | ||
horizon = 3) | ||
|
||
expect_equal(result$breakpoints, c(1, 2, 2, 3, 3, 4, 4, 4, 4, 4)) | ||
}) |
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,61 @@ | ||
test_that("rt_opts returns expected default values", { | ||
result <- rt_opts() | ||
|
||
expect_s3_class(result, "rt_opts") | ||
expect_equal(result$prior, list(mean = 1, sd = 1)) | ||
expect_true(result$use_rt) | ||
expect_equal(result$rw, 0) | ||
expect_true(result$use_breakpoints) | ||
expect_equal(result$future, "latest") | ||
expect_equal(result$pop, 0) | ||
expect_equal(result$gp_on, "R_t-1") | ||
}) | ||
|
||
test_that("rt_opts handles custom inputs correctly", { | ||
result <- rt_opts( | ||
prior = list(mean = 2, sd = 0.5), | ||
use_rt = FALSE, | ||
rw = 7, | ||
use_breakpoints = FALSE, | ||
future = "project", | ||
gp_on = "R0", | ||
pop = 1000000 | ||
) | ||
|
||
expect_equal(result$prior, list(mean = 2, sd = 0.5)) | ||
expect_false(result$use_rt) | ||
expect_equal(result$rw, 7) | ||
expect_true(result$use_breakpoints) # Should be TRUE when rw > 0 | ||
expect_equal(result$future, "project") | ||
expect_equal(result$pop, 1000000) | ||
expect_equal(result$gp_on, "R0") | ||
}) | ||
|
||
test_that("rt_opts sets use_breakpoints to TRUE when rw > 0", { | ||
result <- rt_opts(rw = 3, use_breakpoints = FALSE) | ||
expect_true(result$use_breakpoints) | ||
}) | ||
|
||
test_that("rt_opts throws error for invalid prior", { | ||
expect_error(rt_opts(prior = list(mean = 1)), | ||
"prior must have both a mean and sd specified") | ||
expect_error(rt_opts(prior = list(sd = 1)), | ||
"prior must have both a mean and sd specified") | ||
}) | ||
|
||
test_that("rt_opts validates gp_on argument", { | ||
expect_error(rt_opts(gp_on = "invalid"), "must be one") | ||
}) | ||
|
||
test_that("rt_opts returns object of correct class", { | ||
result <- rt_opts() | ||
expect_s3_class(result, "rt_opts") | ||
expect_true("list" %in% class(result)) | ||
}) | ||
|
||
test_that("rt_opts handles edge cases correctly", { | ||
result <- rt_opts(rw = 0.1, pop = -1) | ||
expect_equal(result$rw, 0.1) | ||
expect_equal(result$pop, -1) | ||
expect_true(result$use_breakpoints) | ||
}) |
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