Skip to content

Commit

Permalink
replace instances of delayfn with onset_to_isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlambert committed Aug 21, 2024
1 parent 891f932 commit e5775b7
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 78 deletions.
8 changes: 2 additions & 6 deletions R/outbreak_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ outbreak_model <- function(num_initial_cases,

# TODO: replace all instances of incfn with incubation_period
incfn <- incubation_period

# TODO: replace all instances of delayfn with onset_to_isolation
delayfn <- onset_to_isolation

# incfn <- dist_setup(dist_shape = 3.303525,dist_scale = 6.68849) # incubation function for ECDC run

# Set initial values for loop indices
Expand All @@ -112,7 +108,7 @@ outbreak_model <- function(num_initial_cases,
case_data <- outbreak_setup(num_initial_cases = num_initial_cases,
incfn = incfn,
prop_asym = prop_asym,
delayfn = delayfn,
onset_to_isolation = onset_to_isolation,
k = k)

# Preallocate
Expand All @@ -131,7 +127,7 @@ outbreak_model <- function(num_initial_cases,
r0community = r0community,
r0subclin = r0subclin,
incfn = incfn,
delayfn = delayfn,
onset_to_isolation = onset_to_isolation,
prop_ascertain = prop_ascertain,
k = k,
quarantine = quarantine,
Expand Down
20 changes: 16 additions & 4 deletions R/outbreak_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
#' # incubation period sampling function
#' incfn <- dist_setup(dist_shape = 2.322737,dist_scale = 6.492272)
#' # delay distribution sampling function
#' delayfn <- dist_setup(delay_shape, delay_scale)
#' outbreak_setup(num_initial_cases = 5,incfn,delayfn,k=1.95,prop_asym=0)
#' onset_to_isolation <- function(x) {
#' rweibull(n = x, shape = 1.651524, scale = 4.287786)
#' }
#' outbreak_setup(
#' num_initial_cases = 5,
#' incfn,
#' onset_to_isolation,
#' k = 1.95,
#' prop_asym = 0
#' )
#'}
outbreak_setup <- function(num_initial_cases, incfn, delayfn, k, prop_asym) {
outbreak_setup <- function(num_initial_cases,
incfn,
onset_to_isolation,
k,
prop_asym) {
# Set up table of initial cases
inc_samples <- incfn(num_initial_cases)

Expand All @@ -36,7 +48,7 @@ outbreak_setup <- function(num_initial_cases, incfn, delayfn, k, prop_asym) {

# set isolation time for cluster to minimum time of onset of symptoms + draw
# from delay distribution
case_data <- case_data[, isolated_time := onset + delayfn(1)
case_data <- case_data[, isolated_time := onset + onset_to_isolation(1)
][, isolated := FALSE]

case_data$isolated_time[case_data$asym] <- Inf
Expand Down
48 changes: 28 additions & 20 deletions R/outbreak_step.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#' @param case_data data.table of cases in outbreak so far; initially generated by outbreak_setup
#' @param incfn `function` that samples from incubation period (Weibull
#' distribution); generated by [dist_setup()]
#' @param delayfn `function` that samples from the onset-to-hospitalisation
#' delay (Weibull distribution); generated by [dist_setup()]
#' @importFrom data.table data.table rbindlist
#' @importFrom purrr map2 map2_dbl map_lgl
#' @importFrom stats rbinom
Expand All @@ -20,23 +18,33 @@
#' # incubation period sampling function
#' incfn <- dist_setup(dist_shape = 2.322737,dist_scale = 6.492272)
#' # delay distribution sampling function
#' delayfn <- dist_setup(1.651524, 4.287786)
#' onset_to_isolation <- function(x) {
#' rweibull(n = x, shape = 1.651524, scale = 4.287786)
#' }
#' # generate initial cases
#' case_data <- outbreak_setup(num_initial_cases = 5,incfn,delayfn,k=1.95,prop_asym=0)
#' case_data <- outbreak_setup(
#' num_initial_cases = 5,
#' incfn,
#' onset_to_isolation,
#' k = 1.95,
#' prop_asym = 0
#' )
#' # generate next generation of cases
#' case_data <- outbreak_step(case_data = case_data,
#' disp_iso = 1,
#' disp_com = 0.16,
#' r0isolated = 0,
#' r0subclin = 1.25,
#' disp_subclin = 0.16,
#' r0community = 2.5,
#' prop_asym = 0,
#' incfn = incfn,
#' delayfn = delayfn,
#' prop_ascertain = 0,
#' k = 1.95,
#' quarantine = FALSE)[[1]]
#' case_data <- outbreak_step(
#' case_data = case_data,
#' disp_iso = 1,
#' disp_com = 0.16,
#' r0isolated = 0,
#' r0subclin = 1.25,
#' disp_subclin = 0.16,
#' r0community = 2.5,
#' prop_asym = 0,
#' incfn = incfn,
#' onset_to_isolation = onset_to_isolation,
#' prop_ascertain = 0,
#' k = 1.95,
#' quarantine = FALSE
#' )[[1]]
#'}
outbreak_step <- function(case_data,
prop_ascertain,
Expand All @@ -50,7 +58,7 @@ outbreak_step <- function(case_data,
prop_asym,
quarantine,
incfn,
delayfn) {
onset_to_isolation) {

# For each case in case_data, draw new_cases from a negative binomial distribution
# with an R0 and dispersion dependent on if isolated=TRUE
Expand Down Expand Up @@ -129,11 +137,11 @@ outbreak_step <- function(case_data,
prob_samples[, isolated_time := ifelse(vect_is_true(asym), Inf,
# If you are not asymptomatic, but you are missed,
# you are isolated at your symptom onset
ifelse(vect_is_true(missed), onset + delayfn(1),
ifelse(vect_is_true(missed), onset + onset_to_isolation(1),
# If you are not asymptomatic and you are traced,
# you are isolated at max(onset,infector isolation time) # max(onset,infector_iso_time)
ifelse(!vect_is_true(rep(quarantine, total_new_cases)),
pmin(onset + delayfn(1), pmax(onset, infector_iso_time)),
pmin(onset + onset_to_isolation(1), pmax(onset, infector_iso_time)),
infector_iso_time)))]


Expand Down
21 changes: 16 additions & 5 deletions man/outbreak_setup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 31 additions & 18 deletions man/outbreak_step.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions man/scenario_sim.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/test-outbreak_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ set.seed(20200410)
test_that("A basic sim setup returns the correct object", {
incfn <- dist_setup(dist_shape = 2.322737, dist_scale = 6.492272)
# delay distribution sampling function
delayfn <- dist_setup(2, 4)
onset_to_isolation <- function(x) rweibull(n = x, shape = 2, scale = 4)
# generate initial cases
case_data <- outbreak_setup(
num_initial_cases = 5,
incfn = incfn,
delayfn = delayfn,
onset_to_isolation = onset_to_isolation,
k = 1.95,
prop_asym = 0
)
Expand All @@ -23,13 +23,13 @@ test_that("A basic sim setup returns the correct object", {
test_that("asym arg works properly", {
incfn <- dist_setup(dist_shape = 2.322737, dist_scale = 6.492272)
# delay distribution sampling function
delayfn <- dist_setup(2, 4)
onset_to_isolation <- function(x) rweibull(n = x, shape = 2, scale = 4)
# generate initial cases
# All asymptomatics
all_asym <- outbreak_setup(
num_initial_cases = 5,
incfn = incfn,
delayfn = delayfn,
onset_to_isolation = onset_to_isolation,
k = 1.95,
prop_asym = 1
)
Expand All @@ -41,7 +41,7 @@ test_that("asym arg works properly", {
mix <- outbreak_setup(
num_initial_cases = 10000,
incfn = incfn,
delayfn = delayfn,
onset_to_isolation = onset_to_isolation,
k = 1.95,
prop_asym = 0.5
)
Expand Down
Loading

0 comments on commit e5775b7

Please sign in to comment.