diff --git a/R/checkers.R b/R/checkers.R index b54c6ee1..01855ce0 100644 --- a/R/checkers.R +++ b/R/checkers.R @@ -117,7 +117,7 @@ R, serial_interval, outbreak_start_date, - min_chain_size, + min_outbreak_size, onset_to_hosp = NULL, onset_to_death = NULL, contact_distribution = NULL, @@ -134,7 +134,7 @@ checkmate::assert_number(R, lower = 0) .check_func_req_args(serial_interval) checkmate::assert_date(outbreak_start_date) - checkmate::assert_integerish(min_chain_size, lower = 1) + checkmate::assert_integerish(min_outbreak_size, lower = 1) stopifnot( "population_age must be two numerics or a data.frame" = diff --git a/R/create_linelist.R b/R/create_linelist.R index 4e9ac0fe..4bf3ba18 100644 --- a/R/create_linelist.R +++ b/R/create_linelist.R @@ -49,8 +49,7 @@ #' serial_interval = serial_interval, #' onset_to_hosp = onset_to_hosp, #' onset_to_death = onset_to_death, -#' hosp_rate = 0.5, -#' add_ct = TRUE +#' hosp_rate = 0.5 #' ) #' ``` #' @return A ``. diff --git a/R/sim_contacts.R b/R/sim_contacts.R index 75bb1d2d..b453d771 100644 --- a/R/sim_contacts.R +++ b/R/sim_contacts.R @@ -40,18 +40,21 @@ sim_contacts <- function(R, serial_interval, contact_distribution, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = 10, + min_outbreak_size = 10, population_age = c(1, 90), contact_tracing_status_probs = c( under_followup = 0.7, lost_to_followup = 0.2, unknown = 0.1 ), - config = create_config(), - ...) { - chkDots(...) - - serial_interval <- as.function(serial_interval, func_typ = "generate") + config = create_config()) { + # check and convert distribution to func if needed before .check_sim_input() + stopifnot( + "Input delay distributions need to be either functions or " = + inherits(serial_interval, c("function", "epidist")) && + inherits(contact_distribution, c("function", "epidist")) + ) + serial_interval <- as.function(serial_interval, func_type = "generate") contact_distribution <- as.function( contact_distribution, func_type = "generate" @@ -62,7 +65,7 @@ sim_contacts <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, contact_distribution = contact_distribution, contact_tracing_status_probs = contact_tracing_status_probs, population_age = population_age @@ -72,7 +75,7 @@ sim_contacts <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, population_age = population_age, config = config ) diff --git a/R/sim_contacts_tbl.R b/R/sim_contacts_tbl.R index 37d222ab..5de90b5a 100644 --- a/R/sim_contacts_tbl.R +++ b/R/sim_contacts_tbl.R @@ -126,7 +126,7 @@ ) # remove infector col - contact_investigation <- contact_investigation[, -1] + contact_investigation$infector <- NULL # return contacts contact_investigation diff --git a/R/sim_linelist.R b/R/sim_linelist.R index 00ec7a8b..88addc35 100644 --- a/R/sim_linelist.R +++ b/R/sim_linelist.R @@ -1,7 +1,7 @@ #' Simulate a line list #' #' @description The line list is simulated using a branching process and -#' parameterised with previously published epidemiological parameters. +#' parameterised with user defined epidemiological parameters. #' #' @details For age-stratified hospitalised and death rates a `` #' will need to be passed to the `hosp_rate` and/or `hosp_death_rate` @@ -46,9 +46,9 @@ #' @param add_names A `logical` boolean for whether to add names to each row #' of the line list. Default is `TRUE`. #' @param add_ct A `logical` boolean for whether to add Ct values to each row -#' of the line list. Default is `FALSE`. -#' @param min_chain_size A single `numeric` defining the minimum chain size for -#' the simulated outbreak. Default is `10`. This can be increased when the +#' of the line list. Default is `TRUE`. +#' @param min_outbreak_size A single `numeric` defining the minimum chain size +#' for the simulated outbreak. Default is `10`. This can be increased when the #' function should simulate a larger outbreak. #' @param population_age Either a `numeric` vector with two elements or a #' `` with age structure in the population. Use a `numeric` vector @@ -62,9 +62,6 @@ #' `"confirmed"`. Values of each case type must sum to one. #' @param config A list of settings to adjust the randomly sampled delays and #' Ct values (if `add_ct = TRUE`). See [create_config()] for more information. -#' @param ... [dots] Extra arguments to be passed to other functions. -#' **Currently not used and will return a warning if extra arguments are -#' supplied**. #' #' @return A line list `` #' @export @@ -101,6 +98,7 @@ #' onset_to_death = onset_to_death, #' hosp_rate = 0.5 #' ) +#' head(linelist) #' #' # example with age-stratified hospitalisation rate #' # 20% for over 80s @@ -117,6 +115,7 @@ #' onset_to_death = onset_to_death, #' hosp_rate = age_dep_hosp_rate #' ) +#' head(linelist) sim_linelist <- function(R, serial_interval, onset_to_hosp, @@ -126,18 +125,22 @@ sim_linelist <- function(R, non_hosp_death_rate = 0.05, outbreak_start_date = as.Date("2023-01-01"), add_names = TRUE, - add_ct = FALSE, - min_chain_size = 10, + add_ct = TRUE, + min_outbreak_size = 10, population_age = c(1, 90), case_type_probs = c( suspected = 0.2, probable = 0.3, confirmed = 0.5 ), - config = create_config(), - ...) { - chkDots(...) - + config = create_config()) { + # check and convert distribution to func if needed before .check_sim_input() + stopifnot( + "Input delay distributions need to be either functions or " = + inherits(serial_interval, c("function", "epidist")) && + inherits(onset_to_hosp, c("function", "epidist")) && + inherits(onset_to_death, c("function", "epidist")) + ) serial_interval <- as.function(serial_interval, func_type = "generate") onset_to_hosp <- as.function(onset_to_hosp, func_type = "generate") onset_to_death <- as.function(onset_to_death, func_type = "generate") @@ -147,7 +150,7 @@ sim_linelist <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, add_names = add_names, @@ -191,7 +194,7 @@ sim_linelist <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, population_age = population_age, config = config ) diff --git a/R/sim_outbreak.R b/R/sim_outbreak.R index 6c6da936..57213e3a 100644 --- a/R/sim_outbreak.R +++ b/R/sim_outbreak.R @@ -63,8 +63,8 @@ sim_outbreak <- function(R, non_hosp_death_rate = 0.05, outbreak_start_date = as.Date("2023-01-01"), add_names = TRUE, - add_ct = FALSE, - min_chain_size = 10, + add_ct = TRUE, + min_outbreak_size = 10, population_age = c(1, 90), case_type_probs = c( suspected = 0.2, @@ -76,10 +76,15 @@ sim_outbreak <- function(R, lost_to_followup = 0.2, unknown = 0.1 ), - config = create_config(), - ...) { - chkDots(...) - + config = create_config()) { + # check and convert distribution to func if needed before .check_sim_input() + stopifnot( + "Input delay distributions need to be either functions or " = + inherits(serial_interval, c("function", "epidist")) && + inherits(onset_to_hosp, c("function", "epidist")) && + inherits(onset_to_death, c("function", "epidist")) && + inherits(contact_distribution, c("function", "epidist")) + ) serial_interval <- as.function(serial_interval, func_type = "generate") onset_to_hosp <- as.function(onset_to_hosp, func_type = "generate") onset_to_death <- as.function(onset_to_death, func_type = "generate") @@ -93,7 +98,7 @@ sim_outbreak <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, contact_distribution = contact_distribution, @@ -139,7 +144,7 @@ sim_outbreak <- function(R, R = R, serial_interval = serial_interval, outbreak_start_date = outbreak_start_date, - min_chain_size = min_chain_size, + min_outbreak_size = min_outbreak_size, population_age = population_age, config = config ) diff --git a/R/sim_utils.R b/R/sim_utils.R index a1eb7548..405f0488 100644 --- a/R/sim_utils.R +++ b/R/sim_utils.R @@ -17,12 +17,13 @@ NULL .sim_bp_linelist <- function(R, serial_interval, outbreak_start_date, - min_chain_size, + min_outbreak_size, population_age, config) { chain_size <- 0 + max_iter <- 0L # condition on a minimum chain size - while (chain_size < min_chain_size) { + while (chain_size < min_outbreak_size) { chain <- bpmodels::chain_sim( n = 1, offspring = "pois", @@ -33,6 +34,14 @@ NULL infinite = 1000 ) chain_size <- max(chain$id) + max_iter <- max_iter + 1L + if (max_iter >= 1e4) { + stop( + "Exceeded maximum number of iterations for simulating outbreak. \n", + "Change input parameters or min_outbreak_size.", + call. = FALSE + ) + } } names(chain)[names(chain) == "ancestor"] <- "infector" diff --git a/man/dot-check_sim_input.Rd b/man/dot-check_sim_input.Rd index 7ed904b0..407bd5f1 100644 --- a/man/dot-check_sim_input.Rd +++ b/man/dot-check_sim_input.Rd @@ -9,7 +9,7 @@ R, serial_interval, outbreak_start_date, - min_chain_size, + min_outbreak_size, onset_to_hosp = NULL, onset_to_death = NULL, contact_distribution = NULL, @@ -34,8 +34,8 @@ the serial interval.} \item{outbreak_start_date}{A \code{date} for the start of the outbreak.} -\item{min_chain_size}{A single \code{numeric} defining the minimum chain size for -the simulated outbreak. Default is \code{10}. This can be increased when the +\item{min_outbreak_size}{A single \code{numeric} defining the minimum chain size +for the simulated outbreak. Default is \code{10}. This can be increased when the function should simulate a larger outbreak.} \item{onset_to_hosp}{An \verb{} object or anonymous function for @@ -53,7 +53,7 @@ that go on to be infected or not infected.} of the line list. Default is \code{TRUE}.} \item{add_ct}{A \code{logical} boolean for whether to add Ct values to each row -of the line list. Default is \code{FALSE}.} +of the line list. Default is \code{TRUE}.} \item{case_type_probs}{A named \code{numeric} vector with the probability of each case type. The names of the vector must be \code{"suspected"}, \code{"probable"}, diff --git a/man/dot-create_linelist.Rd b/man/dot-create_linelist.Rd index 0d7796d8..8aaf5ff6 100644 --- a/man/dot-create_linelist.Rd +++ b/man/dot-create_linelist.Rd @@ -64,8 +64,7 @@ linelist <- sim_linelist( serial_interval = serial_interval, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, - hosp_rate = 0.5, - add_ct = TRUE + hosp_rate = 0.5 ) }\if{html}{\out{}} } diff --git a/man/dot-sim_utils.Rd b/man/dot-sim_utils.Rd index 5ba658d0..4278ed71 100644 --- a/man/dot-sim_utils.Rd +++ b/man/dot-sim_utils.Rd @@ -11,7 +11,7 @@ within \pkg{simulist}} R, serial_interval, outbreak_start_date, - min_chain_size, + min_outbreak_size, population_age, config ) @@ -38,8 +38,8 @@ the serial interval.} \item{outbreak_start_date}{A \code{date} for the start of the outbreak.} -\item{min_chain_size}{A single \code{numeric} defining the minimum chain size for -the simulated outbreak. Default is \code{10}. This can be increased when the +\item{min_outbreak_size}{A single \code{numeric} defining the minimum chain size +for the simulated outbreak. Default is \code{10}. This can be increased when the function should simulate a larger outbreak.} \item{population_age}{Either a \code{numeric} vector with two elements or a @@ -80,7 +80,7 @@ for more information.} of the line list. Default is \code{TRUE}.} \item{add_ct}{A \code{logical} boolean for whether to add Ct values to each row -of the line list. Default is \code{FALSE}.} +of the line list. Default is \code{TRUE}.} \item{case_type_probs}{A named \code{numeric} vector with the probability of each case type. The names of the vector must be \code{"suspected"}, \code{"probable"}, diff --git a/man/sim_contacts.Rd b/man/sim_contacts.Rd index 87cdca0c..994ea3ba 100644 --- a/man/sim_contacts.Rd +++ b/man/sim_contacts.Rd @@ -9,12 +9,11 @@ sim_contacts( serial_interval, contact_distribution, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = 10, + min_outbreak_size = 10, population_age = c(1, 90), contact_tracing_status_probs = c(under_followup = 0.7, lost_to_followup = 0.2, unknown = 0.1), - config = create_config(), - ... + config = create_config() ) } \arguments{ @@ -30,8 +29,8 @@ that go on to be infected or not infected.} \item{outbreak_start_date}{A \code{date} for the start of the outbreak.} -\item{min_chain_size}{A single \code{numeric} defining the minimum chain size for -the simulated outbreak. Default is \code{10}. This can be increased when the +\item{min_outbreak_size}{A single \code{numeric} defining the minimum chain size +for the simulated outbreak. Default is \code{10}. This can be increased when the function should simulate a larger outbreak.} \item{population_age}{Either a \code{numeric} vector with two elements or a @@ -49,10 +48,6 @@ contact tracing status must sum to one.} \item{config}{A list of settings to adjust the randomly sampled delays and Ct values (if \code{add_ct = TRUE}). See \code{\link[=create_config]{create_config()}} for more information.} - -\item{...}{\link{dots} Extra arguments to be passed to other functions. -\strong{Currently not used and will return a warning if extra arguments are -supplied}.} } \value{ A contacts \verb{} diff --git a/man/sim_linelist.Rd b/man/sim_linelist.Rd index 7bb79f05..42cbd10d 100644 --- a/man/sim_linelist.Rd +++ b/man/sim_linelist.Rd @@ -14,12 +14,11 @@ sim_linelist( non_hosp_death_rate = 0.05, outbreak_start_date = as.Date("2023-01-01"), add_names = TRUE, - add_ct = FALSE, - min_chain_size = 10, + add_ct = TRUE, + min_outbreak_size = 10, population_age = c(1, 90), case_type_probs = c(suspected = 0.2, probable = 0.3, confirmed = 0.5), - config = create_config(), - ... + config = create_config() ) } \arguments{ @@ -57,10 +56,10 @@ for more information.} of the line list. Default is \code{TRUE}.} \item{add_ct}{A \code{logical} boolean for whether to add Ct values to each row -of the line list. Default is \code{FALSE}.} +of the line list. Default is \code{TRUE}.} -\item{min_chain_size}{A single \code{numeric} defining the minimum chain size for -the simulated outbreak. Default is \code{10}. This can be increased when the +\item{min_outbreak_size}{A single \code{numeric} defining the minimum chain size +for the simulated outbreak. Default is \code{10}. This can be increased when the function should simulate a larger outbreak.} \item{population_age}{Either a \code{numeric} vector with two elements or a @@ -77,17 +76,13 @@ each case type. The names of the vector must be \code{"suspected"}, \code{"proba \item{config}{A list of settings to adjust the randomly sampled delays and Ct values (if \code{add_ct = TRUE}). See \code{\link[=create_config]{create_config()}} for more information.} - -\item{...}{\link{dots} Extra arguments to be passed to other functions. -\strong{Currently not used and will return a warning if extra arguments are -supplied}.} } \value{ A line list \verb{} } \description{ The line list is simulated using a branching process and -parameterised with previously published epidemiological parameters. +parameterised with user defined epidemiological parameters. } \details{ For age-stratified hospitalised and death rates a \verb{} @@ -141,6 +136,7 @@ linelist <- sim_linelist( onset_to_death = onset_to_death, hosp_rate = 0.5 ) +head(linelist) # example with age-stratified hospitalisation rate # 20\% for over 80s @@ -157,6 +153,7 @@ linelist <- sim_linelist( onset_to_death = onset_to_death, hosp_rate = age_dep_hosp_rate ) +head(linelist) } \author{ Joshua W. Lambert, Carmen Tamayo diff --git a/man/sim_outbreak.Rd b/man/sim_outbreak.Rd index 44c9f12a..1f2bd19c 100644 --- a/man/sim_outbreak.Rd +++ b/man/sim_outbreak.Rd @@ -15,14 +15,13 @@ sim_outbreak( non_hosp_death_rate = 0.05, outbreak_start_date = as.Date("2023-01-01"), add_names = TRUE, - add_ct = FALSE, - min_chain_size = 10, + add_ct = TRUE, + min_outbreak_size = 10, population_age = c(1, 90), case_type_probs = c(suspected = 0.2, probable = 0.3, confirmed = 0.5), contact_tracing_status_probs = c(under_followup = 0.7, lost_to_followup = 0.2, unknown = 0.1), - config = create_config(), - ... + config = create_config() ) } \arguments{ @@ -65,10 +64,10 @@ for more information.} of the line list. Default is \code{TRUE}.} \item{add_ct}{A \code{logical} boolean for whether to add Ct values to each row -of the line list. Default is \code{FALSE}.} +of the line list. Default is \code{TRUE}.} -\item{min_chain_size}{A single \code{numeric} defining the minimum chain size for -the simulated outbreak. Default is \code{10}. This can be increased when the +\item{min_outbreak_size}{A single \code{numeric} defining the minimum chain size +for the simulated outbreak. Default is \code{10}. This can be increased when the function should simulate a larger outbreak.} \item{population_age}{Either a \code{numeric} vector with two elements or a @@ -90,10 +89,6 @@ contact tracing status must sum to one.} \item{config}{A list of settings to adjust the randomly sampled delays and Ct values (if \code{add_ct = TRUE}). See \code{\link[=create_config]{create_config()}} for more information.} - -\item{...}{\link{dots} Extra arguments to be passed to other functions. -\strong{Currently not used and will return a warning if extra arguments are -supplied}.} } \value{ A list with two elements: diff --git a/tests/testthat/test-checkers.R b/tests/testthat/test-checkers.R index 5f314c79..5977fbbf 100644 --- a/tests/testthat/test-checkers.R +++ b/tests/testthat/test-checkers.R @@ -170,7 +170,7 @@ test_that(".check_sim_input works as expected", { R = 1, serial_interval = serial_interval, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = 10, + min_outbreak_size = 10, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, contact_distribution = contact_distribution, @@ -199,7 +199,7 @@ test_that(".check_sim_input works as expected", { R = 1, serial_interval = serial_interval, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = 10, + min_outbreak_size = 10, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, add_names = TRUE, @@ -222,7 +222,7 @@ test_that(".check_sim_input works as expected", { R = 1, serial_interval = serial_interval, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = 10, + min_outbreak_size = 10, contact_distribution = contact_distribution, contact_tracing_status_probs = c( under_followup = 0.7, @@ -263,8 +263,8 @@ test_that(".check_sim_input fails as expected", { R = 1, serial_interval = serial_interval, outbreak_start_date = as.Date("2023-01-01"), - min_chain_size = "10" + min_outbreak_size = "10" ), - regexp = "(Assertion on)*(min_chain_size)*(failed)" + regexp = "(Assertion on)*(min_outbreak_size)*(failed)" ) }) diff --git a/tests/testthat/test-sim_linelist.R b/tests/testthat/test-sim_linelist.R index ff554dec..4479c7e0 100644 --- a/tests/testthat/test-sim_linelist.R +++ b/tests/testthat/test-sim_linelist.R @@ -31,13 +31,13 @@ test_that("sim_linelist works as expected", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -67,35 +67,35 @@ test_that("sim_linelist works as expected with age-strat rates", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) -test_that("sim_linelist works as expected with Ct", { +test_that("sim_linelist works as expected without Ct", { set.seed(1) linelist <- sim_linelist( R = 1.1, serial_interval = serial_interval, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, - add_ct = TRUE + add_ct = FALSE ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 11L)) + expect_identical(dim(linelist), c(42L, 10L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact", "ct_value" + "date_last_contact" ) ) }) @@ -111,13 +111,13 @@ test_that("sim_linelist works as expected with anonymous", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 9L)) + expect_identical(dim(linelist), c(42L, 10L)) expect_identical( colnames(linelist), c( "id", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -138,13 +138,13 @@ test_that("sim_linelist works as expected with age structure", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -170,13 +170,13 @@ test_that("sim_linelist works as expected with age-strat rates & age struct", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -229,13 +229,13 @@ test_that("sim_linelist works as expected with modified config", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -253,13 +253,13 @@ test_that("sim_linelist works as expected with modified config parameters", { ) expect_s3_class(linelist, class = "data.frame") - expect_identical(dim(linelist), c(42L, 10L)) + expect_identical(dim(linelist), c(42L, 11L)) expect_identical( colnames(linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) }) @@ -305,3 +305,16 @@ test_that("sim_linelist fails as expected with empty config", { regexp = "Distribution parameters are missing, check config" ) }) + +test_that("sim_linelist fails as expected exceeding max iter for bpmodel", { + set.seed(1) + expect_error( + sim_linelist( + R = 0.2, + serial_interval = serial_interval, + onset_to_hosp = onset_to_hosp, + onset_to_death = onset_to_death + ), + regexp = "(Exceeded maximum number of iterations for simulating outbreak)" + ) +}) diff --git a/tests/testthat/test-sim_outbreak.R b/tests/testthat/test-sim_outbreak.R index ed1b3ca0..64243a65 100644 --- a/tests/testthat/test-sim_outbreak.R +++ b/tests/testthat/test-sim_outbreak.R @@ -41,14 +41,14 @@ test_that("sim_outbreak works as expected", { expect_type(outbreak, type = "list") expect_s3_class(outbreak$linelist, class = "data.frame") expect_s3_class(outbreak$contacts, class = "data.frame") - expect_identical(dim(outbreak$linelist), c(42L, 10L)) - expect_identical(dim(outbreak$contacts), c(168L, 8L)) + expect_identical(dim(outbreak$linelist), c(42L, 11L)) + expect_identical(dim(outbreak$contacts), c(172L, 8L)) expect_identical( colnames(outbreak$linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) expect_identical( @@ -74,14 +74,14 @@ test_that("sim_outbreak works as expected with add_names = FALSE", { expect_type(outbreak, type = "list") expect_s3_class(outbreak$linelist, class = "data.frame") expect_s3_class(outbreak$contacts, class = "data.frame") - expect_identical(dim(outbreak$linelist), c(42L, 9L)) + expect_identical(dim(outbreak$linelist), c(42L, 10L)) expect_identical(dim(outbreak$contacts), c(170L, 8L)) expect_identical( colnames(outbreak$linelist), c( "id", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) expect_identical( @@ -121,14 +121,14 @@ test_that("sim_outbreak works as expected with age-strat rates", { expect_type(outbreak, type = "list") expect_s3_class(outbreak$linelist, class = "data.frame") expect_s3_class(outbreak$contacts, class = "data.frame") - expect_identical(dim(outbreak$linelist), c(42L, 10L)) - expect_identical(dim(outbreak$contacts), c(168L, 8L)) + expect_identical(dim(outbreak$linelist), c(42L, 11L)) + expect_identical(dim(outbreak$contacts), c(173L, 8L)) expect_identical( colnames(outbreak$linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) expect_identical( @@ -159,14 +159,14 @@ test_that("sim_outbreak works as expected with age structure", { expect_type(outbreak, type = "list") expect_s3_class(outbreak$linelist, class = "data.frame") expect_s3_class(outbreak$contacts, class = "data.frame") - expect_identical(dim(outbreak$linelist), c(42L, 10L)) - expect_identical(dim(outbreak$contacts), c(176L, 8L)) + expect_identical(dim(outbreak$linelist), c(42L, 11L)) + expect_identical(dim(outbreak$contacts), c(175L, 8L)) expect_identical( colnames(outbreak$linelist), c( "id", "case_name", "case_type", "gender", "age", "date_onset", "date_admission", "date_death", "date_first_contact", - "date_last_contact" + "date_last_contact", "ct_value" ) ) expect_identical( diff --git a/vignettes/simulist.Rmd b/vignettes/simulist.Rmd index 52836247..bca5e675 100644 --- a/vignettes/simulist.Rmd +++ b/vignettes/simulist.Rmd @@ -107,9 +107,9 @@ The way {simulist} assigns case types is by pasting case types onto existing cas The reproduction number has a strong influence on the size of an outbreak. However, the {simulist} package generates line list data using a stochastic algorithm, so even when $R < 1$ it can produce a substantial outbreak by chance, or an $R >> 1$ will sometimes not produce a vast epidemic in one simulation (i.e. one replicate) due to the stochasticity. -When requiring a minimum outbreak size we can specify the `min_chain_size` argument in `sim_linelist()`. By default this is set to 10. This means that the simulation will not return a line list until the conditioning has been met. In other words, the simulation will resimulate a branching process model until an outbreak infects at least 10 people. +When requiring a minimum outbreak size we can specify the `min_outbreak_size` argument in `sim_linelist()`. By default this is set to 10. This means that the simulation will not return a line list until the conditioning has been met. In other words, the simulation will resimulate a branching process model until an outbreak infects at least 10 people. -When requiring a line list that represents a large outbreak, such as the COVID-19 outbreak, setting the `min_chain_size` to a larger number guarantees a line list of at least that size. Here we simulate a line list requiring at least 250 cases. +When requiring a line list that represents a large outbreak, such as the COVID-19 outbreak, setting the `min_outbreak_size` to a larger number guarantees a line list of at least that size. Here we simulate a line list requiring at least 250 cases. ```{r, sim-large-linelist} linelist <- sim_linelist( @@ -117,12 +117,12 @@ linelist <- sim_linelist( serial_interval = serial_interval, onset_to_hosp = onset_to_hosp, onset_to_death = onset_to_death, - min_chain_size = 250 + min_outbreak_size = 250 ) head(linelist) ``` -The amount of time the simulation takes can be determined by the reproduction number (`R`) and the minimum outbreak size (`min_chain_size`). If the `min_outbreak_size` is large, for example hundreds or thousands of cases, and the reproduction number is below one, it will take many branching process simulations until finding one that produces a sufficiently large epidemic. +The amount of time the simulation takes can be determined by the reproduction number (`R`) and the minimum outbreak size (`min_outbreak_size`). If the `min_outbreak_size` is large, for example hundreds or thousands of cases, and the reproduction number is below one, it will take many branching process simulations until finding one that produces a sufficiently large epidemic. ## Anonymous line list