From 94b4dcd37e662eb1e525dc241817c8dd5d4681fc Mon Sep 17 00:00:00 2001 From: Naeem Model Date: Sat, 2 Nov 2024 18:13:28 +0000 Subject: Add input validation to estimators --- R/util.R | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 R/util.R (limited to 'R/util.R') 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 +} -- cgit v1.2.3