-
-
Notifications
You must be signed in to change notification settings - Fork 4
Adding ard_regression()
#26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9ea318e
adding ard_regression
ddsjoberg 236cf18
Merge branch 'main' into ard_regression
ddsjoberg 11b44cd
Merge branch 'main' into ard_regression
ddsjoberg 2b8e612
Merge branch 'main' into ard_regression
ddsjoberg 8fcaafe
Merge branch 'main' into ard_regression
ddsjoberg e0124ea
Merge branch 'main' into ard_regression
ddsjoberg 6eff950
Merge commit '7978fbd94631089352318c07a05398888e6beefa'
ddsjoberg 6d94a5b
Merge branch 'main' into ard_regression
ddsjoberg 68d4727
Merge branch 'main' into ard_regression
ddsjoberg 3bf4901
Merge branch 'main' into ard_regression
ddsjoberg cd9c8ab
Merge commit '8dbdc9d81e4d9f9e6e71249b69417dd52d8dce94'
ddsjoberg 9234207
Update R/ard_regression.R
ddsjoberg 92af0b3
doc updates
ddsjoberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
#' Regression ARD | ||
#' | ||
#' Function takes a regression model object and converts it to a ARD | ||
#' structure using the `broom.helpers` package. | ||
#' | ||
#' @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 | ||
#' | ||
#' @examples | ||
#' lm(AGE ~ ARM, data = cards::ADSL) |> | ||
#' ard_regression(add_estimate_to_reference_rows = TRUE) | ||
NULL | ||
|
||
#' @rdname ard_regression | ||
#' @export | ||
ard_regression <- function(x, ...) { | ||
UseMethod("ard_regression") | ||
} | ||
|
||
#' @rdname ard_regression | ||
#' @export | ||
ard_regression.default <- 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") | ||
|
||
# summarize model ------------------------------------------------------------ | ||
broom.helpers::tidy_plus_plus( | ||
model = x, | ||
tidy_fun = tidy_fun, | ||
... | ||
) |> | ||
dplyr::mutate( | ||
variable_level = dplyr::if_else(.data$var_type %in% "continuous", NA_character_, .data$label), | ||
dplyr::across(-c("variable", "variable_level"), .fns = as.list) | ||
) |> | ||
tidyr::pivot_longer( | ||
cols = -c("variable", "variable_level"), | ||
names_to = "stat_name", | ||
values_to = "statistic" | ||
) |> | ||
dplyr::filter(map_lgl(.data$statistic, Negate(is.na))) |> | ||
dplyr::mutate( | ||
statistic_fmt_fn = | ||
lapply( | ||
.data$statistic, | ||
function(x) { | ||
switch(is.integer(x), 0L) %||% # styler: off | ||
switch(is.numeric(x), 1L) # styler: off | ||
} | ||
), | ||
context = "regression", | ||
stat_label = | ||
dplyr::case_when( | ||
.data$stat_name %in% "var_label" ~ "Label", | ||
.data$stat_name %in% "var_class" ~ "Class", | ||
.data$stat_name %in% "var_type" ~ "Type", | ||
.data$stat_name %in% "var_nlevels" ~ "N Levels", | ||
.data$stat_name %in% "contrasts_type" ~ "Contrast Type", | ||
.data$stat_name %in% "label" ~ "Level Label", | ||
.data$stat_name %in% "n_obs" ~ "N Obs.", | ||
.data$stat_name %in% "n_event" ~ "N Events", | ||
.data$stat_name %in% "exposure" ~ "Exposure Time", | ||
.data$stat_name %in% "estimate" ~ "Coefficient", | ||
.data$stat_name %in% "std.error" ~ "Standard Error", | ||
.data$stat_name %in% "p.value" ~ "p-value", | ||
.data$stat_name %in% "conf.low" ~ "CI Lower Bound", | ||
.data$stat_name %in% "conf.high" ~ "CI Upper Bound", | ||
TRUE ~ .data$stat_name | ||
) | ||
) |> | ||
cards::tidy_ard_column_order() %>% | ||
{structure(., class = c("card", class(.)))} # styler: off | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
## usethis namespace: start | ||
## usethis namespace: end | ||
NULL | ||
|
||
utils::globalVariables(c(".")) |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,10 @@ | ||
test_that("ard_regression() works", { | ||
expect_snapshot( | ||
lm(AGE ~ ARM, data = cards::ADSL) |> | ||
ard_regression(add_estimate_to_reference_rows = TRUE) |> | ||
dplyr::mutate( | ||
statistic = lapply(statistic, function(x) ifelse(is.numeric(x), cards::round5(x, 3), x)) | ||
) |> | ||
as.data.frame() | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.