Skip to content

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 13 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
tidyr (>= 1.3.0)
Suggests:
broom (>= 1.0.5),
broom.helpers (>= 1.13.0),
spelling,
testthat (>= 3.2.0),
withr
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(ard_regression,default)
export("%>%")
export(all_of)
export(any_of)
Expand All @@ -8,6 +9,7 @@ export(ard_fishertest)
export(ard_paired_ttest)
export(ard_paired_wilcoxtest)
export(ard_proportion_ci)
export(ard_regression)
export(ard_ttest)
export(ard_wilcoxtest)
export(contains)
Expand Down
81 changes: 81 additions & 0 deletions R/ard_regression.R
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
}
2 changes: 2 additions & 0 deletions R/cardx-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
## usethis namespace: start
## usethis namespace: end
NULL

utils::globalVariables(c("."))
8 changes: 6 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ reference:
- title: "ARD Creation"
- subtitle: "Inference"
- contents:
- ard_ttest
- ard_fishertest
- ard_chisqtest
- ard_fishertest
- ard_ttest
- ard_wilcoxtest

- subtitle: "Estimation"
- contents:
- ard_proportion_ci
- ard_regression

- title: "Helpers"
- contents:
Expand Down
30 changes: 30 additions & 0 deletions man/ard_regression.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-ard_regression.R
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()
)
})