1
1
# ' Simulate cases from a uniform distribution
2
2
# '
3
3
# ' 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` .
5
5
# '
6
6
# ' @param sample_size The number of cases to simulate.
7
7
# ' @param t Upper bound of the uniform distribution to generate primary event
8
8
# ' times.
9
9
# '
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).
12
12
# '
13
13
# ' @family simulate
14
14
# ' @export
@@ -21,18 +21,17 @@ simulate_uniform_cases <- function(sample_size = 1000, t = 60) {
21
21
# ' Simulate exponential cases
22
22
# '
23
23
# ' 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.
28
27
# '
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.
30
29
# ' @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.
32
31
# ' @param t Upper bound of the survival time. Defaults to 30.
33
32
# '
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).
36
35
# '
37
36
# ' @family simulate
38
37
# ' @export
@@ -48,56 +47,55 @@ simulate_exponential_cases <- function(r = 0.2,
48
47
if (r == 0 ) {
49
48
ptime <- quant * t
50
49
} else {
51
- ptime <- log(1 + quant * (exp(r * t ) - 1 ))/ r
50
+ ptime <- log(1 + quant * (exp(r * t ) - 1 )) / r
52
51
}
53
-
52
+
54
53
cases <- data.table :: data.table(
55
54
case = seq_along(ptime ),
56
55
ptime = ptime
57
56
)
58
57
return (cases )
59
58
}
60
59
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}.
62
66
# '
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.
68
68
# ' @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.
72
72
# '
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).
75
75
# '
76
76
# ' @family simulate
77
77
# ' @export
78
78
simulate_gillespie <- function (r = 0.2 ,
79
79
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
82
82
seed ) {
83
83
if (! missing(seed )) {
84
84
set.seed(seed )
85
85
}
86
86
t <- 0
87
- state <- c(n - init_I , init_I , 0 )
87
+ state <- c(N - I0 , I0 , 0 )
88
88
beta <- r + gamma
89
89
go <- TRUE
90
90
ptime <- NULL
91
91
92
92
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 ])
94
94
srates <- sum(rates )
95
95
96
96
if (srates > 0 ) {
97
97
deltat <- rexp(1 , rate = srates )
98
-
99
98
t <- t + deltat
100
-
101
99
wevent <- sample(seq_along(rates ), size = 1 , prob = rates )
102
100
103
101
if (wevent == 1 ) {
@@ -106,6 +104,7 @@ simulate_gillespie <- function(r = 0.2,
106
104
} else {
107
105
state <- c(state [1 ], state [2 ] - 1 , state [3 ] + 1 )
108
106
}
107
+
109
108
} else {
110
109
go <- FALSE
111
110
}
@@ -115,37 +114,39 @@ simulate_gillespie <- function(r = 0.2,
115
114
case = seq_along(ptime ),
116
115
ptime = ptime
117
116
)
117
+
118
118
return (cases )
119
119
}
120
120
121
121
# ' Simulate secondary events based on a delay distribution
122
122
# '
123
123
# ' This function simulates secondary events based on a given delay
124
124
# ' distribution. The input dataset should have the primary event times in a
125
- # ' column named ' ptime' .
125
+ # ' column named ` ptime` .
126
126
# '
127
127
# ' @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()] .
129
129
# ' @param ... Arguments to be passed to the delay distribution function.
130
130
# '
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).
133
133
# '
134
134
# ' @family simulate
135
135
# ' @export
136
136
simulate_secondary <- function (linelist , dist = rlnorm , ... ) {
137
+ delay <- ptime <- stime <- NULL
137
138
obs <- data.table :: copy(linelist )
138
-
139
+
139
140
obs [, delay : = dist(.N , ... )]
140
141
obs [, stime : = ptime + delay ]
141
-
142
+
142
143
return (obs )
143
144
}
144
145
145
146
# ' Simulate a censored PMF
146
147
# '
147
148
# ' 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.
149
150
# ' Additionally, the user can specify the random number generator functions for
150
151
# ' primary events, secondary events, and delays.
151
152
# '
@@ -156,7 +157,7 @@ simulate_secondary <- function(linelist, dist = rlnorm, ...) {
156
157
# ' @param rprimary Random number generator function for primary events.
157
158
# ' Defaults to runif.
158
159
# ' @param rdelay Random number generator function for delays. Defaults to
159
- # ' rlnorm.
160
+ # ' [ rlnorm()] .
160
161
# ' @param delay_obs_process Observation process for delays. Defaults to
161
162
# ' using the `floor` function to round both primary and secondary events to the
162
163
# ' nearest integer. Internally the delay is also bounded to be non-negative.
0 commit comments