diff options
Diffstat (limited to 'R')
-rw-r--r-- | R/ID.R | 13 | ||||
-rw-r--r-- | R/IDEA.R | 13 | ||||
-rw-r--r-- | R/WP.R | 10 | ||||
-rw-r--r-- | R/WP_known.R | 2 | ||||
-rw-r--r-- | R/WP_unknown.R | 4 | ||||
-rw-r--r-- | R/seqB.R | 13 |
6 files changed, 22 insertions, 33 deletions
@@ -20,26 +20,21 @@ #' \code{inputs} is a list of the original input variables \code{NT, mu}.
#'
#' @examples
-#'
#' ## ===================================================== ##
#' ## Illustrate on weekly data ##
#' ## ===================================================== ##
#'
#' NT <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
#' ## obtain Rhat when serial distribution has mean of five days
-#' res1 <- ID(NT=NT, mu=5/7)
-#' res1$Rhat
+#' ID(NT=NT, mu=5/7)
#' ## obtain Rhat when serial distribution has mean of three days
-#' res2 <- ID(NT=NT, mu=3/7)
-#' res2$Rhat
+#' ID(NT=NT, mu=3/7)
#'
#' ## ========================================================= ##
#' ## Compute Rhat using only the first five weeks of data ##
#' ## ========================================================= ##
#'
-#'
-#' res3 <- ID(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
-#' res3$Rhat
+#' ID(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
#'
#' @export
ID <- function(NT, mu) {
@@ -50,5 +45,5 @@ ID <- function(NT, mu) { R0_ID <- exp(sum(y) / TT)
- return(list=c(Rhat=R0_ID, inputs=list(NT=NT, mu=mu)))
+ return(R0_ID)
}
@@ -21,26 +21,21 @@ #' \code{inputs} is a list of the original input variables \code{NT, mu}.
#'
#' @examples
-#'
#' ## ===================================================== ##
#' ## Illustrate on weekly data ##
#' ## ===================================================== ##
#'
#' NT <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
#' ## obtain Rhat when serial distribution has mean of five days
-#' res1 <- IDEA(NT=NT, mu=5/7)
-#' res1$Rhat
+#' IDEA(NT=NT, mu=5/7)
#' ## obtain Rhat when serial distribution has mean of three days
-#' res2 <- IDEA(NT=NT, mu=3/7)
-#' res2$Rhat
+#' IDEA(NT=NT, mu=3/7)
#'
#' ## ========================================================= ##
#' ## Compute Rhat using only the first five weeks of data ##
#' ## ========================================================= ##
#'
-#'
-#' res3 <- IDEA(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
-#' res3$Rhat
+#' IDEA(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days
#'
#' @export
IDEA <- function(NT, mu) {
@@ -59,6 +54,6 @@ IDEA <- function(NT, mu) { IDEA2 <- TT * sum(y2) - sum(s)^2
IDEA <- exp(IDEA1 / IDEA2)
- return(list(Rhat=IDEA, inputs=list(NT=NT, mu=mu)))
+ return(IDEA)
}
}
@@ -68,14 +68,14 @@ #' res3 <- WP(NT=NT) #' res3$Rhat #' ## find mean of estimated serial distribution -#' serial <- res3$SD -#' sum(serial$supp*serial$pmf) +#' serial <- res3$SD +#' sum(serial$supp * serial$pmf) #' #' ## ========================================================= ## #' ## Compute Rhat using only the first five weeks of data ## #' ## ========================================================= ## #' -#' res4 <- WP(NT=NT[1:5], mu=5/7, method="known") # serial distribution has mean of five days +#' res4 <- WP(NT=NT[1:5], mu=5/7, method="known") # serial distribution has mean of five days #' res4$Rhat #' #' @export @@ -99,10 +99,10 @@ WP <- function(NT, mu="NA", method="unknown", search=list(B=100, shape.max=10, s p <- diff(pexp(0:range.max, 1/mu)) p <- p / sum(p) res <- WP_known(NT=NT, p=p) - Rhat <- res$Rhat + Rhat <- res JJ <- NA } } - return(list(Rhat=Rhat, check=length(JJ), SD=list(supp=1:range.max, pmf=p), inputs=list(NT=NT, mu=mu, method=method, search=search, tol=tol))) + return(list(Rhat=Rhat, check=length(JJ), SD=list(supp=1:range.max, pmf=p))) } diff --git a/R/WP_known.R b/R/WP_known.R index 6b0e2ea..563b7ae 100644 --- a/R/WP_known.R +++ b/R/WP_known.R @@ -19,5 +19,5 @@ WP_known <- function(NT, p) { } Rhat <- sum(NT[-1]) / sum(mu_t) - return(list(Rhat=Rhat)) + return(Rhat) } diff --git a/R/WP_unknown.R b/R/WP_unknown.R index c5b0a35..ebb7cc7 100644 --- a/R/WP_unknown.R +++ b/R/WP_unknown.R @@ -36,8 +36,8 @@ WP_unknown <- function(NT, B=100, shape.max=10, scale.max=10, tol=0.999) { p <- diff(pgamma(0:range.max, shape=shape[i], scale=scale[j])) p <- p / sum(p) mle <- WP_known(NT, p) - resLL[i,j] <- computeLL(p, NT, mle$R) - resR0[i,j] <- mle$R + resLL[i,j] <- computeLL(p, NT, mle) + resR0[i,j] <- mle } } @@ -51,17 +51,17 @@ #' ## ============================================================= ## #' #' Rpost <- res1$posterior -#' loc <- which(Rpost$pmf==max(Rpost$pmf)) -#' Rpost$supp[loc] # posterior mode -#' res1$Rhat # compare with posterior mean +#' loc <- which(Rpost$pmf == max(Rpost$pmf)) +#' Rpost$supp[loc] # posterior mode +#' res1$Rhat # compare with posterior mean #' -#' par(mfrow=c(2,1), mar=c(2,2,1,1)) +#' par(mfrow=c(2, 1), mar=c(2, 2, 1, 1)) #' plot(Rpost$supp, Rpost$pmf, col="black", type="l", xlab="", ylab="") #' abline(h=1/(20/0.01+1), col="red") #' abline(v=res1$Rhat, col="blue") #' abline(v=Rpost$supp[loc], col="purple") #' legend("topright", legend=c("prior", "posterior", "posterior mean (Rhat)", "posterior mode"), col=c("red", "black", "blue", "purple"), lty=1) -#' plot(Rpost$supp, Rpost$pmf, col="black", type="l", xlim=c(0.5,1.5), xlab="", ylab="") +#' plot(Rpost$supp, Rpost$pmf, col="black", type="l", xlim=c(0.5, 1.5), xlab="", ylab="") #' abline(h=1/(20/0.01+1), col="red") #' abline(v=res1$Rhat, col="blue") #' abline(v=Rpost$supp[loc], col="purple") @@ -70,7 +70,6 @@ #' ## ========================================================= ## #' ## Compute Rhat using only the first five weeks of data ## #' ## ========================================================= ## -#' #' #' res3 <- seqB(NT=NT[1:5], mu=5/7) # serial distribution has mean of five days #' res3$Rhat @@ -121,6 +120,6 @@ seqB <- function(NT, mu, kappa=20) { Rhat <- sum(R * posterior) - return(list(Rhat=Rhat, posterior=list(supp=R, pmf=posterior), group=group, inputs=list(NT=NT, mu=mu, kappa=kappa))) + return(list(Rhat=Rhat, posterior=list(supp=R, pmf=posterior), group=group)) } } |