Skip to content
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

Update in tbl_ard_summary() to assign default summary types for non-standard ARDs #2001

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -80,6 +80,7 @@ Suggests:
parsnip (>= 0.1.7),
rmarkdown,
smd (>= 0.6.6),
spelling,
survey (>= 4.2),
survival (>= 3.6-4),
testthat (>= 3.2.0),
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gtsummary (development version)

* Update in `tbl_ard_summary()` to better handle non-standard ARDs (i.e. not our typical continuous or categorical summaries) by assigning them a default summary type. (#1991)

* Made the `oneway.test()` available in `add_p.tbl_continuous()`. (#1970)

* Added argument `tbl_ard_summary(overall)`. When `TRUE`, the ARD is parsed into primary ARD and the Overall ARD and we run `tbl_ard_summary() |> add_overall()`. (#1940)
Expand Down Expand Up @@ -138,7 +140,7 @@ Updates to address regressions in the v2.0.0 release:

* Added `pkgdown_print.gtsummary()` method that is only registered when the pkgdown package is loaded. This enables printing of gtsummary tables on the pkgdown site in the Examples section. (#1771)

* The package now uses updated `survey::svyquantile()` function to calculate quatiles, which was introduced in survey v4.1
* The package now uses updated `survey::svyquantile()` function to calculate quantiles, which was introduced in survey v4.1

### Bug fixes

Expand Down Expand Up @@ -170,7 +172,7 @@ Updates to address regressions in the v2.0.0 release:

* Arguments `modify_header(update)`, `modify_footnote(update)`, `modify_spanning_header(update)`, and `modify_fmt_fun(update)` have been deprecated. Use dynamic dots instead, e.g. `modify_header(...)`, which has been the preferred method for passing updates for a few years.

* Function `continuous_summary()` has been deprecated immediately. Apologies for the inconvenience of the immeidate deprecation. The way the function originally worked is not compatible with the updated internal structures. In most cases, users can use the `tbl_continuous()` function instead.
* Function `continuous_summary()` has been deprecated immediately. Apologies for the inconvenience of the immediate deprecation. The way the function originally worked is not compatible with the updated internal structures. In most cases, users can use the `tbl_continuous()` function instead.

* Arguments `add_stat(fmt_fun, header, footnote, new_col_name)` have been deprecated since v1.4.0 (2021-04-13). They have now been fully removed from the package.

Expand Down
2 changes: 1 addition & 1 deletion R/add_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ add_p <- function(x, ...) {
#' @section test argument:
#'
#' See the [?tests][tests] help file for details on available tests and creating custom tests.
#' The [?tests][tests] help file also includes psuedo-code for each test to be clear
#' The [?tests][tests] help file also includes pseudo-code for each test to be clear
#' precisely how the calculation is performed.
#'
#' The default test used in `add_p()` primarily depends on these factors:
Expand Down
2 changes: 1 addition & 1 deletion R/assign_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' @param cont_variable (`string`)\cr
#' a column name of the continuous summary variable in `tbl_continuous()`
#' @param summary_type (named `list`)\cr
#' naemd list of summary types
#' named list of summary types
#' @inheritParams cli::cli_abort
#'
#' @return A table of class `'gtsummary'`
Expand Down
13 changes: 11 additions & 2 deletions R/tbl_ard_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ tbl_ard_summary <- function(cards,
) |>
deframe() |>
as.list()
# for non-standard ARDs, fill in the missing default types
for (v in setdiff(include, names(default_types))) {
if (!"variable_level" %in% names(cards) ||
is_empty(compact(dplyr::filter(cards, .data$variable %in%.env$v)$variable_level))) {
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
default_types[[v]] <- "continuous"
}
else default_types[[v]] <- "categorical" # styler: off
}
if (exists("v")) remove("v") # styler: off

# process arguments ----------------------------------------------------------
cards::process_formula_selectors(
Expand All @@ -187,14 +196,14 @@ tbl_ard_summary <- function(cards,
walk(
include,
function(variable) {
if (default_types[[variable]] %in% "continuous" &&
if (isTRUE(default_types[[variable]] %in% "continuous") &&
!type[[variable]] %in% c("continuous", "continuous2")) {
cli::cli_abort(
"Summary type for variable {.val {variable}} must be one of
{.val {c('continuous', 'continuous2')}}, not {.val {type[[variable]]}}.",
call = get_cli_abort_call()
)
} else if (default_types[[variable]] %in% c("categorical", "dichotomous") &&
} else if (isTRUE(default_types[[variable]] %in% c("categorical", "dichotomous")) &&
!identical(type[[variable]], default_types[[variable]])) {
cli::cli_abort(
"Summary type for variable {.val {variable}} must be
Expand Down
2 changes: 1 addition & 1 deletion R/tbl_uvregression.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#' is the dependent variable, and `{x}` represents a single covariate. For a
#' random intercept model, the formula may be `formula = "{y} ~ {x} + (1 | gear)"`.
#' @param method.args (named `list`)\cr
#' Named list of arguments assed to `method`.
#' Named list of arguments passed to `method`.
#' @param hide_n (scalar `logical`)\cr
#' Hide N column. Default is `FALSE`
#' @inheritParams tbl_regression
Expand Down
4 changes: 2 additions & 2 deletions R/theme_gtsummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @param set_theme (scalar `logical`)\cr
#' Logical indicating whether to set the theme. Default is `TRUE`.
#' When `FALSE` the named list of theme elements is returned invisibly
#' @param font_size (scaler `numeric`)\cr
#' @param font_size (scalar `numeric`)\cr
#' Numeric font size for compact theme.
#' Default is 13 for gt tables, and 8 for all other output types
#'
Expand All @@ -29,7 +29,7 @@
#' - `tbl_summary()` all percentages rounded to one decimal place
#' - `tbl_regression()`,`tbl_uvregression()` add significance stars with `add_significance_stars()`;
#' hides CI and p-value from output
#' - For flaxtable and huxtable output, the coeficient's standard error is placed below. For gt, it is placed to the right.
#' - For flextable and huxtable output, the coefficients' standard error is placed below. For gt, it is placed to the right.
#' - `theme_gtsummary_compact()`
#' - tables printed with gt, flextable, kableExtra, or huxtable will be compact with smaller font size and reduced cell padding
#' - `theme_gtsummary_printer(print_engine)`
Expand Down
31 changes: 24 additions & 7 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
ANCOVA
ARD
ARDs
Agresti
BH
Biostatistics
CDISC
CMD
Codecov
Coull
DOI
GGally
GVIF
Gehan
Hmisc
JAMA
Jeffreys
Kaplan
Lifecycle
Likert
MacOS
McNemar
McNemar's
Expand All @@ -21,20 +30,22 @@ RJ
RStudio
RTF
Rao
Rmarkdown
SHA
Tarone
Tidiers
Univariable
VIF
Waerden's
Wainberg
YAML
bonferroni
bstfun
cardx
cli
codebase
coef
conf
coxph
crosstab
customizability
customizable
der
Expand All @@ -44,27 +55,30 @@ dplyr
effectsize
emmeans
exponentiate
fdr
flextable
forcats
ftExtra
ggplot
ggstats
glm
hochberg
holm
hommel
huxtable
kable
kableExtra
knitr
labelled
lifecycle
lm
likert
linebreaks
lme
logLik
mL
mira
mis
nevent
ng
nnet
obejcts
pkgdown
pre
pvalue
quosure
Expand All @@ -80,11 +94,14 @@ th
tibble
tibbles
tidiers
tidycmprsk
tidyr
tidyselect
tidyselect's
tidyverse
tilda
un
unhidden
unhide
unicode
univariable
Expand Down
2 changes: 1 addition & 1 deletion man/add_p.tbl_summary.Rd

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

2 changes: 1 addition & 1 deletion man/assign_tests.Rd

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

2 changes: 1 addition & 1 deletion man/tbl_uvregression.Rd

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

4 changes: 2 additions & 2 deletions man/theme_gtsummary.Rd

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

3 changes: 3 additions & 0 deletions tests/spelling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(requireNamespace('spelling', quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/tbl_ard_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,14 @@
Error in `tbl_ard_summary()`:
! Variable "AGE" is type `continuous` and `statistic` argument value must be a string of length one.

# tbl_ard_summary() non-standard ARDs (ie not 'continuous', 'categorical', etc)

Code
as.data.frame(tbl_ard_summary(ard, by = trt, statistic = ~"{estimate}"))
Output
**Characteristic** **Drug A** **Drug B**
1 time <NA> <NA>
2 12 90.8 86.3
3 24 46.9 41.2
4 age 47.0 47.4

37 changes: 37 additions & 0 deletions tests/testthat/test-tbl_ard_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,40 @@ test_that("tbl_ard_summary() existing 'gts_column'", {
)
})

test_that("tbl_ard_summary() non-standard ARDs (ie not 'continuous', 'categorical', etc)", {
# This ARD was created with this code:
# cards::bind_ard(
# survival::survfit(survival::Surv(ttdeath, death) ~ trt, trial) |>
# cardx::ard_survival_survfit(times = c(12, 24)) |>
# dplyr::filter(stat_name %in% c("estimate")) |>
# dplyr::mutate(
# fmt_fn = list("xx.x%"),
# group1_level = unlist(group1_level) |> as.character() |> as.list()
# ),
# cardx::ard_stats_t_test_onesample(trial, variables = age, by = trt) |>
# dplyr::filter(stat_name %in% c("estimate"))
# ) |>
# dplyr::select(-cards::all_missing_columns()) |>
# dput()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not creating it? does it take a long time?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH boy, IDK what I was thinking! Updated to just build the ARDs. THANK YOU for questioning haha


ard <-
structure(list(group1 = c("trt", "trt", "trt", "trt", "trt",
"trt"), group1_level = list("Drug A", "Drug A", "Drug B", "Drug B",
"Drug A", "Drug B"), variable = c("time", "time", "time",
"time", "age", "age"), variable_level = list(12, 24, 12, 24,
NULL, NULL), context = c("survival", "survival", "survival",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something is off with the styler here

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't like this style?? 😝😝😝

"survival", "stats_t_test_onesample", "stats_t_test_onesample"
), stat_name = c("estimate", "estimate", "estimate", "estimate",
"estimate", "estimate"), stat_label = c("Survival Probability",
"Survival Probability", "Survival Probability", "Survival Probability",
"Mean", "Mean"), stat = list(0.908163265306122, 0.46938775510204,
0.862745098039216, 0.411764705882353, c(`mean of x` = 47.010989010989),
c(`mean of x` = 47.4489795918367)), fmt_fn = list("xx.x%",
"xx.x%", "xx.x%", "xx.x%", 1L, 1L)), row.names = c(NA, -6L
), class = c("card", "tbl_df", "tbl", "data.frame"))

expect_snapshot(
tbl_ard_summary(ard, by = trt, statistic = ~ "{estimate}") |>
as.data.frame()
)
})