]> nmode's Git Repositories - Rnaught/blob - R/seqB.R
Update seqB.R
[Rnaught] / R / seqB.R
1 #' seqB method
2 #'
3 #' This function implements a sequential Bayesian estimation method of R0 due to Bettencourt and Riberio (PloS One, 2008). See details for important implementation notes.
4 #'
5 #'The method sets a uniform prior distribution on R0 with possible values between zero and \code{kappa}, discretized to a fine grid. The distribution of R0 is then updated sequentially, with one update for each new case count observation. The final estimate of R0 is \code{Rhat}, the mean of the (last) posterior distribution.
6 #' The prior distribution is the initial belief of the distribution of R0; which in this implementation is the uninformative uniform distribution with values between zero and kappa. Users can change the value of kappa only (ie. the prior distribution cannot be changed from the uniform). As more case counts are observed, the influence of the prior distribution should lessen on the final estimate \code{Rhat}.
7 #'
8 #' This method is based on an approximation of the SIR model, which is most valid at the beginning of an epidemic. The method assumes that the mean of the serial distribution (sometimes called the serial interval) is known. The final estimate can be quite sensitive to this value, so sensitivity testing is strongly recommended. Users should be careful about units of time (e.g. are counts observed daily or weekly?) when implementing.
9 #'
10 #' Our code has been modified to provide an estimate even if case counts equal to zero are present in some time intervals. This is done by grouping the counts over such periods of time. Without grouping, and in the presence of zero counts, no estimate can be provided.
11 #'
12 #' @param NT Vector of case counts
13 #' @param mu Mean of the serial distribution (needs to match case counts in time units; for example, if case counts are weekly and the serial distribution has a mean of seven days, then mu should be set to 1=7/7, if case counts are dailty and the serial distribution has a mean of seven days, then mu should be set to seven).
14 #' @param kappa Largest possible value of uniform prior, defaults to 20. This describes the prior belief on ranges of R0, so should be set to a higher value if R0 is believed to be larger.
15 #'
16 #' @return secB returns a list containing the following components: \code{Rhat} is the estimate of R0 (the posterior mean), \code{posterior} is the posterior distribution of R0 from which alternate estimates can be obtained (see examples), \code{group} is an indicator variable (if \code{group=TRUE}, zero values of NT were input and grouping was done to obtain \code{Rhat}), and \code{inputs} is a list of the original input variables \code{NT, gamma, kappa}. The variable \code{posterior} is returned as a list made up of \code{supp} the support of the distribution and \code{pmf} the probability mass function.
17 #'
18 #' @examples
19 #'
20 #' ## ===================================================== ##
21 #' ## Illustrate on weekly data ##
22 #' ## ===================================================== ##
23 #'
24 #' NT <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
25 #' ## obtain Rhat when serial distribution has mean of five days
26 #' res1 <- seqB(NT=NT, mu=5/7)
27 #' res1$Rhat
28 #' ## obtain Rhat when serial distribution has mean of three days
29 #' res2 <- seqB(NT=NT, mu=3/7)
30 #' res2$Rhat
31 #'
32 #' ## ============================================================= ##
33 #' ## Compute posterior mode instead of posterior mean and plot ##
34 #' ## ============================================================= ##
35 #'
36 #' Rpost <- res1$posterior
37 #' loc <- which(Rpost$pmf==max(Rpost$pmf))
38 #' Rpost$supp[loc] # posterior mode
39 #' res1$Rhat # compare with posterior mean
40 #'
41 #' par(mfrow=c(2,1), mar=c(2,2,1,1))
42 #' plot(Rpost$supp, Rpost$pmf, col="black", type="l", xlab="", ylab="")
43 #' abline(h=1/(20/0.01+1), col="red")
44 #' abline(v=res1$Rhat, col="blue")
45 #' abline(v=Rpost$supp[loc], col="purple")
46 #' legend("topright", legend=c("prior", "posterior", "posterior mean (Rhat)", "posterior mode"), col=c("red", "black", "blue", "purple"), lty=1)
47 #' plot(Rpost$supp, Rpost$pmf, col="black", type="l", xlim=c(0.5,1.5), xlab="", ylab="")
48 #' abline(h=1/(20/0.01+1), col="red")
49 #' abline(v=res1$Rhat, col="blue")
50 #' abline(v=Rpost$supp[loc], col="purple")
51 #' legend("topright", legend=c("prior", "posterior", "posterior mean (Rhat)", "posterior mode"), col=c("red", "black", "blue", "purple"), lty=1)
52 #'
53 #' ## ========================================================= ##
54 #' ## Compute Rhat using only the first five weeks of data ##
55 #' ## ========================================================= ##
56 #'
57 #'
58 #' res3 <- seqB(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
59 #' res3$Rhat
60 #' @export
61
62 seqB <- function(NT, mu, kappa=20){
63
64 gamma <- 1/mu
65
66 if(length(NT)<2) {
67 print("Warning: length of NT should be at least two.")
68 }
69 else{
70 if(min(NT)>0){
71 times <- 1:length(NT)
72 tau <- diff(times)
73 }
74
75 group <- FALSE
76
77 if(min(NT)==0){
78 times <- which(NT>0)
79 NT <- NT[times]
80 tau <- diff(times)
81 group <- TRUE
82 }
83
84 R <- seq(0, kappa, 0.01)
85 prior0 <- rep(1,kappa/0.01+1)
86 prior0 <- prior0/sum(prior0)
87
88 k <- length(NT)-1
89 R0.post <- matrix(0, nrow=k, ncol=length(R))
90
91 prior <- prior0
92 posterior <- seq(0, length(prior0))
93
94 for(i in 1:k){
95
96 mm1 <- NT[i]
97 mm2 <- NT[i+1]
98 lambda <- tau[i]*gamma*(R-1)
99 lambda <- log(mm1)+lambda
100 loglik <- mm2*lambda-exp(lambda)
101
102 # numerical issues solve???
103
104 # if((mm1==0)*(mm2==0)==0){
105 maxll <- max(loglik)
106 const <- 0
107 if(maxll>700){
108 const <- maxll-700
109 }
110 loglik <- loglik-const
111 # }
112
113 # end numerical issues solve???
114
115 posterior <- exp(loglik)*prior
116 posterior <- posterior/sum(posterior)
117
118
119 prior <- posterior
120
121 }
122
123 Rhat <- sum(R*posterior)
124
125 return(list(Rhat=Rhat, posterior=list(supp=R, pmf=posterior), group=group, inputs=list(NT=NT, mu=mu, kappa=kappa)))
126 }
127 }
128
129