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

Expand get_stats to cover all analysis functions #1123

Merged
merged 19 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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 NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export(tabulate_survival_biomarkers)
export(tabulate_survival_subgroups)
export(tern_default_formats)
export(tern_default_labels)
export(tern_default_stats)
export(test_proportion_diff)
export(to_n)
export(to_string_matrix)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Added `ref_group_position` function to place the reference group facet last, first or at a certain position.
* Added `keep_level_order` split function to retain original order of levels in a split.
* Added `level_order` split function to reorder manually the levels.
* Added list containing default statistics for each method group, `tern_default_stats`.

### Enhancements
* Added `ref_group_coxph` parameter to `g_km` to specify the reference group used for pairwise Cox-PH calculations when `annot_coxph = TRUE`.
Expand Down
106 changes: 70 additions & 36 deletions R/utils_default_stats_formats_labels.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' Defaults for statistical method names and their associated formats & labels
#' Get default statistical methods and their associated formats, labels, and indent modifiers
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Utility functions to get valid statistic methods for different method groups
#' (`.stats`) and their associated formats (`.formats`) and labels (`.labels`). This utility
#' is used across `tern`, but some of its working principles can be seen in [analyze_vars()].
#' See notes to understand why this is experimental.
#' (`.stats`) and their associated formats (`.formats`), labels (`.labels`), and indent modifiers
#' (`.indent_mods`). This utility is used across `tern`, but some of its working principles can be
#' seen in [analyze_vars()]. See notes to understand why this is experimental.
#'
#' @param stats (`character`)\cr statistical methods to get defaults formats or labels for.
#' @param stats (`character`)\cr statistical methods to get defaults for.
#'
#' @details
#' Current choices for `type` are `counts` and `numeric` for [analyze_vars()] and affect `get_stats()`.
Expand All @@ -19,18 +19,18 @@
#' @name default_stats_formats_labels
NULL

#' @describeIn default_stats_formats_labels Get defaults statistical methods for different
#' groups of methods.
#' @describeIn default_stats_formats_labels Get statistics available for a given method
#' group (analyze function).
#'
#' @param method_groups (`character`)\cr indicates the group of statistical methods that
#' we need the defaults from. A character vector can be used to collect more than one group of statistical
#' methods.
#' @param stats_in (`character`)\cr desired stats to be picked out from the selected method group.
#' @param add_pval (`flag`)\cr should `"pval"` or `"pval_counts"` (if `method_groups` contains
#' @param method_groups (`character`)\cr indicates the statistical method group (`tern` analyze function)
#' to retrieve default statistics for. A character vector can be used to specify more than one statistical
#' method group.
#' @param stats_in (`character`)\cr statistics to retrieve for the selected method group.
#' @param add_pval (`flag`)\cr should `"pval"` (or `"pval_counts"` if `method_groups` contains
#' `"analyze_vars_counts"`) be added to the statistical methods?
#'
#' @return
#' * `get_stats()` returns a character vector with all default statistical methods.
#' * `get_stats()` returns a `character` vector of statistical methods.
#'
#' @examples
#' # analyze_vars is numeric
Expand Down Expand Up @@ -66,21 +66,11 @@ get_stats <- function(method_groups = "analyze_vars_numeric", stats_in = NULL, a

# Loop for multiple method groups
for (mgi in method_groups) {
# Main switcher
out_tmp <- switch(mgi,
"count_occurrences" = c("count", "count_fraction", "count_fraction_fixed_dp", "fraction"),
"summarize_num_patients" = c("unique", "nonunique", "unique_count"),
"analyze_vars_counts" = c("n", "count", "count_fraction", "n_blq"),
"analyze_vars_numeric" = c(
"n", "sum", "mean", "sd", "se", "mean_sd", "mean_se", "mean_ci", "mean_sei",
"mean_sdi", "mean_pval", "median", "mad", "median_ci", "quantiles", "iqr",
"range", "min", "max", "median_range", "cv", "geom_mean", "geom_mean_ci",
"geom_cv"
),
stop(
"The selected method group (", mgi, ") has no default statistical method."
)
)
out_tmp <- if (mgi %in% names(tern_default_stats)) {
tern_default_stats[[mgi]]
} else {
stop("The selected method group (", mgi, ") has no default statistical method.")
}
out <- unique(c(out, out_tmp))
}

Expand Down Expand Up @@ -131,14 +121,14 @@ get_stats <- function(method_groups = "analyze_vars_numeric", stats_in = NULL, a
out
}

#' @describeIn default_stats_formats_labels Get formats from vector of statistical methods. If not
#' present `NULL` is returned.
#' @describeIn default_stats_formats_labels Get formats corresponding to a list of statistics.
#'
#' @param formats_in (named `vector`) \cr inserted formats to replace defaults. It can be a
#' character vector from [formatters::list_valid_format_labels()] or a custom format function.
#'
#' @return
#' * `get_formats_from_stats()` returns a named list of formats, they being a value from
#' * `get_formats_from_stats()` returns a named vector of formats (if present in either
#' `tern_default_formats` or `formats_in`, otherwise `NULL`). Values can be taken from
#' [formatters::list_valid_format_labels()] or a custom function (e.g. [formatting_functions]).
#'
#' @note Formats in `tern` and `rtables` can be functions that take in the table cell value and
Expand Down Expand Up @@ -187,18 +177,18 @@ get_formats_from_stats <- function(stats, formats_in = NULL) {
out
}

#' @describeIn default_stats_formats_labels Get labels from vector of statistical methods.
#' @describeIn default_stats_formats_labels Get labels corresponding to a list of statistics.
#'
#' @param labels_in (named `vector`)\cr inserted labels to replace defaults.
#' @param labels_in (named `vector` of `character`)\cr inserted labels to replace defaults.
#' @param row_nms (`character`)\cr row names. Levels of a `factor` or `character` variable, each
#' of which the statistics in `.stats` will be calculated for. If this parameter is set, these
#' variable levels will be used as the defaults, and the names of the given custom values should
#' correspond to levels (or have format `statistic.level`) instead of statistics. Can also be
#' variable names if rows correspond to different variables instead of levels. Defaults to `NULL`.
#'
#' @return
#' * `get_labels_from_stats()` returns a named character vector of default labels (if present
#' otherwise `NULL`).
#' * `get_labels_from_stats()` returns a named `character` vector of labels (if present in either
#' `tern_default_labels` or `labels_in`, otherwise `NULL`).
#'
#' @examples
#' # Defaults labels
Expand Down Expand Up @@ -306,7 +296,51 @@ get_indents_from_stats <- function(stats, indents_in = NULL, row_nms = NULL) {
out
}

#' @describeIn default_stats_formats_labels Named list of default formats for `tern`.
#' @describeIn default_stats_formats_labels Named list of available statistics by method group for `tern`.
#'
#' @format
#' * `tern_default_stats` is a named list of available statistics, with each element
#' named for their corresponding statistical method group.
#'
#' @export
tern_default_stats <- list(
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
abnormal = c("fraction"),
abnormal_by_baseline = c("fraction"),
abnormal_by_marked = c("count_fraction"),
abnormal_by_worst_grade = c("count_fraction"),
abnormal_by_worst_grade_worsen = c("fraction"),
analyze_patients_exposure_in_cols = c("n_patients", "sum_exposure"),
analyze_vars_counts = c("n", "count", "count_fraction", "n_blq"),
analyze_vars_numeric = c(
"n", "sum", "mean", "sd", "se", "mean_sd", "mean_se", "mean_ci", "mean_sei", "mean_sdi", "mean_pval",
"median", "mad", "median_ci", "quantiles", "iqr", "range", "min", "max", "median_range", "cv",
"geom_mean", "geom_mean_ci", "geom_cv"
),
count_cumulative = c("count_fraction"),
count_missed_doses = c("n", "count_fraction"),
count_occurrences = c("count", "count_fraction", "count_fraction_fixed_dp", "fraction"),
count_occurrences_by_grade = c("count_fraction"),
count_patients_with_event = c("n", "count", "count_fraction", "n_blq"),
count_patients_with_flags = c("n", "count", "count_fraction", "n_blq"),
count_values = c("n", "count", "count_fraction", "n_blq"),
coxph_pairwise = c("pvalue", "hr", "hr_ci", "n_tot", "n_tot_events"),
estimate_incidence_rate = c("person_years", "n_events", "rate", "rate_ci"),
estimate_multinomial_response = c("n_prop", "prop_ci"),
estimate_odds_ratio = c("or_ci", "n_tot"),
estimate_proportion = c("n_prop", "prop_ci"),
estimate_proportion_diff = c("diff", "diff_ci"),
summarize_ancova = c("n", "lsmean", "lsmean_diff", "lsmean_diff_ci", "pval"),
summarize_coxreg = c("n", "hr", "ci", "pval", "pval_inter"),
summarize_glm_count = c("n", "rate", "rate_ci", "rate_ratio", "rate_ratio_ci", "pval"),
summarize_num_patients = c("unique", "nonunique", "unique_count"),
summarize_patients_events_in_cols = c("unique", "all"),
surv_time = c("median", "median_ci", "quantiles", "range_censor", "range_event", "range"),
surv_timepoint = c("pt_at_risk", "event_free_rate", "rate_se", "rate_ci", "rate_diff", "rate_diff_ci", "ztest_pval"),
test_proportion_diff = c("pval")
)

#' @describeIn default_stats_formats_labels Named vector of default formats for `tern`.
#'
#' @format
#' * `tern_default_formats` is a list of available formats, named after their relevant
#' statistic.
Expand Down
52 changes: 31 additions & 21 deletions man/default_stats_formats_labels.Rd

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