diff --git a/R/utils.r b/R/utils.r index d50a9bf..76397c5 100644 --- a/R/utils.r +++ b/R/utils.r @@ -46,7 +46,7 @@ rgen_length <- function(n, x, prob) { #' Negative binomial random numbers parametrized #' in terms of mean and dispersion coefficient #' @param n number of samples to draw -#' @param mn mean of distribution +#' @param mn mean of distribution; Must be > 0. #' @param disp dispersion coefficient (var/mean); Must be > 1. #' @return vector containing the random numbers #' @author Flavio Finger @@ -55,13 +55,13 @@ rgen_length <- function(n, x, prob) { #' rnbinom_mean_disp(n = 5, mn = 4, disp = 2) rnbinom_mean_disp <- function(n, mn, disp) { checkmate::assert_number( - n, lower = 0, finite = TRUE, na.ok = FALSE + n, lower = 1, finite = TRUE, na.ok = FALSE ) checkmate::assert_number( disp, lower = 1, finite = TRUE, na.ok = FALSE ) checkmate::assert_number( - mn, lower = 0, finite = TRUE, na.ok = FALSE + mn, lower = 1E-100, finite = TRUE, na.ok = FALSE ) size <- mn / (disp - 1) stats::rnbinom(n, size = size, mu = mn) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 235e459..d96e78b 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -8,10 +8,44 @@ test_that("Util functions work", { }) test_that("Errors are thrown", { + # Checks on 'disp' argument expect_error( rnbinom_mean_disp(n = 5, mn = 4, disp = 0.9), "is not >= 1" ) + expect_error( + rnbinom_mean_disp(n = 5, mn = 4, disp = NA), + "May not be NA" + ) + expect_error( + rnbinom_mean_disp(n = 5, mn = 4, disp = Inf), + "Must be finite" + ) + # Checks on 'n' argument + expect_error( + rnbinom_mean_disp(n = 0, mn = 4, disp = 2), + "is not >= 1" + ) + expect_error( + rnbinom_mean_disp(n = NA, mn = 4, disp = 2), + "May not be NA" + ) + expect_error( + rnbinom_mean_disp(n = Inf, mn = 4, disp = 2), + "Must be finite" + ) + # Checks on 'mn' argument + expect_error( + rnbinom_mean_disp(n = 2, mn = 0, disp = 2) + ) + expect_error( + rnbinom_mean_disp(n = 2, mn = NA, disp = 2), + "May not be NA" + ) + expect_error( + rnbinom_mean_disp(n = 2, mn = Inf, disp = 2), + "Must be finite" + ) }) test_that("Errors are thrown", {