Skip to content

Commit d83da34

Browse files
authored
Issue 53: Update simulate_gillespie() documentation and argument names (#58)
* Update simulate_gillespie documentation and argument naming * Use \eqn rather than $$ for maths * Remove excess capitals * Use devtools::document() * Missed a lowercase "n" * Try to fix some linter issues * Try fix linting issues again * Update other roxygen2 text entries in simulate.R Former-commit-id: 9c58d0d Former-commit-id: 60775ecada490558f422aa815c858f79a48523a6
1 parent dd1b82e commit d83da34

7 files changed

+74
-64
lines changed

R/simulate.R

+39-38
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#' Simulate cases from a uniform distribution
22
#'
33
#' This function simulates cases from a uniform distribution, where the primary
4-
#' event times are uniformly distributed between 0 and t.
4+
#' event times are uniformly distributed between 0 and `t`.
55
#'
66
#' @param sample_size The number of cases to simulate.
77
#' @param t Upper bound of the uniform distribution to generate primary event
88
#' times.
99
#'
10-
#' @return A data table with two columns: case (case number) and ptime (primar
11-
#' event time).
10+
#' @return A `data.table` with two columns: `case` (case number) and `ptime`
11+
#' (primary event time).
1212
#'
1313
#' @family simulate
1414
#' @export
@@ -21,18 +21,17 @@ simulate_uniform_cases <- function(sample_size = 1000, t = 60) {
2121
#' Simulate exponential cases
2222
#'
2323
#' This function simulates cases from an exponential distribution. The user may
24-
#' specify the rate parameter r, the sample size, and the upper bound of the
25-
#' survival time.
26-
#' If the rate parameter is 0, then this function defaults to the uniform
27-
#' distribution.
24+
#' specify the rate parameter `r`, the sample size, and the upper bound of the
25+
#' survival time. If the rate parameter is 0, then this function defaults to the
26+
#' uniform distribution.
2827
#'
29-
#' @param r The rate parameter for the exponential distribution. Defaults to 0.2.
28+
#' @param r The exponential growth rate parameter. Defaults to 0.2.
3029
#' @param sample_size The number of cases to simulate. Defaults to 10000.
31-
#' @param seed The random seed to be used in the simulation process.
30+
#' @param seed The random seed to be used in the simulation process.
3231
#' @param t Upper bound of the survival time. Defaults to 30.
3332
#'
34-
#' @return A data table with two columns: case (case number) and ptime (primary
35-
#' event time).
33+
#' @return A `data.table` with two columns: `case` (case number) and `ptime`
34+
#' (primary event time).
3635
#'
3736
#' @family simulate
3837
#' @export
@@ -48,56 +47,55 @@ simulate_exponential_cases <- function(r = 0.2,
4847
if (r == 0) {
4948
ptime <- quant * t
5049
} else {
51-
ptime <- log(1 + quant * (exp(r * t) - 1))/r
50+
ptime <- log(1 + quant * (exp(r * t) - 1)) / r
5251
}
53-
52+
5453
cases <- data.table::data.table(
5554
case = seq_along(ptime),
5655
ptime = ptime
5756
)
5857
return(cases)
5958
}
6059

61-
#' Simulate cases from a Stochastic SIR model
60+
#' Simulate cases from a stochastic SIR model
61+
#'
62+
#' This function simulates cases from an stochastic SIR model. The user may
63+
#' specify the initial epidemic growth rate \eqn{r}, the rate of recovery gamma
64+
#' \eqn{\gamma}, the initial number of infected cases \eqn{I_0}, and the total
65+
#' population size \eqn{N}.
6266
#'
63-
#' This function simulates cases from an Stochastic SIR model. The user may
64-
#' specify the rate of infection r, the rate of recovery gamma, the initial
65-
#' number of infected cases I0, and the total population size N.
66-
#'
67-
#' @param r The rate of infection. Defaults to 0.2.
67+
#' @param r The initial epidemic growth rate. Defaults to 0.2.
6868
#' @param gamma The rate of recovery. Defaults to 1/7.
69-
#' @param init_I The initial number of infected people. Defaults to 50.
70-
#' @param n The total population size. Defaults to 10000.
71-
#' @param seed The random seed to be used in the simulation process.
69+
#' @param I0 The initial number of infected people. Defaults to 50.
70+
#' @param N The total population size. Defaults to 10000.
71+
#' @param seed The random seed to be used in the simulation process.
7272
#'
73-
#' @return A data table with two columns: case (case number) and ptime (primary
74-
#' event time).
73+
#' @return A `data.table` with two columns: `case` (case number) and `ptime`
74+
#' (primary event time).
7575
#'
7676
#' @family simulate
7777
#' @export
7878
simulate_gillespie <- function(r = 0.2,
7979
gamma = 1 / 7,
80-
init_I = 50, ## to avoid extinction
81-
n = 10000,
80+
I0 = 50, # nolint: object_name_linter
81+
N = 10000, # nolint: object_name_linter
8282
seed) {
8383
if (!missing(seed)) {
8484
set.seed(seed)
8585
}
8686
t <- 0
87-
state <- c(n - init_I, init_I, 0)
87+
state <- c(N - I0, I0, 0)
8888
beta <- r + gamma
8989
go <- TRUE
9090
ptime <- NULL
9191

9292
while (go) {
93-
rates <- c(beta * state[1] * state[2] / n, gamma * state[2])
93+
rates <- c(beta * state[1] * state[2] / N, gamma * state[2])
9494
srates <- sum(rates)
9595

9696
if (srates > 0) {
9797
deltat <- rexp(1, rate = srates)
98-
9998
t <- t + deltat
100-
10199
wevent <- sample(seq_along(rates), size = 1, prob = rates)
102100

103101
if (wevent == 1) {
@@ -106,6 +104,7 @@ simulate_gillespie <- function(r = 0.2,
106104
} else {
107105
state <- c(state[1], state[2] - 1, state[3] + 1)
108106
}
107+
109108
} else {
110109
go <- FALSE
111110
}
@@ -115,37 +114,39 @@ simulate_gillespie <- function(r = 0.2,
115114
case = seq_along(ptime),
116115
ptime = ptime
117116
)
117+
118118
return(cases)
119119
}
120120

121121
#' Simulate secondary events based on a delay distribution
122122
#'
123123
#' This function simulates secondary events based on a given delay
124124
#' distribution. The input dataset should have the primary event times in a
125-
#' column named 'ptime'.
125+
#' column named `ptime`.
126126
#'
127127
#' @param linelist A data frame with the primary event times.
128-
#' @param dist The delay distribution to be used. Defaults to rlnorm.
128+
#' @param dist The delay distribution to be used. Defaults to [rlnorm()].
129129
#' @param ... Arguments to be passed to the delay distribution function.
130130
#'
131-
#' @return A data table that augments linelist with two new columns: delay
132-
#' (secondary event latency) and stime (the time of the secondary event).
131+
#' @return A `data.table` that augments `linelist` with two new columns: `delay`
132+
#' (secondary event latency) and `stime` (the time of the secondary event).
133133
#'
134134
#' @family simulate
135135
#' @export
136136
simulate_secondary <- function(linelist, dist = rlnorm, ...) {
137+
delay <- ptime <- stime <- NULL
137138
obs <- data.table::copy(linelist)
138-
139+
139140
obs[, delay := dist(.N, ...)]
140141
obs[, stime := ptime + delay]
141-
142+
142143
return(obs)
143144
}
144145

145146
#' Simulate a censored PMF
146147
#'
147148
#' This function simulates a double-censored probability mass function (PMF).
148-
#' The user may specify the alpha, beta, and upper bound of the event times.
149+
#' The user may specify the `alpha`, `beta`, and upper bound of the event times.
149150
#' Additionally, the user can specify the random number generator functions for
150151
#' primary events, secondary events, and delays.
151152
#'
@@ -156,7 +157,7 @@ simulate_secondary <- function(linelist, dist = rlnorm, ...) {
156157
#' @param rprimary Random number generator function for primary events.
157158
#' Defaults to runif.
158159
#' @param rdelay Random number generator function for delays. Defaults to
159-
#' rlnorm.
160+
#' [rlnorm()].
160161
#' @param delay_obs_process Observation process for delays. Defaults to
161162
#' using the `floor` function to round both primary and secondary events to the
162163
#' nearest integer. Internally the delay is also bounded to be non-negative.

man/epidist-package.Rd

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_double_censored_pmf.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_exponential_cases.Rd

+6-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_gillespie.Rd

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_secondary.Rd

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_uniform_cases.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)