]>
nmode's Git Repositories - Rnaught/blob - R/WP.R
c8178b7b168c462175bb5e4a320d78a98858c569
3 #' This function implements an R0 estimation due to White and Pagano (Statistics in Medicine, 2008).
4 #' The method is based on maximum likelihood estimation in a Poisson transmission model.
5 #' See details for important implementation notes.
7 #' This method is based on a Poisson transmission model, and hence may be most most valid at the beginning
8 #' of an epidemic. In their model, the serial distribution is assumed to be discrete with a finite number
9 #' of posible values. In this implementation, if \code{mu} is not {NA}, the serial distribution is taken to
10 #' be a discretized version of a gamma distribution with mean \code{mu}, shape parameter one, and largest
11 #' possible value based on parameter \code{tol}. When \code{mu} is \code{NA}, the function implements a
12 #' grid search algorithm to find the maximum likelihood estimator over all possible gamma distributions
13 #' with unknown mean and variance, restricting these to a prespecified grid (see \code{search} parameter).
15 #' When the serial distribution is known (i.e., \code{mu} is not \code{NA}), sensitivity testing of \code{mu}
16 #' is strongly recommended. If the serial distribution is unknown (i.e., \code{mu} is \code{NA}), the
17 #' likelihood function can be flat near the maximum, resulting in numerical instability of the optimizer.
18 #' When \code{mu} is \code{NA}, the implementation takes considerably longer to run. Users should be careful
19 #' about units of time (e.g., are counts observed daily or weekly?) when implementing.
21 #' The model developed in White and Pagano (2008) is discrete, and hence the serial distribution is finite
22 #' discrete. In our implementation, the input value \code{mu} is that of a continuous distribution. The
23 #' algorithm discretizes this input when \code{mu} is not \code{NA}, and hence the mean of the serial
24 #' distribution returned in the list \code{SD} will differ from \code{mu} somewhat. That is to say, if the
25 #' user notices that the input \code{mu} and output mean of \code{SD} are different, this is to be expected,
26 #' and is caused by the discretization.
28 #' @param NT Vector of case counts.
29 #' @param mu Mean of the serial distribution (needs to match case counts in time units; for example, if case
30 #' counts are weekly and the serial distribution has a mean of seven days, then \code{mu} should be
31 #' set to one). The default value of \code{mu} is set to \code{NA}.
32 #' @param search List of default values for the grid search algorithm. The list includes three elements: the
33 #' first is \code{B}, which is the length of the grid in one dimension; the second is
34 #' \code{scale.max}, which is the largest possible value of the scale parameter; and the third
35 #' is \code{shape.max}, which is the largest possible value of the shape parameter. Defaults to
36 #' \code{B=100, scale.max=10, shape.max=10}. For both shape and scale, the smallest possible
37 #' value is 1/\code{B}.
38 #' @param tol Cutoff value for cumulative distribution function of the pre-discretization gamma serial
39 #' distribution. Defaults to 0.999 (i.e. in the discretization, the maximum is chosen such that the
40 #' original gamma distribution has cumulative probability of no more than 0.999 at this maximum).
42 #' @return \code{WP} returns a list containing the following components: \code{Rhat} is the estimate of R0,
43 #' and \code{SD} is either the discretized serial distribution (if \code{mu} is not \code{NA}), or the
44 #' estimated discretized serial distribution (if \code{mu} is \code{NA}). The list also returns the
45 #' variable \code{check}, which is equal to the number of non-unique maximum likelihood estimators.
46 #' The serial distribution \code{SD} is returned as a list made up of \code{supp} (the support of
47 #' the distribution) and \code{pmf} (the probability mass function).
50 #' ## ===================================================== ##
51 #' ## Illustrate on weekly data ##
52 #' ## ===================================================== ##
54 #' NT <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
55 #' ## obtain Rhat when serial distribution has mean of five days
56 #' res1 <- WP(NT=NT, mu=5/7)
58 #' ## obtain Rhat when serial distribution has mean of three days
59 #' res2 <- WP(NT=NT, mu=3/7)
61 #' ## obtain Rhat when serial distribution is unknown
62 #' ## NOTE: this implementation will take longer to run
65 #' ## find mean of estimated serial distribution
67 #' sum(serial$supp * serial$pmf)
69 #' ## ========================================================= ##
70 #' ## Compute Rhat using only the first five weeks of data ##
71 #' ## ========================================================= ##
73 #' res4 <- WP(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
76 #' @importFrom stats pexp qexp
79 WP
<- function(NT
, mu
=NA, search
=list(B
=100, shape.max
=10, scale.max
=10), tol
=0.999) {
81 print("You have assumed that the serial distribution is unknown.")
82 res
<- WP_unknown(NT
=NT
, B
=search$B
, shape.max
=search$shape.max
, scale.max
=search$scale.max
, tol
=tol
)
85 range.max
<- res$range.max
88 print("You have assumed that the serial distribution is known.")
89 range.max
<- ceiling(qexp(tol
, rate
=1/mu
))
90 p
<- diff(pexp(0:range.max
, 1/mu
))
92 res
<- WP_known(NT
=NT
, p
=p
)
97 return(list(Rhat
=Rhat
, check
=length(JJ
), SD
=list(supp
=1:range.max
, pmf
=p
)))