Skip to content

Commit cc286b9

Browse files
authored
Adding ard_smd() (#65)
**What changes are proposed in this pull request?** * Adding `ard_smd()` **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ closes #4 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a ✅ - [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. - [x] 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 ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
1 parent ec100fc commit cc286b9

File tree

6 files changed

+154
-1
lines changed

6 files changed

+154
-1
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues
1515
Depends:
1616
R (>= 4.1)
1717
Imports:
18-
cards (>= 0.0.0.9049),
18+
cards (>= 0.1.0.9002),
1919
cli (>= 3.6.1),
2020
dplyr (>= 1.1.2),
2121
glue (>= 1.6.2),
@@ -27,6 +27,7 @@ Suggests:
2727
effectsize (>= 0.6.0),
2828
parameters (>= 0.20.2),
2929
spelling,
30+
smd (>= 0.6.6),
3031
testthat (>= 3.2.0),
3132
withr
3233
Remotes:

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export(ard_proportion_ci)
1919
export(ard_proptest)
2020
export(ard_regression)
2121
export(ard_regression_basic)
22+
export(ard_smd)
2223
export(ard_ttest)
2324
export(ard_wilcoxtest)
2425
export(contains)

R/ard_smd.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#' ARD Standardized Mean Difference
2+
#'
3+
#' @description
4+
#' Standardized mean difference calculated via [`smd::smd()`] with `na.rm = TRUE`.
5+
#'
6+
#' @param data (`data.frame`)\cr
7+
#' a data frame.
8+
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
9+
#' column name to compare by
10+
#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
11+
#' column name to be compared.
12+
#' @inheritDotParams smd::smd -x -g -w -na.rm
13+
#'
14+
#' @return ARD data frame
15+
#' @export
16+
#'
17+
#' @examplesIf cards::is_pkg_installed("smd", reference_pkg = "cardx")
18+
#' ard_smd(cards::ADSL, by = ARM, variable = AGE, std.error = TRUE)
19+
#' ard_smd(cards::ADSL, by = ARM, variable = AGEGR1, std.error = TRUE)
20+
ard_smd <- function(data, by, variable, ...) {
21+
# check installed packages ---------------------------------------------------
22+
cards::check_pkg_installed("smd", reference_pkg = "cardx")
23+
24+
# check/process inputs -------------------------------------------------------
25+
check_not_missing(data)
26+
check_not_missing(variable)
27+
check_not_missing(by)
28+
check_data_frame(data)
29+
data <- dplyr::ungroup(data)
30+
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
31+
check_scalar(by)
32+
check_scalar(variable)
33+
34+
# build ARD ------------------------------------------------------------------
35+
.format_smd_results(
36+
by = by,
37+
variable = variable,
38+
lst_tidy =
39+
cards::eval_capture_conditions(
40+
smd::smd(x = data[[variable]], g = data[[by]], na.rm = TRUE, ...) |>
41+
dplyr::select(-any_of("term"))
42+
),
43+
...
44+
)
45+
}
46+
47+
48+
.format_smd_results <- function(by, variable, lst_tidy, ...) {
49+
# build ARD ------------------------------------------------------------------
50+
ret <-
51+
cards::tidy_as_ard(
52+
lst_tidy = lst_tidy,
53+
tidy_result_names = c("estimate", "std.error"),
54+
fun_args_to_record = "gref",
55+
formals = formals(smd::smd)["gref"],
56+
# removing the `std.error` ARGUMENT (not the result)
57+
passed_args = dots_list(...) |> utils::modifyList(list(std.error = NULL)),
58+
lst_ard_columns = list(group1 = by, variable = variable, context = "smd")
59+
)
60+
61+
# add the stat label ---------------------------------------------------------
62+
ret |>
63+
dplyr::left_join(
64+
dplyr::tribble(
65+
~stat_name, ~stat_label,
66+
"estimate", "Standardized Mean Difference",
67+
"std.error", "Standard Error",
68+
"gref", "Integer Reference Group Level"
69+
),
70+
by = "stat_name"
71+
) |>
72+
dplyr::mutate(stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name)) |>
73+
cards::tidy_ard_column_order()
74+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ reference:
4040
- ard_proportion_ci
4141
- ard_regression
4242
- ard_regression_basic
43+
- ard_smd
4344

4445
- title: "Helpers"
4546
- contents:

man/ard_smd.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ard_smd.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
skip_if_not(cards::is_pkg_installed("smd", reference_pkg = "cardx"))
2+
3+
test_that("ard_smd() works", {
4+
expect_error(
5+
ard_smd <-
6+
mtcars |>
7+
ard_smd(by = vs, variable = am, std.error = TRUE),
8+
NA
9+
)
10+
11+
expect_equal(
12+
ard_smd |>
13+
cards::get_ard_statistics(stat_name %in% c("estimate", "std.error")),
14+
smd::smd(x = mtcars$am, g = mtcars$vs, std.error = TRUE) |>
15+
dplyr::select(-term) |>
16+
unclass(),
17+
ignore_attr = TRUE
18+
)
19+
})
20+
21+
test_that("ard_proptest() error messaging", {
22+
# mis-specify the gref argument
23+
expect_error(
24+
bad_gref <-
25+
ard_smd(cards::ADSL, by = ARM, variable = AGE, std.error = TRUE, gref = 0) |>
26+
as.data.frame(),
27+
NA
28+
)
29+
# check all the stats still appear despite the errors
30+
expect_equal(nrow(bad_gref), 3L)
31+
expect_setequal(bad_gref$stat_name, c("estimate", "std.error", "gref"))
32+
# check the error message it the one we expect
33+
expect_equal(
34+
bad_gref$error |> unique() |> cli::ansi_strip(),
35+
"gref must be an integer within 3"
36+
)
37+
})

0 commit comments

Comments
 (0)