From 67ada6629f719ac03d04814ea7dfe68d8887e077 Mon Sep 17 00:00:00 2001 From: Rohan Parmar Date: Mon, 26 Feb 2024 03:50:03 +0000 Subject: [PATCH] Adds ard_regression_basic(). --- NAMESPACE | 1 + R/ard_regression.R | 40 ++++++ man/ard_regression_basic.Rd | 31 +++++ tests/testthat/_snaps/ard_regression.md | 44 ++++++ tests/testthat/_snaps/ard_regression.new.md | 144 ++++++++++++++++++++ tests/testthat/test-ard_regression.R | 14 ++ 6 files changed, 274 insertions(+) create mode 100644 man/ard_regression_basic.Rd create mode 100644 tests/testthat/_snaps/ard_regression.new.md diff --git a/NAMESPACE b/NAMESPACE index d58df76b4..d43dd2fe4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(ard_paired_ttest) export(ard_paired_wilcoxtest) export(ard_proportion_ci) export(ard_regression) +export(ard_regression_basic) export(ard_ttest) export(ard_wilcoxtest) export(contains) diff --git a/R/ard_regression.R b/R/ard_regression.R index 2b74bbbca..cc5580810 100644 --- a/R/ard_regression.R +++ b/R/ard_regression.R @@ -79,3 +79,43 @@ ard_regression.default <- function(x, tidy_fun = broom.helpers::tidy_with_broom_ cards::tidy_ard_column_order() %>% {structure(., class = c("card", class(.)))} # styler: off } + +#' ard_regression_basic +#' +#' @description +#' A function that takes a regression model and provides basic statistics in an +#' ARD structure. +#' +#' +#' @param x regression model object +#' @param tidy_fun(`function`)\cr +#' a tidier. Default is [`broom.helpers::tidy_with_broom_or_parameters`] +#' @param ... Arguments passed to `broom.helpers::tidy_plus_plus()` +#' +#' @return data frame +#' @name ard_regression_basic +#' @export +#' +#' @examples +#' lm(AGE ~ ARM, data = cards::ADSL) |> +#' ard_regression_basic() +#' +ard_regression_basic <- function(x, tidy_fun = broom.helpers::tidy_with_broom_or_parameters, ...) { + # check installed packages --------------------------------------------------- + cards::check_pkg_installed("broom.helpers", reference_pkg = "cards") + + # check inputs --------------------------------------------------------------- + check_not_missing(x, "model") + + args <- + list( + add_reference_rows = FALSE, + add_estimate_to_reference_rows = FALSE, + add_n = FALSE, + intercept = FALSE + ) |> + utils::modifyList(val = rlang::dots_list(...)) + + rlang::inject(ard_regression(x = x, tidy_fun = tidy_fun, !!!args)) |> + dplyr::filter(!.data$stat_name %in% c("term", "var_type", "var_label", "var_class", "label")) +} diff --git a/man/ard_regression_basic.Rd b/man/ard_regression_basic.Rd new file mode 100644 index 000000000..0ac8b551c --- /dev/null +++ b/man/ard_regression_basic.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ard_regression.R +\name{ard_regression_basic} +\alias{ard_regression_basic} +\title{ard_regression_basic} +\usage{ +ard_regression_basic( + x, + tidy_fun = broom.helpers::tidy_with_broom_or_parameters, + ... +) +} +\arguments{ +\item{x}{regression model object} + +\item{...}{Arguments passed to \code{broom.helpers::tidy_plus_plus()}} + +\item{tidy_fun(`function`)\cr}{a tidier. Default is \code{\link[broom.helpers:tidy_with_broom_or_parameters]{broom.helpers::tidy_with_broom_or_parameters}}} +} +\value{ +data frame +} +\description{ +A function that takes a regression model and provides basic statistics in an +ARD structure. +} +\examples{ +lm(AGE ~ ARM, data = cards::ADSL) |> + ard_regression_basic() + +} diff --git a/tests/testthat/_snaps/ard_regression.md b/tests/testthat/_snaps/ard_regression.md index 79191e34f..ce707021d 100644 --- a/tests/testthat/_snaps/ard_regression.md +++ b/tests/testthat/_snaps/ard_regression.md @@ -54,3 +54,47 @@ Message i 1 more variable: statistic_fmt_fn +# ard_regression_basic() works + + Code + as.data.frame(ard_regression_basic(lm(AGE ~ ARM, data = cards::ADSL))) + Output + variable variable_level context stat_name stat_label + 1 ARM Xanomeline High Dose regression var_nlevels N Levels + 2 ARM Xanomeline High Dose regression contrasts contrasts + 3 ARM Xanomeline High Dose regression contrasts_type Contrast Type + 4 ARM Xanomeline High Dose regression estimate Coefficient + 5 ARM Xanomeline High Dose regression std.error Standard Error + 6 ARM Xanomeline High Dose regression statistic statistic + 7 ARM Xanomeline High Dose regression p.value p-value + 8 ARM Xanomeline High Dose regression conf.low CI Lower Bound + 9 ARM Xanomeline High Dose regression conf.high CI Upper Bound + 10 ARM Xanomeline Low Dose regression var_nlevels N Levels + 11 ARM Xanomeline Low Dose regression contrasts contrasts + 12 ARM Xanomeline Low Dose regression contrasts_type Contrast Type + 13 ARM Xanomeline Low Dose regression estimate Coefficient + 14 ARM Xanomeline Low Dose regression std.error Standard Error + 15 ARM Xanomeline Low Dose regression statistic statistic + 16 ARM Xanomeline Low Dose regression p.value p-value + 17 ARM Xanomeline Low Dose regression conf.low CI Lower Bound + 18 ARM Xanomeline Low Dose regression conf.high CI Upper Bound + statistic statistic_fmt_fn + 1 3 0 + 2 contr.treatment NULL + 3 treatment NULL + 4 -0.8283499 1 + 5 1.267394 1 + 6 -0.653585 1 + 7 0.5139775 1 + 8 -3.324433 1 + 9 1.667733 1 + 10 3 0 + 11 contr.treatment NULL + 12 treatment NULL + 13 0.4573643 1 + 14 1.267394 1 + 15 0.3608698 1 + 16 0.7185003 1 + 17 -2.038718 1 + 18 2.953447 1 + diff --git a/tests/testthat/_snaps/ard_regression.new.md b/tests/testthat/_snaps/ard_regression.new.md new file mode 100644 index 000000000..49285aec6 --- /dev/null +++ b/tests/testthat/_snaps/ard_regression.new.md @@ -0,0 +1,144 @@ +# ard_regression() works + + Code + print(dplyr::mutate(ard_regression(lm(AGE ~ ARM, data = cards::ADSL), + add_estimate_to_reference_rows = TRUE), statistic = lapply(statistic, function( + x) ifelse(is.numeric(x), cards::round5(x, 3), x))), n = Inf) + Message + {cards} data frame: 43 x 7 + Output + variable variable_level stat_name stat_label statistic + 1 ARM Placebo term term ARMPlacebo + 2 ARM Placebo var_label Label Description of Planned Arm + 3 ARM Placebo var_class Class character + 4 ARM Placebo var_type Type categorical + 5 ARM Placebo var_nlevels N Levels 3 + 6 ARM Placebo contrasts contrasts contr.treatment + 7 ARM Placebo contrasts_type Contrast… treatment + 8 ARM Placebo reference_row referenc… TRUE + 9 ARM Placebo label Level La… Placebo + 10 ARM Placebo n_obs N Obs. 86 + 11 ARM Placebo estimate Coeffici… 0 + 12 ARM Xanomeli… term term ARMXanomeline High Dose + 13 ARM Xanomeli… var_label Label Description of Planned Arm + 14 ARM Xanomeli… var_class Class character + 15 ARM Xanomeli… var_type Type categorical + 16 ARM Xanomeli… var_nlevels N Levels 3 + 17 ARM Xanomeli… contrasts contrasts contr.treatment + 18 ARM Xanomeli… contrasts_type Contrast… treatment + 19 ARM Xanomeli… reference_row referenc… FALSE + 20 ARM Xanomeli… label Level La… Xanomeline High Dose + 21 ARM Xanomeli… n_obs N Obs. 84 + 22 ARM Xanomeli… estimate Coeffici… -0.828 + 23 ARM Xanomeli… std.error Standard… 1.267 + 24 ARM Xanomeli… statistic statistic -0.654 + 25 ARM Xanomeli… p.value p-value 0.514 + 26 ARM Xanomeli… conf.low CI Lower… -3.324 + 27 ARM Xanomeli… conf.high CI Upper… 1.668 + 28 ARM Xanomeli… term term ARMXanomeline Low Dose + 29 ARM Xanomeli… var_label Label Description of Planned Arm + 30 ARM Xanomeli… var_class Class character + 31 ARM Xanomeli… var_type Type categorical + 32 ARM Xanomeli… var_nlevels N Levels 3 + 33 ARM Xanomeli… contrasts contrasts contr.treatment + 34 ARM Xanomeli… contrasts_type Contrast… treatment + 35 ARM Xanomeli… reference_row referenc… FALSE + 36 ARM Xanomeli… label Level La… Xanomeline Low Dose + 37 ARM Xanomeli… n_obs N Obs. 84 + 38 ARM Xanomeli… estimate Coeffici… 0.457 + 39 ARM Xanomeli… std.error Standard… 1.267 + 40 ARM Xanomeli… statistic statistic 0.361 + 41 ARM Xanomeli… p.value p-value 0.719 + 42 ARM Xanomeli… conf.low CI Lower… -2.039 + 43 ARM Xanomeli… conf.high CI Upper… 2.953 + statistic_fmt_fn + 1 NULL + 2 NULL + 3 NULL + 4 NULL + 5 0 + 6 NULL + 7 NULL + 8 NULL + 9 NULL + 10 1 + 11 1 + 12 NULL + 13 NULL + 14 NULL + 15 NULL + 16 0 + 17 NULL + 18 NULL + 19 NULL + 20 NULL + 21 1 + 22 1 + 23 1 + 24 1 + 25 1 + 26 1 + 27 1 + 28 NULL + 29 NULL + 30 NULL + 31 NULL + 32 0 + 33 NULL + 34 NULL + 35 NULL + 36 NULL + 37 1 + 38 1 + 39 1 + 40 1 + 41 1 + 42 1 + 43 1 + Message + i 1 more variable: context + +# ard_regression_basic() works + + Code + as.data.frame(ard_regression_basic(lm(AGE ~ ARM, data = cards::ADSL))) + Output + variable variable_level context stat_name stat_label + 1 ARM Xanomeline High Dose regression var_nlevels N Levels + 2 ARM Xanomeline High Dose regression contrasts contrasts + 3 ARM Xanomeline High Dose regression contrasts_type Contrast Type + 4 ARM Xanomeline High Dose regression estimate Coefficient + 5 ARM Xanomeline High Dose regression std.error Standard Error + 6 ARM Xanomeline High Dose regression statistic statistic + 7 ARM Xanomeline High Dose regression p.value p-value + 8 ARM Xanomeline High Dose regression conf.low CI Lower Bound + 9 ARM Xanomeline High Dose regression conf.high CI Upper Bound + 10 ARM Xanomeline Low Dose regression var_nlevels N Levels + 11 ARM Xanomeline Low Dose regression contrasts contrasts + 12 ARM Xanomeline Low Dose regression contrasts_type Contrast Type + 13 ARM Xanomeline Low Dose regression estimate Coefficient + 14 ARM Xanomeline Low Dose regression std.error Standard Error + 15 ARM Xanomeline Low Dose regression statistic statistic + 16 ARM Xanomeline Low Dose regression p.value p-value + 17 ARM Xanomeline Low Dose regression conf.low CI Lower Bound + 18 ARM Xanomeline Low Dose regression conf.high CI Upper Bound + statistic statistic_fmt_fn + 1 3 0 + 2 contr.treatment NULL + 3 treatment NULL + 4 -0.8283499 1 + 5 1.267394 1 + 6 -0.653585 1 + 7 0.5139775 1 + 8 -3.324433 1 + 9 1.667733 1 + 10 3 0 + 11 contr.treatment NULL + 12 treatment NULL + 13 0.4573643 1 + 14 1.267394 1 + 15 0.3608698 1 + 16 0.7185003 1 + 17 -2.038718 1 + 18 2.953447 1 + diff --git a/tests/testthat/test-ard_regression.R b/tests/testthat/test-ard_regression.R index 392532b50..6eea8b5b8 100644 --- a/tests/testthat/test-ard_regression.R +++ b/tests/testthat/test-ard_regression.R @@ -8,3 +8,17 @@ test_that("ard_regression() works", { print(n = Inf) ) }) + +test_that("ard_regression_basic() works",{ + expect_error( + lm(AGE ~ ARM, data = cards::ADSL) |> + ard_regression_basic(), + NA + ) + + expect_snapshot( + lm(AGE ~ ARM, data = cards::ADSL) |> + ard_regression_basic() |> + as.data.frame() + ) +})