]> nmode's Git Repositories - Rnaught/blob - R/idea.R
Add GitHub action to run 'R CMD check'
[Rnaught] / R / idea.R
1 #' Incidence Decay and Exponential Adjustment (IDEA)
2 #'
3 #' This function implements a least squares estimation method of R0 due to
4 #' Fisman et al. (PloS One, 2013). See details for implementation notes.
5 #'
6 #' This method is closely related to that implemented in [id()]. The method is
7 #' based on an incidence decay model. The estimate of R0 is the value which
8 #' minimizes the sum of squares between observed case counts and case counts
9 #' expected under the model.
10 #'
11 #' This method is based on an approximation of the SIR model, which is most
12 #' valid at the beginning of an epidemic. The method assumes that the mean of
13 #' the serial distribution (sometimes called the serial interval) is known. The
14 #' final estimate can be quite sensitive to this value, so sensitivity testing
15 #' is strongly recommended. Users should be careful about units of time (e.g.,
16 #' are counts observed daily or weekly?) when implementing.
17 #'
18 #' @param cases Vector of case counts. The vector must be of length at least two
19 #' and only contain positive integers.
20 #' @param mu Mean of the serial distribution. This must be a positive number.
21 #' The value should match the case counts in time units. For example, if case
22 #' counts are weekly and the serial distribution has a mean of seven days,
23 #' then `mu` should be set to `1`. If case counts are daily and the serial
24 #' distribution has a mean of seven days, then `mu` should be set to `7`.
25 #'
26 #' @return An estimate of the basic reproduction number (R0).
27 #'
28 #' @references [Fisman et al. (PloS One, 2013)](
29 #' https://doi.org/10.1371/journal.pone.0083622)
30 #'
31 #' @seealso [id()] for a similar method.
32 #'
33 #' @export
34 #'
35 #' @examples
36 #' # Weekly data.
37 #' cases <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
38 #'
39 #' # Obtain R0 when the serial distribution has a mean of five days.
40 #' idea(cases, mu = 5 / 7)
41 #'
42 #' # Obtain R0 when the serial distribution has a mean of three days.
43 #' idea(cases, mu = 3 / 7)
44 idea <- function(cases, mu) {
45 validate_cases(cases, min_length = 2, min_count = 1)
46 if (!is_real(mu) || mu <= 0) {
47 stop("The serial interval (`mu`) must be a number greater than 0.",
48 call. = FALSE
49 )
50 }
51
52 s <- seq_along(cases) / mu
53
54 x1 <- sum(s)
55 x2 <- sum(s^2)
56 x3 <- log(cases)
57
58 y1 <- x2 * sum(x3 / s) - x1 * sum(x3)
59 y2 <- x2 * length(cases) - x1^2
60
61 exp(y1 / y2)
62 }