From 1e73f79ed164b2f1f3095c9f11ca0c328bad9b65 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Sat, 17 Feb 2024 13:47:26 -0800 Subject: [PATCH 1/6] misc updates --- DESCRIPTION | 2 +- tests/testthat/_snaps/ard_proportion_ci.md | 4 +- tests/testthat/_snaps/ard_regression.md | 56 ++++++++++++++++++++++ tests/testthat/test-ard_regression.R | 2 +- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/ard_regression.md diff --git a/DESCRIPTION b/DESCRIPTION index bdaa8473c..c2930261e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues Depends: R (>= 4.0) Imports: - cards (>= 0.0.0.9035), + cards (>= 0.0.0.9037), cli (>= 3.6.1), dplyr (>= 1.1.2), glue (>= 1.6.2), diff --git a/tests/testthat/_snaps/ard_proportion_ci.md b/tests/testthat/_snaps/ard_proportion_ci.md index 3050d5027..7082a15a3 100644 --- a/tests/testthat/_snaps/ard_proportion_ci.md +++ b/tests/testthat/_snaps/ard_proportion_ci.md @@ -11,7 +11,7 @@ 3 rsp proporti… conf.low conf.low 0.487 1 4 rsp proporti… conf.high conf.high 0.719 1 5 rsp proporti… conf.level conf.lev… 0.95 1 - 6 rsp proporti… method method Stratifi… 1 + 6 rsp proporti… method method Stratifi… Message i 2 more variables: warning, error @@ -28,7 +28,7 @@ 3 rsp proporti… conf.low conf.low 0.448 1 4 rsp proporti… conf.high conf.high 0.753 1 5 rsp proporti… conf.level conf.lev… 0.95 1 - 6 rsp proporti… method method Stratifi… 1 + 6 rsp proporti… method method Stratifi… Message i 2 more variables: warning, error diff --git a/tests/testthat/_snaps/ard_regression.md b/tests/testthat/_snaps/ard_regression.md new file mode 100644 index 000000000..79191e34f --- /dev/null +++ b/tests/testthat/_snaps/ard_regression.md @@ -0,0 +1,56 @@ +# 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 context stat_name stat_label statistic + 1 ARM Placebo regressi… term term ARMPlace… + 2 ARM Placebo regressi… var_label Label Descript… + 3 ARM Placebo regressi… var_class Class character + 4 ARM Placebo regressi… var_type Type categori… + 5 ARM Placebo regressi… var_nlevels N Levels 3 + 6 ARM Placebo regressi… contrasts contrasts contr.tr… + 7 ARM Placebo regressi… contrasts_type Contrast… treatment + 8 ARM Placebo regressi… reference_row referenc… TRUE + 9 ARM Placebo regressi… label Level La… Placebo + 10 ARM Placebo regressi… n_obs N Obs. 86 + 11 ARM Placebo regressi… estimate Coeffici… 0 + 12 ARM Xanomeli… regressi… term term ARMXanom… + 13 ARM Xanomeli… regressi… var_label Label Descript… + 14 ARM Xanomeli… regressi… var_class Class character + 15 ARM Xanomeli… regressi… var_type Type categori… + 16 ARM Xanomeli… regressi… var_nlevels N Levels 3 + 17 ARM Xanomeli… regressi… contrasts contrasts contr.tr… + 18 ARM Xanomeli… regressi… contrasts_type Contrast… treatment + 19 ARM Xanomeli… regressi… reference_row referenc… FALSE + 20 ARM Xanomeli… regressi… label Level La… Xanomeli… + 21 ARM Xanomeli… regressi… n_obs N Obs. 84 + 22 ARM Xanomeli… regressi… estimate Coeffici… -0.828 + 23 ARM Xanomeli… regressi… std.error Standard… 1.267 + 24 ARM Xanomeli… regressi… statistic statistic -0.654 + 25 ARM Xanomeli… regressi… p.value p-value 0.514 + 26 ARM Xanomeli… regressi… conf.low CI Lower… -3.324 + 27 ARM Xanomeli… regressi… conf.high CI Upper… 1.668 + 28 ARM Xanomeli… regressi… term term ARMXanom… + 29 ARM Xanomeli… regressi… var_label Label Descript… + 30 ARM Xanomeli… regressi… var_class Class character + 31 ARM Xanomeli… regressi… var_type Type categori… + 32 ARM Xanomeli… regressi… var_nlevels N Levels 3 + 33 ARM Xanomeli… regressi… contrasts contrasts contr.tr… + 34 ARM Xanomeli… regressi… contrasts_type Contrast… treatment + 35 ARM Xanomeli… regressi… reference_row referenc… FALSE + 36 ARM Xanomeli… regressi… label Level La… Xanomeli… + 37 ARM Xanomeli… regressi… n_obs N Obs. 84 + 38 ARM Xanomeli… regressi… estimate Coeffici… 0.457 + 39 ARM Xanomeli… regressi… std.error Standard… 1.267 + 40 ARM Xanomeli… regressi… statistic statistic 0.361 + 41 ARM Xanomeli… regressi… p.value p-value 0.719 + 42 ARM Xanomeli… regressi… conf.low CI Lower… -2.039 + 43 ARM Xanomeli… regressi… conf.high CI Upper… 2.953 + Message + i 1 more variable: statistic_fmt_fn + diff --git a/tests/testthat/test-ard_regression.R b/tests/testthat/test-ard_regression.R index c6ed8c548..392532b50 100644 --- a/tests/testthat/test-ard_regression.R +++ b/tests/testthat/test-ard_regression.R @@ -5,6 +5,6 @@ test_that("ard_regression() works", { dplyr::mutate( statistic = lapply(statistic, function(x) ifelse(is.numeric(x), cards::round5(x, 3), x)) ) |> - as.data.frame() + print(n = Inf) ) }) From 9242aae2f8587e65dc5a134df67cee219db3c919 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Sat, 17 Feb 2024 21:48:19 +0000 Subject: [PATCH 2/6] [skip actions] Bump version to 0.0.0.9034 --- DESCRIPTION | 2 +- NEWS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c2930261e..90c09c5f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: cardx Title: Extra Analysis Results Data Utilities -Version: 0.0.0.9033 +Version: 0.0.0.9034 Authors@R: c( person("Daniel", "Sjoberg", , "sjobergd@gene.com", role = c("aut", "cre")), person("F. Hoffmann-La Roche AG", role = c("cph", "fnd")) diff --git a/NEWS.md b/NEWS.md index 6dc5719a2..c2912029a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# cardx 0.0.0.9033 +# cardx 0.0.0.9034 ### New Features * New package! From 116d3df65d1ede909137ffe295adbe57bdfbd57d Mon Sep 17 00:00:00 2001 From: Zelos Zhu Date: Sat, 17 Feb 2024 18:03:38 -0500 Subject: [PATCH 3/6] Closes #38 adding Kruskal-Wallis test (#45) **What changes are proposed in this pull request?** **Reference GitHub issue associated with pull request.** _e.g., 'closes #38 '_ -------------------------------------------------------------------------------- Reviewer Checklist (if item does not apply, mark is as complete) - [x] Ensure all package dependencies are installed: `devtools::install_dev_deps()` - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [x] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` - [x] `usethis::use_spell_check()` runs with no spelling errors in documentation When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] Increment the version number using `usethis::use_version(which = "dev")` - [ ] Run `usethis::use_spell_check()` again - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge". --------- Signed-off-by: Daniel Sjoberg Signed-off-by: Zelos Zhu Co-authored-by: Daniel Sjoberg --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/ard_kruskaltest.R | 54 ++++++++++++++++++++++++ _pkgdown.yml | 1 + man/ard_kruskaltest.Rd | 30 +++++++++++++ tests/testthat/_snaps/ard_kruskaltest.md | 16 +++++++ tests/testthat/test-ard_kruskaltest.R | 25 +++++++++++ 7 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 R/ard_kruskaltest.R create mode 100644 man/ard_kruskaltest.Rd create mode 100644 tests/testthat/_snaps/ard_kruskaltest.md create mode 100644 tests/testthat/test-ard_kruskaltest.R diff --git a/DESCRIPTION b/DESCRIPTION index 90c09c5f6..647698840 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ License: Apache License 2.0 URL: https://github.com/insightsengineering/cardx BugReports: https://github.com/insightsengineering/cardx/issues Depends: - R (>= 4.0) + R (>= 4.1) Imports: cards (>= 0.0.0.9037), cli (>= 3.6.1), diff --git a/NAMESPACE b/NAMESPACE index d7655978d..d58df76b4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(all_of) export(any_of) export(ard_chisqtest) export(ard_fishertest) +export(ard_kruskaltest) export(ard_paired_ttest) export(ard_paired_wilcoxtest) export(ard_proportion_ci) diff --git a/R/ard_kruskaltest.R b/R/ard_kruskaltest.R new file mode 100644 index 000000000..6e21453bf --- /dev/null +++ b/R/ard_kruskaltest.R @@ -0,0 +1,54 @@ +#' ARD Kruskal-Wallis Test +#' +#' @description +#' Analysis results data for Kruskal-Wallis Rank Sum Test. +#' +#' Calculated with `kruskal.test(data[[variable]], data[[by]], ...)` +#' +#' @param data (`data.frame`)\cr +#' a data frame. +#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr +#' column name to compare by +#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr +#' column name to be compared +#' +#' @return ARD data frame +#' @export +#' +#' @examples +#' cards::ADSL |> +#' ard_kruskaltest(by = "ARM", variable = "AGE") +ard_kruskaltest <- function(data, by, variable) { + # check installed packages --------------------------------------------------- + cards::check_pkg_installed("broom.helpers", reference_pkg = "cards") + + # check/process inputs ------------------------------------------------------- + check_not_missing(data) + check_not_missing(variable) + check_not_missing(by) + check_class_data_frame(x = data) + cards::process_selectors(data, by = {{ by }}, variable = {{ variable }}) + check_scalar(by) + check_scalar(variable) + + # build ARD ------------------------------------------------------------------ + cards::tidy_as_ard( + lst_tidy = + cards::eval_capture_conditions( + stats::kruskal.test(x = data[[variable]], g = data[[by]]) |> + broom::tidy() + ), + tidy_result_names = c("statistic", "p.value", "parameter", "method"), + lst_ard_columns = list(group1 = by, variable = variable, context = "kruskaltest") + ) |> + dplyr::mutate( + .after = "stat_name", + stat_label = + dplyr::case_when( + .data$stat_name %in% "statistic" ~ "Kruskal-Wallis chi-squared Statistic", + .data$stat_name %in% "p.value" ~ "p-value", + .data$stat_name %in% "parameter" ~ "Degrees of Freedom", + TRUE ~ .data$stat_name, + ) + ) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 35ce4f4a5..b3c97b7c0 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -26,6 +26,7 @@ reference: - contents: - ard_chisqtest - ard_fishertest + - ard_kruskaltest - ard_ttest - ard_wilcoxtest diff --git a/man/ard_kruskaltest.Rd b/man/ard_kruskaltest.Rd new file mode 100644 index 000000000..41977681c --- /dev/null +++ b/man/ard_kruskaltest.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ard_kruskaltest.R +\name{ard_kruskaltest} +\alias{ard_kruskaltest} +\title{ARD Kruskal-Wallis Test} +\usage{ +ard_kruskaltest(data, by, variable) +} +\arguments{ +\item{data}{(\code{data.frame})\cr +a data frame.} + +\item{by}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr +column name to compare by} + +\item{variable}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr +column name to be compared} +} +\value{ +ARD data frame +} +\description{ +Analysis results data for Kruskal-Wallis Rank Sum Test. + +Calculated with \code{kruskal.test(data[[variable]], data[[by]], ...)} +} +\examples{ +cards::ADSL |> + ard_kruskaltest(by = "ARM", variable = "AGE") +} diff --git a/tests/testthat/_snaps/ard_kruskaltest.md b/tests/testthat/_snaps/ard_kruskaltest.md new file mode 100644 index 000000000..0e6df424e --- /dev/null +++ b/tests/testthat/_snaps/ard_kruskaltest.md @@ -0,0 +1,16 @@ +# ard_kurskaltest() works + + Code + as.data.frame(ard_kruskaltest(cards::ADSL, by = "ARM", variable = "AGE")) + Output + group1 variable context stat_name stat_label + 1 ARM AGE kruskaltest statistic Kruskal-Wallis chi-squared Statistic + 2 ARM AGE kruskaltest p.value p-value + 3 ARM AGE kruskaltest parameter Degrees of Freedom + 4 ARM AGE kruskaltest method method + statistic statistic_fmt_fn warning error + 1 1.63473 1 NULL NULL + 2 0.4415937 1 NULL NULL + 3 2 1 NULL NULL + 4 Kruskal-Wallis rank sum test NULL NULL NULL + diff --git a/tests/testthat/test-ard_kruskaltest.R b/tests/testthat/test-ard_kruskaltest.R new file mode 100644 index 000000000..293d87578 --- /dev/null +++ b/tests/testthat/test-ard_kruskaltest.R @@ -0,0 +1,25 @@ +test_that("ard_kurskaltest() works", { + expect_error( + ard_kruskaltest <- + cards::ADSL |> + ard_kruskaltest(by = ARM, variable = AGE), + NA + ) + + expect_equal( + ard_kruskaltest |> + cards::get_ard_statistics(stat_name %in% c("statistic", "p.value")), + with(cards::ADSL, kruskal.test(AGE, ARM)) |> + broom::tidy() |> + dplyr::select(statistic, p.value) |> + unclass(), + ignore_attr = TRUE + ) + + # errors are properly handled + expect_snapshot( + cards::ADSL |> + ard_kruskaltest(by = "ARM", variable = "AGE") |> + as.data.frame() + ) +}) From 0eacdcba38e86e7a154785d41cd0457773f1d373 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Sat, 17 Feb 2024 23:04:25 +0000 Subject: [PATCH 4/6] [skip actions] Bump version to 0.0.0.9035 --- DESCRIPTION | 2 +- NEWS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 647698840..207920bbc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: cardx Title: Extra Analysis Results Data Utilities -Version: 0.0.0.9034 +Version: 0.0.0.9035 Authors@R: c( person("Daniel", "Sjoberg", , "sjobergd@gene.com", role = c("aut", "cre")), person("F. Hoffmann-La Roche AG", role = c("cph", "fnd")) diff --git a/NEWS.md b/NEWS.md index c2912029a..f8dc66202 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# cardx 0.0.0.9034 +# cardx 0.0.0.9035 ### New Features * New package! From a08e9ed0be42a0ed7aba870f97fab0b5a365f3f6 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Sun, 18 Feb 2024 16:05:18 -0800 Subject: [PATCH 5/6] {cards} arg rename updates (#53) **What changes are proposed in this pull request?** * Style this entry in a way that can be copied directly into `NEWS.md`. (#, @) Provide more detail here as needed. **Reference GitHub issue associated with pull request.** _e.g., 'closes #'_ -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a :white_check_mark: - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [ ] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a :white_check_mark: - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --- DESCRIPTION | 2 +- R/ard_proportion_ci.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 207920bbc..c83794b60 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues Depends: R (>= 4.1) Imports: - cards (>= 0.0.0.9037), + cards (>= 0.0.0.9039), cli (>= 3.6.1), dplyr (>= 1.1.2), glue (>= 1.6.2), diff --git a/R/ard_proportion_ci.R b/R/ard_proportion_ci.R index 1b341548e..edb623ca3 100644 --- a/R/ard_proportion_ci.R +++ b/R/ard_proportion_ci.R @@ -48,7 +48,7 @@ ard_proportion_ci <- function(data, variables, by = dplyr::group_vars(data), data = data, variables = {{ variables }}, by = {{ by }}, - statistics = + statistic = ~ list( prop_ci = switch(method, From 8d5fbc332caf97d4730085f550327b11cccffd95 Mon Sep 17 00:00:00 2001 From: ddsjoberg Date: Mon, 19 Feb 2024 00:06:05 +0000 Subject: [PATCH 6/6] [skip actions] Bump version to 0.0.0.9036 --- DESCRIPTION | 2 +- NEWS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c83794b60..765b97d3b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: cardx Title: Extra Analysis Results Data Utilities -Version: 0.0.0.9035 +Version: 0.0.0.9036 Authors@R: c( person("Daniel", "Sjoberg", , "sjobergd@gene.com", role = c("aut", "cre")), person("F. Hoffmann-La Roche AG", role = c("cph", "fnd")) diff --git a/NEWS.md b/NEWS.md index f8dc66202..60c6961e9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# cardx 0.0.0.9035 +# cardx 0.0.0.9036 ### New Features * New package!