]>
nmode's Git Repositories - Rnaught/blob - R/WP_internal.R
1 #' WP method background function WP_known
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.
7 #' @param NT Vector of case counts.
8 #' @param p Discretized version of the serial distribution.
10 #' @return The function returns the maximum likelihood estimator of R0.
13 WP_known
<- function(NT
, p
) {
19 Nt
<- NT
[i
:max(1, i
- k
+ 1)]
20 mu_t
[i
] <- sum(p
[1:min(k
, i
)] * Nt
)
23 Rhat
<- sum(NT
[-1]) / sum(mu_t
)
27 #' WP method background function WP_unknown
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.
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).
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.
55 #' @importFrom stats pgamma qgamma
58 WP_unknown
<- function(NT
, B
= 100, shape.max
= 10, scale.max
= 10,
60 shape
<- seq(0, shape.max
, length.out
= B
+ 1)
61 scale
<- seq(0, scale.max
, length.out
= B
+ 1)
65 resLL
<- matrix(0, B
, B
)
66 resR0
<- matrix(0, B
, 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
]))
73 mle
<- WP_known(NT
, p
)
74 resLL
[i
, j
] <- computeLL(p
, NT
, mle
)
78 J0
<- which.max(resLL
)
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]]))
85 return(list(Rhat
= R0hat
, J0
= J0
, ll
= resLL
, Rs
= resR0
, scale
= scale
,
86 shape
= shape
, JJ
= JJ
, p
= p
, range.max
= range.max
))
89 #' WP method background function computeLL
91 #' This is a background/internal function called by \code{WP}. It computes the
94 #' @param NT Vector of case counts.
95 #' @param p Discretized version of the serial distribution.
96 #' @param R0 Basic reproductive ratio.
98 #' @return This function returns the log-likelihood at the input variables and
101 #' @keywords internal
102 computeLL
<- function(p
, NT
, R0
) {
108 Nt
<- NT
[i
:max(1, i
- k
+ 1)]
109 mu_t
[i
] <- sum(p
[1:min(k
, i
)] * Nt
)
113 LL
<- sum(NT
[-1] * log(mu_t
)) - sum(mu_t
)