diff options
author | Naeem Model <me@nmode.ca> | 2024-11-02 18:13:28 +0000 |
---|---|---|
committer | Naeem Model <me@nmode.ca> | 2024-11-02 18:13:28 +0000 |
commit | 94b4dcd37e662eb1e525dc241817c8dd5d4681fc (patch) | |
tree | f5ef5b90bf2307dd28ae946413350e34a159b7fa /R/util.R | |
parent | 9fd931aeeba4ab7bdede1a625f64e7024c2b55aa (diff) |
Add input validation to estimators
Diffstat (limited to 'R/util.R')
-rw-r--r-- | R/util.R | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/R/util.R b/R/util.R new file mode 100644 index 0000000..d8b0b59 --- /dev/null +++ b/R/util.R @@ -0,0 +1,55 @@ +#' Case Counts Validation +#' +#' This is an internal function called by the estimators. It validates the +#' supplied case counts by ensuring it is a vector of integers of length at +#' least `min_length` with entries greater than or equal to `min_count`. +#' Execution is halted if these requirements are not satisfied. +#' +#' @param cases The case counts to be validated. +#' @param min_length The minimum length of the vector of case counts. +#' @param min_count The minimum value of the case count vector's entries. +#' +#' @noRd +validate_cases <- function(cases, min_length, min_count) { + if (!is.vector(cases) || !is.numeric(cases) || any(floor(cases) != cases)) { + stop("Case counts must be a vector of integers.", call. = FALSE) + } + if (length(cases) < min_length) { + stop(paste("Case counts must have at least", min_length, "entries."), + call. = FALSE + ) + } + if (any(cases < min_count)) { + stop(paste0("Case counts cannot be less than ", min_count, "."), + call. = FALSE + ) + } +} + +#' Real Number Testing +#' +#' This is an internal function which checks whether the given argument is a +#' real number. +#' +#' @param x The argument to test for being a real number. +#' +#' @return `TRUE` if the argument is a real number, `FALSE` otherwise. +#' +#' @noRd +is_real <- function(x) { + is.vector(x) && is.numeric(x) && length(x) == 1 +} + +#' Integer Testing +#' +#' This is an internal function which checks whether the given argument is an +#' integer. +#' +#' @param n The argument to test for being an integer. +#' +#' @return `TRUE` if the argument is an integer, `FALSE` otherwise. +#' +#' @noRd +is_integer <- function(n) { + is_real(n) && floor(n) == n +} |