-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the new function to its own script
- Loading branch information
Showing
8 changed files
with
162 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' 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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +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 | ||
|
||
# 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 | ||
stat 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 | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |