]> nmode's Git Repositories - Rnaught/blob - R/WP_internal.R
Split ui.R into components
[Rnaught] / R / WP_internal.R
1 #' WP method background function WP_known
2 #'
3 #' This is a background/internal function called by \code{WP}. It computes the
4 #' maximum likelihood estimator of R0 assuming that the serial distribution is
5 #' known and finite discrete.
6 #'
7 #' @param NT Vector of case counts.
8 #' @param p Discretized version of the serial distribution.
9 #'
10 #' @return The function returns the maximum likelihood estimator of R0.
11 #'
12 #' @noRd
13 WP_known <- function(NT, p) {
14 k <- length(p)
15 TT <- length(NT) - 1
16 mu_t <- rep(0, TT)
17
18 for (i in 1:TT) {
19 Nt <- NT[i:max(1, i - k + 1)]
20 mu_t[i] <- sum(p[1:min(k, i)] * Nt)
21 }
22
23 Rhat <- sum(NT[-1]) / sum(mu_t)
24 return(Rhat)
25 }
26
27 #' WP method background function WP_unknown
28 #'
29 #' This is a background/internal function called by \code{WP}. It computes the
30 #' maximum likelihood estimator of R0 assuming that the serial distribution is
31 #' unknown but comes from a discretized gamma distribution. The function then
32 #' implements a simple grid search algorithm to obtain the maximum likelihood
33 #' estimator of R0 as well as the gamma parameters.
34 #'
35 #' @param NT Vector of case counts.
36 #' @param B Length of grid for shape and scale (grid search parameter).
37 #' @param shape.max Maximum shape value (grid \code{search} parameter).
38 #' @param scale.max Maximum scale value (grid \code{search} parameter).
39 #' @param tol cutoff value for cumulative distribution function of the serial
40 #' distribution (defaults to 0.999).
41 #'
42 #' @return The function returns \code{Rhat}, the maximum likelihood estimator of
43 #' R0, as well as the maximum likelihood estimator of the discretized
44 #' serial distribution given by \code{p} (the probability mass function)
45 #' and \code{range.max} (the distribution has support on the integers
46 #' one to \code{range.max}). The function also returns \code{resLL} (all
47 #' values of the log-likelihood) at \code{shape} (grid for shape
48 #' parameter) and at \code{scale} (grid for scale parameter), as well as
49 #' \code{resR0} (the full vector of maximum likelihood estimators),
50 #' \code{JJ} (the locations for the likelihood for these), and \code{J0}
51 #' (the location for the maximum likelihood estimator \code{Rhat}). If
52 #' \code{JJ} and \code{J0} are not the same, this means that the maximum
53 #' likelihood estimator is not unique.
54 #'
55 #' @importFrom stats pgamma qgamma
56 #'
57 #' @noRd
58 WP_unknown <- function(NT, B = 100, shape.max = 10, scale.max = 10,
59 tol = 0.999) {
60 shape <- seq(0, shape.max, length.out = B + 1)
61 scale <- seq(0, scale.max, length.out = B + 1)
62 shape <- shape[-1]
63 scale <- scale[-1]
64
65 resLL <- matrix(0, B, B)
66 resR0 <- matrix(0, B, B)
67
68 for (i in 1:B)
69 for (j in 1:B) {
70 range.max <- ceiling(qgamma(tol, shape = shape[i], scale = scale[j]))
71 p <- diff(pgamma(0:range.max, shape = shape[i], scale = scale[j]))
72 p <- p / sum(p)
73 mle <- WP_known(NT, p)
74 resLL[i, j] <- computeLL(p, NT, mle)
75 resR0[i, j] <- mle
76 }
77
78 J0 <- which.max(resLL)
79 R0hat <- resR0[J0]
80 JJ <- which(resLL == resLL[J0], arr.ind = TRUE)
81 range.max <- ceiling(qgamma(tol, shape = shape[JJ[1]], scale = scale[JJ[2]]))
82 p <- diff(pgamma(0:range.max, shape = shape[JJ[1]], scale = scale[JJ[2]]))
83 p <- p / sum(p)
84
85 return(list(Rhat = R0hat, J0 = J0, ll = resLL, Rs = resR0, scale = scale,
86 shape = shape, JJ = JJ, p = p, range.max = range.max))
87 }
88
89 #' WP method background function computeLL
90 #'
91 #' This is a background/internal function called by \code{WP}. It computes the
92 #' log-likelihood.
93 #'
94 #' @param p Discretized version of the serial distribution.
95 #' @param NT Vector of case counts.
96 #' @param R0 Basic reproductive ratio.
97 #'
98 #' @return This function returns the log-likelihood at the input variables and
99 #' parameters.
100 #'
101 #' @noRd
102 computeLL <- function(p, NT, R0) {
103 k <- length(p)
104 TT <- length(NT) - 1
105 mu_t <- rep(0, TT)
106
107 for (i in 1:TT) {
108 Nt <- NT[i:max(1, i - k + 1)]
109 mu_t[i] <- sum(p[1:min(k, i)] * Nt)
110 }
111
112 mu_t <- R0 * mu_t
113 LL <- sum(NT[-1] * log(mu_t)) - sum(mu_t)
114
115 return(LL)
116 }