Skip to content

Commit

Permalink
Rename nchains argument to ntrees
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Nov 28, 2023
1 parent b77986b commit 9511c5e
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 89 deletions.
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
4 changes: 2 additions & 2 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ 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_ntrees_valid <- function(ntrees) {
Expand Down
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 @@ -19,7 +19,7 @@
#' of a user-defined named or anonymous function with only one argument `n`,
#' representing the number of serial intervals to generate. 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 @@ -90,7 +90,7 @@
#' @examples
#' set.seed(123)
#' chains <- simulate_tree(
#' nchains = 10,
#' ntrees = 10,
#' statistic = "size",
#' offspring_dist = "pois",
#' stat_max = 10,
Expand All @@ -111,14 +111,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 @@ -152,15 +152,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 @@ -189,7 +189,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 @@ -256,7 +256,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 @@ -281,20 +281,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 @@ -312,11 +312,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 @@ -334,7 +334,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 @@ -356,7 +356,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.

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.

2 changes: 1 addition & 1 deletion tests/testthat/test-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("Checks work", {
"must be a function"
)
expect_error(
check_nchains_valid(1.1),
check_ntrees_valid(1.1),
"less than"
)
})
Loading

0 comments on commit 9511c5e

Please sign in to comment.