diff --git a/R/ard_stats_poisson_test.R b/R/ard_stats_poisson_test.R index bfd8868f..1fb3bcc1 100644 --- a/R/ard_stats_poisson_test.R +++ b/R/ard_stats_poisson_test.R @@ -15,8 +15,7 @@ #' @param conf.level (scalar `numeric`)\cr #' confidence level for confidence interval. Default is `0.95`. #' @param na.rm (scalar `logical`)\cr -#' whether missing values should be removed before summing the numerator and the denominator. Default -#' is `TRUE`. +#' whether missing values should be removed before summing the numerator and the denominator. Default is `TRUE`. #' @param ... arguments passed to [poisson.test()]. #' @return an ARD data frame of class 'card' #' @name ard_stats_poisson_test @@ -124,7 +123,8 @@ ard_stats_poisson_test <- function(data, numerator, denominator, na.rm = TRUE, b formals = formals(asNamespace("stats")[["poisson.test"]]), passed_args = dots_list(...), lst_ard_columns = list(context = "stats_poisson_test") - ) + ) |> + dplyr::distinct() # rename "r" statistic to "mu" ret$stat_name[ret$stat_name == "r"] <- "mu" diff --git a/man/ard_stats_poisson_test.Rd b/man/ard_stats_poisson_test.Rd index 6ccc4d5f..ee6f5031 100644 --- a/man/ard_stats_poisson_test.Rd +++ b/man/ard_stats_poisson_test.Rd @@ -25,8 +25,7 @@ name of the event variable to be summed for computing the numerator.} name of the time variable to be summed for computing the denominator.} \item{na.rm}{(scalar \code{logical})\cr -whether missing values should be removed before summing the numerator and the denominator. Default -is \code{TRUE}.} +whether missing values should be removed before summing the numerator and the denominator. Default is \code{TRUE}.} \item{by}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr optional column name to compare by.} diff --git a/tests/testthat/_snaps/ard_stats_poisson_test.md b/tests/testthat/_snaps/ard_stats_poisson_test.md new file mode 100644 index 00000000..1f710740 --- /dev/null +++ b/tests/testthat/_snaps/ard_stats_poisson_test.md @@ -0,0 +1,8 @@ +# ard_stats_poisson_test() errors are handled correctly + + Code + ard_stats_poisson_test(cards::ADTTE, by = TRTA, numerator = CNSR, denominator = AVAL) + Condition + Error in `ard_stats_poisson_test()`: + ! The `by` argument must have a maximum of two levels. + diff --git a/tests/testthat/test-ard_stats_poisson_test.R b/tests/testthat/test-ard_stats_poisson_test.R new file mode 100644 index 00000000..20bf384c --- /dev/null +++ b/tests/testthat/test-ard_stats_poisson_test.R @@ -0,0 +1,81 @@ +skip_if_not(is_pkg_installed("broom", reference_pkg = "cardx")) + +test_that("ard_stats_poisson_test() works for one sample tests", { + # Default values work + expect_silent( + ard_stats_poisson_test(cards::ADTTE, numerator = CNSR, denominator = AVAL) + ) + + # Custom values work + expect_silent( + ard_single <- ard_stats_poisson_test( + cards::ADTTE, + numerator = "CNSR", + denominator = "AVAL", + conf.level = 0.90, + r = 0.8, + alternative = "greater" + ) + ) + + # Statistics calculated correctly + expect_equal( + ard_single |> + cards::get_ard_statistics( + stat_name %in% + c("estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative") + ), + poisson.test( + x = sum(cards::ADTTE$CNSR), + T = sum(cards::ADTTE$AVAL), + r = 0.8, + conf.level = 0.9, + alternative = "greater" + ) |> + broom::tidy() |> + unclass(), + ignore_attr = TRUE + ) +}) + +test_that("ard_stats_poisson_test() works for two sample tests", { + expect_silent( + ard_compare <- + cards::ADTTE |> + dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |> + ard_stats_poisson_test(by = TRTA, numerator = CNSR, denominator = AVAL) + ) + + # Statistics calculated correctly + expect_equal( + ard_compare |> + cards::get_ard_statistics( + stat_name %in% + c("estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative") + ), + poisson.test( + x = cards::ADTTE |> + dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |> + dplyr::group_by(TRTA) |> + dplyr::summarise(sum = sum(CNSR)) |> + dplyr::pull(sum), + T = cards::ADTTE |> + dplyr::filter(TRTA %in% c("Placebo", "Xanomeline High Dose")) |> + dplyr::group_by(TRTA) |> + dplyr::summarise(sum = sum(AVAL)) |> + dplyr::pull(sum) + ) |> + broom::tidy() |> + unclass(), + ignore_attr = TRUE + ) + +}) + +test_that("ard_stats_poisson_test() errors are handled correctly", { + expect_snapshot( + cards::ADTTE |> + ard_stats_poisson_test(by = TRTA, numerator = CNSR, denominator = AVAL), + error = TRUE + ) +})