]> nmode's Git Repositories - Rnaught/blobdiff - R/wp.R
Update about.html
[Rnaught] / R / wp.R
diff --git a/R/wp.R b/R/wp.R
index 3448911de0cfa33d0cd693b16131cca7fe449500..fbb6ad7956d59c426b56dc5058fcdd93c0e17365 100644 (file)
--- a/R/wp.R
+++ b/R/wp.R
 #'
 #' @param cases Vector of case counts. The vector must be of length at least two
 #'   and only contain positive integers.
-#' @param mu Mean of the serial distribution. This must be a positive number or
-#'   `NA`. If a number is specified, the value should match the 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`. If
-#'   case counts are daily and the serial distribution has a mean of seven days,
-#'   then `mu` should be set to `7`.
+#' @param mu Mean of the serial distribution (defaults to `NA`). This must be a
+#'   positive number or `NA`. If a number is specified, the value should match
+#'   the 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`. If case counts are daily and the serial distribution has a mean of
+#'   seven days, then `mu` should be set to `7`.
 #' @param serial Whether to return the estimated serial distribution in addition
-#'   to the estimate of R0. This must be a value identical to `TRUE` or `FALSE`.
+#'   to the estimate of R0 (defaults to `FALSE`). This must be a value identical
+#'   to `TRUE` or `FALSE`.
 #' @param grid_length The length of the grid in the grid search (defaults to
 #'   100). This must be a positive integer. It will only be used if `mu` is set
 #'   to `NA`. The grid search will go through all combinations of the shape and
@@ -71,9 +72,8 @@
 #'   Otherwise, if `serial` is identical to `FALSE`, only the estimate of R0 is
 #'   returned.
 #'
-#' @references
-#' [White and Pagano (Statistics in Medicine, 2008)](
-#' https://doi.org/10.1002/sim.3136)
+#' @references [White and Pagano (Statistics in Medicine, 2008)](
+#'   https://doi.org/10.1002/sim.3136)
 #'
 #' @seealso `vignette("wp_serial", package="Rnaught")` for examples of using the
 #'   serial distribution.
 #' estimate$pmf
 wp <- function(cases, mu = NA, serial = FALSE,
                grid_length = 100, max_shape = 10, max_scale = 10) {
-  if (is.na(mu)) {
+  validate_cases(cases, min_length = 2, min_count = 1)
+  if (!identical(serial, TRUE) && !identical(serial, FALSE)) {
+    stop(
+      paste("The serial distribution flag (`serial`) must be set to",
+        "`TRUE` or `FALSE`."
+      ), call. = FALSE
+    )
+  }
+
+  if (identical(mu, NA)) {
+    if (!is_integer(grid_length) || grid_length < 1) {
+      stop("The grid length must be a positive integer.", call. = FALSE)
+    }
+    if (!is_real(max_shape) || max_shape <= 0) {
+      stop(
+        paste("The largest value of the shape parameter (`max_shape`)",
+          "must be a positive number."
+        ), call. = FALSE
+      )
+    }
+    if (!is_real(max_scale) || max_scale <= 0) {
+      stop(
+        paste("The largest value of the scale parameter (`max_scale`)",
+          "must be a positive number."
+        ), call. = FALSE
+      )
+    }
+
     search <- wp_search(cases, grid_length, max_shape, max_scale)
     r0 <- search$r0
     serial_supp <- search$supp
     serial_pmf <- search$pmf
   } else {
+    if (!is_real(mu) || mu <= 0) {
+      stop("The serial interval (`mu`) must be a positive number or `NA`.",
+        call. = FALSE
+      )
+    }
+
     max_range <- ceiling(qgamma(0.999, shape = 1, scale = mu))
     serial_supp <- seq_len(max_range)
     serial_pmf <- diff(pgamma(0:max_range, shape = 1, scale = mu))
@@ -152,9 +185,8 @@ wp <- function(cases, mu = NA, serial = FALSE,
 #'   * `pmf` - the probability mass function of the estimated serial
 #'     distribution
 #'
-#' @references
-#' [White and Pagano (Statistics in Medicine, 2008)](
-#' https://doi.org/10.1002/sim.3136)
+#' @references [White and Pagano (Statistics in Medicine, 2008)](
+#'   https://doi.org/10.1002/sim.3136)
 #'
 #' @seealso [wp()] for the function in which this grid search is called.
 #'
@@ -207,9 +239,8 @@ wp_search <- function(cases, grid_length, max_shape, max_scale) {
 #'
 #' @return The sum inside the function `mu(t)` of the log likelihood.
 #'
-#' @references
-#' [White and Pagano (Statistics in Medicine, 2008)](
-#' https://doi.org/10.1002/sim.3136)
+#' @references [White and Pagano (Statistics in Medicine, 2008)](
+#'   https://doi.org/10.1002/sim.3136)
 #'
 #' @seealso [wp()] and [wp_search()] for the functions which require this sum.
 #'