diff --git a/DESCRIPTION b/DESCRIPTION index 26e6386f03..a1111d59d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -145,6 +145,7 @@ Collate: 'summarize_glm_count.R' 'summarize_num_patients.R' 'summarize_patients_exposure_in_cols.R' + 'summary_stats.R' 'survival_biomarkers_subgroups.R' 'survival_coxph_pairwise.R' 'survival_duration_subgroups.R' diff --git a/NAMESPACE b/NAMESPACE index 2717dc01a0..5239b12fcb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -35,7 +35,6 @@ export(a_odds_ratio) export(a_proportion) export(a_proportion_diff) export(a_summary) -export(a_summary_output) export(add_rowcounts) export(aesi_label) export(analyze_num_patients) diff --git a/R/analyze_variables.R b/R/analyze_variables.R index 052a2534a6..5a2eefbd48 100644 --- a/R/analyze_variables.R +++ b/R/analyze_variables.R @@ -1,218 +1,3 @@ -#' Control Function for Descriptive Statistics -#' -#' @description `r lifecycle::badge("stable")` -#' -#' Sets a list of parameters for summaries of descriptive statistics. Typically used internally to specify -#' details for [s_summary()]. This function family is mainly used by [analyze_vars()]. -#' -#' @inheritParams argument_convention -#' @param quantiles (`numeric`)\cr of length two to specify the quantiles to calculate. -#' @param quantile_type (`numeric`)\cr between 1 and 9 selecting quantile algorithms to be used. -#' Default is set to 2 as this matches the default quantile algorithm in SAS `proc univariate` set by `QNTLDEF=5`. -#' This differs from R's default. See more about `type` in [stats::quantile()]. -#' @param test_mean (`numeric`)\cr to test against the mean under the null hypothesis when calculating p-value. -#' -#' @note Deprecation cycle started for `control_summarize_vars` as it is going to renamed into -#' `control_analyze_vars`. Intention is to reflect better the core underlying `rtables` -#' functions; in this case [analyze_vars()] wraps [rtables::analyze()]. -#' -#' @return A list of components with the same names as the arguments. -#' -#' @export control_analyze_vars control_summarize_vars -#' @aliases control_summarize_vars -control_analyze_vars <- function(conf_level = 0.95, - quantiles = c(0.25, 0.75), - quantile_type = 2, - test_mean = 0) { - checkmate::assert_vector(quantiles, len = 2) - checkmate::assert_int(quantile_type, lower = 1, upper = 9) - checkmate::assert_numeric(test_mean) - lapply(quantiles, assert_proportion_value) - assert_proportion_value(conf_level) - list(conf_level = conf_level, quantiles = quantiles, quantile_type = quantile_type, test_mean = test_mean) -} - -control_summarize_vars <- control_analyze_vars - -#' Summary Statistic Settings Functions -#' -#' @description `r lifecycle::badge("stable")` -#' -#' Functions to retrieve default settings for summary statistics and customize these settings. -#' -#' @param type (`character`)\cr choice of summary data type. Only `counts` and `numeric` types are currently supported. -#' @param include_pval (`logical`)\cr whether p-value should be included as a default statistic. -#' -#' @name summary_stats -NULL - -#' @describeIn summary_stats Function to retrieve default formats for summary statistics. Returns format patterns for -#' descriptive statistics which are understood by `rtables`. -#' -#' @return -#' * `summary_formats` returns a named `vector` of default statistic formats for the given data type. -#' -#' @examples -#' summary_formats() -#' summary_formats(type = "count", include_pval = TRUE) -#' -#' @export -summary_formats <- function(type = "numeric", include_pval = FALSE) { - fmts <- if (type == "counts") { - c( - n = "xx.", - count = "xx.", - count_fraction = format_count_fraction, - n_blq = "xx.", - pval = "x.xxxx | (<0.0001)" - ) - } else { - c( - n = "xx.", - sum = "xx.x", - mean = "xx.x", - sd = "xx.x", - se = "xx.x", - mean_sd = "xx.x (xx.x)", - mean_se = "xx.x (xx.x)", - mean_ci = "(xx.xx, xx.xx)", - mean_sei = "(xx.xx, xx.xx)", - mean_sdi = "(xx.xx, xx.xx)", - mean_pval = "xx.xx", - median = "xx.x", - mad = "xx.x", - median_ci = "(xx.xx, xx.xx)", - quantiles = "xx.x - xx.x", - iqr = "xx.x", - range = "xx.x - xx.x", - min = "xx.x", - max = "xx.x", - median_range = "xx.x (xx.x - xx.x)", - cv = "xx.x", - geom_mean = "xx.x", - geom_mean_ci = "(xx.xx, xx.xx)", - geom_cv = "xx.x", - pval = "x.xxxx | (<0.0001)" - ) - } - if (!include_pval) fmts <- head(fmts, -1) - fmts -} - -#' @describeIn summary_stats Function to retrieve default labels for summary statistics. Returns labels of descriptive -#' statistics which are understood by `rtables`. -#' -#' @return -#' * `summary_labels` returns a named `vector` of default statistic labels for the given data type. -#' -#' @examples -#' summary_labels() -#' summary_labels(type = "count", include_pval = TRUE) -#' -#' @export -summary_labels <- function(type = "numeric", include_pval = FALSE) { - lbls <- if (type == "counts") { - c( - n = "n", - count = "count", - count_fraction = "count_fraction", - n_blq = "n_blq", - pval = "p-value (chi-squared test)" - ) - } else { - c( - n = "n", - sum = "Sum", - mean = "Mean", - sd = "SD", - se = "SE", - mean_sd = "Mean (SD)", - mean_se = "Mean (SE)", - mean_ci = "Mean 95% CI", - mean_sei = "Mean -/+ 1xSE", - mean_sdi = "Mean -/+ 1xSD", - mean_pval = "Mean p-value (H0: mean = 0)", - median = "Median", - mad = "Median Absolute Deviation", - median_ci = "Median 95% CI", - quantiles = "25% and 75%-ile", - iqr = "IQR", - range = "Min - Max", - min = "Minimum", - max = "Maximum", - median_range = "Median (Min - Max)", - cv = "CV (%)", - geom_mean = "Geometric Mean", - geom_mean_ci = "Geometric Mean 95% CI", - geom_cv = "CV % Geometric Mean", - pval = "p-value (t-test)" - ) - } - if (!include_pval) lbls <- head(lbls, -1) - lbls -} - -#' @describeIn summary_stats Function to configure settings for default or custom summary statistics for a given data -#' type. In addition to selecting a custom subset of statistics, the user can also set custom formats, labels, and -#' indent modifiers for any of these statistics. -#' -#' @param stats_custom (`named vector` of `character`)\cr vector of statistics to include if not the defaults. This -#' argument overrides `include_pval` and other custom value arguments such that only settings for these statistics -#' will be returned. -#' @param formats_custom (`named vector` of `character`)\cr vector of custom statistics formats to use in place of the -#' defaults defined in [`summary_formats()`]. Names should be a subset of the statistics defined in `stats_custom` (or -#' default statistics if this is `NULL`). -#' @param labels_custom (`named vector` of `character`)\cr vector of custom statistics labels to use in place of the -#' defaults defined in [`summary_labels()`]. Names should be a subset of the statistics defined in `stats_custom` (or -#' default statistics if this is `NULL`). -#' @param indent_mods_custom (`integer` or `named vector` of `integer`)\cr vector of custom indentation modifiers for -#' statistics to use instead of the default of `0L` for all statistics. Names should be a subset of the statistics -#' defined in `stats_custom` (or default statistics if this is `NULL`). Alternatively, the same indentation modifier -#' can be applied to all statistics by setting `indent_mods_custom` to a single integer value. -#' -#' @return -#' * `summary_custom` returns a `list` of 4 named elements: `stats`, `formats`, `labels`, and `indent_mods`. -#' -#' @examples -#' summary_custom() -#' summary_custom(type = "counts", include_pval = TRUE) -#' summary_custom( -#' include_pval = TRUE, stats_custom = c("n", "mean", "sd", "pval"), -#' labels_custom = c(sd = "Std. Dev."), indent_mods_custom = 3L -#' ) -#' -#' @export -summary_custom <- function(type = "numeric", - include_pval = FALSE, - stats_custom = NULL, - formats_custom = NULL, - labels_custom = NULL, - indent_mods_custom = NULL) { - if ("pval" %in% stats_custom) include_pval <- TRUE - - .formats <- summary_formats(type = type, include_pval = include_pval) - .stats <- if (is.null(stats_custom)) names(.formats) else intersect(stats_custom, names(.formats)) - .labels <- summary_labels(type = type, include_pval = include_pval) - .indent_mods <- stats::setNames(rep(0L, length(.stats)), .stats) - - if (!is.null(formats_custom)) .formats[names(formats_custom)] <- formats_custom - if (!is.null(labels_custom)) .labels[names(labels_custom)] <- labels_custom - if (!is.null(indent_mods_custom)) { - if (is.null(names(indent_mods_custom)) && length(indent_mods_custom) == 1) { - .indent_mods[names(.indent_mods)] <- indent_mods_custom - } else { - .indent_mods[names(indent_mods_custom)] <- indent_mods_custom - } - } - - list( - stats = .stats, - formats = .formats[.stats], - labels = .labels[.stats], - indent_mods = .indent_mods[.stats] - ) -} - #' Analyze Variables #' #' @description `r lifecycle::badge("stable")` @@ -590,7 +375,7 @@ s_summary.logical <- function(x, y } -#' @describeIn analyze_variables Formatted analysis helper function which is used within `a_summary`. +#' Helper Function to Create Output Rows for [a_summary()] #' #' @param compare (`logical`)\cr Whether comparison statistics should be analyzed instead of summary statistics #' (`compare = TRUE` adds `pval` statistic comparing against reference group). @@ -598,50 +383,29 @@ s_summary.logical <- function(x, #' `"numeric"`, otherwise type should be `"counts"`. #' #' @return -#' * `a_summary_output()` returns the corresponding list with formatted [rtables::CellValue()]. +#' * `a_summary_internal()` returns a corresponding list with formatted [rtables::CellValue()] used within `a_summary`. #' #' @note #' * To use for comparison (with additional p-value statistic), parameter `compare` must be set to `TRUE`. #' * Ensure that either all `NA` values are converted to an explicit `NA` level or all `NA` values are left as is. #' -#' @examples -#' set.seed(1) -#' x <- rnorm(10) -#' a_summary_output( -#' x = x, .N_col = 10, .N_row = 20, .var = "bla", .df_row = NULL, .ref_group = NULL, -#' .in_ref_col = FALSE, compare = FALSE, type = "numeric", .stats = summary_custom()$stats, -#' .formats = summary_custom()$formats, .labels = summary_custom()$labels, -#' .indent_mods = summary_custom()$indents, na.rm = TRUE, na_level = NA_character_ -#' ) -#' -#' x <- factor(c("a", "a", "b", "c", "a")) -#' a_summary_output( -#' x = x, .N_col = 10, .N_row = 10, .var = "bla", .df_row = NULL, .ref_group = NULL, -#' .in_ref_col = FALSE, compare = FALSE, type = "counts", -#' .stats = summary_custom(type = "counts")$stats, -#' .formats = summary_custom(type = "counts")$formats, -#' .labels = summary_custom(type = "counts")$labels, -#' .indent_mods = summary_custom(type = "counts")$indents, -#' na.rm = TRUE, na_level = NA_character_ -#' ) -#' -#' @export -a_summary_output <- function(x, - .N_col, # nolint - .N_row, # nolint - .var, - .df_row, - .ref_group, - .in_ref_col, - compare, - type, - .stats, - .formats, - .labels, - .indent_mods, - na.rm, # nolint - na_level, - ...) { +#' @keywords internal +a_summary_internal <- function(x, + .N_col, # nolint + .N_row, # nolint + .var, + .df_row, + .ref_group, + .in_ref_col, + compare, + type, + .stats, + .formats, + .labels, + .indent_mods, + na.rm, # nolint + na_level, + ...) { # Remove all-NA rows if (!is.null(.df_row) && ncol(.df_row) > 1) { in_tot_col <- nrow(.df_row) == length(x) @@ -702,6 +466,11 @@ a_summary_output <- function(x, #' @describeIn analyze_variables Formatted analysis function which is used as `afun` in `analyze_vars()` and #' `compare_vars()` and as `cfun` in `summarize_colvars()`. #' +#' @param compare (`logical`)\cr Whether comparison statistics should be analyzed instead of summary statistics +#' (`compare = TRUE` adds `pval` statistic comparing against reference group). +#' @param type (`character`)\cr type of statistics to calculate given `x`. If `x` is numeric `type` should be +#' `"numeric"`, otherwise type should be `"counts"`. +#' #' @return #' * `a_summary()` returns the corresponding list with formatted [rtables::CellValue()]. #' @@ -760,7 +529,7 @@ a_summary.default <- function(x, na.rm = TRUE, # nolint na_level = NA_character_, ...) { - a_summary_output( + a_summary_internal( x = x, .N_col = .N_col, .N_row = .N_row, @@ -804,7 +573,7 @@ a_summary.numeric <- function(x, na.rm = TRUE, # nolint na_level = NA_character_, ...) { - a_summary_output( + a_summary_internal( x = x, .N_col = .N_col, .N_row = .N_row, diff --git a/R/summary_stats.R b/R/summary_stats.R new file mode 100644 index 0000000000..7fffa4f025 --- /dev/null +++ b/R/summary_stats.R @@ -0,0 +1,214 @@ +#' Summary Statistic Settings Functions +#' +#' @description `r lifecycle::badge("stable")` +#' +#' Functions to retrieve default settings for summary statistics and customize these settings. +#' +#' @param type (`character`)\cr choice of summary data type. Only `counts` and `numeric` types are currently supported. +#' @param include_pval (`logical`)\cr whether p-value should be included as a default statistic. +#' +#' @name summary_stats +NULL + +#' @describeIn summary_stats Function to retrieve default formats for summary statistics. Returns format patterns for +#' descriptive statistics which are understood by `rtables`. +#' +#' @return +#' * `summary_formats` returns a named `vector` of default statistic formats for the given data type. +#' +#' @examples +#' summary_formats() +#' summary_formats(type = "count", include_pval = TRUE) +#' +#' @export +summary_formats <- function(type = "numeric", include_pval = FALSE) { + fmts <- if (type == "counts") { + c( + n = "xx.", + count = "xx.", + count_fraction = format_count_fraction, + n_blq = "xx.", + pval = "x.xxxx | (<0.0001)" + ) + } else { + c( + n = "xx.", + sum = "xx.x", + mean = "xx.x", + sd = "xx.x", + se = "xx.x", + mean_sd = "xx.x (xx.x)", + mean_se = "xx.x (xx.x)", + mean_ci = "(xx.xx, xx.xx)", + mean_sei = "(xx.xx, xx.xx)", + mean_sdi = "(xx.xx, xx.xx)", + mean_pval = "xx.xx", + median = "xx.x", + mad = "xx.x", + median_ci = "(xx.xx, xx.xx)", + quantiles = "xx.x - xx.x", + iqr = "xx.x", + range = "xx.x - xx.x", + min = "xx.x", + max = "xx.x", + median_range = "xx.x (xx.x - xx.x)", + cv = "xx.x", + geom_mean = "xx.x", + geom_mean_ci = "(xx.xx, xx.xx)", + geom_cv = "xx.x", + pval = "x.xxxx | (<0.0001)" + ) + } + if (!include_pval) fmts <- head(fmts, -1) + fmts +} + +#' @describeIn summary_stats Function to retrieve default labels for summary statistics. Returns labels of descriptive +#' statistics which are understood by `rtables`. +#' +#' @return +#' * `summary_labels` returns a named `vector` of default statistic labels for the given data type. +#' +#' @examples +#' summary_labels() +#' summary_labels(type = "count", include_pval = TRUE) +#' +#' @export +summary_labels <- function(type = "numeric", include_pval = FALSE) { + lbls <- if (type == "counts") { + c( + n = "n", + count = "count", + count_fraction = "count_fraction", + n_blq = "n_blq", + pval = "p-value (chi-squared test)" + ) + } else { + c( + n = "n", + sum = "Sum", + mean = "Mean", + sd = "SD", + se = "SE", + mean_sd = "Mean (SD)", + mean_se = "Mean (SE)", + mean_ci = "Mean 95% CI", + mean_sei = "Mean -/+ 1xSE", + mean_sdi = "Mean -/+ 1xSD", + mean_pval = "Mean p-value (H0: mean = 0)", + median = "Median", + mad = "Median Absolute Deviation", + median_ci = "Median 95% CI", + quantiles = "25% and 75%-ile", + iqr = "IQR", + range = "Min - Max", + min = "Minimum", + max = "Maximum", + median_range = "Median (Min - Max)", + cv = "CV (%)", + geom_mean = "Geometric Mean", + geom_mean_ci = "Geometric Mean 95% CI", + geom_cv = "CV % Geometric Mean", + pval = "p-value (t-test)" + ) + } + if (!include_pval) lbls <- head(lbls, -1) + lbls +} + +#' @describeIn summary_stats Function to configure settings for default or custom summary statistics for a given data +#' type. In addition to selecting a custom subset of statistics, the user can also set custom formats, labels, and +#' indent modifiers for any of these statistics. +#' +#' @param stats_custom (`named vector` of `character`)\cr vector of statistics to include if not the defaults. This +#' argument overrides `include_pval` and other custom value arguments such that only settings for these statistics +#' will be returned. +#' @param formats_custom (`named vector` of `character`)\cr vector of custom statistics formats to use in place of the +#' defaults defined in [`summary_formats()`]. Names should be a subset of the statistics defined in `stats_custom` (or +#' default statistics if this is `NULL`). +#' @param labels_custom (`named vector` of `character`)\cr vector of custom statistics labels to use in place of the +#' defaults defined in [`summary_labels()`]. Names should be a subset of the statistics defined in `stats_custom` (or +#' default statistics if this is `NULL`). +#' @param indent_mods_custom (`integer` or `named vector` of `integer`)\cr vector of custom indentation modifiers for +#' statistics to use instead of the default of `0L` for all statistics. Names should be a subset of the statistics +#' defined in `stats_custom` (or default statistics if this is `NULL`). Alternatively, the same indentation modifier +#' can be applied to all statistics by setting `indent_mods_custom` to a single integer value. +#' +#' @return +#' * `summary_custom` returns a `list` of 4 named elements: `stats`, `formats`, `labels`, and `indent_mods`. +#' +#' @examples +#' summary_custom() +#' summary_custom(type = "counts", include_pval = TRUE) +#' summary_custom( +#' include_pval = TRUE, stats_custom = c("n", "mean", "sd", "pval"), +#' labels_custom = c(sd = "Std. Dev."), indent_mods_custom = 3L +#' ) +#' +#' @export +summary_custom <- function(type = "numeric", + include_pval = FALSE, + stats_custom = NULL, + formats_custom = NULL, + labels_custom = NULL, + indent_mods_custom = NULL) { + if ("pval" %in% stats_custom) include_pval <- TRUE + + .formats <- summary_formats(type = type, include_pval = include_pval) + .stats <- if (is.null(stats_custom)) names(.formats) else intersect(stats_custom, names(.formats)) + .labels <- summary_labels(type = type, include_pval = include_pval) + .indent_mods <- stats::setNames(rep(0L, length(.stats)), .stats) + + if (!is.null(formats_custom)) .formats[names(formats_custom)] <- formats_custom + if (!is.null(labels_custom)) .labels[names(labels_custom)] <- labels_custom + if (!is.null(indent_mods_custom)) { + if (is.null(names(indent_mods_custom)) && length(indent_mods_custom) == 1) { + .indent_mods[names(.indent_mods)] <- indent_mods_custom + } else { + .indent_mods[names(indent_mods_custom)] <- indent_mods_custom + } + } + + list( + stats = .stats, + formats = .formats[.stats], + labels = .labels[.stats], + indent_mods = .indent_mods[.stats] + ) +} + +#' Control Function for Descriptive Statistics +#' +#' @description `r lifecycle::badge("stable")` +#' +#' Sets a list of parameters for summaries of descriptive statistics. Typically used internally to specify +#' details for [s_summary()]. This function family is mainly used by [analyze_vars()]. +#' +#' @inheritParams argument_convention +#' @param quantiles (`numeric`)\cr of length two to specify the quantiles to calculate. +#' @param quantile_type (`numeric`)\cr between 1 and 9 selecting quantile algorithms to be used. +#' Default is set to 2 as this matches the default quantile algorithm in SAS `proc univariate` set by `QNTLDEF=5`. +#' This differs from R's default. See more about `type` in [stats::quantile()]. +#' @param test_mean (`numeric`)\cr to test against the mean under the null hypothesis when calculating p-value. +#' +#' @note Deprecation cycle started for `control_summarize_vars` as it is going to renamed into +#' `control_analyze_vars`. Intention is to reflect better the core underlying `rtables` +#' functions; in this case [analyze_vars()] wraps [rtables::analyze()]. +#' +#' @return A list of components with the same names as the arguments. +#' +#' @export control_analyze_vars control_summarize_vars +#' @aliases control_summarize_vars +control_analyze_vars <- function(conf_level = 0.95, + quantiles = c(0.25, 0.75), + quantile_type = 2, + test_mean = 0) { + checkmate::assert_vector(quantiles, len = 2) + checkmate::assert_int(quantile_type, lower = 1, upper = 9) + checkmate::assert_numeric(test_mean) + lapply(quantiles, assert_proportion_value) + assert_proportion_value(conf_level) + list(conf_level = conf_level, quantiles = quantiles, quantile_type = quantile_type, test_mean = test_mean) +} + +control_summarize_vars <- control_analyze_vars diff --git a/man/a_summary_internal.Rd b/man/a_summary_internal.Rd new file mode 100644 index 0000000000..63ea602afd --- /dev/null +++ b/man/a_summary_internal.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/analyze_variables.R +\name{a_summary_internal} +\alias{a_summary_internal} +\title{Helper Function to Create Output Rows for \code{\link[=a_summary]{a_summary()}}} +\usage{ +a_summary_internal( + x, + .N_col, + .N_row, + .var, + .df_row, + .ref_group, + .in_ref_col, + compare, + type, + .stats, + .formats, + .labels, + .indent_mods, + na.rm, + na_level, + ... +) +} +\arguments{ +\item{compare}{(\code{logical})\cr Whether comparison statistics should be analyzed instead of summary statistics +(\code{compare = TRUE} adds \code{pval} statistic comparing against reference group).} + +\item{type}{(\code{character})\cr type of statistics to calculate given \code{x}. If \code{x} is numeric \code{type} should be +\code{"numeric"}, otherwise type should be \code{"counts"}.} +} +\value{ +\itemize{ +\item \code{a_summary_internal()} returns a corresponding list with formatted \code{\link[rtables:CellValue]{rtables::CellValue()}} used within \code{a_summary}. +} +} +\description{ +Helper Function to Create Output Rows for \code{\link[=a_summary]{a_summary()}} +} +\note{ +\itemize{ +\item To use for comparison (with additional p-value statistic), parameter \code{compare} must be set to \code{TRUE}. +\item Ensure that either all \code{NA} values are converted to an explicit \code{NA} level or all \code{NA} values are left as is. +} +} +\keyword{internal} diff --git a/man/analyze_variables.Rd b/man/analyze_variables.Rd index 24cbc1d250..38ae596a19 100644 --- a/man/analyze_variables.Rd +++ b/man/analyze_variables.Rd @@ -7,7 +7,6 @@ \alias{s_summary.factor} \alias{s_summary.character} \alias{s_summary.logical} -\alias{a_summary_output} \alias{a_summary} \alias{a_summary.default} \alias{a_summary.numeric} @@ -56,25 +55,6 @@ s_summary(x, na.rm = TRUE, denom, .N_row, .N_col, .var, ...) ... ) -a_summary_output( - x, - .N_col, - .N_row, - .var, - .df_row, - .ref_group, - .in_ref_col, - compare, - type, - .stats, - .formats, - .labels, - .indent_mods, - na.rm, - na_level, - ... -) - a_summary(x, .N_col, .N_row, .var, .df_row, .ref_group, .in_ref_col, ...) \method{a_summary}{default}( @@ -174,9 +154,6 @@ to print out information about factor casting.} \item{compare}{(\code{logical})\cr Whether comparison statistics should be analyzed instead of summary statistics (\code{compare = TRUE} adds \code{pval} statistic comparing against reference group).} -\item{type}{(\code{character})\cr type of statistics to calculate given \code{x}. If \code{x} is numeric \code{type} should be -\code{"numeric"}, otherwise type should be \code{"counts"}.} - \item{.stats}{(\code{character})\cr statistics to select for the table.} \item{.formats}{(named \code{character} or \code{list})\cr formats for the statistics.} @@ -206,6 +183,9 @@ to avoid warnings from \code{rtables}.} \item{section_div}{(\code{string})\cr string which should be repeated as a section divider after each group defined by this split instruction, or \code{NA_character_} (the default) for no section divider.} + +\item{type}{(\code{character})\cr type of statistics to calculate given \code{x}. If \code{x} is numeric \code{type} should be +\code{"numeric"}, otherwise type should be \code{"counts"}.} } \value{ \itemize{ @@ -262,10 +242,6 @@ denominator is zero. Note that \code{NA}s in \code{x} are never counted or leadi } } -\itemize{ -\item \code{a_summary_output()} returns the corresponding list with formatted \code{\link[rtables:CellValue]{rtables::CellValue()}}. -} - \itemize{ \item \code{a_summary()} returns the corresponding list with formatted \code{\link[rtables:CellValue]{rtables::CellValue()}}. } @@ -295,8 +271,6 @@ conversion to factor (with a warning) and then forwards to the method for factor \item \code{s_summary(logical)}: Method for \code{logical} class. -\item \code{a_summary_output()}: Formatted analysis helper function which is used within \code{a_summary}. - \item \code{a_summary()}: Formatted analysis function which is used as \code{afun} in \code{analyze_vars()} and \code{compare_vars()} and as \code{cfun} in \code{summarize_colvars()}. @@ -337,11 +311,6 @@ created from character variables before passing the dataset to \code{\link[rtabl \item Ensure that either all \code{NA} values are converted to an explicit \code{NA} level or all \code{NA} values are left as is. } -\itemize{ -\item To use for comparison (with additional p-value statistic), parameter \code{compare} must be set to \code{TRUE}. -\item Ensure that either all \code{NA} values are converted to an explicit \code{NA} level or all \code{NA} values are left as is. -} - Deprecation cycle started for \code{summarize_vars} as it is going to renamed into \code{analyze_vars}. Intention is to reflect better the core underlying \code{rtables} functions; in this case \code{\link[rtables:analyze]{rtables::analyze()}}. @@ -418,26 +387,6 @@ x <- c(TRUE, FALSE, TRUE, TRUE) s_summary(x, denom = "N_row", .N_row = 10L) s_summary(x, denom = "N_col", .N_col = 20L) -set.seed(1) -x <- rnorm(10) -a_summary_output( - x = x, .N_col = 10, .N_row = 20, .var = "bla", .df_row = NULL, .ref_group = NULL, - .in_ref_col = FALSE, compare = FALSE, type = "numeric", .stats = summary_custom()$stats, - .formats = summary_custom()$formats, .labels = summary_custom()$labels, - .indent_mods = summary_custom()$indents, na.rm = TRUE, na_level = NA_character_ -) - -x <- factor(c("a", "a", "b", "c", "a")) -a_summary_output( - x = x, .N_col = 10, .N_row = 10, .var = "bla", .df_row = NULL, .ref_group = NULL, - .in_ref_col = FALSE, compare = FALSE, type = "counts", - .stats = summary_custom(type = "counts")$stats, - .formats = summary_custom(type = "counts")$formats, - .labels = summary_custom(type = "counts")$labels, - .indent_mods = summary_custom(type = "counts")$indents, - na.rm = TRUE, na_level = NA_character_ -) - a_summary(factor(c("a", "a", "b", "c", "a")), .N_row = 10, .N_col = 10) a_summary( factor(c("a", "a", "b", "c", "a")), diff --git a/man/control_analyze_vars.Rd b/man/control_analyze_vars.Rd index 825166f5ec..d9a335674c 100644 --- a/man/control_analyze_vars.Rd +++ b/man/control_analyze_vars.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/analyze_variables.R +% Please edit documentation in R/summary_stats.R \name{control_analyze_vars} \alias{control_analyze_vars} \alias{control_summarize_vars} diff --git a/man/summary_stats.Rd b/man/summary_stats.Rd index 1821410347..441f7b2299 100644 --- a/man/summary_stats.Rd +++ b/man/summary_stats.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/analyze_variables.R +% Please edit documentation in R/summary_stats.R \name{summary_stats} \alias{summary_stats} \alias{summary_formats} diff --git a/tests/testthat/test-analyze_variables.R b/tests/testthat/test-analyze_variables.R index 4e6da3a58b..016d44c786 100644 --- a/tests/testthat/test-analyze_variables.R +++ b/tests/testthat/test-analyze_variables.R @@ -193,13 +193,13 @@ testthat::test_that("s_summary works with logical vectors and by if requested do testthat::expect_snapshot(res) }) -testthat::test_that("a_summary_output and a_summary work with healthy input.", { +testthat::test_that("a_summary_internal and a_summary work with healthy input.", { options("width" = 100) - # numeric input - a_summary_output + # numeric input - a_summary_internal set.seed(1) x <- rnorm(10) - result <- a_summary_output( + result <- a_summary_internal( x = x, .N_col = 10, .N_row = 20, .var = "bla", .df_row = NULL, .ref_group = NULL, .in_ref_col = FALSE, compare = FALSE, type = "numeric", .stats = summary_custom()$stats, .formats = summary_custom()$formats, .labels = summary_custom()$labels, .indent_mods = summary_custom()$indents, na.rm = TRUE, na_level = NA_character_ @@ -212,9 +212,9 @@ testthat::test_that("a_summary_output and a_summary work with healthy input.", { testthat::expect_identical(res_out, res) testthat::expect_snapshot(res) - # factor input - a_summary_output + # factor input - a_summary_internal x <- factor(c("a", "a", "b", "c", "a")) - result <- a_summary_output( + result <- a_summary_internal( x = x, .N_col = 10, .N_row = 10, .var = "bla", .df_row = NULL, .ref_group = NULL, .in_ref_col = FALSE, compare = FALSE, type = "counts", .stats = summary_custom(type = "counts")$stats, .formats = summary_custom(type = "counts")$formats, .labels = summary_custom(type = "counts")$labels, @@ -228,9 +228,9 @@ testthat::test_that("a_summary_output and a_summary work with healthy input.", { testthat::expect_identical(res_out, res) testthat::expect_snapshot(res) - # character input - a_summary_output + # character input - a_summary_internal x <- c("A", "B", "A", "C") - result <- a_summary_output( + result <- a_summary_internal( x = x, .N_col = 10, .N_row = 10, .var = "x", .df_row = NULL, .ref_group = NULL, .in_ref_col = FALSE, compare = FALSE, type = "counts", .stats = summary_custom(type = "counts")$stats, .formats = summary_custom(type = "counts")$formats, .labels = summary_custom(type = "counts")$labels, @@ -245,9 +245,9 @@ testthat::test_that("a_summary_output and a_summary work with healthy input.", { testthat::expect_identical(res_out, res) testthat::expect_snapshot(res) - # logical input - a_summary_output + # logical input - a_summary_internal x <- c(TRUE, FALSE, FALSE, TRUE, TRUE) - result <- a_summary_output( + result <- a_summary_internal( x = x, .N_col = 10, .N_row = 10, .var = NULL, .df_row = NULL, .ref_group = NULL, .in_ref_col = FALSE, compare = FALSE, type = "counts", .stats = summary_custom(type = "counts")$stats, .formats = summary_custom(type = "counts")$formats, .labels = summary_custom(type = "counts")$labels,