Skip to content

Commit

Permalink
Merge branch 'main' into 43_ard_survfit@main
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Sjoberg <[email protected]>
  • Loading branch information
ddsjoberg authored Mar 1, 2024
2 parents e50c47d + 73015e0 commit b91020e
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 58 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cardx
Title: Extra Analysis Results Data Utilities
Version: 0.0.0.9043
Version: 0.0.0.9044
Authors@R: c(
person("Daniel", "Sjoberg", , "[email protected]", role = c("aut", "cre")),
person("Emily", "de la Rua", , "[email protected]", role = c("aut")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(ard_paired_wilcoxtest)
export(ard_proportion_ci)
export(ard_regression)
export(ard_survfit)
export(ard_regression_basic)
export(ard_ttest)
export(ard_wilcoxtest)
export(contains)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cardx 0.0.0.9043
# cardx 0.0.0.9044

### New Features
* New package!
2 changes: 1 addition & 1 deletion R/ard_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @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()`
#' @param ... Arguments passed to [`broom.helpers::tidy_plus_plus()`]
#'
#' @return data frame
#' @name ard_regression
Expand Down
57 changes: 57 additions & 0 deletions R/ard_regression_basic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Basic Regression ARD
#'
#' @description
#' A function that takes a regression model and provides basic statistics in an
#' ARD structure.
#' The default output is simpler than [`ard_regression()`].
#' The function primarily matches regression terms to underlying variable names
#' and levels.
#' The default arguments used are
#'
#' ```r
#' broom.helpers::tidy_plus_plus(
#' add_reference_rows = FALSE,
#' add_estimate_to_reference_rows = FALSE,
#' add_n = FALSE,
#' intercept = FALSE
#' )
#' ```
#'
#' @inheritParams ard_regression
#' @param stats_to_remove (`character`)\cr
#' character vector of statistic names to remove. Default is
#' `c("term", "var_type", "var_label", "var_class", "label", "contrasts_type", "contrasts", "var_nlevels")`.
#'
#' @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,
stats_to_remove = c(
"term", "var_type", "var_label", "var_class",
"label", "contrasts_type", "contrasts", "var_nlevels"
),
...) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom.helpers", reference_pkg = "cards")

# check inputs ---------------------------------------------------------------
check_not_missing(x)
check_class(stats_to_remove, cls = "character", allow_empty = TRUE)
if (is_empty(stats_to_remove)) stats_to_remove <- character(0L) # styler: off

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% stats_to_remove)
}
3 changes: 2 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ reference:
- contents:
- ard_proportion_ci
- ard_regression
- ard_regression_basic
- ard_survfit

- title: "Helpers"
- contents:
- proportion_ci
2 changes: 1 addition & 1 deletion man/ard_regression.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions man/ard_regression_basic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 47 additions & 51 deletions tests/testthat/_snaps/ard_regression.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
# ard_regression() works

Code
print(dplyr::mutate(ard_regression(lm(AGE ~ ARM, data = cards::ADSL),
add_estimate_to_reference_rows = TRUE), stat = lapply(stat, function(x) ifelse(
is.numeric(x), cards::round5(x, 3), x))), n = Inf)
Message
{cards} data frame: 43 x 7
dplyr::mutate(dplyr::select(as.data.frame(ard_regression(lm(AGE ~ ARM, data = cards::ADSL),
add_estimate_to_reference_rows = TRUE)), -context, -stat_label, -fmt_fn), stat = lapply(
stat, function(x) ifelse(is.numeric(x), cards::round5(x, 3), x)))
Output
variable variable_level context stat_name stat_label stat
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: fmt_fn
variable variable_level stat_name stat
1 ARM Placebo term ARMPlacebo
2 ARM Placebo var_label Description of Planned Arm
3 ARM Placebo var_class character
4 ARM Placebo var_type categorical
5 ARM Placebo var_nlevels 3
6 ARM Placebo contrasts contr.treatment
7 ARM Placebo contrasts_type treatment
8 ARM Placebo reference_row TRUE
9 ARM Placebo label Placebo
10 ARM Placebo n_obs 86
11 ARM Placebo estimate 0
12 ARM Xanomeline High Dose term ARMXanomeline High Dose
13 ARM Xanomeline High Dose var_label Description of Planned Arm
14 ARM Xanomeline High Dose var_class character
15 ARM Xanomeline High Dose var_type categorical
16 ARM Xanomeline High Dose var_nlevels 3
17 ARM Xanomeline High Dose contrasts contr.treatment
18 ARM Xanomeline High Dose contrasts_type treatment
19 ARM Xanomeline High Dose reference_row FALSE
20 ARM Xanomeline High Dose label Xanomeline High Dose
21 ARM Xanomeline High Dose n_obs 84
22 ARM Xanomeline High Dose estimate -0.828
23 ARM Xanomeline High Dose std.error 1.267
24 ARM Xanomeline High Dose statistic -0.654
25 ARM Xanomeline High Dose p.value 0.514
26 ARM Xanomeline High Dose conf.low -3.324
27 ARM Xanomeline High Dose conf.high 1.668
28 ARM Xanomeline Low Dose term ARMXanomeline Low Dose
29 ARM Xanomeline Low Dose var_label Description of Planned Arm
30 ARM Xanomeline Low Dose var_class character
31 ARM Xanomeline Low Dose var_type categorical
32 ARM Xanomeline Low Dose var_nlevels 3
33 ARM Xanomeline Low Dose contrasts contr.treatment
34 ARM Xanomeline Low Dose contrasts_type treatment
35 ARM Xanomeline Low Dose reference_row FALSE
36 ARM Xanomeline Low Dose label Xanomeline Low Dose
37 ARM Xanomeline Low Dose n_obs 84
38 ARM Xanomeline Low Dose estimate 0.457
39 ARM Xanomeline Low Dose std.error 1.267
40 ARM Xanomeline Low Dose statistic 0.361
41 ARM Xanomeline Low Dose p.value 0.719
42 ARM Xanomeline Low Dose conf.low -2.039
43 ARM Xanomeline Low Dose conf.high 2.953

19 changes: 19 additions & 0 deletions tests/testthat/_snaps/ard_regression_basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ard_regression_basic() works

Code
dplyr::select(as.data.frame(ard), -fmt_fn)
Output
variable variable_level context stat_name stat_label stat
1 ARM Xanomeline High Dose regression estimate Coefficient -0.8283499
2 ARM Xanomeline High Dose regression std.error Standard Error 1.267394
3 ARM Xanomeline High Dose regression statistic statistic -0.653585
4 ARM Xanomeline High Dose regression p.value p-value 0.5139775
5 ARM Xanomeline High Dose regression conf.low CI Lower Bound -3.324433
6 ARM Xanomeline High Dose regression conf.high CI Upper Bound 1.667733
7 ARM Xanomeline Low Dose regression estimate Coefficient 0.4573643
8 ARM Xanomeline Low Dose regression std.error Standard Error 1.267394
9 ARM Xanomeline Low Dose regression statistic statistic 0.3608698
10 ARM Xanomeline Low Dose regression p.value p-value 0.7185003
11 ARM Xanomeline Low Dose regression conf.low CI Lower Bound -2.038718
12 ARM Xanomeline Low Dose regression conf.high CI Upper Bound 2.953447

14 changes: 12 additions & 2 deletions tests/testthat/test-ard_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ test_that("ard_regression() works", {
expect_snapshot(
lm(AGE ~ ARM, data = cards::ADSL) |>
ard_regression(add_estimate_to_reference_rows = TRUE) |>
as.data.frame() |>
dplyr::select(-context, -stat_label, -fmt_fn) |>
dplyr::mutate(
stat = lapply(stat, function(x) ifelse(is.numeric(x), cards::round5(x, 3), x))
) |>
print(n = Inf)
)
)

# checking non-syntactic names
expect_equal(
lm(AGE ~ `Treatment Arm`, data = cards::ADSL |> dplyr::rename(`Treatment Arm` = ARM)) |>
ard_regression(add_estimate_to_reference_rows = TRUE) |>
dplyr::pull(variable) |>
unique(),
"Treatment Arm"
)
})
9 changes: 9 additions & 0 deletions tests/testthat/test-ard_regression_basic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("ard_regression_basic() works", {
expect_error(
ard <- lm(AGE ~ ARM, data = cards::ADSL) |>
ard_regression_basic(),
NA
)

expect_snapshot(as.data.frame(ard) |> dplyr::select(-fmt_fn))
})

0 comments on commit b91020e

Please sign in to comment.