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