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

Rename nchains argument to ntrees #113

Merged
merged 2 commits into from
Dec 1, 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
2 changes: 1 addition & 1 deletion R/borel.r
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rborel <- function(n, mu, infinite = Inf) {
)
# Run simulations
simulate_summary(
nchains = n,
ntrees = n,
offspring_dist = "pois",
statistic = "size",
stat_max = infinite,
Expand Down
10 changes: 5 additions & 5 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ check_serial_valid <- function(serials_dist) {
}


#' Check that nchains is greater than 0 and not infinity
#' Check that `ntrees` is greater than 0 and not infinity
#'
#' @param nchains Number of chains to simulate.
#' @param ntrees Number of trees to simulate.
#'
#' @keywords internal
check_nchains_valid <- function(nchains) {
if (!checkmate::test_count(nchains, positive = TRUE)) {
stop("`nchains` must be > 0 but less than `Inf`")
check_ntrees_valid <- function(ntrees) {
if (!checkmate::test_count(ntrees, positive = TRUE)) {
stop("`ntrees` must be > 0 but less than `Inf`")
}
}
2 changes: 1 addition & 1 deletion R/epichains.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ tail.epichains <- function(x, ...) {
#' @examples
#' set.seed(123)
#' chains <- simulate_tree(
#' nchains = 10,
#' ntrees = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' stat_max = 10,
Expand Down
42 changes: 21 additions & 21 deletions R/simulate.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Simulate transmission trees from an initial number of infections
#'
#' @param nchains Number of chains to simulate.
#' @param ntrees Number of trees to simulate.
#' @param offspring_dist Offspring distribution: a character string
#' corresponding to the R distribution function (e.g., "pois" for Poisson,
#' where \code{\link{rpois}} is the R function to generate Poisson random
Expand All @@ -20,7 +20,7 @@
#' called `n`, that returns a numeric vector of `n` randomly sampled serial
#' intervals. See details.
#' @param t0 Start time (if serial interval is given); either a single value
#' or a vector of same length as `nchains` (number of simulations) with
#' or a vector of same length as `ntrees` (number of simulations) with
#' initial times. Defaults to 0.
#' @param tf End time (if serial interval is given).
#' @param ... Parameters of the offspring distribution as required by R.
Expand Down Expand Up @@ -91,7 +91,7 @@
#' @examples
#' set.seed(123)
#' chains <- simulate_tree(
#' nchains = 10,
#' ntrees = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' stat_max = 10,
Expand All @@ -112,14 +112,14 @@
#' Jacob C. (2010). Branching processes: their role in epidemiology.
#' International journal of environmental research and public health, 7(3),
#' 1186–1204. \doi{https://doi.org/10.3390/ijerph7031204}
simulate_tree <- function(nchains, statistic = c("size", "length"),
simulate_tree <- function(ntrees, statistic = c("size", "length"),
offspring_dist, stat_max = Inf,
serials_dist, t0 = 0,
tf = Inf, ...) {
statistic <- match.arg(statistic)

# Input checking
check_nchains_valid(nchains = nchains)
check_ntrees_valid(ntrees = ntrees)
checkmate::assert_character(statistic)

# check that offspring is properly specified
Expand Down Expand Up @@ -153,15 +153,15 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
}

# Initialisations
stat_track <- rep(1, nchains) # track length or size (depending on `statistic`) #nolint
n_offspring <- rep(1, nchains) # current number of offspring
sim <- seq_len(nchains) # track chains that are still being simulated
ancestor_ids <- rep(1, nchains) # all chains start in generation 1
stat_track <- rep(1, ntrees) # track length or size (depending on `statistic`) #nolint
n_offspring <- rep(1, ntrees) # current number of offspring
sim <- seq_len(ntrees) # track chains that are still being simulated
ancestor_ids <- rep(1, ntrees) # all chains start in generation 1

# initialise data frame to hold the transmission trees
generation <- 1L
tree_df <- data.frame(
chain_id = seq_len(nchains),
chain_id = seq_len(ntrees),
sim_id = 1L,
ancestor = NA_integer_,
generation = generation
Expand Down Expand Up @@ -190,7 +190,7 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
indices <- rep(sim, n_offspring[sim])

# initialise placeholder for the number of offspring
n_offspring <- rep(0, nchains)
n_offspring <- rep(0, ntrees)
# assign offspring sum to indices still being simulated
n_offspring[sim] <- tapply(next_gen, indices, sum)

Expand Down Expand Up @@ -257,7 +257,7 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),

structure(
tree_df,
chains = nchains,
chains = ntrees,
chain_type = "chains_tree",
rownames = NULL,
track_pop = FALSE,
Expand All @@ -282,20 +282,20 @@ simulate_tree <- function(nchains, statistic = c("size", "length"),
#' susceptible or partially immune population.
#' @examples
#' simulate_summary(
#' nchains = 10,
#' ntrees = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' stat_max = 10,
#' lambda = 2
#' )
#' @export
simulate_summary <- function(nchains, statistic = c("size", "length"),
simulate_summary <- function(ntrees, statistic = c("size", "length"),
offspring_dist,
stat_max = Inf, ...) {
statistic <- match.arg(statistic)

# Input checking
check_nchains_valid(nchains = nchains)
check_ntrees_valid(ntrees = ntrees)
checkmate::assert_character(statistic)

# check that offspring is properly specified
Expand All @@ -313,11 +313,11 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
pars <- list(...)

# Initialisations
stat_track <- rep(1, nchains) ## track length or size (depending on `stat`)
n_offspring <- rep(1, nchains) ## current number of offspring
sim <- seq_len(nchains) ## track chains that are still being simulated
stat_track <- rep(1, ntrees) ## track length or size (depending on `stat`)
n_offspring <- rep(1, ntrees) ## current number of offspring
sim <- seq_len(ntrees) ## track chains that are still being simulated

## next, simulate nchains chains
## next, simulate ntrees chains
while (length(sim) > 0) {
## simulate next generation
next_gen <- do.call(
Expand All @@ -335,7 +335,7 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
indices <- rep(sim, n_offspring[sim])

## initialise number of offspring
n_offspring <- rep(0, nchains)
n_offspring <- rep(0, ntrees)
## assign offspring sum to indices still being simulated
n_offspring[sim] <- tapply(next_gen, indices, sum)

Expand All @@ -357,7 +357,7 @@ simulate_summary <- function(nchains, statistic = c("size", "length"),
stat_track,
chain_type = "chains_summary",
statistic = statistic,
chains = nchains,
chains = ntrees,
class = c("epichains", class(stat_track))
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/stat_likelihoods.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ offspring_ll <- function(x, offspring_dist, statistic,

# Simulate the chains
dist <- simulate_summary(
nchains = nsim_offspring,
ntrees = nsim_offspring,
offspring_dist = offspring_dist,
statistic = statistic,
...
Expand Down
2 changes: 1 addition & 1 deletion man/aggregate.epichains.Rd

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

15 changes: 0 additions & 15 deletions man/check_nchains_valid.Rd

This file was deleted.

15 changes: 15 additions & 0 deletions man/check_ntrees_valid.Rd

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

6 changes: 3 additions & 3 deletions man/simulate_summary.Rd

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

8 changes: 4 additions & 4 deletions man/simulate_tree.Rd

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

10 changes: 1 addition & 9 deletions tests/testthat/test-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ test_that("Checks work", {
"must be a function"
)
expect_error(
check_serial_valid(function(x) rep("a", 10)),
"numeric"
)
expect_error(
check_serial_valid(function(x) 3),
"vector of length"
)
expect_error(
check_nchains_valid(1.1),
check_ntrees_valid(1.1),
"less than"
)
})
Loading