Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interventions vignette (and remove intvn_reduce_mean argument) #106

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# epichains 0.1.9999

## Documentation
* A vignette outlining how to simulate interventions has been added

# epichains 0.1.0

## Package name change
Expand Down
51 changes: 0 additions & 51 deletions R/intervention.R

This file was deleted.

90 changes: 0 additions & 90 deletions R/simulate.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#' Simulate transmission trees from an initial number of infections
#'
#' @inheritParams intvn_reduce_mean
#' @param nchains Number of chains to simulate.
#' @param offspring_dist Offspring distribution: a character string
#' corresponding to the R distribution function (e.g., "pois" for Poisson,
Expand Down Expand Up @@ -98,19 +97,6 @@
#' serials_dist = function(x) 3,
#' lambda = 2
#' )
#'
#' # Run model with intervention a 50% reduction in R0.
#' chains_with_intvn <- simulate_tree(
#' nchains = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' intvn_mean_reduction = 0.5,
#' stat_max = 10,
#' serials_dist = function(x) 3,
#' lambda = 2
#' )
#'
#' chains_with_intvn
#' @references
#' Lehtinen S, Ashcroft P, Bonhoeffer S. On the relationship
#' between serial interval, infectiousness profile and generation time.
Expand All @@ -127,7 +113,6 @@
#' 1186–1204. \doi{https://doi.org/10.3390/ijerph7031204}
simulate_tree <- function(nchains, statistic = c("size", "length"),
offspring_dist, stat_max = Inf,
intvn_mean_reduction = 0,
serials_dist, t0 = 0,
tf = Inf, ...) {
statistic <- match.arg(statistic)
Expand All @@ -137,29 +122,13 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
# check that offspring is properly specified
check_offspring_valid(offspring_dist)

# Check that the intvn_mean_reduction is well specified
checkmate::assert_number(
intvn_mean_reduction,
lower = 0,
upper = 1
)

# check that offspring function exists in base R
roffspring_name <- paste0("r", offspring_dist)
check_offspring_func_valid(roffspring_name)

# Gather offspring distribution parameters
pars <- list(...)

# Prepare interventions if specified
if (intvn_mean_reduction > 0) {
pars <- intvn_reduce_mean(
intvn_mean_reduction = intvn_mean_reduction,
offspring_dist = offspring_dist,
pars_list = pars
)
}

if (!missing(serials_dist)) {
check_serial_valid(serials_dist)
} else if (!missing(tf)) {
Expand Down Expand Up @@ -284,7 +253,6 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
#' Simulate transmission chains sizes/lengths
#'
#' @inheritParams simulate_tree
#' @inheritParams intvn_reduce_mean
#' @param stat_max A cut off for the chain statistic (size/length) being
#' computed. Results above the specified value, are set to `Inf`.
#' @inheritSection simulate_tree Calculating chain sizes and lengths
Expand All @@ -303,22 +271,9 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
#' stat_max = 10,
#' lambda = 2
#' )
#'
#' # Run model with intervention a 50% reduction in R0.
#' chain_summary_with_intvn <- simulate_summary(
#' nchains = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' intvn_mean_reduction = 0.5,
#' stat_max = 10,
#' lambda = 2
#' )
#'
#' chain_summary_with_intvn
#' @export
simulate_summary <- function(nchains, statistic = c("size", "length"),
offspring_dist,
intvn_mean_reduction = 0,
stat_max = Inf, ...) {
statistic <- match.arg(statistic)

Expand All @@ -327,29 +282,13 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
# check that offspring is properly specified
check_offspring_valid(offspring_dist)

# Check that the intvn_mean_reduction is well specified
checkmate::assert_number(
intvn_mean_reduction,
lower = 0,
upper = 1
)

# check that offspring function exists in base R
roffspring_name <- paste0("r", offspring_dist)
check_offspring_func_valid(roffspring_name)

# Gather offspring distribution parameters
pars <- list(...)

# Prepare interventions if specified
if (intvn_mean_reduction > 0) {
pars <- intvn_reduce_mean(
intvn_mean_reduction = intvn_mean_reduction,
offspring_dist = offspring_dist,
pars_list = pars
)
}

# Initialisations
stat_track <- rep(1, nchains) ## track length or size (depending on `stat`)
n_offspring <- rep(1, nchains) ## current number of offspring
Expand Down Expand Up @@ -404,7 +343,6 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
#' population
#'
#' @inheritParams simulate_tree
#' @inheritParams intvn_mean_reduction
#' @param pop The susceptible population size.
#' @param offspring_dist Offspring distribution: a character string
#' corresponding to the R distribution function (e.g., "pois" for Poisson,
Expand Down Expand Up @@ -465,47 +403,19 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
#' size = 1.1,
#' serials_dist = function(x) 3
#' )
#'
#' # Simulate with negative binomial offspring with intervention (50%
#' # reduction in R0)
#' simulate_tree_from_pop(
#' pop = 100,
#' offspring_dist = "nbinom",
#' intvn_mean_reduction = 0.5,
#' mu = 0.5,
#' size = 1.1,
#' serials_dist = function(x) 3
#' )
#' @export
simulate_tree_from_pop <- function(pop,
offspring_dist = c("pois", "nbinom"),
intvn_mean_reduction = 0,
serials_dist,
initial_immune = 0,
t0 = 0,
tf = Inf,
...) {
offspring_dist <- match.arg(offspring_dist)

# Check that the intvn_mean_reduction is well specified
checkmate::assert_number(
intvn_mean_reduction,
lower = 0,
upper = 1
)

# Gather offspring distribution parameters
pars <- list(...)

# Prepare interventions if specified
if (intvn_mean_reduction > 0) {
pars <- intvn_reduce_mean(
intvn_mean_reduction = intvn_mean_reduction,
offspring_dist = offspring_dist,
pars_list = pars
)
}

if (offspring_dist == "pois") {
## Use a right truncated poisson distribution
## to avoid more cases than susceptibles
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ articles:
navbar: Package vignettes
contents:
- projecting_incidence
- interventions
- title: Modelling guides and background
navbar: Modelling guides and background
contents:
Expand Down
55 changes: 0 additions & 55 deletions man/intvn_reduce_mean.Rd

This file was deleted.

19 changes: 0 additions & 19 deletions man/simulate_summary.Rd

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

20 changes: 0 additions & 20 deletions man/simulate_tree.Rd

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

Loading