| 1 |
#' Cumulative counts of numeric variable by thresholds |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_cumulative()] creates a layout element to calculate cumulative counts of values in a |
|
| 6 |
#' numeric variable that are less than, less or equal to, greater than, or greater or equal to user-specified |
|
| 7 |
#' threshold values. |
|
| 8 |
#' |
|
| 9 |
#' This function analyzes numeric variable `vars` against the threshold values supplied to the `thresholds` |
|
| 10 |
#' argument as a numeric vector. Whether counts should include the threshold values, and whether to count |
|
| 11 |
#' values lower or higher than the threshold values can be set via the `include_eq` and `lower_tail` |
|
| 12 |
#' parameters, respectively. |
|
| 13 |
#' |
|
| 14 |
#' @inheritParams h_count_cumulative |
|
| 15 |
#' @inheritParams argument_convention |
|
| 16 |
#' @param thresholds (`numeric`)\cr vector of cutoff values for the counts. |
|
| 17 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 18 |
#' |
|
| 19 |
#' Options are: ``r shQuote(get_stats("count_cumulative"), type = "sh")``
|
|
| 20 |
#' |
|
| 21 |
#' @seealso Relevant helper function [h_count_cumulative()], and descriptive function [d_count_cumulative()]. |
|
| 22 |
#' |
|
| 23 |
#' @name count_cumulative |
|
| 24 |
#' @order 1 |
|
| 25 |
NULL |
|
| 26 | ||
| 27 |
#' Helper function for `s_count_cumulative()` |
|
| 28 |
#' |
|
| 29 |
#' @description `r lifecycle::badge("stable")`
|
|
| 30 |
#' |
|
| 31 |
#' Helper function to calculate count and fraction of `x` values in the lower or upper tail given a threshold. |
|
| 32 |
#' |
|
| 33 |
#' @inheritParams argument_convention |
|
| 34 |
#' @param threshold (`numeric(1)`)\cr a cutoff value as threshold to count values of `x`. |
|
| 35 |
#' @param lower_tail (`flag`)\cr whether to count lower tail, default is `TRUE`. |
|
| 36 |
#' @param include_eq (`flag`)\cr whether to include value equal to the `threshold` in |
|
| 37 |
#' count, default is `TRUE`. |
|
| 38 |
#' |
|
| 39 |
#' @return A named vector with items: |
|
| 40 |
#' * `count`: the count of values less than, less or equal to, greater than, or greater or equal to a threshold |
|
| 41 |
#' of user specification. |
|
| 42 |
#' * `fraction`: the fraction of the count. |
|
| 43 |
#' |
|
| 44 |
#' @seealso [count_cumulative] |
|
| 45 |
#' |
|
| 46 |
#' @examples |
|
| 47 |
#' set.seed(1, kind = "Mersenne-Twister") |
|
| 48 |
#' x <- c(sample(1:10, 10), NA) |
|
| 49 |
#' .N_col <- length(x) |
|
| 50 |
#' |
|
| 51 |
#' h_count_cumulative(x, 5, denom = .N_col) |
|
| 52 |
#' h_count_cumulative(x, 5, lower_tail = FALSE, include_eq = FALSE, na_rm = FALSE, denom = .N_col) |
|
| 53 |
#' h_count_cumulative(x, 0, lower_tail = FALSE, denom = .N_col) |
|
| 54 |
#' h_count_cumulative(x, 100, lower_tail = FALSE, denom = .N_col) |
|
| 55 |
#' |
|
| 56 |
#' @export |
|
| 57 |
h_count_cumulative <- function(x, |
|
| 58 |
threshold, |
|
| 59 |
lower_tail = TRUE, |
|
| 60 |
include_eq = TRUE, |
|
| 61 |
na_rm = TRUE, |
|
| 62 |
denom) {
|
|
| 63 | 48x |
checkmate::assert_numeric(x) |
| 64 | 48x |
checkmate::assert_numeric(threshold) |
| 65 | 48x |
checkmate::assert_numeric(denom) |
| 66 | 48x |
checkmate::assert_flag(lower_tail) |
| 67 | 48x |
checkmate::assert_flag(include_eq) |
| 68 | 48x |
checkmate::assert_flag(na_rm) |
| 69 | ||
| 70 | 48x |
is_keep <- if (na_rm) !is.na(x) else rep(TRUE, length(x)) |
| 71 | 48x |
count <- if (lower_tail && include_eq) {
|
| 72 | 19x |
length(x[is_keep & x <= threshold]) |
| 73 | 48x |
} else if (lower_tail && !include_eq) {
|
| 74 | ! |
length(x[is_keep & x < threshold]) |
| 75 | 48x |
} else if (!lower_tail && include_eq) {
|
| 76 | 14x |
length(x[is_keep & x >= threshold]) |
| 77 | 48x |
} else if (!lower_tail && !include_eq) {
|
| 78 | 15x |
length(x[is_keep & x > threshold]) |
| 79 |
} |
|
| 80 | ||
| 81 | 48x |
result <- c( |
| 82 | 48x |
count = count, |
| 83 | 48x |
fraction = if (count == 0 && denom == 0) 0 else count / denom |
| 84 |
) |
|
| 85 | 48x |
result |
| 86 |
} |
|
| 87 | ||
| 88 |
#' Description of cumulative count |
|
| 89 |
#' |
|
| 90 |
#' @description `r lifecycle::badge("stable")`
|
|
| 91 |
#' |
|
| 92 |
#' This is a helper function that describes the analysis in [s_count_cumulative()]. |
|
| 93 |
#' |
|
| 94 |
#' @inheritParams h_count_cumulative |
|
| 95 |
#' |
|
| 96 |
#' @return Labels for [s_count_cumulative()]. |
|
| 97 |
#' |
|
| 98 |
#' @export |
|
| 99 |
d_count_cumulative <- function(threshold, lower_tail = TRUE, include_eq = TRUE) {
|
|
| 100 | 46x |
checkmate::assert_numeric(threshold) |
| 101 | 46x |
lg <- if (lower_tail) "<" else ">" |
| 102 | 46x |
eq <- if (include_eq) "=" else "" |
| 103 | 46x |
paste0(lg, eq, " ", threshold) |
| 104 |
} |
|
| 105 | ||
| 106 |
#' @describeIn count_cumulative Statistics function that produces a named list given a numeric vector of thresholds. |
|
| 107 |
#' |
|
| 108 |
#' @return |
|
| 109 |
#' * `s_count_cumulative()` returns a named list of `count_fraction`s: a list with each `thresholds` value as a |
|
| 110 |
#' component, each component containing a vector for the count and fraction. |
|
| 111 |
#' |
|
| 112 |
#' @keywords internal |
|
| 113 |
s_count_cumulative <- function(x, |
|
| 114 |
thresholds, |
|
| 115 |
lower_tail = TRUE, |
|
| 116 |
include_eq = TRUE, |
|
| 117 |
denom = c("N_col", "n", "N_row"),
|
|
| 118 |
.N_col, # nolint |
|
| 119 |
.N_row, # nolint |
|
| 120 |
na_rm = TRUE, |
|
| 121 |
...) {
|
|
| 122 | 23x |
checkmate::assert_numeric(thresholds, min.len = 1, any.missing = FALSE) |
| 123 | ||
| 124 | 23x |
denom <- match.arg(denom) %>% |
| 125 | 23x |
switch( |
| 126 | 23x |
n = length(x), |
| 127 | 23x |
N_row = .N_row, |
| 128 | 23x |
N_col = .N_col |
| 129 |
) |
|
| 130 | ||
| 131 | 23x |
count_fraction_list <- Map(function(thres) {
|
| 132 | 46x |
result <- h_count_cumulative(x, thres, lower_tail, include_eq, na_rm = na_rm, denom = denom) |
| 133 | 46x |
label <- d_count_cumulative(thres, lower_tail, include_eq) |
| 134 | 46x |
formatters::with_label(result, label) |
| 135 | 23x |
}, thresholds) |
| 136 | ||
| 137 | 23x |
names(count_fraction_list) <- thresholds |
| 138 | 23x |
list(count_fraction = count_fraction_list) |
| 139 |
} |
|
| 140 | ||
| 141 |
#' @describeIn count_cumulative Formatted analysis function which is used as `afun` |
|
| 142 |
#' in `count_cumulative()`. |
|
| 143 |
#' |
|
| 144 |
#' @return |
|
| 145 |
#' * `a_count_cumulative()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 146 |
#' |
|
| 147 |
#' @keywords internal |
|
| 148 |
a_count_cumulative <- function(x, |
|
| 149 |
..., |
|
| 150 |
.stats = NULL, |
|
| 151 |
.stat_names = NULL, |
|
| 152 |
.formats = NULL, |
|
| 153 |
.labels = NULL, |
|
| 154 |
.indent_mods = NULL) {
|
|
| 155 | 14x |
dots_extra_args <- list(...) |
| 156 | ||
| 157 |
# Check if there are user-defined functions |
|
| 158 | 14x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 159 | 14x |
.stats <- default_and_custom_stats_list$all_stats |
| 160 | 14x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 161 | ||
| 162 |
# Adding automatically extra parameters to the statistic function (see ?rtables::additional_fun_params) |
|
| 163 | 14x |
extra_afun_params <- retrieve_extra_afun_params( |
| 164 | 14x |
names(dots_extra_args$.additional_fun_parameters) |
| 165 |
) |
|
| 166 | 14x |
dots_extra_args$.additional_fun_parameters <- NULL # After extraction we do not need them anymore |
| 167 | ||
| 168 |
# Main statistical functions application |
|
| 169 | 14x |
x_stats <- .apply_stat_functions( |
| 170 | 14x |
default_stat_fnc = s_count_cumulative, |
| 171 | 14x |
custom_stat_fnc_list = custom_stat_functions, |
| 172 | 14x |
args_list = c( |
| 173 | 14x |
x = list(x), |
| 174 | 14x |
extra_afun_params, |
| 175 | 14x |
dots_extra_args |
| 176 |
) |
|
| 177 |
) |
|
| 178 | ||
| 179 |
# Fill in with stats defaults if needed |
|
| 180 | 14x |
.stats <- get_stats("count_cumulative",
|
| 181 | 14x |
stats_in = .stats, |
| 182 | 14x |
custom_stats_in = names(custom_stat_functions) |
| 183 |
) |
|
| 184 | ||
| 185 | 14x |
x_stats <- x_stats[.stats] |
| 186 | 14x |
levels_per_stats <- lapply(x_stats, names) |
| 187 | ||
| 188 |
# Fill in formats/indents/labels with custom input and defaults |
|
| 189 | 14x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 190 | 14x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 191 | 14x |
.labels <- get_labels_from_stats( |
| 192 | 14x |
.stats, .labels, levels_per_stats, |
| 193 | 14x |
label_attr_from_stats = sapply(.unlist_keep_nulls(x_stats), attr, "label") |
| 194 |
) |
|
| 195 | ||
| 196 |
# Unlist stats |
|
| 197 | 14x |
x_stats <- x_stats %>% |
| 198 | 14x |
.unlist_keep_nulls() %>% |
| 199 | 14x |
setNames(names(.formats)) |
| 200 | ||
| 201 |
# Auto format handling |
|
| 202 | 14x |
.formats <- apply_auto_formatting( |
| 203 | 14x |
.formats, |
| 204 | 14x |
x_stats, |
| 205 | 14x |
extra_afun_params$.df_row, |
| 206 | 14x |
extra_afun_params$.var |
| 207 |
) |
|
| 208 | ||
| 209 |
# Get and check statistical names from defaults |
|
| 210 | 14x |
.stat_names <- get_stat_names(x_stats, .stat_names) # note is x_stats |
| 211 | ||
| 212 | 14x |
in_rows( |
| 213 | 14x |
.list = x_stats, |
| 214 | 14x |
.formats = .formats, |
| 215 | 14x |
.names = names(.labels), |
| 216 | 14x |
.stat_names = .stat_names, |
| 217 | 14x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 218 | 14x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 219 |
) |
|
| 220 |
} |
|
| 221 | ||
| 222 |
#' @describeIn count_cumulative Layout-creating function which can take statistics function arguments |
|
| 223 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 224 |
#' |
|
| 225 |
#' @return |
|
| 226 |
#' * `count_cumulative()` returns a layout object suitable for passing to further layouting functions, |
|
| 227 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 228 |
#' the statistics from `s_count_cumulative()` to the table layout. |
|
| 229 |
#' |
|
| 230 |
#' @examples |
|
| 231 |
#' basic_table() %>% |
|
| 232 |
#' split_cols_by("ARM") %>%
|
|
| 233 |
#' add_colcounts() %>% |
|
| 234 |
#' count_cumulative( |
|
| 235 |
#' vars = "AGE", |
|
| 236 |
#' thresholds = c(40, 60) |
|
| 237 |
#' ) %>% |
|
| 238 |
#' build_table(tern_ex_adsl) |
|
| 239 |
#' |
|
| 240 |
#' @export |
|
| 241 |
#' @order 2 |
|
| 242 |
count_cumulative <- function(lyt, |
|
| 243 |
vars, |
|
| 244 |
thresholds, |
|
| 245 |
lower_tail = TRUE, |
|
| 246 |
include_eq = TRUE, |
|
| 247 |
var_labels = vars, |
|
| 248 |
show_labels = "visible", |
|
| 249 |
na_str = default_na_str(), |
|
| 250 |
nested = TRUE, |
|
| 251 |
table_names = vars, |
|
| 252 |
..., |
|
| 253 |
na_rm = TRUE, |
|
| 254 |
.stats = c("count_fraction"),
|
|
| 255 |
.stat_names = NULL, |
|
| 256 |
.formats = NULL, |
|
| 257 |
.labels = NULL, |
|
| 258 |
.indent_mods = NULL) {
|
|
| 259 |
# Depending on main functions |
|
| 260 | 6x |
extra_args <- list( |
| 261 | 6x |
"na_rm" = na_rm, |
| 262 | 6x |
"thresholds" = thresholds, |
| 263 | 6x |
"lower_tail" = lower_tail, |
| 264 | 6x |
"include_eq" = include_eq, |
| 265 |
... |
|
| 266 |
) |
|
| 267 | ||
| 268 |
# Needed defaults |
|
| 269 | 6x |
if (!is.null(.stats)) extra_args[[".stats"]] <- .stats |
| 270 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 271 | ! |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 272 | 1x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 273 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 274 | ||
| 275 |
# Adding all additional information from layout to analysis functions (see ?rtables::additional_fun_params) |
|
| 276 | 6x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 277 | 6x |
formals(a_count_cumulative) <- c( |
| 278 | 6x |
formals(a_count_cumulative), |
| 279 | 6x |
extra_args[[".additional_fun_parameters"]] |
| 280 |
) |
|
| 281 | ||
| 282 |
# Main {rtables} structural call
|
|
| 283 | 6x |
analyze( |
| 284 | 6x |
lyt, |
| 285 | 6x |
vars, |
| 286 | 6x |
afun = a_count_cumulative, |
| 287 | 6x |
na_str = na_str, |
| 288 | 6x |
inclNAs = !na_rm, |
| 289 | 6x |
table_names = table_names, |
| 290 | 6x |
var_labels = var_labels, |
| 291 | 6x |
show_labels = show_labels, |
| 292 | 6x |
nested = nested, |
| 293 | 6x |
extra_args = extra_args |
| 294 |
) |
|
| 295 |
} |
| 1 |
#' Univariate formula special term |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The special term `univariate` indicate that the model should be fitted individually for |
|
| 6 |
#' every variable included in univariate. |
|
| 7 |
#' |
|
| 8 |
#' @param x (`character`)\cr a vector of variable names separated by commas. |
|
| 9 |
#' |
|
| 10 |
#' @return When used within a model formula, produces univariate models for each variable provided. |
|
| 11 |
#' |
|
| 12 |
#' @details |
|
| 13 |
#' If provided alongside with pairwise specification, the model |
|
| 14 |
#' `y ~ ARM + univariate(SEX, AGE, RACE)` lead to the study and comparison of the models |
|
| 15 |
#' + `y ~ ARM` |
|
| 16 |
#' + `y ~ ARM + SEX` |
|
| 17 |
#' + `y ~ ARM + AGE` |
|
| 18 |
#' + `y ~ ARM + RACE` |
|
| 19 |
#' |
|
| 20 |
#' @export |
|
| 21 |
univariate <- function(x) {
|
|
| 22 | 2x |
structure(x, varname = deparse(substitute(x))) |
| 23 |
} |
|
| 24 | ||
| 25 |
# Get the right-hand-term of a formula |
|
| 26 |
rht <- function(x) {
|
|
| 27 | 4x |
checkmate::assert_formula(x) |
| 28 | 4x |
y <- as.character(rev(x)[[1]]) |
| 29 | 4x |
return(y) |
| 30 |
} |
|
| 31 | ||
| 32 |
#' Hazard ratio estimation in interactions |
|
| 33 |
#' |
|
| 34 |
#' This function estimates the hazard ratios between arms when an interaction variable is given with |
|
| 35 |
#' specific values. |
|
| 36 |
#' |
|
| 37 |
#' @param variable,given (`character(2)`)\cr names of the two variables in the interaction. We seek the estimation of |
|
| 38 |
#' the levels of `variable` given the levels of `given`. |
|
| 39 |
#' @param lvl_var,lvl_given (`character`)\cr corresponding levels given by [levels()]. |
|
| 40 |
#' @param mmat (named `numeric`) a vector filled with `0`s used as a template to obtain the design matrix. |
|
| 41 |
#' @param coef (`numeric`)\cr vector of estimated coefficients. |
|
| 42 |
#' @param vcov (`matrix`)\cr variance-covariance matrix of underlying model. |
|
| 43 |
#' @param conf_level (`proportion`)\cr confidence level of estimate intervals. |
|
| 44 |
#' |
|
| 45 |
#' @details Given the cox regression investigating the effect of Arm (A, B, C; reference A) |
|
| 46 |
#' and Sex (F, M; reference Female). The model is abbreviated: y ~ Arm + Sex + Arm x Sex. |
|
| 47 |
#' The cox regression estimates the coefficients along with a variance-covariance matrix for: |
|
| 48 |
#' |
|
| 49 |
#' - b1 (arm b), b2 (arm c) |
|
| 50 |
#' - b3 (sex m) |
|
| 51 |
#' - b4 (arm b: sex m), b5 (arm c: sex m) |
|
| 52 |
#' |
|
| 53 |
#' Given that I want an estimation of the Hazard Ratio for arm C/sex M, the estimation |
|
| 54 |
#' will be given in reference to arm A/Sex M by exp(b2 + b3 + b5)/ exp(b3) = exp(b2 + b5), |
|
| 55 |
#' therefore the interaction coefficient is given by b2 + b5 while the standard error is obtained |
|
| 56 |
#' as $1.96 * sqrt(Var b2 + Var b5 + 2 * covariance (b2,b5))$ for a confidence level of 0.95. |
|
| 57 |
#' |
|
| 58 |
#' @return A list of matrices (one per level of variable) with rows corresponding to the combinations of |
|
| 59 |
#' `variable` and `given`, with columns: |
|
| 60 |
#' * `coef_hat`: Estimation of the coefficient. |
|
| 61 |
#' * `coef_se`: Standard error of the estimation. |
|
| 62 |
#' * `hr`: Hazard ratio. |
|
| 63 |
#' * `lcl, ucl`: Lower/upper confidence limit of the hazard ratio. |
|
| 64 |
#' |
|
| 65 |
#' @seealso [s_cox_multivariate()]. |
|
| 66 |
#' |
|
| 67 |
#' @examples |
|
| 68 |
#' library(dplyr) |
|
| 69 |
#' library(survival) |
|
| 70 |
#' |
|
| 71 |
#' ADSL <- tern_ex_adsl %>% |
|
| 72 |
#' filter(SEX %in% c("F", "M"))
|
|
| 73 |
#' |
|
| 74 |
#' adtte <- tern_ex_adtte %>% filter(PARAMCD == "PFS") |
|
| 75 |
#' adtte$ARMCD <- droplevels(adtte$ARMCD) |
|
| 76 |
#' adtte$SEX <- droplevels(adtte$SEX) |
|
| 77 |
#' |
|
| 78 |
#' mod <- coxph( |
|
| 79 |
#' formula = Surv(time = AVAL, event = 1 - CNSR) ~ (SEX + ARMCD)^2, |
|
| 80 |
#' data = adtte |
|
| 81 |
#' ) |
|
| 82 |
#' |
|
| 83 |
#' mmat <- stats::model.matrix(mod)[1, ] |
|
| 84 |
#' mmat[!mmat == 0] <- 0 |
|
| 85 |
#' |
|
| 86 |
#' @keywords internal |
|
| 87 |
estimate_coef <- function(variable, given, |
|
| 88 |
lvl_var, lvl_given, |
|
| 89 |
coef, |
|
| 90 |
mmat, |
|
| 91 |
vcov, |
|
| 92 |
conf_level = 0.95) {
|
|
| 93 | 8x |
var_lvl <- paste0(variable, lvl_var[-1]) # [-1]: reference level |
| 94 | 8x |
giv_lvl <- paste0(given, lvl_given) |
| 95 | ||
| 96 | 8x |
design_mat <- expand.grid(variable = var_lvl, given = giv_lvl) |
| 97 | 8x |
design_mat <- design_mat[order(design_mat$variable, design_mat$given), ] |
| 98 | 8x |
design_mat <- within( |
| 99 | 8x |
data = design_mat, |
| 100 | 8x |
expr = {
|
| 101 | 8x |
inter <- paste0(variable, ":", given) |
| 102 | 8x |
rev_inter <- paste0(given, ":", variable) |
| 103 |
} |
|
| 104 |
) |
|
| 105 | ||
| 106 | 8x |
split_by_variable <- design_mat$variable |
| 107 | 8x |
interaction_names <- paste(design_mat$variable, design_mat$given, sep = "/") |
| 108 | ||
| 109 | 8x |
design_mat <- apply( |
| 110 | 8x |
X = design_mat, MARGIN = 1, FUN = function(x) {
|
| 111 | 27x |
mmat[names(mmat) %in% x[-which(names(x) == "given")]] <- 1 |
| 112 | 27x |
return(mmat) |
| 113 |
} |
|
| 114 |
) |
|
| 115 | 8x |
colnames(design_mat) <- interaction_names |
| 116 | ||
| 117 | 8x |
betas <- as.matrix(coef) |
| 118 | ||
| 119 | 8x |
coef_hat <- t(design_mat) %*% betas |
| 120 | 8x |
dimnames(coef_hat)[2] <- "coef" |
| 121 | ||
| 122 | 8x |
coef_se <- apply(design_mat, 2, function(x) {
|
| 123 | 27x |
vcov_el <- as.logical(x) |
| 124 | 27x |
y <- vcov[vcov_el, vcov_el] |
| 125 | 27x |
y <- sum(y) |
| 126 | 27x |
y <- sqrt(y) |
| 127 | 27x |
return(y) |
| 128 |
}) |
|
| 129 | ||
| 130 | 8x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
| 131 | 8x |
y <- cbind(coef_hat, `se(coef)` = coef_se) |
| 132 | ||
| 133 | 8x |
y <- apply(y, 1, function(x) {
|
| 134 | 27x |
x["hr"] <- exp(x["coef"]) |
| 135 | 27x |
x["lcl"] <- exp(x["coef"] - q_norm * x["se(coef)"]) |
| 136 | 27x |
x["ucl"] <- exp(x["coef"] + q_norm * x["se(coef)"]) |
| 137 | ||
| 138 | 27x |
return(x) |
| 139 |
}) |
|
| 140 | ||
| 141 | 8x |
y <- t(y) |
| 142 | 8x |
y <- by(y, split_by_variable, identity) |
| 143 | 8x |
y <- lapply(y, as.matrix) |
| 144 | ||
| 145 | 8x |
attr(y, "details") <- paste0( |
| 146 | 8x |
"Estimations of ", variable, |
| 147 | 8x |
" hazard ratio given the level of ", given, " compared to ", |
| 148 | 8x |
variable, " level ", lvl_var[1], "." |
| 149 |
) |
|
| 150 | 8x |
return(y) |
| 151 |
} |
|
| 152 | ||
| 153 |
#' `tryCatch` around `car::Anova` |
|
| 154 |
#' |
|
| 155 |
#' Captures warnings when executing [car::Anova]. |
|
| 156 |
#' |
|
| 157 |
#' @inheritParams car::Anova |
|
| 158 |
#' |
|
| 159 |
#' @return A list with item `aov` for the result of the model and `error_text` for the captured warnings. |
|
| 160 |
#' |
|
| 161 |
#' @examples |
|
| 162 |
#' # `car::Anova` on cox regression model including strata and expected |
|
| 163 |
#' # a likelihood ratio test triggers a warning as only Wald method is |
|
| 164 |
#' # accepted. |
|
| 165 |
#' |
|
| 166 |
#' library(survival) |
|
| 167 |
#' |
|
| 168 |
#' mod <- coxph( |
|
| 169 |
#' formula = Surv(time = futime, event = fustat) ~ factor(rx) + strata(ecog.ps), |
|
| 170 |
#' data = ovarian |
|
| 171 |
#' ) |
|
| 172 |
#' |
|
| 173 |
#' @keywords internal |
|
| 174 |
try_car_anova <- function(mod, |
|
| 175 |
test.statistic) { # nolint
|
|
| 176 | 2x |
y <- tryCatch( |
| 177 | 2x |
withCallingHandlers( |
| 178 | 2x |
expr = {
|
| 179 | 2x |
warn_text <- c() |
| 180 | 2x |
list( |
| 181 | 2x |
aov = car::Anova( |
| 182 | 2x |
mod, |
| 183 | 2x |
test.statistic = test.statistic, |
| 184 | 2x |
type = "III" |
| 185 |
), |
|
| 186 | 2x |
warn_text = warn_text |
| 187 |
) |
|
| 188 |
}, |
|
| 189 | 2x |
warning = function(w) {
|
| 190 |
# If a warning is detected it is handled as "w". |
|
| 191 | ! |
warn_text <<- trimws(paste0("Warning in `try_car_anova`: ", w))
|
| 192 | ||
| 193 |
# A warning is sometimes expected, then, we want to restart |
|
| 194 |
# the execution while ignoring the warning. |
|
| 195 | ! |
invokeRestart("muffleWarning")
|
| 196 |
} |
|
| 197 |
), |
|
| 198 | 2x |
finally = {
|
| 199 |
} |
|
| 200 |
) |
|
| 201 | ||
| 202 | 2x |
return(y) |
| 203 |
} |
|
| 204 | ||
| 205 |
#' Fit a Cox regression model and ANOVA |
|
| 206 |
#' |
|
| 207 |
#' The functions derives the effect p-values using [car::Anova()] from [survival::coxph()] results. |
|
| 208 |
#' |
|
| 209 |
#' @inheritParams t_coxreg |
|
| 210 |
#' |
|
| 211 |
#' @return A list with items `mod` (results of [survival::coxph()]), `msum` (result of `summary`) and |
|
| 212 |
#' `aov` (result of [car::Anova()]). |
|
| 213 |
#' |
|
| 214 |
#' @noRd |
|
| 215 |
fit_n_aov <- function(formula, |
|
| 216 |
data = data, |
|
| 217 |
conf_level = conf_level, |
|
| 218 |
pval_method = c("wald", "likelihood"),
|
|
| 219 |
...) {
|
|
| 220 | 1x |
pval_method <- match.arg(pval_method) |
| 221 | ||
| 222 | 1x |
environment(formula) <- environment() |
| 223 | 1x |
suppressWarnings({
|
| 224 |
# We expect some warnings due to coxph which fails strict programming. |
|
| 225 | 1x |
mod <- survival::coxph(formula, data = data, ...) |
| 226 | 1x |
msum <- summary(mod, conf.int = conf_level) |
| 227 |
}) |
|
| 228 | ||
| 229 | 1x |
aov <- try_car_anova( |
| 230 | 1x |
mod, |
| 231 | 1x |
test.statistic = switch(pval_method, |
| 232 | 1x |
"wald" = "Wald", |
| 233 | 1x |
"likelihood" = "LR" |
| 234 |
) |
|
| 235 |
) |
|
| 236 | ||
| 237 | 1x |
warn_attr <- aov$warn_text |
| 238 | ! |
if (!is.null(aov$warn_text)) message(warn_attr) |
| 239 | ||
| 240 | 1x |
aov <- aov$aov |
| 241 | 1x |
y <- list(mod = mod, msum = msum, aov = aov) |
| 242 | 1x |
attr(y, "message") <- warn_attr |
| 243 | ||
| 244 | 1x |
return(y) |
| 245 |
} |
|
| 246 | ||
| 247 |
# argument_checks |
|
| 248 |
check_formula <- function(formula) {
|
|
| 249 | 1x |
if (!(inherits(formula, "formula"))) {
|
| 250 | 1x |
stop("Check `formula`. A formula should resemble `Surv(time = AVAL, event = 1 - CNSR) ~ study_arm(ARMCD)`.")
|
| 251 |
} |
|
| 252 | ||
| 253 | ! |
invisible() |
| 254 |
} |
|
| 255 | ||
| 256 |
check_covariate_formulas <- function(covariates) {
|
|
| 257 | 1x |
if (!all(vapply(X = covariates, FUN = inherits, what = "formula", FUN.VALUE = TRUE)) || is.null(covariates)) {
|
| 258 | 1x |
stop("Check `covariates`, it should be a list of right-hand-term formulas, e.g. list(Age = ~AGE).")
|
| 259 |
} |
|
| 260 | ||
| 261 | ! |
invisible() |
| 262 |
} |
|
| 263 | ||
| 264 |
name_covariate_names <- function(covariates) {
|
|
| 265 | 1x |
miss_names <- names(covariates) == "" |
| 266 | 1x |
no_names <- is.null(names(covariates)) |
| 267 | ! |
if (any(miss_names)) names(covariates)[miss_names] <- vapply(covariates[miss_names], FUN = rht, FUN.VALUE = "name") |
| 268 | ! |
if (no_names) names(covariates) <- vapply(covariates, FUN = rht, FUN.VALUE = "name") |
| 269 | 1x |
return(covariates) |
| 270 |
} |
|
| 271 | ||
| 272 |
check_increments <- function(increments, covariates) {
|
|
| 273 | 1x |
if (!is.null(increments)) {
|
| 274 | 1x |
covariates <- vapply(covariates, FUN = rht, FUN.VALUE = "name") |
| 275 | 1x |
lapply( |
| 276 | 1x |
X = names(increments), FUN = function(x) {
|
| 277 | 3x |
if (!x %in% covariates) {
|
| 278 | 1x |
warning( |
| 279 | 1x |
paste( |
| 280 | 1x |
"Check `increments`, the `increment` for ", x, |
| 281 | 1x |
"doesn't match any names in investigated covariate(s)." |
| 282 |
) |
|
| 283 |
) |
|
| 284 |
} |
|
| 285 |
} |
|
| 286 |
) |
|
| 287 |
} |
|
| 288 | ||
| 289 | 1x |
invisible() |
| 290 |
} |
|
| 291 | ||
| 292 |
#' Multivariate Cox model - summarized results |
|
| 293 |
#' |
|
| 294 |
#' Analyses based on multivariate Cox model are usually not performed for the Controlled Substance Reporting or |
|
| 295 |
#' regulatory documents but serve exploratory purposes only (e.g., for publication). In practice, the model usually |
|
| 296 |
#' includes only the main effects (without interaction terms). It produces the hazard ratio estimates for each of the |
|
| 297 |
#' covariates included in the model. |
|
| 298 |
#' The analysis follows the same principles (e.g., stratified vs. unstratified analysis and tie handling) as the |
|
| 299 |
#' usual Cox model analysis. Since there is usually no pre-specified hypothesis testing for such analysis, |
|
| 300 |
#' the p.values need to be interpreted with caution. (**Statistical Analysis of Clinical Trials Data with R**, |
|
| 301 |
#' `NEST's bookdown`) |
|
| 302 |
#' |
|
| 303 |
#' @param formula (`formula`)\cr a formula corresponding to the investigated [survival::Surv()] survival model |
|
| 304 |
#' including covariates. |
|
| 305 |
#' @param data (`data.frame`)\cr a data frame which includes the variable in formula and covariates. |
|
| 306 |
#' @param conf_level (`proportion`)\cr the confidence level for the hazard ratio interval estimations. Default is 0.95. |
|
| 307 |
#' @param pval_method (`string`)\cr the method used for the estimation of p-values, should be one of |
|
| 308 |
#' `"wald"` (default) or `"likelihood"`. |
|
| 309 |
#' @param ... optional parameters passed to [survival::coxph()]. Can include `ties`, a character string specifying the |
|
| 310 |
#' method for tie handling, one of `exact` (default), `efron`, `breslow`. |
|
| 311 |
#' |
|
| 312 |
#' @return A `list` with elements `mod`, `msum`, `aov`, and `coef_inter`. |
|
| 313 |
#' |
|
| 314 |
#' @details The output is limited to single effect terms. Work in ongoing for estimation of interaction terms |
|
| 315 |
#' but is out of scope as defined by the Global Data Standards Repository |
|
| 316 |
#' (**`GDS_Standard_TLG_Specs_Tables_2.doc`**). |
|
| 317 |
#' |
|
| 318 |
#' @seealso [estimate_coef()]. |
|
| 319 |
#' |
|
| 320 |
#' @examples |
|
| 321 |
#' library(dplyr) |
|
| 322 |
#' |
|
| 323 |
#' adtte <- tern_ex_adtte |
|
| 324 |
#' adtte_f <- subset(adtte, PARAMCD == "OS") # _f: filtered |
|
| 325 |
#' adtte_f <- filter( |
|
| 326 |
#' adtte_f, |
|
| 327 |
#' PARAMCD == "OS" & |
|
| 328 |
#' SEX %in% c("F", "M") &
|
|
| 329 |
#' RACE %in% c("ASIAN", "BLACK OR AFRICAN AMERICAN", "WHITE")
|
|
| 330 |
#' ) |
|
| 331 |
#' adtte_f$SEX <- droplevels(adtte_f$SEX) |
|
| 332 |
#' adtte_f$RACE <- droplevels(adtte_f$RACE) |
|
| 333 |
#' |
|
| 334 |
#' @keywords internal |
|
| 335 |
s_cox_multivariate <- function(formula, data, |
|
| 336 |
conf_level = 0.95, |
|
| 337 |
pval_method = c("wald", "likelihood"),
|
|
| 338 |
...) {
|
|
| 339 | 1x |
tf <- stats::terms(formula, specials = c("strata"))
|
| 340 | 1x |
covariates <- rownames(attr(tf, "factors"))[-c(1, unlist(attr(tf, "specials")))] |
| 341 | 1x |
lapply( |
| 342 | 1x |
X = covariates, |
| 343 | 1x |
FUN = function(x) {
|
| 344 | 3x |
if (is.character(data[[x]])) {
|
| 345 | 1x |
data[[x]] <<- as.factor(data[[x]]) |
| 346 |
} |
|
| 347 | 3x |
invisible() |
| 348 |
} |
|
| 349 |
) |
|
| 350 | 1x |
pval_method <- match.arg(pval_method) |
| 351 | ||
| 352 |
# Results directly exported from environment(fit_n_aov) to environment(s_function_draft) |
|
| 353 | 1x |
y <- fit_n_aov( |
| 354 | 1x |
formula = formula, |
| 355 | 1x |
data = data, |
| 356 | 1x |
conf_level = conf_level, |
| 357 | 1x |
pval_method = pval_method, |
| 358 |
... |
|
| 359 |
) |
|
| 360 | 1x |
mod <- y$mod |
| 361 | 1x |
aov <- y$aov |
| 362 | 1x |
msum <- y$msum |
| 363 | 1x |
list2env(as.list(y), environment()) |
| 364 | ||
| 365 | 1x |
all_term_labs <- attr(mod$terms, "term.labels") |
| 366 | 1x |
term_labs <- all_term_labs[which(attr(mod$terms, "order") == 1)] |
| 367 | 1x |
names(term_labs) <- term_labs |
| 368 | ||
| 369 | 1x |
coef_inter <- NULL |
| 370 | 1x |
if (any(attr(mod$terms, "order") > 1)) {
|
| 371 | 1x |
for_inter <- all_term_labs[attr(mod$terms, "order") > 1] |
| 372 | 1x |
names(for_inter) <- for_inter |
| 373 | 1x |
mmat <- stats::model.matrix(mod)[1, ] |
| 374 | 1x |
mmat[!mmat == 0] <- 0 |
| 375 | 1x |
mcoef <- stats::coef(mod) |
| 376 | 1x |
mvcov <- stats::vcov(mod) |
| 377 | ||
| 378 | 1x |
estimate_coef_local <- function(variable, given) {
|
| 379 | 6x |
estimate_coef( |
| 380 | 6x |
variable, given, |
| 381 | 6x |
coef = mcoef, mmat = mmat, vcov = mvcov, conf_level = conf_level, |
| 382 | 6x |
lvl_var = levels(data[[variable]]), lvl_given = levels(data[[given]]) |
| 383 |
) |
|
| 384 |
} |
|
| 385 | ||
| 386 | 1x |
coef_inter <- lapply( |
| 387 | 1x |
for_inter, function(x) {
|
| 388 | 3x |
y <- attr(mod$terms, "factors")[, x] |
| 389 | 3x |
y <- names(y[y > 0]) |
| 390 | 3x |
Map(estimate_coef_local, variable = y, given = rev(y)) |
| 391 |
} |
|
| 392 |
) |
|
| 393 |
} |
|
| 394 | ||
| 395 | 1x |
list(mod = mod, msum = msum, aov = aov, coef_inter = coef_inter) |
| 396 |
} |
| 1 |
#' Stack multiple grobs |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 4 |
#' |
|
| 5 |
#' Stack grobs as a new grob with 1 column and multiple rows layout. |
|
| 6 |
#' |
|
| 7 |
#' @param ... grobs. |
|
| 8 |
#' @param grobs (`list` of `grob`)\cr a list of grobs. |
|
| 9 |
#' @param padding (`grid::unit`)\cr unit of length 1, space between each grob. |
|
| 10 |
#' @param vp (`viewport` or `NULL`)\cr a [viewport()] object (or `NULL`). |
|
| 11 |
#' @param name (`string`)\cr a character identifier for the grob. |
|
| 12 |
#' @param gp (`gpar`)\cr a [gpar()] object. |
|
| 13 |
#' |
|
| 14 |
#' @return A `grob`. |
|
| 15 |
#' |
|
| 16 |
#' @examples |
|
| 17 |
#' library(grid) |
|
| 18 |
#' |
|
| 19 |
#' g1 <- circleGrob(gp = gpar(col = "blue")) |
|
| 20 |
#' g2 <- circleGrob(gp = gpar(col = "red")) |
|
| 21 |
#' g3 <- textGrob("TEST TEXT")
|
|
| 22 |
#' grid.newpage() |
|
| 23 |
#' grid.draw(stack_grobs(g1, g2, g3)) |
|
| 24 |
#' |
|
| 25 |
#' showViewport() |
|
| 26 |
#' |
|
| 27 |
#' grid.newpage() |
|
| 28 |
#' pushViewport(viewport(layout = grid.layout(1, 2))) |
|
| 29 |
#' vp1 <- viewport(layout.pos.row = 1, layout.pos.col = 2) |
|
| 30 |
#' grid.draw(stack_grobs(g1, g2, g3, vp = vp1, name = "test")) |
|
| 31 |
#' |
|
| 32 |
#' showViewport() |
|
| 33 |
#' grid.ls(grobs = TRUE, viewports = TRUE, print = FALSE) |
|
| 34 |
#' |
|
| 35 |
#' @export |
|
| 36 |
stack_grobs <- function(..., |
|
| 37 |
grobs = list(...), |
|
| 38 |
padding = grid::unit(2, "line"), |
|
| 39 |
vp = NULL, |
|
| 40 |
gp = NULL, |
|
| 41 |
name = NULL) {
|
|
| 42 | 4x |
lifecycle::deprecate_warn( |
| 43 | 4x |
"0.9.4", |
| 44 | 4x |
"stack_grobs()", |
| 45 | 4x |
details = "`tern` plotting functions no longer generate `grob` objects." |
| 46 |
) |
|
| 47 | ||
| 48 | 4x |
checkmate::assert_true( |
| 49 | 4x |
all(vapply(grobs, grid::is.grob, logical(1))) |
| 50 |
) |
|
| 51 | ||
| 52 | 4x |
if (length(grobs) == 1) {
|
| 53 | 1x |
return(grobs[[1]]) |
| 54 |
} |
|
| 55 | ||
| 56 | 3x |
n_layout <- 2 * length(grobs) - 1 |
| 57 | 3x |
hts <- lapply( |
| 58 | 3x |
seq(1, n_layout), |
| 59 | 3x |
function(i) {
|
| 60 | 39x |
if (i %% 2 != 0) {
|
| 61 | 21x |
grid::unit(1, "null") |
| 62 |
} else {
|
|
| 63 | 18x |
padding |
| 64 |
} |
|
| 65 |
} |
|
| 66 |
) |
|
| 67 | 3x |
hts <- do.call(grid::unit.c, hts) |
| 68 | ||
| 69 | 3x |
main_vp <- grid::viewport( |
| 70 | 3x |
layout = grid::grid.layout(nrow = n_layout, ncol = 1, heights = hts) |
| 71 |
) |
|
| 72 | ||
| 73 | 3x |
nested_grobs <- Map(function(g, i) {
|
| 74 | 21x |
grid::gTree( |
| 75 | 21x |
children = grid::gList(g), |
| 76 | 21x |
vp = grid::viewport(layout.pos.row = i, layout.pos.col = 1) |
| 77 |
) |
|
| 78 | 3x |
}, grobs, seq_along(grobs) * 2 - 1) |
| 79 | ||
| 80 | 3x |
grobs_mainvp <- grid::gTree( |
| 81 | 3x |
children = do.call(grid::gList, nested_grobs), |
| 82 | 3x |
vp = main_vp |
| 83 |
) |
|
| 84 | ||
| 85 | 3x |
grid::gTree( |
| 86 | 3x |
children = grid::gList(grobs_mainvp), |
| 87 | 3x |
vp = vp, |
| 88 | 3x |
gp = gp, |
| 89 | 3x |
name = name |
| 90 |
) |
|
| 91 |
} |
|
| 92 | ||
| 93 |
#' Arrange multiple grobs |
|
| 94 |
#' |
|
| 95 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 96 |
#' |
|
| 97 |
#' Arrange grobs as a new grob with `n * m (rows * cols)` layout. |
|
| 98 |
#' |
|
| 99 |
#' @inheritParams stack_grobs |
|
| 100 |
#' @param ncol (`integer(1)`)\cr number of columns in layout. |
|
| 101 |
#' @param nrow (`integer(1)`)\cr number of rows in layout. |
|
| 102 |
#' @param padding_ht (`grid::unit`)\cr unit of length 1, vertical space between each grob. |
|
| 103 |
#' @param padding_wt (`grid::unit`)\cr unit of length 1, horizontal space between each grob. |
|
| 104 |
#' |
|
| 105 |
#' @return A `grob`. |
|
| 106 |
#' |
|
| 107 |
#' @examples |
|
| 108 |
#' library(grid) |
|
| 109 |
#' |
|
| 110 |
#' \donttest{
|
|
| 111 |
#' num <- lapply(1:9, textGrob) |
|
| 112 |
#' grid::grid.newpage() |
|
| 113 |
#' grid.draw(arrange_grobs(grobs = num, ncol = 2)) |
|
| 114 |
#' |
|
| 115 |
#' showViewport() |
|
| 116 |
#' |
|
| 117 |
#' g1 <- circleGrob(gp = gpar(col = "blue")) |
|
| 118 |
#' g2 <- circleGrob(gp = gpar(col = "red")) |
|
| 119 |
#' g3 <- textGrob("TEST TEXT")
|
|
| 120 |
#' grid::grid.newpage() |
|
| 121 |
#' grid.draw(arrange_grobs(g1, g2, g3, nrow = 2)) |
|
| 122 |
#' |
|
| 123 |
#' showViewport() |
|
| 124 |
#' |
|
| 125 |
#' grid::grid.newpage() |
|
| 126 |
#' grid.draw(arrange_grobs(g1, g2, g3, ncol = 3)) |
|
| 127 |
#' |
|
| 128 |
#' grid::grid.newpage() |
|
| 129 |
#' grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2))) |
|
| 130 |
#' vp1 <- grid::viewport(layout.pos.row = 1, layout.pos.col = 2) |
|
| 131 |
#' grid.draw(arrange_grobs(g1, g2, g3, ncol = 2, vp = vp1)) |
|
| 132 |
#' |
|
| 133 |
#' showViewport() |
|
| 134 |
#' } |
|
| 135 |
#' @export |
|
| 136 |
arrange_grobs <- function(..., |
|
| 137 |
grobs = list(...), |
|
| 138 |
ncol = NULL, nrow = NULL, |
|
| 139 |
padding_ht = grid::unit(2, "line"), |
|
| 140 |
padding_wt = grid::unit(2, "line"), |
|
| 141 |
vp = NULL, |
|
| 142 |
gp = NULL, |
|
| 143 |
name = NULL) {
|
|
| 144 | 5x |
lifecycle::deprecate_warn( |
| 145 | 5x |
"0.9.4", |
| 146 | 5x |
"arrange_grobs()", |
| 147 | 5x |
details = "`tern` plotting functions no longer generate `grob` objects." |
| 148 |
) |
|
| 149 | ||
| 150 | 5x |
checkmate::assert_true( |
| 151 | 5x |
all(vapply(grobs, grid::is.grob, logical(1))) |
| 152 |
) |
|
| 153 | ||
| 154 | 5x |
if (length(grobs) == 1) {
|
| 155 | 1x |
return(grobs[[1]]) |
| 156 |
} |
|
| 157 | ||
| 158 | 4x |
if (is.null(ncol) && is.null(nrow)) {
|
| 159 | 1x |
ncol <- 1 |
| 160 | 1x |
nrow <- ceiling(length(grobs) / ncol) |
| 161 | 3x |
} else if (!is.null(ncol) && is.null(nrow)) {
|
| 162 | 1x |
nrow <- ceiling(length(grobs) / ncol) |
| 163 | 2x |
} else if (is.null(ncol) && !is.null(nrow)) {
|
| 164 | ! |
ncol <- ceiling(length(grobs) / nrow) |
| 165 |
} |
|
| 166 | ||
| 167 | 4x |
if (ncol * nrow < length(grobs)) {
|
| 168 | 1x |
stop("specififed ncol and nrow are not enough for arranging the grobs ")
|
| 169 |
} |
|
| 170 | ||
| 171 | 3x |
if (ncol == 1) {
|
| 172 | 2x |
return(stack_grobs(grobs = grobs, padding = padding_ht, vp = vp, gp = gp, name = name)) |
| 173 |
} |
|
| 174 | ||
| 175 | 1x |
n_col <- 2 * ncol - 1 |
| 176 | 1x |
n_row <- 2 * nrow - 1 |
| 177 | 1x |
hts <- lapply( |
| 178 | 1x |
seq(1, n_row), |
| 179 | 1x |
function(i) {
|
| 180 | 5x |
if (i %% 2 != 0) {
|
| 181 | 3x |
grid::unit(1, "null") |
| 182 |
} else {
|
|
| 183 | 2x |
padding_ht |
| 184 |
} |
|
| 185 |
} |
|
| 186 |
) |
|
| 187 | 1x |
hts <- do.call(grid::unit.c, hts) |
| 188 | ||
| 189 | 1x |
wts <- lapply( |
| 190 | 1x |
seq(1, n_col), |
| 191 | 1x |
function(i) {
|
| 192 | 5x |
if (i %% 2 != 0) {
|
| 193 | 3x |
grid::unit(1, "null") |
| 194 |
} else {
|
|
| 195 | 2x |
padding_wt |
| 196 |
} |
|
| 197 |
} |
|
| 198 |
) |
|
| 199 | 1x |
wts <- do.call(grid::unit.c, wts) |
| 200 | ||
| 201 | 1x |
main_vp <- grid::viewport( |
| 202 | 1x |
layout = grid::grid.layout(nrow = n_row, ncol = n_col, widths = wts, heights = hts) |
| 203 |
) |
|
| 204 | ||
| 205 | 1x |
nested_grobs <- list() |
| 206 | 1x |
k <- 0 |
| 207 | 1x |
for (i in seq(nrow) * 2 - 1) {
|
| 208 | 3x |
for (j in seq(ncol) * 2 - 1) {
|
| 209 | 9x |
k <- k + 1 |
| 210 | 9x |
if (k <= length(grobs)) {
|
| 211 | 9x |
nested_grobs <- c( |
| 212 | 9x |
nested_grobs, |
| 213 | 9x |
list(grid::gTree( |
| 214 | 9x |
children = grid::gList(grobs[[k]]), |
| 215 | 9x |
vp = grid::viewport(layout.pos.row = i, layout.pos.col = j) |
| 216 |
)) |
|
| 217 |
) |
|
| 218 |
} |
|
| 219 |
} |
|
| 220 |
} |
|
| 221 | 1x |
grobs_mainvp <- grid::gTree( |
| 222 | 1x |
children = do.call(grid::gList, nested_grobs), |
| 223 | 1x |
vp = main_vp |
| 224 |
) |
|
| 225 | ||
| 226 | 1x |
grid::gTree( |
| 227 | 1x |
children = grid::gList(grobs_mainvp), |
| 228 | 1x |
vp = vp, |
| 229 | 1x |
gp = gp, |
| 230 | 1x |
name = name |
| 231 |
) |
|
| 232 |
} |
|
| 233 | ||
| 234 |
#' Draw `grob` |
|
| 235 |
#' |
|
| 236 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 237 |
#' |
|
| 238 |
#' Draw grob on device page. |
|
| 239 |
#' |
|
| 240 |
#' @param grob (`grob`)\cr grid object. |
|
| 241 |
#' @param newpage (`flag`)\cr draw on a new page. |
|
| 242 |
#' @param vp (`viewport` or `NULL`)\cr a [viewport()] object (or `NULL`). |
|
| 243 |
#' |
|
| 244 |
#' @return A `grob`. |
|
| 245 |
#' |
|
| 246 |
#' @examples |
|
| 247 |
#' library(dplyr) |
|
| 248 |
#' library(grid) |
|
| 249 |
#' |
|
| 250 |
#' \donttest{
|
|
| 251 |
#' rect <- rectGrob(width = grid::unit(0.5, "npc"), height = grid::unit(0.5, "npc")) |
|
| 252 |
#' rect %>% draw_grob(vp = grid::viewport(angle = 45)) |
|
| 253 |
#' |
|
| 254 |
#' num <- lapply(1:10, textGrob) |
|
| 255 |
#' num %>% |
|
| 256 |
#' arrange_grobs(grobs = .) %>% |
|
| 257 |
#' draw_grob() |
|
| 258 |
#' showViewport() |
|
| 259 |
#' } |
|
| 260 |
#' |
|
| 261 |
#' @export |
|
| 262 |
draw_grob <- function(grob, newpage = TRUE, vp = NULL) {
|
|
| 263 | 3x |
lifecycle::deprecate_warn( |
| 264 | 3x |
"0.9.4", |
| 265 | 3x |
"draw_grob()", |
| 266 | 3x |
details = "`tern` plotting functions no longer generate `grob` objects." |
| 267 |
) |
|
| 268 | ||
| 269 | 3x |
if (newpage) {
|
| 270 | 3x |
grid::grid.newpage() |
| 271 |
} |
|
| 272 | 3x |
if (!is.null(vp)) {
|
| 273 | 1x |
grid::pushViewport(vp) |
| 274 |
} |
|
| 275 | 3x |
grid::grid.draw(grob) |
| 276 |
} |
|
| 277 | ||
| 278 |
tern_grob <- function(x) {
|
|
| 279 | ! |
class(x) <- unique(c("ternGrob", class(x)))
|
| 280 | ! |
x |
| 281 |
} |
|
| 282 | ||
| 283 |
#' @keywords internal |
|
| 284 |
print.ternGrob <- function(x, ...) {
|
|
| 285 | ! |
grid::grid.newpage() |
| 286 | ! |
grid::grid.draw(x) |
| 287 |
} |
| 1 |
#' Convert `rtable` objects to `ggplot` objects |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 4 |
#' |
|
| 5 |
#' Given a [rtables::rtable()] object, performs basic conversion to a [ggplot2::ggplot()] object built using |
|
| 6 |
#' functions from the `ggplot2` package. Any table titles and/or footnotes are ignored. |
|
| 7 |
#' |
|
| 8 |
#' @param tbl (`VTableTree`)\cr `rtables` table object. |
|
| 9 |
#' @param fontsize (`numeric(1)`)\cr font size. |
|
| 10 |
#' @param colwidths (`numeric` or `NULL`)\cr a vector of column widths. Each element's position in |
|
| 11 |
#' `colwidths` corresponds to the column of `tbl` in the same position. If `NULL`, column widths |
|
| 12 |
#' are calculated according to maximum number of characters per column. |
|
| 13 |
#' @param lbl_col_padding (`numeric`)\cr additional padding to use when calculating spacing between |
|
| 14 |
#' the first (label) column and the second column of `tbl`. If `colwidths` is specified, |
|
| 15 |
#' the width of the first column becomes `colwidths[1] + lbl_col_padding`. Defaults to 0. |
|
| 16 |
#' |
|
| 17 |
#' @return A `ggplot` object. |
|
| 18 |
#' |
|
| 19 |
#' @examples |
|
| 20 |
#' dta <- data.frame( |
|
| 21 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)), |
|
| 22 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
| 23 |
#' AVAL = c(9:1, rep(NA, 9)) |
|
| 24 |
#' ) |
|
| 25 |
#' |
|
| 26 |
#' lyt <- basic_table() %>% |
|
| 27 |
#' split_cols_by(var = "ARM") %>% |
|
| 28 |
#' split_rows_by(var = "AVISIT") %>% |
|
| 29 |
#' analyze_vars(vars = "AVAL") |
|
| 30 |
#' |
|
| 31 |
#' tbl <- build_table(lyt, df = dta) |
|
| 32 |
#' |
|
| 33 |
#' rtable2gg(tbl) |
|
| 34 |
#' |
|
| 35 |
#' rtable2gg(tbl, fontsize = 15, colwidths = c(2, 1, 1, 1)) |
|
| 36 |
#' |
|
| 37 |
#' @export |
|
| 38 |
rtable2gg <- function(tbl, fontsize = 12, colwidths = NULL, lbl_col_padding = 0) {
|
|
| 39 | 6x |
mat <- rtables::matrix_form(tbl, indent_rownames = TRUE) |
| 40 | 6x |
mat_strings <- formatters::mf_strings(mat) |
| 41 | 6x |
mat_aligns <- formatters::mf_aligns(mat) |
| 42 | 6x |
mat_indent <- formatters::mf_rinfo(mat)$indent |
| 43 | 6x |
mat_display <- formatters::mf_display(mat) |
| 44 | 6x |
nlines_hdr <- formatters::mf_nlheader(mat) |
| 45 | 6x |
shared_hdr_rows <- which(apply(mat_display, 1, function(x) (any(!x)))) |
| 46 | ||
| 47 | 6x |
tbl_df <- data.frame(mat_strings) |
| 48 | 6x |
body_rows <- seq(nlines_hdr + 1, nrow(tbl_df)) |
| 49 | 6x |
mat_aligns <- apply(mat_aligns, 1:2, function(x) if (x == "left") 0 else if (x == "right") 1 else 0.5) |
| 50 | ||
| 51 |
# Apply indentation in first column |
|
| 52 | 6x |
tbl_df[body_rows, 1] <- sapply(body_rows, function(i) {
|
| 53 | 42x |
ind_i <- mat_indent[i - nlines_hdr] * 4 |
| 54 | 18x |
if (ind_i > 0) paste0(paste(rep(" ", ind_i), collapse = ""), tbl_df[i, 1]) else tbl_df[i, 1]
|
| 55 |
}) |
|
| 56 | ||
| 57 |
# Get column widths |
|
| 58 | 6x |
if (is.null(colwidths)) {
|
| 59 | 6x |
colwidths <- apply(tbl_df, 2, function(x) max(nchar(x))) + 1 |
| 60 |
} |
|
| 61 | 6x |
tot_width <- sum(colwidths) + lbl_col_padding |
| 62 | ||
| 63 | 6x |
if (length(shared_hdr_rows) > 0) {
|
| 64 | 5x |
tbl_df <- tbl_df[-shared_hdr_rows, ] |
| 65 | 5x |
mat_aligns <- mat_aligns[-shared_hdr_rows, ] |
| 66 |
} |
|
| 67 | ||
| 68 | 6x |
res <- ggplot(data = tbl_df) + |
| 69 | 6x |
theme_void() + |
| 70 | 6x |
scale_x_continuous(limits = c(0, tot_width)) + |
| 71 | 6x |
scale_y_continuous(limits = c(0, nrow(mat_strings))) + |
| 72 | 6x |
annotate( |
| 73 | 6x |
"segment", |
| 74 | 6x |
x = 0, xend = tot_width, |
| 75 | 6x |
y = nrow(mat_strings) - nlines_hdr + 0.5, yend = nrow(mat_strings) - nlines_hdr + 0.5 |
| 76 |
) |
|
| 77 | ||
| 78 |
# If header content spans multiple columns, center over these columns |
|
| 79 | 6x |
if (length(shared_hdr_rows) > 0) {
|
| 80 | 5x |
mat_strings[shared_hdr_rows, ] <- trimws(mat_strings[shared_hdr_rows, ]) |
| 81 | 5x |
for (hr in shared_hdr_rows) {
|
| 82 | 6x |
hdr_lbls <- mat_strings[1:hr, mat_display[hr, -1]] |
| 83 | 6x |
hdr_lbls <- matrix(hdr_lbls[nzchar(hdr_lbls)], nrow = hr) |
| 84 | 6x |
for (idx_hl in seq_len(ncol(hdr_lbls))) {
|
| 85 | 13x |
cur_lbl <- tail(hdr_lbls[, idx_hl], 1) |
| 86 | 13x |
which_cols <- if (hr == 1) {
|
| 87 | 9x |
which(mat_strings[hr, ] == hdr_lbls[idx_hl]) |
| 88 | 13x |
} else { # for >2 col splits, only print labels for each unique combo of nested columns
|
| 89 | 4x |
which( |
| 90 | 4x |
apply(mat_strings[1:hr, ], 2, function(x) all(x == hdr_lbls[1:hr, idx_hl])) |
| 91 |
) |
|
| 92 |
} |
|
| 93 | 13x |
line_pos <- c( |
| 94 | 13x |
sum(colwidths[1:(which_cols[1] - 1)]) + 1 + lbl_col_padding, |
| 95 | 13x |
sum(colwidths[1:max(which_cols)]) - 1 + lbl_col_padding |
| 96 |
) |
|
| 97 | ||
| 98 | 13x |
res <- res + |
| 99 | 13x |
annotate( |
| 100 | 13x |
"text", |
| 101 | 13x |
x = mean(line_pos), |
| 102 | 13x |
y = nrow(mat_strings) + 1 - hr, |
| 103 | 13x |
label = cur_lbl, |
| 104 | 13x |
size = fontsize / .pt |
| 105 |
) + |
|
| 106 | 13x |
annotate( |
| 107 | 13x |
"segment", |
| 108 | 13x |
x = line_pos[1], |
| 109 | 13x |
xend = line_pos[2], |
| 110 | 13x |
y = nrow(mat_strings) - hr + 0.5, |
| 111 | 13x |
yend = nrow(mat_strings) - hr + 0.5 |
| 112 |
) |
|
| 113 |
} |
|
| 114 |
} |
|
| 115 |
} |
|
| 116 | ||
| 117 |
# Add table columns |
|
| 118 | 6x |
for (i in seq_len(ncol(tbl_df))) {
|
| 119 | 40x |
res <- res + annotate( |
| 120 | 40x |
"text", |
| 121 | 40x |
x = if (i == 1) 0 else sum(colwidths[1:i]) - 0.5 * colwidths[i] + lbl_col_padding, |
| 122 | 40x |
y = rev(seq_len(nrow(tbl_df))), |
| 123 | 40x |
label = tbl_df[, i], |
| 124 | 40x |
hjust = mat_aligns[, i], |
| 125 | 40x |
size = fontsize / .pt |
| 126 |
) |
|
| 127 |
} |
|
| 128 | ||
| 129 | 6x |
res |
| 130 |
} |
|
| 131 | ||
| 132 |
#' Convert `data.frame` object to `ggplot` object |
|
| 133 |
#' |
|
| 134 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 135 |
#' |
|
| 136 |
#' Given a `data.frame` object, performs basic conversion to a [ggplot2::ggplot()] object built using |
|
| 137 |
#' functions from the `ggplot2` package. |
|
| 138 |
#' |
|
| 139 |
#' @param df (`data.frame`)\cr a data frame. |
|
| 140 |
#' @param colwidths (`numeric` or `NULL`)\cr a vector of column widths. Each element's position in |
|
| 141 |
#' `colwidths` corresponds to the column of `df` in the same position. If `NULL`, column widths |
|
| 142 |
#' are calculated according to maximum number of characters per column. |
|
| 143 |
#' @param font_size (`numeric(1)`)\cr font size. |
|
| 144 |
#' @param col_labels (`flag`)\cr whether the column names (labels) of `df` should be used as the first row |
|
| 145 |
#' of the output table. |
|
| 146 |
#' @param col_lab_fontface (`string`)\cr font face to apply to the first row (of column labels |
|
| 147 |
#' if `col_labels = TRUE`). Defaults to `"bold"`. |
|
| 148 |
#' @param hline (`flag`)\cr whether a horizontal line should be printed below the first row of the table. |
|
| 149 |
#' @param bg_fill (`string`)\cr table background fill color. |
|
| 150 |
#' |
|
| 151 |
#' @return A `ggplot` object. |
|
| 152 |
#' |
|
| 153 |
#' @examples |
|
| 154 |
#' \dontrun{
|
|
| 155 |
#' df2gg(head(iris, 5)) |
|
| 156 |
#' |
|
| 157 |
#' df2gg(head(iris, 5), font_size = 15, colwidths = c(1, 1, 1, 1, 1)) |
|
| 158 |
#' } |
|
| 159 |
#' @keywords internal |
|
| 160 |
df2gg <- function(df, |
|
| 161 |
colwidths = NULL, |
|
| 162 |
font_size = 10, |
|
| 163 |
col_labels = TRUE, |
|
| 164 |
col_lab_fontface = "bold", |
|
| 165 |
hline = TRUE, |
|
| 166 |
bg_fill = NULL) {
|
|
| 167 |
# convert to text |
|
| 168 | 19x |
df <- as.data.frame(apply(df, 1:2, function(x) if (is.na(x)) "NA" else as.character(x))) |
| 169 | ||
| 170 | 19x |
if (col_labels) {
|
| 171 | 10x |
df <- as.matrix(df) |
| 172 | 10x |
df <- rbind(colnames(df), df) |
| 173 |
} |
|
| 174 | ||
| 175 |
# Get column widths |
|
| 176 | 19x |
if (is.null(colwidths)) {
|
| 177 | 1x |
colwidths <- apply(df, 2, function(x) max(nchar(x), na.rm = TRUE)) |
| 178 |
} |
|
| 179 | 19x |
tot_width <- sum(colwidths) |
| 180 | ||
| 181 | 19x |
res <- ggplot(data = df) + |
| 182 | 19x |
theme_void() + |
| 183 | 19x |
scale_x_continuous(limits = c(0, tot_width)) + |
| 184 | 19x |
scale_y_continuous(limits = c(1, nrow(df))) |
| 185 | ||
| 186 | 9x |
if (!is.null(bg_fill)) res <- res + theme(plot.background = element_rect(fill = bg_fill)) |
| 187 | ||
| 188 | 19x |
if (hline) {
|
| 189 | 10x |
res <- res + |
| 190 | 10x |
annotate( |
| 191 | 10x |
"segment", |
| 192 | 10x |
x = 0 + 0.2 * colwidths[2], xend = tot_width - 0.1 * tail(colwidths, 1), |
| 193 | 10x |
y = nrow(df) - 0.5, yend = nrow(df) - 0.5 |
| 194 |
) |
|
| 195 |
} |
|
| 196 | ||
| 197 | 19x |
for (i in seq_len(ncol(df))) {
|
| 198 | 86x |
line_pos <- c( |
| 199 | 86x |
if (i == 1) 0 else sum(colwidths[1:(i - 1)]), |
| 200 | 86x |
sum(colwidths[1:i]) |
| 201 |
) |
|
| 202 | 86x |
res <- res + |
| 203 | 86x |
annotate( |
| 204 | 86x |
"text", |
| 205 | 86x |
x = mean(line_pos), |
| 206 | 86x |
y = rev(seq_len(nrow(df))), |
| 207 | 86x |
label = df[, i], |
| 208 | 86x |
size = font_size / .pt, |
| 209 | 86x |
fontface = if (col_labels) {
|
| 210 | 32x |
c(col_lab_fontface, rep("plain", nrow(df) - 1))
|
| 211 |
} else {
|
|
| 212 | 54x |
rep("plain", nrow(df))
|
| 213 |
} |
|
| 214 |
) |
|
| 215 |
} |
|
| 216 | ||
| 217 | 19x |
res |
| 218 |
} |
| 1 |
#' Split function to configure risk difference column |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Wrapper function for [rtables::add_combo_levels()] which configures settings for the risk difference |
|
| 6 |
#' column to be added to an `rtables` object. To add a risk difference column to a table, this function |
|
| 7 |
#' should be used as `split_fun` in calls to [rtables::split_cols_by()], followed by setting argument |
|
| 8 |
#' `riskdiff` to `TRUE` in all following analyze function calls. |
|
| 9 |
#' |
|
| 10 |
#' @param arm_x (`string`)\cr name of reference arm to use in risk difference calculations. |
|
| 11 |
#' @param arm_y (`character`)\cr names of one or more arms to compare to reference arm in risk difference |
|
| 12 |
#' calculations. A new column will be added for each value of `arm_y`. |
|
| 13 |
#' @param col_label (`character`)\cr labels to use when rendering the risk difference column within the table. |
|
| 14 |
#' If more than one comparison arm is specified in `arm_y`, default labels will specify which two arms are |
|
| 15 |
#' being compared (reference arm vs. comparison arm). |
|
| 16 |
#' @param pct (`flag`)\cr whether output should be returned as percentages. Defaults to `TRUE`. |
|
| 17 |
#' |
|
| 18 |
#' @return A closure suitable for use as a split function (`split_fun`) within [rtables::split_cols_by()] |
|
| 19 |
#' when creating a table layout. |
|
| 20 |
#' |
|
| 21 |
#' @seealso [stat_propdiff_ci()] for details on risk difference calculation. |
|
| 22 |
#' |
|
| 23 |
#' @examples |
|
| 24 |
#' adae <- tern_ex_adae |
|
| 25 |
#' adae$AESEV <- factor(adae$AESEV) |
|
| 26 |
#' |
|
| 27 |
#' lyt <- basic_table() %>% |
|
| 28 |
#' split_cols_by("ARMCD", split_fun = add_riskdiff(arm_x = "ARM A", arm_y = c("ARM B", "ARM C"))) %>%
|
|
| 29 |
#' count_occurrences_by_grade( |
|
| 30 |
#' var = "AESEV", |
|
| 31 |
#' riskdiff = TRUE |
|
| 32 |
#' ) |
|
| 33 |
#' |
|
| 34 |
#' tbl <- build_table(lyt, df = adae) |
|
| 35 |
#' tbl |
|
| 36 |
#' |
|
| 37 |
#' @export |
|
| 38 |
add_riskdiff <- function(arm_x, |
|
| 39 |
arm_y, |
|
| 40 |
col_label = paste0( |
|
| 41 |
"Risk Difference (%) (95% CI)", if (length(arm_y) > 1) paste0("\n", arm_x, " vs. ", arm_y)
|
|
| 42 |
), |
|
| 43 |
pct = TRUE) {
|
|
| 44 | 19x |
checkmate::assert_character(arm_x, len = 1) |
| 45 | 19x |
checkmate::assert_character(arm_y, min.len = 1) |
| 46 | 19x |
checkmate::assert_character(col_label, len = length(arm_y)) |
| 47 | ||
| 48 | 19x |
combodf <- tibble::tribble(~valname, ~label, ~levelcombo, ~exargs) |
| 49 | 19x |
for (i in seq_len(length(arm_y))) {
|
| 50 | 20x |
combodf <- rbind( |
| 51 | 20x |
combodf, |
| 52 | 20x |
tibble::tribble( |
| 53 | 20x |
~valname, ~label, ~levelcombo, ~exargs, |
| 54 | 20x |
paste("riskdiff", arm_x, arm_y[i], sep = "_"), col_label[i], c(arm_x, arm_y[i]), list()
|
| 55 |
) |
|
| 56 |
) |
|
| 57 |
} |
|
| 58 | 19x |
if (pct) combodf$valname <- paste0(combodf$valname, "_pct") |
| 59 | 19x |
add_combo_levels(combodf) |
| 60 |
} |
|
| 61 | ||
| 62 |
#' Analysis function to calculate risk difference column values |
|
| 63 |
#' |
|
| 64 |
#' In the risk difference column, this function uses the statistics function associated with `afun` to |
|
| 65 |
#' calculates risk difference values from arm X (reference group) and arm Y. These arms are specified |
|
| 66 |
#' when configuring the risk difference column which is done using the [add_riskdiff()] split function in |
|
| 67 |
#' the previous call to [rtables::split_cols_by()]. For all other columns, applies `afun` as usual. This |
|
| 68 |
#' function utilizes the [stat_propdiff_ci()] function to perform risk difference calculations. |
|
| 69 |
#' |
|
| 70 |
#' @inheritParams argument_convention |
|
| 71 |
#' @param afun (named `list`)\cr a named list containing one name-value pair where the name corresponds to |
|
| 72 |
#' the name of the statistics function that should be used in calculations and the value is the corresponding |
|
| 73 |
#' analysis function. |
|
| 74 |
#' |
|
| 75 |
#' @return A list of formatted [rtables::CellValue()]. |
|
| 76 |
#' |
|
| 77 |
#' @seealso |
|
| 78 |
#' * [stat_propdiff_ci()] for details on risk difference calculation. |
|
| 79 |
#' * Split function [add_riskdiff()] which, when used as `split_fun` within [rtables::split_cols_by()] with |
|
| 80 |
#' `riskdiff` argument set to `TRUE` in subsequent analyze functions calls, adds a risk difference column |
|
| 81 |
#' to a table layout. |
|
| 82 |
#' |
|
| 83 |
#' @keywords internal |
|
| 84 |
afun_riskdiff <- function(df, |
|
| 85 |
labelstr = "", |
|
| 86 |
afun, |
|
| 87 |
..., |
|
| 88 |
.stats = NULL, |
|
| 89 |
.stat_names = NULL, |
|
| 90 |
.formats = NULL, |
|
| 91 |
.labels = NULL, |
|
| 92 |
.indent_mods = NULL) {
|
|
| 93 | 146x |
if (!any(grepl("riskdiff", names(.spl_context)))) {
|
| 94 | ! |
stop( |
| 95 | ! |
"Please set up levels to use in risk difference calculations using the `add_riskdiff` ", |
| 96 | ! |
"split function within `split_cols_by`. See ?add_riskdiff for details." |
| 97 |
) |
|
| 98 |
} |
|
| 99 | 146x |
checkmate::assert_list(afun, len = 1, types = "function") |
| 100 | 146x |
checkmate::assert_named(afun) |
| 101 | ||
| 102 | 146x |
sfun <- names(afun) |
| 103 | 146x |
dots_extra_args <- list(...)[intersect(names(list(...)), names(formals(sfun)))] |
| 104 | 146x |
extra_args <- list( |
| 105 | 146x |
.var = .var, .df_row = .df_row, .N_col = .N_col, .N_row = .N_row, .stats = .stats, .formats = .formats, |
| 106 | 146x |
.labels = .labels, .indent_mods = .indent_mods |
| 107 |
) |
|
| 108 | 146x |
cur_split <- tail(.spl_context$cur_col_split_val[[1]], 1) |
| 109 | ||
| 110 | 146x |
if (!grepl("^riskdiff", cur_split)) {
|
| 111 |
# Apply basic afun (no risk difference) in all other columns |
|
| 112 | 108x |
do.call(afun[[1]], args = c(list(df = df, labelstr = labelstr), extra_args, dots_extra_args)) |
| 113 |
} else {
|
|
| 114 | 38x |
arm_x <- strsplit(cur_split, "_")[[1]][2] |
| 115 | 38x |
arm_y <- strsplit(cur_split, "_")[[1]][3] |
| 116 | 38x |
if (length(.spl_context$cur_col_split[[1]]) > 1) { # Different split name for nested column splits
|
| 117 | 8x |
arm_spl_x <- gsub("riskdiff", "", paste0(strsplit(.spl_context$cur_col_id[1], "_")[[1]][c(1, 2)], collapse = ""))
|
| 118 | 8x |
arm_spl_y <- gsub("riskdiff", "", paste0(strsplit(.spl_context$cur_col_id[1], "_")[[1]][c(1, 3)], collapse = ""))
|
| 119 |
} else {
|
|
| 120 | 30x |
arm_spl_x <- arm_x |
| 121 | 30x |
arm_spl_y <- arm_y |
| 122 |
} |
|
| 123 | 38x |
N_col_x <- .all_col_counts[[arm_spl_x]] # nolint |
| 124 | 38x |
N_col_y <- .all_col_counts[[arm_spl_y]] # nolint |
| 125 | 38x |
cur_var <- tail(.spl_context$cur_col_split[[1]], 1) |
| 126 | ||
| 127 |
# Apply statistics function to arm X and arm Y data |
|
| 128 | 38x |
s_args <- c(dots_extra_args, extra_args[intersect(setdiff(names(extra_args), ".N_col"), names(formals(sfun)))]) |
| 129 | 38x |
s_x <- do.call(sfun, args = c(list(df = df[df[[cur_var]] == arm_x, ], .N_col = N_col_x), s_args)) |
| 130 | 38x |
s_y <- do.call(sfun, args = c(list(df = df[df[[cur_var]] == arm_y, ], .N_col = N_col_y), s_args)) |
| 131 | ||
| 132 |
# Get statistic name and row names |
|
| 133 | 38x |
stat <- ifelse("count_fraction" %in% names(s_x), "count_fraction", "unique")
|
| 134 | 38x |
if ("flag_variables" %in% names(s_args)) {
|
| 135 | 2x |
var_nms <- s_args$flag_variables |
| 136 | 36x |
} else if (is.list(s_x[[stat]]) && !is.null(names(s_x[[stat]]))) {
|
| 137 | 24x |
var_nms <- names(s_x[[stat]]) |
| 138 |
} else {
|
|
| 139 | 12x |
var_nms <- "" |
| 140 | 12x |
s_x[[stat]] <- list(s_x[[stat]]) |
| 141 | 12x |
s_y[[stat]] <- list(s_y[[stat]]) |
| 142 |
} |
|
| 143 | ||
| 144 |
# Calculate risk difference for each row, repeated if multiple statistics in table |
|
| 145 | 38x |
pct <- tail(strsplit(cur_split, "_")[[1]], 1) == "pct" |
| 146 | 38x |
rd_ci <- rep(stat_propdiff_ci( |
| 147 | 38x |
lapply(s_x[[stat]], `[`, 1), lapply(s_y[[stat]], `[`, 1), |
| 148 | 38x |
N_col_x, N_col_y, |
| 149 | 38x |
list_names = var_nms, |
| 150 | 38x |
pct = pct |
| 151 | 38x |
), max(1, length(.stats))) |
| 152 | ||
| 153 | 38x |
in_rows(.list = rd_ci, .formats = "xx.x (xx.x - xx.x)", .indent_mods = .indent_mods) |
| 154 |
} |
|
| 155 |
} |
|
| 156 | ||
| 157 |
#' Control function for risk difference column |
|
| 158 |
#' |
|
| 159 |
#' @description `r lifecycle::badge("stable")`
|
|
| 160 |
#' |
|
| 161 |
#' Sets a list of parameters to use when generating a risk (proportion) difference column. Used as input to the |
|
| 162 |
#' `riskdiff` parameter of [tabulate_rsp_subgroups()] and [tabulate_survival_subgroups()]. |
|
| 163 |
#' |
|
| 164 |
#' @inheritParams add_riskdiff |
|
| 165 |
#' @param format (`string` or `function`)\cr the format label (string) or formatting function to apply to the risk |
|
| 166 |
#' difference statistic. See the `3d` string options in [formatters::list_valid_format_labels()] for possible format |
|
| 167 |
#' strings. Defaults to `"xx.x (xx.x - xx.x)"`. |
|
| 168 |
#' |
|
| 169 |
#' @return A `list` of items with names corresponding to the arguments. |
|
| 170 |
#' |
|
| 171 |
#' @seealso [add_riskdiff()], [tabulate_rsp_subgroups()], and [tabulate_survival_subgroups()]. |
|
| 172 |
#' |
|
| 173 |
#' @examples |
|
| 174 |
#' control_riskdiff() |
|
| 175 |
#' control_riskdiff(arm_x = "ARM A", arm_y = "ARM B") |
|
| 176 |
#' |
|
| 177 |
#' @export |
|
| 178 |
control_riskdiff <- function(arm_x = NULL, |
|
| 179 |
arm_y = NULL, |
|
| 180 |
format = "xx.x (xx.x - xx.x)", |
|
| 181 |
col_label = "Risk Difference (%) (95% CI)", |
|
| 182 |
pct = TRUE) {
|
|
| 183 | 4x |
checkmate::assert_character(arm_x, len = 1, null.ok = TRUE) |
| 184 | 4x |
checkmate::assert_character(arm_y, min.len = 1, null.ok = TRUE) |
| 185 | 4x |
checkmate::assert_character(format, len = 1) |
| 186 | 4x |
checkmate::assert_character(col_label) |
| 187 | 4x |
checkmate::assert_flag(pct) |
| 188 | ||
| 189 | 4x |
list(arm_x = arm_x, arm_y = arm_y, format = format, col_label = col_label, pct = pct) |
| 190 |
} |
| 1 |
#' Helper functions for multivariate logistic regression |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Helper functions used in calculations for logistic regression. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams argument_convention |
|
| 8 |
#' @param fit_glm (`glm`)\cr logistic regression model fitted by [stats::glm()] with "binomial" family. |
|
| 9 |
#' Limited functionality is also available for conditional logistic regression models fitted by |
|
| 10 |
#' [survival::clogit()], currently this is used only by [extract_rsp_biomarkers()]. |
|
| 11 |
#' @param x (`character`)\cr a variable or interaction term in `fit_glm` (depending on the helper function used). |
|
| 12 |
#' |
|
| 13 |
#' @examples |
|
| 14 |
#' library(dplyr) |
|
| 15 |
#' library(broom) |
|
| 16 |
#' |
|
| 17 |
#' adrs_f <- tern_ex_adrs %>% |
|
| 18 |
#' filter(PARAMCD == "BESRSPI") %>% |
|
| 19 |
#' filter(RACE %in% c("ASIAN", "WHITE", "BLACK OR AFRICAN AMERICAN")) %>%
|
|
| 20 |
#' mutate( |
|
| 21 |
#' Response = case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
|
|
| 22 |
#' RACE = factor(RACE), |
|
| 23 |
#' SEX = factor(SEX) |
|
| 24 |
#' ) |
|
| 25 |
#' formatters::var_labels(adrs_f) <- c(formatters::var_labels(tern_ex_adrs), Response = "Response") |
|
| 26 |
#' mod1 <- fit_logistic( |
|
| 27 |
#' data = adrs_f, |
|
| 28 |
#' variables = list( |
|
| 29 |
#' response = "Response", |
|
| 30 |
#' arm = "ARMCD", |
|
| 31 |
#' covariates = c("AGE", "RACE")
|
|
| 32 |
#' ) |
|
| 33 |
#' ) |
|
| 34 |
#' mod2 <- fit_logistic( |
|
| 35 |
#' data = adrs_f, |
|
| 36 |
#' variables = list( |
|
| 37 |
#' response = "Response", |
|
| 38 |
#' arm = "ARMCD", |
|
| 39 |
#' covariates = c("AGE", "RACE"),
|
|
| 40 |
#' interaction = "AGE" |
|
| 41 |
#' ) |
|
| 42 |
#' ) |
|
| 43 |
#' |
|
| 44 |
#' @name h_logistic_regression |
|
| 45 |
NULL |
|
| 46 | ||
| 47 |
#' @describeIn h_logistic_regression Helper function to extract interaction variable names from a fitted |
|
| 48 |
#' model assuming only one interaction term. |
|
| 49 |
#' |
|
| 50 |
#' @return Vector of names of interaction variables. |
|
| 51 |
#' |
|
| 52 |
#' @export |
|
| 53 |
h_get_interaction_vars <- function(fit_glm) {
|
|
| 54 | 34x |
checkmate::assert_class(fit_glm, "glm") |
| 55 | 34x |
terms_name <- attr(stats::terms(fit_glm), "term.labels") |
| 56 | 34x |
terms_order <- attr(stats::terms(fit_glm), "order") |
| 57 | 34x |
interaction_term <- terms_name[terms_order == 2] |
| 58 | 34x |
checkmate::assert_string(interaction_term) |
| 59 | 34x |
strsplit(interaction_term, split = ":")[[1]] |
| 60 |
} |
|
| 61 | ||
| 62 |
#' @describeIn h_logistic_regression Helper function to get the right coefficient name from the |
|
| 63 |
#' interaction variable names and the given levels. The main value here is that the order |
|
| 64 |
#' of first and second variable is checked in the `interaction_vars` input. |
|
| 65 |
#' |
|
| 66 |
#' @param interaction_vars (`character(2)`)\cr interaction variable names. |
|
| 67 |
#' @param first_var_with_level (`character(2)`)\cr the first variable name with the interaction level. |
|
| 68 |
#' @param second_var_with_level (`character(2)`)\cr the second variable name with the interaction level. |
|
| 69 |
#' |
|
| 70 |
#' @return Name of coefficient. |
|
| 71 |
#' |
|
| 72 |
#' @export |
|
| 73 |
h_interaction_coef_name <- function(interaction_vars, |
|
| 74 |
first_var_with_level, |
|
| 75 |
second_var_with_level) {
|
|
| 76 | 55x |
checkmate::assert_character(interaction_vars, len = 2, any.missing = FALSE) |
| 77 | 55x |
checkmate::assert_character(first_var_with_level, len = 2, any.missing = FALSE) |
| 78 | 55x |
checkmate::assert_character(second_var_with_level, len = 2, any.missing = FALSE) |
| 79 | 55x |
checkmate::assert_subset(c(first_var_with_level[1], second_var_with_level[1]), interaction_vars) |
| 80 | ||
| 81 | 55x |
first_name <- paste(first_var_with_level, collapse = "") |
| 82 | 55x |
second_name <- paste(second_var_with_level, collapse = "") |
| 83 | 55x |
if (first_var_with_level[1] == interaction_vars[1]) {
|
| 84 | 36x |
paste(first_name, second_name, sep = ":") |
| 85 | 19x |
} else if (second_var_with_level[1] == interaction_vars[1]) {
|
| 86 | 19x |
paste(second_name, first_name, sep = ":") |
| 87 |
} |
|
| 88 |
} |
|
| 89 | ||
| 90 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates |
|
| 91 |
#' for the case when both the odds ratio and the interaction variable are categorical. |
|
| 92 |
#' |
|
| 93 |
#' @param odds_ratio_var (`string`)\cr the odds ratio variable. |
|
| 94 |
#' @param interaction_var (`string`)\cr the interaction variable. |
|
| 95 |
#' |
|
| 96 |
#' @return Odds ratio. |
|
| 97 |
#' |
|
| 98 |
#' @export |
|
| 99 |
h_or_cat_interaction <- function(odds_ratio_var, |
|
| 100 |
interaction_var, |
|
| 101 |
fit_glm, |
|
| 102 |
conf_level = 0.95) {
|
|
| 103 | 8x |
interaction_vars <- h_get_interaction_vars(fit_glm) |
| 104 | 8x |
checkmate::assert_string(odds_ratio_var) |
| 105 | 8x |
checkmate::assert_string(interaction_var) |
| 106 | 8x |
checkmate::assert_subset(c(odds_ratio_var, interaction_var), interaction_vars) |
| 107 | 8x |
checkmate::assert_vector(interaction_vars, len = 2) |
| 108 | ||
| 109 | 8x |
xs_level <- fit_glm$xlevels |
| 110 | 8x |
xs_coef <- stats::coef(fit_glm) |
| 111 | 8x |
xs_vcov <- stats::vcov(fit_glm) |
| 112 | 8x |
y <- list() |
| 113 | 8x |
for (var_level in xs_level[[odds_ratio_var]][-1]) {
|
| 114 | 14x |
x <- list() |
| 115 | 14x |
for (ref_level in xs_level[[interaction_var]]) {
|
| 116 | 38x |
coef_names <- paste0(odds_ratio_var, var_level) |
| 117 | 38x |
if (ref_level != xs_level[[interaction_var]][1]) {
|
| 118 | 24x |
interaction_coef_name <- h_interaction_coef_name( |
| 119 | 24x |
interaction_vars, |
| 120 | 24x |
c(odds_ratio_var, var_level), |
| 121 | 24x |
c(interaction_var, ref_level) |
| 122 |
) |
|
| 123 | 24x |
coef_names <- c( |
| 124 | 24x |
coef_names, |
| 125 | 24x |
interaction_coef_name |
| 126 |
) |
|
| 127 |
} |
|
| 128 | 38x |
if (length(coef_names) > 1) {
|
| 129 | 24x |
ones <- t(c(1, 1)) |
| 130 | 24x |
est <- as.numeric(ones %*% xs_coef[coef_names]) |
| 131 | 24x |
se <- sqrt(as.numeric(ones %*% xs_vcov[coef_names, coef_names] %*% t(ones))) |
| 132 |
} else {
|
|
| 133 | 14x |
est <- xs_coef[coef_names] |
| 134 | 14x |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
| 135 |
} |
|
| 136 | 38x |
or <- exp(est) |
| 137 | 38x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
| 138 | 38x |
x[[ref_level]] <- list(or = or, ci = ci) |
| 139 |
} |
|
| 140 | 14x |
y[[var_level]] <- x |
| 141 |
} |
|
| 142 | 8x |
y |
| 143 |
} |
|
| 144 | ||
| 145 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates |
|
| 146 |
#' for the case when either the odds ratio or the interaction variable is continuous. |
|
| 147 |
#' |
|
| 148 |
#' @param at (`numeric` or `NULL`)\cr optional values for the interaction variable. Otherwise |
|
| 149 |
#' the median is used. |
|
| 150 |
#' |
|
| 151 |
#' @return Odds ratio. |
|
| 152 |
#' |
|
| 153 |
#' @note We don't provide a function for the case when both variables are continuous because |
|
| 154 |
#' this does not arise in this table, as the treatment arm variable will always be involved |
|
| 155 |
#' and categorical. |
|
| 156 |
#' |
|
| 157 |
#' @export |
|
| 158 |
h_or_cont_interaction <- function(odds_ratio_var, |
|
| 159 |
interaction_var, |
|
| 160 |
fit_glm, |
|
| 161 |
at = NULL, |
|
| 162 |
conf_level = 0.95) {
|
|
| 163 | 13x |
interaction_vars <- h_get_interaction_vars(fit_glm) |
| 164 | 13x |
checkmate::assert_string(odds_ratio_var) |
| 165 | 13x |
checkmate::assert_string(interaction_var) |
| 166 | 13x |
checkmate::assert_subset(c(odds_ratio_var, interaction_var), interaction_vars) |
| 167 | 13x |
checkmate::assert_vector(interaction_vars, len = 2) |
| 168 | 13x |
checkmate::assert_numeric(at, min.len = 1, null.ok = TRUE, any.missing = FALSE) |
| 169 | 13x |
xs_level <- fit_glm$xlevels |
| 170 | 13x |
xs_coef <- stats::coef(fit_glm) |
| 171 | 13x |
xs_vcov <- stats::vcov(fit_glm) |
| 172 | 13x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
| 173 | 13x |
model_data <- fit_glm$model |
| 174 | 13x |
if (!is.null(at)) {
|
| 175 | 3x |
checkmate::assert_set_equal(xs_class[interaction_var], "numeric") |
| 176 |
} |
|
| 177 | 12x |
y <- list() |
| 178 | 12x |
if (xs_class[interaction_var] == "numeric") {
|
| 179 | 7x |
if (is.null(at)) {
|
| 180 | 5x |
at <- ceiling(stats::median(model_data[[interaction_var]])) |
| 181 |
} |
|
| 182 | ||
| 183 | 7x |
for (var_level in xs_level[[odds_ratio_var]][-1]) {
|
| 184 | 14x |
x <- list() |
| 185 | 14x |
for (increment in at) {
|
| 186 | 20x |
coef_names <- paste0(odds_ratio_var, var_level) |
| 187 | 20x |
if (increment != 0) {
|
| 188 | 20x |
interaction_coef_name <- h_interaction_coef_name( |
| 189 | 20x |
interaction_vars, |
| 190 | 20x |
c(odds_ratio_var, var_level), |
| 191 | 20x |
c(interaction_var, "") |
| 192 |
) |
|
| 193 | 20x |
coef_names <- c( |
| 194 | 20x |
coef_names, |
| 195 | 20x |
interaction_coef_name |
| 196 |
) |
|
| 197 |
} |
|
| 198 | 20x |
if (length(coef_names) > 1) {
|
| 199 | 20x |
xvec <- t(c(1, increment)) |
| 200 | 20x |
est <- as.numeric(xvec %*% xs_coef[coef_names]) |
| 201 | 20x |
se <- sqrt(as.numeric(xvec %*% xs_vcov[coef_names, coef_names] %*% t(xvec))) |
| 202 |
} else {
|
|
| 203 | ! |
est <- xs_coef[coef_names] |
| 204 | ! |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
| 205 |
} |
|
| 206 | 20x |
or <- exp(est) |
| 207 | 20x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
| 208 | 20x |
x[[as.character(increment)]] <- list(or = or, ci = ci) |
| 209 |
} |
|
| 210 | 14x |
y[[var_level]] <- x |
| 211 |
} |
|
| 212 |
} else {
|
|
| 213 | 5x |
checkmate::assert_set_equal(xs_class[odds_ratio_var], "numeric") |
| 214 | 5x |
checkmate::assert_set_equal(xs_class[interaction_var], "factor") |
| 215 | 5x |
for (var_level in xs_level[[interaction_var]]) {
|
| 216 | 15x |
coef_names <- odds_ratio_var |
| 217 | 15x |
if (var_level != xs_level[[interaction_var]][1]) {
|
| 218 | 10x |
interaction_coef_name <- h_interaction_coef_name( |
| 219 | 10x |
interaction_vars, |
| 220 | 10x |
c(odds_ratio_var, ""), |
| 221 | 10x |
c(interaction_var, var_level) |
| 222 |
) |
|
| 223 | 10x |
coef_names <- c( |
| 224 | 10x |
coef_names, |
| 225 | 10x |
interaction_coef_name |
| 226 |
) |
|
| 227 |
} |
|
| 228 | 15x |
if (length(coef_names) > 1) {
|
| 229 | 10x |
xvec <- t(c(1, 1)) |
| 230 | 10x |
est <- as.numeric(xvec %*% xs_coef[coef_names]) |
| 231 | 10x |
se <- sqrt(as.numeric(xvec %*% xs_vcov[coef_names, coef_names] %*% t(xvec))) |
| 232 |
} else {
|
|
| 233 | 5x |
est <- xs_coef[coef_names] |
| 234 | 5x |
se <- sqrt(as.numeric(xs_vcov[coef_names, coef_names])) |
| 235 |
} |
|
| 236 | 15x |
or <- exp(est) |
| 237 | 15x |
ci <- exp(est + c(lcl = -1, ucl = 1) * stats::qnorm((1 + conf_level) / 2) * se) |
| 238 | 15x |
y[[var_level]] <- list(or = or, ci = ci) |
| 239 |
} |
|
| 240 |
} |
|
| 241 | 12x |
y |
| 242 |
} |
|
| 243 | ||
| 244 |
#' @describeIn h_logistic_regression Helper function to calculate the odds ratio estimates |
|
| 245 |
#' in case of an interaction. This is a wrapper for [h_or_cont_interaction()] and |
|
| 246 |
#' [h_or_cat_interaction()]. |
|
| 247 |
#' |
|
| 248 |
#' @return Odds ratio. |
|
| 249 |
#' |
|
| 250 |
#' @export |
|
| 251 |
h_or_interaction <- function(odds_ratio_var, |
|
| 252 |
interaction_var, |
|
| 253 |
fit_glm, |
|
| 254 |
at = NULL, |
|
| 255 |
conf_level = 0.95) {
|
|
| 256 | 15x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
| 257 | 15x |
if (any(xs_class[c(odds_ratio_var, interaction_var)] == "numeric")) {
|
| 258 | 9x |
h_or_cont_interaction( |
| 259 | 9x |
odds_ratio_var, |
| 260 | 9x |
interaction_var, |
| 261 | 9x |
fit_glm, |
| 262 | 9x |
at = at, |
| 263 | 9x |
conf_level = conf_level |
| 264 |
) |
|
| 265 | 6x |
} else if (all(xs_class[c(odds_ratio_var, interaction_var)] == "factor")) {
|
| 266 | 6x |
h_or_cat_interaction( |
| 267 | 6x |
odds_ratio_var, |
| 268 | 6x |
interaction_var, |
| 269 | 6x |
fit_glm, |
| 270 | 6x |
conf_level = conf_level |
| 271 |
) |
|
| 272 |
} else {
|
|
| 273 | ! |
stop("wrong interaction variable class, the interaction variable is not a numeric nor a factor")
|
| 274 |
} |
|
| 275 |
} |
|
| 276 | ||
| 277 |
#' @describeIn h_logistic_regression Helper function to construct term labels from simple terms and the table |
|
| 278 |
#' of numbers of patients. |
|
| 279 |
#' |
|
| 280 |
#' @param terms (`character`)\cr simple terms. |
|
| 281 |
#' @param table (`table`)\cr table containing numbers for terms. |
|
| 282 |
#' |
|
| 283 |
#' @return Term labels containing numbers of patients. |
|
| 284 |
#' |
|
| 285 |
#' @export |
|
| 286 |
h_simple_term_labels <- function(terms, |
|
| 287 |
table) {
|
|
| 288 | 54x |
checkmate::assert_true(is.table(table)) |
| 289 | 54x |
checkmate::assert_multi_class(terms, classes = c("factor", "character"))
|
| 290 | 54x |
terms <- as.character(terms) |
| 291 | 54x |
term_n <- table[terms] |
| 292 | 54x |
paste0(terms, ", n = ", term_n) |
| 293 |
} |
|
| 294 | ||
| 295 |
#' @describeIn h_logistic_regression Helper function to construct term labels from interaction terms and the table |
|
| 296 |
#' of numbers of patients. |
|
| 297 |
#' |
|
| 298 |
#' @param terms1 (`character`)\cr terms for first dimension (rows). |
|
| 299 |
#' @param terms2 (`character`)\cr terms for second dimension (rows). |
|
| 300 |
#' @param any (`flag`)\cr whether any of `term1` and `term2` can be fulfilled to count the |
|
| 301 |
#' number of patients. In that case they can only be scalar (strings). |
|
| 302 |
#' |
|
| 303 |
#' @return Term labels containing numbers of patients. |
|
| 304 |
#' |
|
| 305 |
#' @export |
|
| 306 |
h_interaction_term_labels <- function(terms1, |
|
| 307 |
terms2, |
|
| 308 |
table, |
|
| 309 |
any = FALSE) {
|
|
| 310 | 8x |
checkmate::assert_true(is.table(table)) |
| 311 | 8x |
checkmate::assert_flag(any) |
| 312 | 8x |
checkmate::assert_multi_class(terms1, classes = c("factor", "character"))
|
| 313 | 8x |
checkmate::assert_multi_class(terms2, classes = c("factor", "character"))
|
| 314 | 8x |
terms1 <- as.character(terms1) |
| 315 | 8x |
terms2 <- as.character(terms2) |
| 316 | 8x |
if (any) {
|
| 317 | 4x |
checkmate::assert_scalar(terms1) |
| 318 | 4x |
checkmate::assert_scalar(terms2) |
| 319 | 4x |
paste0( |
| 320 | 4x |
terms1, " or ", terms2, ", n = ", |
| 321 |
# Note that we double count in the initial sum the cell [terms1, terms2], therefore subtract. |
|
| 322 | 4x |
sum(c(table[terms1, ], table[, terms2])) - table[terms1, terms2] |
| 323 |
) |
|
| 324 |
} else {
|
|
| 325 | 4x |
term_n <- table[cbind(terms1, terms2)] |
| 326 | 4x |
paste0(terms1, " * ", terms2, ", n = ", term_n) |
| 327 |
} |
|
| 328 |
} |
|
| 329 | ||
| 330 |
#' @describeIn h_logistic_regression Helper function to tabulate the main effect |
|
| 331 |
#' results of a (conditional) logistic regression model. |
|
| 332 |
#' |
|
| 333 |
#' @return Tabulated main effect results from a logistic regression model. |
|
| 334 |
#' |
|
| 335 |
#' @examples |
|
| 336 |
#' h_glm_simple_term_extract("AGE", mod1)
|
|
| 337 |
#' h_glm_simple_term_extract("ARMCD", mod1)
|
|
| 338 |
#' |
|
| 339 |
#' @export |
|
| 340 |
h_glm_simple_term_extract <- function(x, fit_glm) {
|
|
| 341 | 78x |
checkmate::assert_multi_class(fit_glm, c("glm", "clogit"))
|
| 342 | 78x |
checkmate::assert_string(x) |
| 343 | ||
| 344 | 78x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
| 345 | 78x |
xs_level <- fit_glm$xlevels |
| 346 | 78x |
xs_coef <- summary(fit_glm)$coefficients |
| 347 | 78x |
stats <- if (inherits(fit_glm, "glm")) {
|
| 348 | 66x |
c("estimate" = "Estimate", "std_error" = "Std. Error", "pvalue" = "Pr(>|z|)")
|
| 349 |
} else {
|
|
| 350 | 12x |
c("estimate" = "coef", "std_error" = "se(coef)", "pvalue" = "Pr(>|z|)")
|
| 351 |
} |
|
| 352 |
# Make sure x is not an interaction term. |
|
| 353 | 78x |
checkmate::assert_subset(x, names(xs_class)) |
| 354 | 78x |
x_sel <- if (xs_class[x] == "numeric") x else paste0(x, xs_level[[x]][-1]) |
| 355 | 78x |
x_stats <- as.data.frame(xs_coef[x_sel, stats, drop = FALSE], stringsAsFactors = FALSE) |
| 356 | 78x |
colnames(x_stats) <- names(stats) |
| 357 | 78x |
x_stats$estimate <- as.list(x_stats$estimate) |
| 358 | 78x |
x_stats$std_error <- as.list(x_stats$std_error) |
| 359 | 78x |
x_stats$pvalue <- as.list(x_stats$pvalue) |
| 360 | 78x |
x_stats$df <- as.list(1) |
| 361 | 78x |
if (xs_class[x] == "numeric") {
|
| 362 | 60x |
x_stats$term <- x |
| 363 | 60x |
x_stats$term_label <- if (inherits(fit_glm, "glm")) {
|
| 364 | 48x |
formatters::var_labels(fit_glm$data[x], fill = TRUE) |
| 365 |
} else {
|
|
| 366 |
# We just fill in here with the `term` itself as we don't have the data available. |
|
| 367 | 12x |
x |
| 368 |
} |
|
| 369 | 60x |
x_stats$is_variable_summary <- FALSE |
| 370 | 60x |
x_stats$is_term_summary <- TRUE |
| 371 |
} else {
|
|
| 372 | 18x |
checkmate::assert_class(fit_glm, "glm") |
| 373 |
# The reason is that we don't have the original data set in the `clogit` object |
|
| 374 |
# and therefore cannot determine the `x_numbers` here. |
|
| 375 | 18x |
x_numbers <- table(fit_glm$data[[x]]) |
| 376 | 18x |
x_stats$term <- xs_level[[x]][-1] |
| 377 | 18x |
x_stats$term_label <- h_simple_term_labels(x_stats$term, x_numbers) |
| 378 | 18x |
x_stats$is_variable_summary <- FALSE |
| 379 | 18x |
x_stats$is_term_summary <- TRUE |
| 380 | 18x |
main_effects <- car::Anova(fit_glm, type = 3, test.statistic = "Wald") |
| 381 | 18x |
x_main <- data.frame( |
| 382 | 18x |
pvalue = main_effects[x, "Pr(>Chisq)", drop = TRUE], |
| 383 | 18x |
term = xs_level[[x]][1], |
| 384 | 18x |
term_label = paste("Reference", h_simple_term_labels(xs_level[[x]][1], x_numbers)),
|
| 385 | 18x |
df = main_effects[x, "Df", drop = TRUE], |
| 386 | 18x |
stringsAsFactors = FALSE |
| 387 |
) |
|
| 388 | 18x |
x_main$pvalue <- as.list(x_main$pvalue) |
| 389 | 18x |
x_main$df <- as.list(x_main$df) |
| 390 | 18x |
x_main$estimate <- list(numeric(0)) |
| 391 | 18x |
x_main$std_error <- list(numeric(0)) |
| 392 | 18x |
if (length(xs_level[[x]][-1]) == 1) {
|
| 393 | 8x |
x_main$pvalue <- list(numeric(0)) |
| 394 | 8x |
x_main$df <- list(numeric(0)) |
| 395 |
} |
|
| 396 | 18x |
x_main$is_variable_summary <- TRUE |
| 397 | 18x |
x_main$is_term_summary <- FALSE |
| 398 | 18x |
x_stats <- rbind(x_main, x_stats) |
| 399 |
} |
|
| 400 | 78x |
x_stats$variable <- x |
| 401 | 78x |
x_stats$variable_label <- if (inherits(fit_glm, "glm")) {
|
| 402 | 66x |
formatters::var_labels(fit_glm$data[x], fill = TRUE) |
| 403 |
} else {
|
|
| 404 | 12x |
x |
| 405 |
} |
|
| 406 | 78x |
x_stats$interaction <- "" |
| 407 | 78x |
x_stats$interaction_label <- "" |
| 408 | 78x |
x_stats$reference <- "" |
| 409 | 78x |
x_stats$reference_label <- "" |
| 410 | 78x |
rownames(x_stats) <- NULL |
| 411 | 78x |
x_stats[c( |
| 412 | 78x |
"variable", |
| 413 | 78x |
"variable_label", |
| 414 | 78x |
"term", |
| 415 | 78x |
"term_label", |
| 416 | 78x |
"interaction", |
| 417 | 78x |
"interaction_label", |
| 418 | 78x |
"reference", |
| 419 | 78x |
"reference_label", |
| 420 | 78x |
"estimate", |
| 421 | 78x |
"std_error", |
| 422 | 78x |
"df", |
| 423 | 78x |
"pvalue", |
| 424 | 78x |
"is_variable_summary", |
| 425 | 78x |
"is_term_summary" |
| 426 |
)] |
|
| 427 |
} |
|
| 428 | ||
| 429 |
#' @describeIn h_logistic_regression Helper function to tabulate the interaction term |
|
| 430 |
#' results of a logistic regression model. |
|
| 431 |
#' |
|
| 432 |
#' @return Tabulated interaction term results from a logistic regression model. |
|
| 433 |
#' |
|
| 434 |
#' @examples |
|
| 435 |
#' h_glm_interaction_extract("ARMCD:AGE", mod2)
|
|
| 436 |
#' |
|
| 437 |
#' @export |
|
| 438 |
h_glm_interaction_extract <- function(x, fit_glm) {
|
|
| 439 | 7x |
vars <- h_get_interaction_vars(fit_glm) |
| 440 | 7x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
| 441 | ||
| 442 | 7x |
checkmate::assert_string(x) |
| 443 | ||
| 444 |
# Only take two-way interaction |
|
| 445 | 7x |
checkmate::assert_vector(vars, len = 2) |
| 446 | ||
| 447 |
# Only consider simple case: first variable in interaction is arm, a categorical variable |
|
| 448 | 7x |
checkmate::assert_disjunct(xs_class[vars[1]], "numeric") |
| 449 | ||
| 450 | 7x |
xs_level <- fit_glm$xlevels |
| 451 | 7x |
xs_coef <- summary(fit_glm)$coefficients |
| 452 | 7x |
main_effects <- car::Anova(fit_glm, type = 3, test.statistic = "Wald") |
| 453 | 7x |
stats <- c("estimate" = "Estimate", "std_error" = "Std. Error", "pvalue" = "Pr(>|z|)")
|
| 454 | 7x |
v1_comp <- xs_level[[vars[1]]][-1] |
| 455 | 7x |
if (xs_class[vars[2]] == "numeric") {
|
| 456 | 4x |
x_stats <- as.data.frame( |
| 457 | 4x |
xs_coef[paste0(vars[1], v1_comp, ":", vars[2]), stats, drop = FALSE], |
| 458 | 4x |
stringsAsFactors = FALSE |
| 459 |
) |
|
| 460 | 4x |
colnames(x_stats) <- names(stats) |
| 461 | 4x |
x_stats$term <- v1_comp |
| 462 | 4x |
x_numbers <- table(fit_glm$data[[vars[1]]]) |
| 463 | 4x |
x_stats$term_label <- h_simple_term_labels(v1_comp, x_numbers) |
| 464 | 4x |
v1_ref <- xs_level[[vars[1]]][1] |
| 465 | 4x |
term_main <- v1_ref |
| 466 | 4x |
ref_label <- h_simple_term_labels(v1_ref, x_numbers) |
| 467 | 3x |
} else if (xs_class[vars[2]] != "numeric") {
|
| 468 | 3x |
v2_comp <- xs_level[[vars[2]]][-1] |
| 469 | 3x |
v1_v2_grid <- expand.grid(v1 = v1_comp, v2 = v2_comp) |
| 470 | 3x |
x_sel <- paste( |
| 471 | 3x |
paste0(vars[1], v1_v2_grid$v1), |
| 472 | 3x |
paste0(vars[2], v1_v2_grid$v2), |
| 473 | 3x |
sep = ":" |
| 474 |
) |
|
| 475 | 3x |
x_stats <- as.data.frame(xs_coef[x_sel, stats, drop = FALSE], stringsAsFactors = FALSE) |
| 476 | 3x |
colnames(x_stats) <- names(stats) |
| 477 | 3x |
x_stats$term <- paste(v1_v2_grid$v1, "*", v1_v2_grid$v2) |
| 478 | 3x |
x_numbers <- table(fit_glm$data[[vars[1]]], fit_glm$data[[vars[2]]]) |
| 479 | 3x |
x_stats$term_label <- h_interaction_term_labels(v1_v2_grid$v1, v1_v2_grid$v2, x_numbers) |
| 480 | 3x |
v1_ref <- xs_level[[vars[1]]][1] |
| 481 | 3x |
v2_ref <- xs_level[[vars[2]]][1] |
| 482 | 3x |
term_main <- paste(vars[1], vars[2], sep = " * ") |
| 483 | 3x |
ref_label <- h_interaction_term_labels(v1_ref, v2_ref, x_numbers, any = TRUE) |
| 484 |
} |
|
| 485 | 7x |
x_stats$df <- as.list(1) |
| 486 | 7x |
x_stats$pvalue <- as.list(x_stats$pvalue) |
| 487 | 7x |
x_stats$is_variable_summary <- FALSE |
| 488 | 7x |
x_stats$is_term_summary <- TRUE |
| 489 | 7x |
x_main <- data.frame( |
| 490 | 7x |
pvalue = main_effects[x, "Pr(>Chisq)", drop = TRUE], |
| 491 | 7x |
term = term_main, |
| 492 | 7x |
term_label = paste("Reference", ref_label),
|
| 493 | 7x |
df = main_effects[x, "Df", drop = TRUE], |
| 494 | 7x |
stringsAsFactors = FALSE |
| 495 |
) |
|
| 496 | 7x |
x_main$pvalue <- as.list(x_main$pvalue) |
| 497 | 7x |
x_main$df <- as.list(x_main$df) |
| 498 | 7x |
x_main$estimate <- list(numeric(0)) |
| 499 | 7x |
x_main$std_error <- list(numeric(0)) |
| 500 | 7x |
x_main$is_variable_summary <- TRUE |
| 501 | 7x |
x_main$is_term_summary <- FALSE |
| 502 | ||
| 503 | 7x |
x_stats <- rbind(x_main, x_stats) |
| 504 | 7x |
x_stats$variable <- x |
| 505 | 7x |
x_stats$variable_label <- paste( |
| 506 | 7x |
"Interaction of", |
| 507 | 7x |
formatters::var_labels(fit_glm$data[vars[1]], fill = TRUE), |
| 508 |
"*", |
|
| 509 | 7x |
formatters::var_labels(fit_glm$data[vars[2]], fill = TRUE) |
| 510 |
) |
|
| 511 | 7x |
x_stats$interaction <- "" |
| 512 | 7x |
x_stats$interaction_label <- "" |
| 513 | 7x |
x_stats$reference <- "" |
| 514 | 7x |
x_stats$reference_label <- "" |
| 515 | 7x |
rownames(x_stats) <- NULL |
| 516 | 7x |
x_stats[c( |
| 517 | 7x |
"variable", |
| 518 | 7x |
"variable_label", |
| 519 | 7x |
"term", |
| 520 | 7x |
"term_label", |
| 521 | 7x |
"interaction", |
| 522 | 7x |
"interaction_label", |
| 523 | 7x |
"reference", |
| 524 | 7x |
"reference_label", |
| 525 | 7x |
"estimate", |
| 526 | 7x |
"std_error", |
| 527 | 7x |
"df", |
| 528 | 7x |
"pvalue", |
| 529 | 7x |
"is_variable_summary", |
| 530 | 7x |
"is_term_summary" |
| 531 |
)] |
|
| 532 |
} |
|
| 533 | ||
| 534 |
#' @describeIn h_logistic_regression Helper function to tabulate the interaction |
|
| 535 |
#' results of a logistic regression model. This basically is a wrapper for |
|
| 536 |
#' [h_or_interaction()] and [h_glm_simple_term_extract()] which puts the results |
|
| 537 |
#' in the right data frame format. |
|
| 538 |
#' |
|
| 539 |
#' @return A `data.frame` of tabulated interaction term results from a logistic regression model. |
|
| 540 |
#' |
|
| 541 |
#' @examples |
|
| 542 |
#' h_glm_inter_term_extract("AGE", "ARMCD", mod2)
|
|
| 543 |
#' |
|
| 544 |
#' @export |
|
| 545 |
h_glm_inter_term_extract <- function(odds_ratio_var, |
|
| 546 |
interaction_var, |
|
| 547 |
fit_glm, |
|
| 548 |
...) {
|
|
| 549 |
# First obtain the main effects. |
|
| 550 | 13x |
main_stats <- h_glm_simple_term_extract(odds_ratio_var, fit_glm) |
| 551 | 13x |
main_stats$is_reference_summary <- FALSE |
| 552 | 13x |
main_stats$odds_ratio <- NA |
| 553 | 13x |
main_stats$lcl <- NA |
| 554 | 13x |
main_stats$ucl <- NA |
| 555 | ||
| 556 |
# Then we get the odds ratio estimates and put into df form. |
|
| 557 | 13x |
or_numbers <- h_or_interaction(odds_ratio_var, interaction_var, fit_glm, ...) |
| 558 | 13x |
is_num_or_var <- attr(fit_glm$terms, "dataClasses")[odds_ratio_var] == "numeric" |
| 559 | ||
| 560 | 13x |
if (is_num_or_var) {
|
| 561 |
# Numeric OR variable case. |
|
| 562 | 4x |
references <- names(or_numbers) |
| 563 | 4x |
n_ref <- length(references) |
| 564 | ||
| 565 | 4x |
extract_from_list <- function(l, name, pos = 1) {
|
| 566 | 12x |
unname(unlist( |
| 567 | 12x |
lapply(or_numbers, function(x) {
|
| 568 | 36x |
x[[name]][pos] |
| 569 |
}) |
|
| 570 |
)) |
|
| 571 |
} |
|
| 572 | 4x |
or_stats <- data.frame( |
| 573 | 4x |
variable = odds_ratio_var, |
| 574 | 4x |
variable_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
| 575 | 4x |
term = odds_ratio_var, |
| 576 | 4x |
term_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
| 577 | 4x |
interaction = interaction_var, |
| 578 | 4x |
interaction_label = unname(formatters::var_labels(fit_glm$data[interaction_var], fill = TRUE)), |
| 579 | 4x |
reference = references, |
| 580 | 4x |
reference_label = references, |
| 581 | 4x |
estimate = NA, |
| 582 | 4x |
std_error = NA, |
| 583 | 4x |
odds_ratio = extract_from_list(or_numbers, "or"), |
| 584 | 4x |
lcl = extract_from_list(or_numbers, "ci", pos = "lcl"), |
| 585 | 4x |
ucl = extract_from_list(or_numbers, "ci", pos = "ucl"), |
| 586 | 4x |
df = NA, |
| 587 | 4x |
pvalue = NA, |
| 588 | 4x |
is_variable_summary = FALSE, |
| 589 | 4x |
is_term_summary = FALSE, |
| 590 | 4x |
is_reference_summary = TRUE |
| 591 |
) |
|
| 592 |
} else {
|
|
| 593 |
# Categorical OR variable case. |
|
| 594 | 9x |
references <- names(or_numbers[[1]]) |
| 595 | 9x |
n_ref <- length(references) |
| 596 | ||
| 597 | 9x |
extract_from_list <- function(l, name, pos = 1) {
|
| 598 | 27x |
unname(unlist( |
| 599 | 27x |
lapply(or_numbers, function(x) {
|
| 600 | 48x |
lapply(x, function(y) y[[name]][pos]) |
| 601 |
}) |
|
| 602 |
)) |
|
| 603 |
} |
|
| 604 | 9x |
or_stats <- data.frame( |
| 605 | 9x |
variable = odds_ratio_var, |
| 606 | 9x |
variable_label = unname(formatters::var_labels(fit_glm$data[odds_ratio_var], fill = TRUE)), |
| 607 | 9x |
term = rep(names(or_numbers), each = n_ref), |
| 608 | 9x |
term_label = h_simple_term_labels(rep(names(or_numbers), each = n_ref), table(fit_glm$data[[odds_ratio_var]])), |
| 609 | 9x |
interaction = interaction_var, |
| 610 | 9x |
interaction_label = unname(formatters::var_labels(fit_glm$data[interaction_var], fill = TRUE)), |
| 611 | 9x |
reference = unlist(lapply(or_numbers, names)), |
| 612 | 9x |
reference_label = unlist(lapply(or_numbers, names)), |
| 613 | 9x |
estimate = NA, |
| 614 | 9x |
std_error = NA, |
| 615 | 9x |
odds_ratio = extract_from_list(or_numbers, "or"), |
| 616 | 9x |
lcl = extract_from_list(or_numbers, "ci", pos = "lcl"), |
| 617 | 9x |
ucl = extract_from_list(or_numbers, "ci", pos = "ucl"), |
| 618 | 9x |
df = NA, |
| 619 | 9x |
pvalue = NA, |
| 620 | 9x |
is_variable_summary = FALSE, |
| 621 | 9x |
is_term_summary = FALSE, |
| 622 | 9x |
is_reference_summary = TRUE |
| 623 |
) |
|
| 624 |
} |
|
| 625 | ||
| 626 | 13x |
df <- rbind( |
| 627 | 13x |
main_stats[, names(or_stats)], |
| 628 | 13x |
or_stats |
| 629 |
) |
|
| 630 | 13x |
df[order(-df$is_variable_summary, df$term, -df$is_term_summary, df$reference), ] |
| 631 |
} |
|
| 632 | ||
| 633 |
#' @describeIn h_logistic_regression Helper function to tabulate the results including |
|
| 634 |
#' odds ratios and confidence intervals of simple terms. |
|
| 635 |
#' |
|
| 636 |
#' @return Tabulated statistics for the given variable(s) from the logistic regression model. |
|
| 637 |
#' |
|
| 638 |
#' @examples |
|
| 639 |
#' h_logistic_simple_terms("AGE", mod1)
|
|
| 640 |
#' |
|
| 641 |
#' @export |
|
| 642 |
h_logistic_simple_terms <- function(x, fit_glm, conf_level = 0.95) {
|
|
| 643 | 53x |
checkmate::assert_multi_class(fit_glm, c("glm", "clogit"))
|
| 644 | 53x |
if (inherits(fit_glm, "glm")) {
|
| 645 | 42x |
checkmate::assert_set_equal(fit_glm$family$family, "binomial") |
| 646 |
} |
|
| 647 | 53x |
terms_name <- attr(stats::terms(fit_glm), "term.labels") |
| 648 | 53x |
xs_class <- attr(fit_glm$terms, "dataClasses") |
| 649 | 53x |
interaction <- terms_name[which(!terms_name %in% names(xs_class))] |
| 650 | 53x |
checkmate::assert_subset(x, terms_name) |
| 651 | 53x |
if (length(interaction) != 0) {
|
| 652 |
# Make sure any item in x is not part of interaction term |
|
| 653 | 2x |
checkmate::assert_disjunct(x, unlist(strsplit(interaction, ":"))) |
| 654 |
} |
|
| 655 | 53x |
x_stats <- lapply(x, h_glm_simple_term_extract, fit_glm) |
| 656 | 53x |
x_stats <- do.call(rbind, x_stats) |
| 657 | 53x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
| 658 | 53x |
x_stats$odds_ratio <- lapply(x_stats$estimate, exp) |
| 659 | 53x |
x_stats$lcl <- Map(function(or, se) exp(log(or) - q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
| 660 | 53x |
x_stats$ucl <- Map(function(or, se) exp(log(or) + q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
| 661 | 53x |
x_stats$ci <- Map(function(lcl, ucl) c(lcl, ucl), lcl = x_stats$lcl, ucl = x_stats$ucl) |
| 662 | 53x |
x_stats |
| 663 |
} |
|
| 664 | ||
| 665 |
#' @describeIn h_logistic_regression Helper function to tabulate the results including |
|
| 666 |
#' odds ratios and confidence intervals of interaction terms. |
|
| 667 |
#' |
|
| 668 |
#' @return Tabulated statistics for the given variable(s) from the logistic regression model. |
|
| 669 |
#' |
|
| 670 |
#' @examples |
|
| 671 |
#' h_logistic_inter_terms(c("RACE", "AGE", "ARMCD", "AGE:ARMCD"), mod2)
|
|
| 672 |
#' |
|
| 673 |
#' @export |
|
| 674 |
h_logistic_inter_terms <- function(x, |
|
| 675 |
fit_glm, |
|
| 676 |
conf_level = 0.95, |
|
| 677 |
at = NULL) {
|
|
| 678 |
# Find out the interaction variables and interaction term. |
|
| 679 | 5x |
inter_vars <- h_get_interaction_vars(fit_glm) |
| 680 | 5x |
checkmate::assert_vector(inter_vars, len = 2) |
| 681 | ||
| 682 | ||
| 683 | 5x |
inter_term_index <- intersect(grep(inter_vars[1], x), grep(inter_vars[2], x)) |
| 684 | 5x |
inter_term <- x[inter_term_index] |
| 685 | ||
| 686 |
# For the non-interaction vars we need the standard stuff. |
|
| 687 | 5x |
normal_terms <- setdiff(x, union(inter_vars, inter_term)) |
| 688 | ||
| 689 | 5x |
x_stats <- lapply(normal_terms, h_glm_simple_term_extract, fit_glm) |
| 690 | 5x |
x_stats <- do.call(rbind, x_stats) |
| 691 | 5x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
| 692 | 5x |
x_stats$odds_ratio <- lapply(x_stats$estimate, exp) |
| 693 | 5x |
x_stats$lcl <- Map(function(or, se) exp(log(or) - q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
| 694 | 5x |
x_stats$ucl <- Map(function(or, se) exp(log(or) + q_norm * se), x_stats$odds_ratio, x_stats$std_error) |
| 695 | 5x |
normal_stats <- x_stats |
| 696 | 5x |
normal_stats$is_reference_summary <- FALSE |
| 697 | ||
| 698 |
# Now the interaction term itself. |
|
| 699 | 5x |
inter_term_stats <- h_glm_interaction_extract(inter_term, fit_glm) |
| 700 | 5x |
inter_term_stats$odds_ratio <- NA |
| 701 | 5x |
inter_term_stats$lcl <- NA |
| 702 | 5x |
inter_term_stats$ucl <- NA |
| 703 | 5x |
inter_term_stats$is_reference_summary <- FALSE |
| 704 | ||
| 705 | 5x |
is_intervar1_numeric <- attr(fit_glm$terms, "dataClasses")[inter_vars[1]] == "numeric" |
| 706 | ||
| 707 |
# Interaction stuff. |
|
| 708 | 5x |
inter_stats_one <- h_glm_inter_term_extract( |
| 709 | 5x |
inter_vars[1], |
| 710 | 5x |
inter_vars[2], |
| 711 | 5x |
fit_glm, |
| 712 | 5x |
conf_level = conf_level, |
| 713 | 5x |
at = `if`(is_intervar1_numeric, NULL, at) |
| 714 |
) |
|
| 715 | 5x |
inter_stats_two <- h_glm_inter_term_extract( |
| 716 | 5x |
inter_vars[2], |
| 717 | 5x |
inter_vars[1], |
| 718 | 5x |
fit_glm, |
| 719 | 5x |
conf_level = conf_level, |
| 720 | 5x |
at = `if`(is_intervar1_numeric, at, NULL) |
| 721 |
) |
|
| 722 | ||
| 723 |
# Now just combine everything in one data frame. |
|
| 724 | 5x |
col_names <- c( |
| 725 | 5x |
"variable", |
| 726 | 5x |
"variable_label", |
| 727 | 5x |
"term", |
| 728 | 5x |
"term_label", |
| 729 | 5x |
"interaction", |
| 730 | 5x |
"interaction_label", |
| 731 | 5x |
"reference", |
| 732 | 5x |
"reference_label", |
| 733 | 5x |
"estimate", |
| 734 | 5x |
"std_error", |
| 735 | 5x |
"df", |
| 736 | 5x |
"pvalue", |
| 737 | 5x |
"odds_ratio", |
| 738 | 5x |
"lcl", |
| 739 | 5x |
"ucl", |
| 740 | 5x |
"is_variable_summary", |
| 741 | 5x |
"is_term_summary", |
| 742 | 5x |
"is_reference_summary" |
| 743 |
) |
|
| 744 | 5x |
df <- rbind( |
| 745 | 5x |
inter_stats_one[, col_names], |
| 746 | 5x |
inter_stats_two[, col_names], |
| 747 | 5x |
inter_term_stats[, col_names] |
| 748 |
) |
|
| 749 | 5x |
if (length(normal_terms) > 0) {
|
| 750 | 5x |
df <- rbind( |
| 751 | 5x |
normal_stats[, col_names], |
| 752 | 5x |
df |
| 753 |
) |
|
| 754 |
} |
|
| 755 | 5x |
df$ci <- combine_vectors(df$lcl, df$ucl) |
| 756 | 5x |
df |
| 757 |
} |
| 1 |
#' Cox regression helper function for interactions |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Test and estimate the effect of a treatment in interaction with a covariate. |
|
| 6 |
#' The effect is estimated as the HR of the tested treatment for a given level |
|
| 7 |
#' of the covariate, in comparison to the treatment control. |
|
| 8 |
#' |
|
| 9 |
#' @inheritParams argument_convention |
|
| 10 |
#' @param x (`numeric` or `factor`)\cr the values of the covariate to be tested. |
|
| 11 |
#' @param effect (`string`)\cr the name of the effect to be tested and estimated. |
|
| 12 |
#' @param covar (`string`)\cr the name of the covariate in the model. |
|
| 13 |
#' @param mod (`coxph`)\cr the Cox regression model. |
|
| 14 |
#' @param label (`string`)\cr the label to be returned as `term_label`. |
|
| 15 |
#' @param control (`list`)\cr a list of controls as returned by [control_coxreg()]. |
|
| 16 |
#' @param ... see methods. |
|
| 17 |
#' |
|
| 18 |
#' @examples |
|
| 19 |
#' library(survival) |
|
| 20 |
#' |
|
| 21 |
#' set.seed(1, kind = "Mersenne-Twister") |
|
| 22 |
#' |
|
| 23 |
#' # Testing dataset [survival::bladder]. |
|
| 24 |
#' dta_bladder <- with( |
|
| 25 |
#' data = bladder[bladder$enum < 5, ], |
|
| 26 |
#' data.frame( |
|
| 27 |
#' time = stop, |
|
| 28 |
#' status = event, |
|
| 29 |
#' armcd = as.factor(rx), |
|
| 30 |
#' covar1 = as.factor(enum), |
|
| 31 |
#' covar2 = factor( |
|
| 32 |
#' sample(as.factor(enum)), |
|
| 33 |
#' levels = 1:4, |
|
| 34 |
#' labels = c("F", "F", "M", "M")
|
|
| 35 |
#' ) |
|
| 36 |
#' ) |
|
| 37 |
#' ) |
|
| 38 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
| 39 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels |
|
| 40 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE) |
|
| 41 |
#' |
|
| 42 |
#' plot( |
|
| 43 |
#' survfit(Surv(time, status) ~ armcd + covar1, data = dta_bladder), |
|
| 44 |
#' lty = 2:4, |
|
| 45 |
#' xlab = "Months", |
|
| 46 |
#' col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
|
|
| 47 |
#' ) |
|
| 48 |
#' |
|
| 49 |
#' @name cox_regression_inter |
|
| 50 |
NULL |
|
| 51 | ||
| 52 |
#' @describeIn cox_regression_inter S3 generic helper function to determine interaction effect. |
|
| 53 |
#' |
|
| 54 |
#' @return |
|
| 55 |
#' * `h_coxreg_inter_effect()` returns a `data.frame` of covariate interaction effects consisting of the following |
|
| 56 |
#' variables: `effect`, `term`, `term_label`, `level`, `n`, `hr`, `lcl`, `ucl`, `pval`, and `pval_inter`. |
|
| 57 |
#' |
|
| 58 |
#' @export |
|
| 59 |
h_coxreg_inter_effect <- function(x, |
|
| 60 |
effect, |
|
| 61 |
covar, |
|
| 62 |
mod, |
|
| 63 |
label, |
|
| 64 |
control, |
|
| 65 |
...) {
|
|
| 66 | 29x |
UseMethod("h_coxreg_inter_effect", x)
|
| 67 |
} |
|
| 68 | ||
| 69 |
#' @describeIn cox_regression_inter Method for `numeric` class. Estimates the interaction with a `numeric` covariate. |
|
| 70 |
#' |
|
| 71 |
#' @method h_coxreg_inter_effect numeric |
|
| 72 |
#' |
|
| 73 |
#' @param at (`list`)\cr a list with items named after the covariate, every |
|
| 74 |
#' item is a vector of levels at which the interaction should be estimated. |
|
| 75 |
#' |
|
| 76 |
#' @export |
|
| 77 |
h_coxreg_inter_effect.numeric <- function(x, |
|
| 78 |
effect, |
|
| 79 |
covar, |
|
| 80 |
mod, |
|
| 81 |
label, |
|
| 82 |
control, |
|
| 83 |
at, |
|
| 84 |
...) {
|
|
| 85 | 7x |
betas <- stats::coef(mod) |
| 86 | 7x |
attrs <- attr(stats::terms(mod), "term.labels") |
| 87 | 7x |
term_indices <- grep( |
| 88 | 7x |
pattern = effect, |
| 89 | 7x |
x = attrs[!grepl("strata\\(", attrs)]
|
| 90 |
) |
|
| 91 | 7x |
checkmate::assert_vector(term_indices, len = 2) |
| 92 | 7x |
betas <- betas[term_indices] |
| 93 | 7x |
betas_var <- diag(stats::vcov(mod))[term_indices] |
| 94 | 7x |
betas_cov <- stats::vcov(mod)[term_indices[1], term_indices[2]] |
| 95 | 7x |
xval <- if (is.null(at[[covar]])) {
|
| 96 | 6x |
stats::median(x) |
| 97 |
} else {
|
|
| 98 | 1x |
at[[covar]] |
| 99 |
} |
|
| 100 | 7x |
effect_index <- !grepl(covar, names(betas)) |
| 101 | 7x |
coef_hat <- betas[effect_index] + xval * betas[!effect_index] |
| 102 | 7x |
coef_se <- sqrt( |
| 103 | 7x |
betas_var[effect_index] + |
| 104 | 7x |
xval ^ 2 * betas_var[!effect_index] + # styler: off |
| 105 | 7x |
2 * xval * betas_cov |
| 106 |
) |
|
| 107 | 7x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
| 108 | 7x |
data.frame( |
| 109 | 7x |
effect = "Covariate:", |
| 110 | 7x |
term = rep(covar, length(xval)), |
| 111 | 7x |
term_label = paste0(" ", xval),
|
| 112 | 7x |
level = as.character(xval), |
| 113 | 7x |
n = NA, |
| 114 | 7x |
hr = exp(coef_hat), |
| 115 | 7x |
lcl = exp(coef_hat - q_norm * coef_se), |
| 116 | 7x |
ucl = exp(coef_hat + q_norm * coef_se), |
| 117 | 7x |
pval = NA, |
| 118 | 7x |
pval_inter = NA, |
| 119 | 7x |
stringsAsFactors = FALSE |
| 120 |
) |
|
| 121 |
} |
|
| 122 | ||
| 123 |
#' @describeIn cox_regression_inter Method for `factor` class. Estimate the interaction with a `factor` covariate. |
|
| 124 |
#' |
|
| 125 |
#' @method h_coxreg_inter_effect factor |
|
| 126 |
#' |
|
| 127 |
#' @param data (`data.frame`)\cr the data frame on which the model was fit. |
|
| 128 |
#' |
|
| 129 |
#' @export |
|
| 130 |
h_coxreg_inter_effect.factor <- function(x, |
|
| 131 |
effect, |
|
| 132 |
covar, |
|
| 133 |
mod, |
|
| 134 |
label, |
|
| 135 |
control, |
|
| 136 |
data, |
|
| 137 |
...) {
|
|
| 138 | 17x |
lvl_given <- levels(x) |
| 139 | 17x |
y <- h_coxreg_inter_estimations( |
| 140 | 17x |
variable = effect, given = covar, |
| 141 | 17x |
lvl_var = levels(data[[effect]]), |
| 142 | 17x |
lvl_given = lvl_given, |
| 143 | 17x |
mod = mod, |
| 144 | 17x |
conf_level = 0.95 |
| 145 | 17x |
)[[1]] |
| 146 | ||
| 147 | 17x |
data.frame( |
| 148 | 17x |
effect = "Covariate:", |
| 149 | 17x |
term = rep(covar, nrow(y)), |
| 150 | 17x |
term_label = paste0(" ", lvl_given),
|
| 151 | 17x |
level = lvl_given, |
| 152 | 17x |
n = NA, |
| 153 | 17x |
hr = y[, "hr"], |
| 154 | 17x |
lcl = y[, "lcl"], |
| 155 | 17x |
ucl = y[, "ucl"], |
| 156 | 17x |
pval = NA, |
| 157 | 17x |
pval_inter = NA, |
| 158 | 17x |
stringsAsFactors = FALSE |
| 159 |
) |
|
| 160 |
} |
|
| 161 | ||
| 162 |
#' @describeIn cox_regression_inter Method for `character` class. Estimate the interaction with a `character` covariate. |
|
| 163 |
#' This makes an automatic conversion to `factor` and then forwards to the method for factors. |
|
| 164 |
#' |
|
| 165 |
#' @method h_coxreg_inter_effect character |
|
| 166 |
#' |
|
| 167 |
#' @note |
|
| 168 |
#' * Automatic conversion of character to factor does not guarantee results can be generated correctly. It is |
|
| 169 |
#' therefore better to always pre-process the dataset such that factors are manually created from character |
|
| 170 |
#' variables before passing the dataset to [rtables::build_table()]. |
|
| 171 |
#' |
|
| 172 |
#' @export |
|
| 173 |
h_coxreg_inter_effect.character <- function(x, |
|
| 174 |
effect, |
|
| 175 |
covar, |
|
| 176 |
mod, |
|
| 177 |
label, |
|
| 178 |
control, |
|
| 179 |
data, |
|
| 180 |
...) {
|
|
| 181 | 5x |
y <- as.factor(x) |
| 182 | ||
| 183 | 5x |
h_coxreg_inter_effect( |
| 184 | 5x |
x = y, |
| 185 | 5x |
effect = effect, |
| 186 | 5x |
covar = covar, |
| 187 | 5x |
mod = mod, |
| 188 | 5x |
label = label, |
| 189 | 5x |
control = control, |
| 190 | 5x |
data = data, |
| 191 |
... |
|
| 192 |
) |
|
| 193 |
} |
|
| 194 | ||
| 195 |
#' @describeIn cox_regression_inter A higher level function to get |
|
| 196 |
#' the results of the interaction test and the estimated values. |
|
| 197 |
#' |
|
| 198 |
#' @return |
|
| 199 |
#' * `h_coxreg_extract_interaction()` returns the result of an interaction test and the estimated values. If |
|
| 200 |
#' no interaction, [h_coxreg_univar_extract()] is applied instead. |
|
| 201 |
#' |
|
| 202 |
#' @examples |
|
| 203 |
#' mod <- coxph(Surv(time, status) ~ armcd * covar1, data = dta_bladder) |
|
| 204 |
#' h_coxreg_extract_interaction( |
|
| 205 |
#' mod = mod, effect = "armcd", covar = "covar1", data = dta_bladder, |
|
| 206 |
#' control = control_coxreg() |
|
| 207 |
#' ) |
|
| 208 |
#' |
|
| 209 |
#' @export |
|
| 210 |
h_coxreg_extract_interaction <- function(effect, |
|
| 211 |
covar, |
|
| 212 |
mod, |
|
| 213 |
data, |
|
| 214 |
at, |
|
| 215 |
control) {
|
|
| 216 | 31x |
if (!any(attr(stats::terms(mod), "order") == 2)) {
|
| 217 | 12x |
y <- h_coxreg_univar_extract( |
| 218 | 12x |
effect = effect, covar = covar, mod = mod, data = data, control = control |
| 219 |
) |
|
| 220 | 12x |
y$pval_inter <- NA |
| 221 | 12x |
y |
| 222 |
} else {
|
|
| 223 | 19x |
test_statistic <- c(wald = "Wald", likelihood = "LR")[control$pval_method] |
| 224 | ||
| 225 |
# Test the main treatment effect. |
|
| 226 | 19x |
mod_aov <- muffled_car_anova(mod, test_statistic) |
| 227 | 19x |
sum_anova <- broom::tidy(mod_aov) |
| 228 | 19x |
pval <- sum_anova[sum_anova$term == effect, ][["p.value"]] |
| 229 | ||
| 230 |
# Test the interaction effect. |
|
| 231 | 19x |
pval_inter <- sum_anova[grep(":", sum_anova$term), ][["p.value"]]
|
| 232 | 19x |
covar_test <- data.frame( |
| 233 | 19x |
effect = "Covariate:", |
| 234 | 19x |
term = covar, |
| 235 | 19x |
term_label = unname(labels_or_names(data[covar])), |
| 236 | 19x |
level = "", |
| 237 | 19x |
n = mod$n, hr = NA, lcl = NA, ucl = NA, pval = pval, |
| 238 | 19x |
pval_inter = pval_inter, |
| 239 | 19x |
stringsAsFactors = FALSE |
| 240 |
) |
|
| 241 |
# Estimate the interaction. |
|
| 242 | 19x |
y <- h_coxreg_inter_effect( |
| 243 | 19x |
data[[covar]], |
| 244 | 19x |
covar = covar, |
| 245 | 19x |
effect = effect, |
| 246 | 19x |
mod = mod, |
| 247 | 19x |
label = unname(labels_or_names(data[covar])), |
| 248 | 19x |
at = at, |
| 249 | 19x |
control = control, |
| 250 | 19x |
data = data |
| 251 |
) |
|
| 252 | 19x |
rbind(covar_test, y) |
| 253 |
} |
|
| 254 |
} |
|
| 255 | ||
| 256 |
#' @describeIn cox_regression_inter Hazard ratio estimation in interactions. |
|
| 257 |
#' |
|
| 258 |
#' @param variable,given (`string`)\cr the name of variables in interaction. We seek the estimation |
|
| 259 |
#' of the levels of `variable` given the levels of `given`. |
|
| 260 |
#' @param lvl_var,lvl_given (`character`)\cr corresponding levels as given by [levels()]. |
|
| 261 |
#' @param mod (`coxph`)\cr a fitted Cox regression model (see [survival::coxph()]). |
|
| 262 |
#' |
|
| 263 |
#' @details Given the cox regression investigating the effect of Arm (A, B, C; reference A) |
|
| 264 |
#' and Sex (F, M; reference Female) and the model being abbreviated: y ~ Arm + Sex + Arm:Sex. |
|
| 265 |
#' The cox regression estimates the coefficients along with a variance-covariance matrix for: |
|
| 266 |
#' |
|
| 267 |
#' - b1 (arm b), b2 (arm c) |
|
| 268 |
#' - b3 (sex m) |
|
| 269 |
#' - b4 (arm b: sex m), b5 (arm c: sex m) |
|
| 270 |
#' |
|
| 271 |
#' The estimation of the Hazard Ratio for arm C/sex M is given in reference |
|
| 272 |
#' to arm A/Sex M by exp(b2 + b3 + b5)/ exp(b3) = exp(b2 + b5). |
|
| 273 |
#' The interaction coefficient is deduced by b2 + b5 while the standard error |
|
| 274 |
#' is obtained as $sqrt(Var b2 + Var b5 + 2 * covariance (b2,b5))$. |
|
| 275 |
#' |
|
| 276 |
#' @return |
|
| 277 |
#' * `h_coxreg_inter_estimations()` returns a list of matrices (one per level of variable) with rows corresponding |
|
| 278 |
#' to the combinations of `variable` and `given`, with columns: |
|
| 279 |
#' * `coef_hat`: Estimation of the coefficient. |
|
| 280 |
#' * `coef_se`: Standard error of the estimation. |
|
| 281 |
#' * `hr`: Hazard ratio. |
|
| 282 |
#' * `lcl, ucl`: Lower/upper confidence limit of the hazard ratio. |
|
| 283 |
#' |
|
| 284 |
#' @examples |
|
| 285 |
#' mod <- coxph(Surv(time, status) ~ armcd * covar1, data = dta_bladder) |
|
| 286 |
#' result <- h_coxreg_inter_estimations( |
|
| 287 |
#' variable = "armcd", given = "covar1", |
|
| 288 |
#' lvl_var = levels(dta_bladder$armcd), |
|
| 289 |
#' lvl_given = levels(dta_bladder$covar1), |
|
| 290 |
#' mod = mod, conf_level = .95 |
|
| 291 |
#' ) |
|
| 292 |
#' result |
|
| 293 |
#' |
|
| 294 |
#' @export |
|
| 295 |
h_coxreg_inter_estimations <- function(variable, |
|
| 296 |
given, |
|
| 297 |
lvl_var, |
|
| 298 |
lvl_given, |
|
| 299 |
mod, |
|
| 300 |
conf_level = 0.95) {
|
|
| 301 | 18x |
var_lvl <- paste0(variable, lvl_var[-1]) # [-1]: reference level |
| 302 | 18x |
giv_lvl <- paste0(given, lvl_given) |
| 303 | 18x |
design_mat <- expand.grid(variable = var_lvl, given = giv_lvl) |
| 304 | 18x |
design_mat <- design_mat[order(design_mat$variable, design_mat$given), ] |
| 305 | 18x |
design_mat <- within( |
| 306 | 18x |
data = design_mat, |
| 307 | 18x |
expr = {
|
| 308 | 18x |
inter <- paste0(variable, ":", given) |
| 309 | 18x |
rev_inter <- paste0(given, ":", variable) |
| 310 |
} |
|
| 311 |
) |
|
| 312 | 18x |
split_by_variable <- design_mat$variable |
| 313 | 18x |
interaction_names <- paste(design_mat$variable, design_mat$given, sep = "/") |
| 314 | ||
| 315 | 18x |
mmat <- stats::model.matrix(mod)[1, ] |
| 316 | 18x |
mmat[!mmat == 0] <- 0 |
| 317 | ||
| 318 | 18x |
design_mat <- apply( |
| 319 | 18x |
X = design_mat, MARGIN = 1, FUN = function(x) {
|
| 320 | 52x |
mmat[names(mmat) %in% x[-which(names(x) == "given")]] <- 1 |
| 321 | 52x |
mmat |
| 322 |
} |
|
| 323 |
) |
|
| 324 | 18x |
colnames(design_mat) <- interaction_names |
| 325 | ||
| 326 | 18x |
coef <- stats::coef(mod) |
| 327 | 18x |
vcov <- stats::vcov(mod) |
| 328 | 18x |
betas <- as.matrix(coef) |
| 329 | 18x |
coef_hat <- t(design_mat) %*% betas |
| 330 | 18x |
dimnames(coef_hat)[2] <- "coef" |
| 331 | 18x |
coef_se <- apply( |
| 332 | 18x |
design_mat, 2, |
| 333 | 18x |
function(x) {
|
| 334 | 52x |
vcov_el <- as.logical(x) |
| 335 | 52x |
y <- vcov[vcov_el, vcov_el] |
| 336 | 52x |
y <- sum(y) |
| 337 | 52x |
y <- sqrt(y) |
| 338 | 52x |
return(y) |
| 339 |
} |
|
| 340 |
) |
|
| 341 | 18x |
q_norm <- stats::qnorm((1 + conf_level) / 2) |
| 342 | 18x |
y <- cbind(coef_hat, `se(coef)` = coef_se) |
| 343 | 18x |
y <- apply(y, 1, function(x) {
|
| 344 | 52x |
x["hr"] <- exp(x["coef"]) |
| 345 | 52x |
x["lcl"] <- exp(x["coef"] - q_norm * x["se(coef)"]) |
| 346 | 52x |
x["ucl"] <- exp(x["coef"] + q_norm * x["se(coef)"]) |
| 347 | 52x |
x |
| 348 |
}) |
|
| 349 | 18x |
y <- t(y) |
| 350 | 18x |
y <- by(y, split_by_variable, identity) |
| 351 | 18x |
y <- lapply(y, as.matrix) |
| 352 | 18x |
attr(y, "details") <- paste0( |
| 353 | 18x |
"Estimations of ", variable, |
| 354 | 18x |
" hazard ratio given the level of ", given, " compared to ", |
| 355 | 18x |
variable, " level ", lvl_var[1], "." |
| 356 |
) |
|
| 357 | 18x |
y |
| 358 |
} |
| 1 |
#' Count patients by most extreme post-baseline toxicity grade per direction of abnormality |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_abnormal_by_worst_grade()] creates a layout element to count patients by highest (worst) |
|
| 6 |
#' analysis toxicity grade post-baseline for each direction, categorized by parameter value. |
|
| 7 |
#' |
|
| 8 |
#' This function analyzes primary analysis variable `var` which indicates toxicity grades. Additional |
|
| 9 |
#' analysis variables that can be supplied as a list via the `variables` parameter are `id` (defaults to |
|
| 10 |
#' `USUBJID`), a variable to indicate unique subject identifiers, `param` (defaults to `PARAM`), a variable |
|
| 11 |
#' to indicate parameter values, and `grade_dir` (defaults to `GRADE_DIR`), a variable to indicate directions |
|
| 12 |
#' (e.g. High or Low) for each toxicity grade supplied in `var`. |
|
| 13 |
#' |
|
| 14 |
#' For each combination of `param` and `grade_dir` levels, patient counts by worst |
|
| 15 |
#' grade are calculated as follows: |
|
| 16 |
#' * `1` to `4`: The number of patients with worst grades 1-4, respectively. |
|
| 17 |
#' * `Any`: The number of patients with at least one abnormality (i.e. grade is not 0). |
|
| 18 |
#' |
|
| 19 |
#' Fractions are calculated by dividing the above counts by the number of patients with at least one |
|
| 20 |
#' valid measurement recorded during treatment. |
|
| 21 |
#' |
|
| 22 |
#' Pre-processing is crucial when using this function and can be done automatically using the |
|
| 23 |
#' [h_adlb_abnormal_by_worst_grade()] helper function. See the description of this function for details on the |
|
| 24 |
#' necessary pre-processing steps. |
|
| 25 |
#' |
|
| 26 |
#' Prior to using this function in your table layout you must use [rtables::split_rows_by()] to create two row |
|
| 27 |
#' splits, one on variable `param` and one on variable `grade_dir`. |
|
| 28 |
#' |
|
| 29 |
#' @inheritParams argument_convention |
|
| 30 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 31 |
#' |
|
| 32 |
#' Options are: ``r shQuote(get_stats("abnormal_by_worst_grade"), type = "sh")``
|
|
| 33 |
#' |
|
| 34 |
#' @seealso [h_adlb_abnormal_by_worst_grade()] which pre-processes ADLB data frames to be used in |
|
| 35 |
#' [count_abnormal_by_worst_grade()]. |
|
| 36 |
#' |
|
| 37 |
#' @name abnormal_by_worst_grade |
|
| 38 |
#' @order 1 |
|
| 39 |
NULL |
|
| 40 | ||
| 41 |
#' @describeIn abnormal_by_worst_grade Statistics function which counts patients by worst grade. |
|
| 42 |
#' |
|
| 43 |
#' @return |
|
| 44 |
#' * `s_count_abnormal_by_worst_grade()` returns the single statistic `count_fraction` with grades 1 to 4 and |
|
| 45 |
#' "Any" results. |
|
| 46 |
#' |
|
| 47 |
#' @keywords internal |
|
| 48 |
s_count_abnormal_by_worst_grade <- function(df, |
|
| 49 |
.var = "GRADE_ANL", |
|
| 50 |
.spl_context, |
|
| 51 |
variables = list( |
|
| 52 |
id = "USUBJID", |
|
| 53 |
param = "PARAM", |
|
| 54 |
grade_dir = "GRADE_DIR" |
|
| 55 |
), |
|
| 56 |
...) {
|
|
| 57 | 5x |
checkmate::assert_string(.var) |
| 58 | 5x |
assert_valid_factor(df[[.var]]) |
| 59 | 5x |
assert_valid_factor(df[[variables$param]]) |
| 60 | 4x |
assert_valid_factor(df[[variables$grade_dir]]) |
| 61 | 4x |
assert_df_with_variables(df, c(a = .var, variables)) |
| 62 | 4x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character"))
|
| 63 | ||
| 64 |
# To verify that the `split_rows_by` are performed with correct variables. |
|
| 65 | 4x |
checkmate::assert_subset(c(variables[["param"]], variables[["grade_dir"]]), .spl_context$split) |
| 66 | 4x |
first_row <- .spl_context[.spl_context$split == variables[["param"]], ] |
| 67 | 4x |
x_lvls <- c(setdiff(levels(df[[.var]]), "0"), "Any") |
| 68 | 4x |
result <- split(numeric(0), factor(x_lvls)) |
| 69 | ||
| 70 | 4x |
subj <- first_row$full_parent_df[[1]][[variables[["id"]]]] |
| 71 | 4x |
subj_cur_col <- subj[first_row$cur_col_subset[[1]]] |
| 72 |
# Some subjects may have a record for high and low directions but |
|
| 73 |
# should be counted only once. |
|
| 74 | 4x |
denom <- length(unique(subj_cur_col)) |
| 75 | ||
| 76 | 4x |
for (lvl in x_lvls) {
|
| 77 | 20x |
if (lvl != "Any") {
|
| 78 | 16x |
df_lvl <- df[df[[.var]] == lvl, ] |
| 79 |
} else {
|
|
| 80 | 4x |
df_lvl <- df[df[[.var]] != 0, ] |
| 81 |
} |
|
| 82 | 20x |
num <- length(unique(df_lvl[[variables[["id"]]]])) |
| 83 | 20x |
fraction <- ifelse(denom == 0, 0, num / denom) |
| 84 | 20x |
result[[lvl]] <- formatters::with_label(c(count = num, fraction = fraction), lvl) |
| 85 |
} |
|
| 86 | ||
| 87 | 4x |
result <- list(count_fraction = result) |
| 88 | 4x |
result |
| 89 |
} |
|
| 90 | ||
| 91 |
#' @describeIn abnormal_by_worst_grade Formatted analysis function which is used as `afun` |
|
| 92 |
#' in `count_abnormal_by_worst_grade()`. |
|
| 93 |
#' |
|
| 94 |
#' @return |
|
| 95 |
#' * `a_count_abnormal_by_worst_grade()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 96 |
#' |
|
| 97 |
#' @keywords internal |
|
| 98 |
a_count_abnormal_by_worst_grade <- function(df, |
|
| 99 |
..., |
|
| 100 |
.stats = NULL, |
|
| 101 |
.stat_names = NULL, |
|
| 102 |
.formats = NULL, |
|
| 103 |
.labels = NULL, |
|
| 104 |
.indent_mods = NULL) {
|
|
| 105 |
# Check for additional parameters to the statistics function |
|
| 106 | 4x |
dots_extra_args <- list(...) |
| 107 | 4x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 108 | 4x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 109 | ||
| 110 |
# Check for user-defined functions |
|
| 111 | 4x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 112 | 4x |
.stats <- default_and_custom_stats_list$all_stats |
| 113 | 4x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 114 | ||
| 115 |
# Apply statistics function |
|
| 116 | 4x |
x_stats <- .apply_stat_functions( |
| 117 | 4x |
default_stat_fnc = s_count_abnormal_by_worst_grade, |
| 118 | 4x |
custom_stat_fnc_list = custom_stat_functions, |
| 119 | 4x |
args_list = c( |
| 120 | 4x |
df = list(df), |
| 121 | 4x |
extra_afun_params, |
| 122 | 4x |
dots_extra_args |
| 123 |
) |
|
| 124 |
) |
|
| 125 | ||
| 126 |
# Fill in formatting defaults |
|
| 127 | 3x |
.stats <- get_stats("abnormal_by_worst_grade", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 128 | 3x |
levels_per_stats <- lapply(x_stats, names) |
| 129 | 3x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 130 | 3x |
.labels <- get_labels_from_stats(.stats, .labels, levels_per_stats) |
| 131 | 3x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 132 | ||
| 133 | 3x |
x_stats <- x_stats[.stats] %>% |
| 134 | 3x |
.unlist_keep_nulls() %>% |
| 135 | 3x |
setNames(names(.formats)) |
| 136 | ||
| 137 |
# Auto format handling |
|
| 138 | 3x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 139 | ||
| 140 |
# Get and check statistical names |
|
| 141 | 3x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 142 | ||
| 143 | 3x |
in_rows( |
| 144 | 3x |
.list = x_stats, |
| 145 | 3x |
.formats = .formats, |
| 146 | 3x |
.names = .labels %>% .unlist_keep_nulls(), |
| 147 | 3x |
.stat_names = .stat_names, |
| 148 | 3x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 149 | 3x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 150 |
) |
|
| 151 |
} |
|
| 152 | ||
| 153 |
#' @describeIn abnormal_by_worst_grade Layout-creating function which can take statistics function arguments |
|
| 154 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 155 |
#' |
|
| 156 |
#' @return |
|
| 157 |
#' * `count_abnormal_by_worst_grade()` returns a layout object suitable for passing to further layouting functions, |
|
| 158 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 159 |
#' the statistics from `s_count_abnormal_by_worst_grade()` to the table layout. |
|
| 160 |
#' |
|
| 161 |
#' @examples |
|
| 162 |
#' library(dplyr) |
|
| 163 |
#' library(forcats) |
|
| 164 |
#' adlb <- tern_ex_adlb |
|
| 165 |
#' |
|
| 166 |
#' # Data is modified in order to have some parameters with grades only in one direction |
|
| 167 |
#' # and simulate the real data. |
|
| 168 |
#' adlb$ATOXGR[adlb$PARAMCD == "ALT" & adlb$ATOXGR %in% c("1", "2", "3", "4")] <- "-1"
|
|
| 169 |
#' adlb$ANRIND[adlb$PARAMCD == "ALT" & adlb$ANRIND == "HIGH"] <- "LOW" |
|
| 170 |
#' adlb$WGRHIFL[adlb$PARAMCD == "ALT"] <- "" |
|
| 171 |
#' |
|
| 172 |
#' adlb$ATOXGR[adlb$PARAMCD == "IGA" & adlb$ATOXGR %in% c("-1", "-2", "-3", "-4")] <- "1"
|
|
| 173 |
#' adlb$ANRIND[adlb$PARAMCD == "IGA" & adlb$ANRIND == "LOW"] <- "HIGH" |
|
| 174 |
#' adlb$WGRLOFL[adlb$PARAMCD == "IGA"] <- "" |
|
| 175 |
#' |
|
| 176 |
#' # Pre-processing |
|
| 177 |
#' adlb_f <- adlb %>% h_adlb_abnormal_by_worst_grade() |
|
| 178 |
#' |
|
| 179 |
#' # Map excludes records without abnormal grade since they should not be displayed |
|
| 180 |
#' # in the table. |
|
| 181 |
#' map <- unique(adlb_f[adlb_f$GRADE_DIR != "ZERO", c("PARAM", "GRADE_DIR", "GRADE_ANL")]) %>%
|
|
| 182 |
#' lapply(as.character) %>% |
|
| 183 |
#' as.data.frame() %>% |
|
| 184 |
#' arrange(PARAM, desc(GRADE_DIR), GRADE_ANL) |
|
| 185 |
#' |
|
| 186 |
#' basic_table() %>% |
|
| 187 |
#' split_cols_by("ARMCD") %>%
|
|
| 188 |
#' split_rows_by("PARAM") %>%
|
|
| 189 |
#' split_rows_by("GRADE_DIR", split_fun = trim_levels_to_map(map)) %>%
|
|
| 190 |
#' count_abnormal_by_worst_grade( |
|
| 191 |
#' var = "GRADE_ANL", |
|
| 192 |
#' variables = list(id = "USUBJID", param = "PARAM", grade_dir = "GRADE_DIR") |
|
| 193 |
#' ) %>% |
|
| 194 |
#' build_table(df = adlb_f) |
|
| 195 |
#' |
|
| 196 |
#' @export |
|
| 197 |
#' @order 2 |
|
| 198 |
count_abnormal_by_worst_grade <- function(lyt, |
|
| 199 |
var, |
|
| 200 |
variables = list( |
|
| 201 |
id = "USUBJID", |
|
| 202 |
param = "PARAM", |
|
| 203 |
grade_dir = "GRADE_DIR" |
|
| 204 |
), |
|
| 205 |
na_str = default_na_str(), |
|
| 206 |
nested = TRUE, |
|
| 207 |
..., |
|
| 208 |
.stats = "count_fraction", |
|
| 209 |
.stat_names = NULL, |
|
| 210 |
.formats = list(count_fraction = format_count_fraction), |
|
| 211 |
.labels = NULL, |
|
| 212 |
.indent_mods = NULL) {
|
|
| 213 |
# Process standard extra arguments |
|
| 214 | 2x |
extra_args <- list(".stats" = .stats)
|
| 215 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 216 | 2x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 217 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 218 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 219 | ||
| 220 |
# Process additional arguments to the statistic function |
|
| 221 | 2x |
extra_args <- c(extra_args, "variables" = list(variables), ...) |
| 222 | ||
| 223 |
# Append additional info from layout to the analysis function |
|
| 224 | 2x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 225 | 2x |
formals(a_count_abnormal_by_worst_grade) <- c( |
| 226 | 2x |
formals(a_count_abnormal_by_worst_grade), extra_args[[".additional_fun_parameters"]] |
| 227 |
) |
|
| 228 | ||
| 229 | 2x |
analyze( |
| 230 | 2x |
lyt = lyt, |
| 231 | 2x |
vars = var, |
| 232 | 2x |
afun = a_count_abnormal_by_worst_grade, |
| 233 | 2x |
na_str = na_str, |
| 234 | 2x |
nested = nested, |
| 235 | 2x |
extra_args = extra_args, |
| 236 | 2x |
show_labels = "hidden" |
| 237 |
) |
|
| 238 |
} |
|
| 239 | ||
| 240 |
#' Helper function to prepare ADLB for `count_abnormal_by_worst_grade()` |
|
| 241 |
#' |
|
| 242 |
#' @description `r lifecycle::badge("stable")`
|
|
| 243 |
#' |
|
| 244 |
#' Helper function to prepare an ADLB data frame to be used as input in |
|
| 245 |
#' [count_abnormal_by_worst_grade()]. The following pre-processing steps are applied: |
|
| 246 |
#' |
|
| 247 |
#' 1. `adlb` is filtered on variable `avisit` to only include post-baseline visits. |
|
| 248 |
#' 2. `adlb` is filtered on variables `worst_flag_low` and `worst_flag_high` so that only |
|
| 249 |
#' worst grades (in either direction) are included. |
|
| 250 |
#' 3. From the standard lab grade variable `atoxgr`, the following two variables are derived |
|
| 251 |
#' and added to `adlb`: |
|
| 252 |
#' * A grade direction variable (e.g. `GRADE_DIR`). The variable takes value `"HIGH"` when |
|
| 253 |
#' `atoxgr > 0`, `"LOW"` when `atoxgr < 0`, and `"ZERO"` otherwise. |
|
| 254 |
#' * A toxicity grade variable (e.g. `GRADE_ANL`) where all negative values from `atoxgr` are |
|
| 255 |
#' replaced by their absolute values. |
|
| 256 |
#' 4. Unused factor levels are dropped from `adlb` via [droplevels()]. |
|
| 257 |
#' |
|
| 258 |
#' @param adlb (`data.frame`)\cr ADLB data frame. |
|
| 259 |
#' @param atoxgr (`string`)\cr name of the analysis toxicity grade variable. This must be a `factor` |
|
| 260 |
#' variable. |
|
| 261 |
#' @param avisit (`string`)\cr name of the analysis visit variable. |
|
| 262 |
#' @param worst_flag_low (`string`)\cr name of the worst low lab grade flag variable. This variable is |
|
| 263 |
#' set to `"Y"` when indicating records of worst low lab grades. |
|
| 264 |
#' @param worst_flag_high (`string`)\cr name of the worst high lab grade flag variable. This variable is |
|
| 265 |
#' set to `"Y"` when indicating records of worst high lab grades. |
|
| 266 |
#' |
|
| 267 |
#' @return `h_adlb_abnormal_by_worst_grade()` returns the `adlb` data frame with two new |
|
| 268 |
#' variables: `GRADE_DIR` and `GRADE_ANL`. |
|
| 269 |
#' |
|
| 270 |
#' @seealso [abnormal_by_worst_grade] |
|
| 271 |
#' |
|
| 272 |
#' @examples |
|
| 273 |
#' h_adlb_abnormal_by_worst_grade(tern_ex_adlb) %>% |
|
| 274 |
#' dplyr::select(ATOXGR, GRADE_DIR, GRADE_ANL) %>% |
|
| 275 |
#' head(10) |
|
| 276 |
#' |
|
| 277 |
#' @export |
|
| 278 |
h_adlb_abnormal_by_worst_grade <- function(adlb, |
|
| 279 |
atoxgr = "ATOXGR", |
|
| 280 |
avisit = "AVISIT", |
|
| 281 |
worst_flag_low = "WGRLOFL", |
|
| 282 |
worst_flag_high = "WGRHIFL") {
|
|
| 283 | 1x |
adlb %>% |
| 284 | 1x |
dplyr::filter( |
| 285 | 1x |
!.data[[avisit]] %in% c("SCREENING", "BASELINE"),
|
| 286 | 1x |
.data[[worst_flag_low]] == "Y" | .data[[worst_flag_high]] == "Y" |
| 287 |
) %>% |
|
| 288 | 1x |
dplyr::mutate( |
| 289 | 1x |
GRADE_DIR = factor( |
| 290 | 1x |
dplyr::case_when( |
| 291 | 1x |
.data[[atoxgr]] %in% c("-1", "-2", "-3", "-4") ~ "LOW",
|
| 292 | 1x |
.data[[atoxgr]] == "0" ~ "ZERO", |
| 293 | 1x |
.data[[atoxgr]] %in% c("1", "2", "3", "4") ~ "HIGH"
|
| 294 |
), |
|
| 295 | 1x |
levels = c("LOW", "ZERO", "HIGH")
|
| 296 |
), |
|
| 297 | 1x |
GRADE_ANL = forcats::fct_relevel( |
| 298 | 1x |
forcats::fct_recode(.data[[atoxgr]], `1` = "-1", `2` = "-2", `3` = "-3", `4` = "-4"), |
| 299 | 1x |
c("0", "1", "2", "3", "4")
|
| 300 |
) |
|
| 301 |
) %>% |
|
| 302 | 1x |
droplevels() |
| 303 |
} |
| 1 |
#' Tabulate binary response by subgroup |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The [tabulate_rsp_subgroups()] function creates a layout element to tabulate binary response by subgroup, returning |
|
| 6 |
#' statistics including response rate and odds ratio for each population subgroup. The table is created from `df`, a |
|
| 7 |
#' list of data frames returned by [extract_rsp_subgroups()], with the statistics to include specified via the `vars` |
|
| 8 |
#' parameter. |
|
| 9 |
#' |
|
| 10 |
#' A forest plot can be created from the resulting table using the [g_forest()] function. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams extract_rsp_subgroups |
|
| 13 |
#' @inheritParams argument_convention |
|
| 14 |
#' |
|
| 15 |
#' @details These functions create a layout starting from a data frame which contains |
|
| 16 |
#' the required statistics. Tables typically used as part of forest plot. |
|
| 17 |
#' |
|
| 18 |
#' @seealso [extract_rsp_subgroups()] |
|
| 19 |
#' |
|
| 20 |
#' @examples |
|
| 21 |
#' library(dplyr) |
|
| 22 |
#' library(forcats) |
|
| 23 |
#' |
|
| 24 |
#' adrs <- tern_ex_adrs |
|
| 25 |
#' adrs_labels <- formatters::var_labels(adrs) |
|
| 26 |
#' |
|
| 27 |
#' adrs_f <- adrs %>% |
|
| 28 |
#' filter(PARAMCD == "BESRSPI") %>% |
|
| 29 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
| 30 |
#' droplevels() %>% |
|
| 31 |
#' mutate( |
|
| 32 |
#' # Reorder levels of factor to make the placebo group the reference arm. |
|
| 33 |
#' ARM = fct_relevel(ARM, "B: Placebo"), |
|
| 34 |
#' rsp = AVALC == "CR" |
|
| 35 |
#' ) |
|
| 36 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response") |
|
| 37 |
#' |
|
| 38 |
#' # Unstratified analysis. |
|
| 39 |
#' df <- extract_rsp_subgroups( |
|
| 40 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
| 41 |
#' data = adrs_f |
|
| 42 |
#' ) |
|
| 43 |
#' df |
|
| 44 |
#' |
|
| 45 |
#' # Stratified analysis. |
|
| 46 |
#' df_strat <- extract_rsp_subgroups( |
|
| 47 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2"), strata = "STRATA1"),
|
|
| 48 |
#' data = adrs_f |
|
| 49 |
#' ) |
|
| 50 |
#' df_strat |
|
| 51 |
#' |
|
| 52 |
#' # Grouping of the BMRKR2 levels. |
|
| 53 |
#' df_grouped <- extract_rsp_subgroups( |
|
| 54 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "BMRKR2")),
|
|
| 55 |
#' data = adrs_f, |
|
| 56 |
#' groups_lists = list( |
|
| 57 |
#' BMRKR2 = list( |
|
| 58 |
#' "low" = "LOW", |
|
| 59 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
| 60 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
| 61 |
#' ) |
|
| 62 |
#' ) |
|
| 63 |
#' ) |
|
| 64 |
#' df_grouped |
|
| 65 |
#' |
|
| 66 |
#' @name response_subgroups |
|
| 67 |
#' @order 1 |
|
| 68 |
NULL |
|
| 69 | ||
| 70 |
#' Prepare response data for population subgroups in data frames |
|
| 71 |
#' |
|
| 72 |
#' @description `r lifecycle::badge("stable")`
|
|
| 73 |
#' |
|
| 74 |
#' Prepares response rates and odds ratios for population subgroups in data frames. Simple wrapper |
|
| 75 |
#' for [h_odds_ratio_subgroups_df()] and [h_proportion_subgroups_df()]. Result is a list of two |
|
| 76 |
#' `data.frames`: `prop` and `or`. `variables` corresponds to the names of variables found in `data`, |
|
| 77 |
#' passed as a named `list` and requires elements `rsp`, `arm` and optionally `subgroups` and `strata`. |
|
| 78 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables. |
|
| 79 |
#' |
|
| 80 |
#' @inheritParams argument_convention |
|
| 81 |
#' @inheritParams response_subgroups |
|
| 82 |
#' @param label_all (`string`)\cr label for the total population analysis. |
|
| 83 |
#' |
|
| 84 |
#' @return A named list of two elements: |
|
| 85 |
#' * `prop`: A `data.frame` containing columns `arm`, `n`, `n_rsp`, `prop`, `subgroup`, `var`, |
|
| 86 |
#' `var_label`, and `row_type`. |
|
| 87 |
#' * `or`: A `data.frame` containing columns `arm`, `n_tot`, `or`, `lcl`, `ucl`, `conf_level`, |
|
| 88 |
#' `subgroup`, `var`, `var_label`, and `row_type`. |
|
| 89 |
#' |
|
| 90 |
#' @seealso [response_subgroups] |
|
| 91 |
#' |
|
| 92 |
#' @export |
|
| 93 |
extract_rsp_subgroups <- function(variables, |
|
| 94 |
data, |
|
| 95 |
groups_lists = list(), |
|
| 96 |
conf_level = 0.95, |
|
| 97 |
method = NULL, |
|
| 98 |
label_all = "All Patients") {
|
|
| 99 | 14x |
if ("strat" %in% names(variables)) {
|
| 100 | ! |
warning( |
| 101 | ! |
"Warning: the `strat` element name of the `variables` list argument to `extract_rsp_subgroups() ", |
| 102 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 103 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 104 |
) |
|
| 105 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 106 |
} |
|
| 107 | ||
| 108 | 14x |
df_prop <- h_proportion_subgroups_df( |
| 109 | 14x |
variables, |
| 110 | 14x |
data, |
| 111 | 14x |
groups_lists = groups_lists, |
| 112 | 14x |
label_all = label_all |
| 113 |
) |
|
| 114 | 14x |
df_or <- h_odds_ratio_subgroups_df( |
| 115 | 14x |
variables, |
| 116 | 14x |
data, |
| 117 | 14x |
groups_lists = groups_lists, |
| 118 | 14x |
conf_level = conf_level, |
| 119 | 14x |
method = method, |
| 120 | 14x |
label_all = label_all |
| 121 |
) |
|
| 122 | ||
| 123 | 14x |
list(prop = df_prop, or = df_or) |
| 124 |
} |
|
| 125 | ||
| 126 |
#' @describeIn response_subgroups Formatted analysis function which is used as `afun` in `tabulate_rsp_subgroups()`. |
|
| 127 |
#' |
|
| 128 |
#' @return |
|
| 129 |
#' * `a_response_subgroups()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 130 |
#' |
|
| 131 |
#' @keywords internal |
|
| 132 |
a_response_subgroups <- function(df, |
|
| 133 |
labelstr = "", |
|
| 134 |
..., |
|
| 135 |
.stats = NULL, |
|
| 136 |
.stat_names = NULL, |
|
| 137 |
.formats = NULL, |
|
| 138 |
.labels = NULL, |
|
| 139 |
.indent_mods = NULL) {
|
|
| 140 |
# Check for additional parameters to the statistics function |
|
| 141 | 375x |
dots_extra_args <- list(...) |
| 142 | 375x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 143 | 375x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 144 | 375x |
cur_col_stat <- extra_afun_params$.var %||% .stats |
| 145 | ||
| 146 |
# Uniquely name & label rows |
|
| 147 | 375x |
var_lvls <- if ("biomarker" %in% names(dots_extra_args) && "biomarker" %in% names(df)) {
|
| 148 | 90x |
if ("overall" %in% names(dots_extra_args)) { # label rows for (nested) biomarker tables - e.g. "AGE", "BMRKR1"
|
| 149 | 42x |
as.character(df$biomarker) |
| 150 | 375x |
} else { # data rows for (nested) biomarker tables - e.g. "AGE.LOW", "BMRKR1.Total Patients"
|
| 151 | 48x |
paste(as.character(df$biomarker), as.character(df$subgroup), sep = ".") |
| 152 |
} |
|
| 153 | 375x |
} else { # data rows for non-biomarker tables - e.g. "Total Patients", "F", "M"
|
| 154 | 285x |
make.unique(as.character(df$subgroup)) |
| 155 |
} |
|
| 156 | ||
| 157 |
# if empty, return NA |
|
| 158 | 375x |
if (nrow(df) == 0) {
|
| 159 | 1x |
return(in_rows(.list = list(NA) %>% stats::setNames(cur_col_stat))) |
| 160 |
} |
|
| 161 | ||
| 162 |
# Main statistics taken from df |
|
| 163 | 374x |
x_stats <- as.list(df) |
| 164 | ||
| 165 |
# Fill in formatting defaults |
|
| 166 | 374x |
.stats <- get_stats("tabulate_rsp_subgroups", stats_in = cur_col_stat)
|
| 167 | 374x |
levels_per_stats <- rep(list(var_lvls), length(.stats)) %>% setNames(.stats) |
| 168 | 374x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 169 | 374x |
.labels <- get_labels_from_stats( |
| 170 | 374x |
.stats, .labels, levels_per_stats, |
| 171 |
# default labels are pre-determined in extract_*() function |
|
| 172 | 374x |
tern_defaults = as.list(as.character(df$subgroup)) %>% setNames(var_lvls) |
| 173 |
) |
|
| 174 | 374x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 175 | ||
| 176 | 374x |
x_stats <- lapply( |
| 177 | 374x |
.stats, |
| 178 | 374x |
function(x) x_stats[[x]] %>% stats::setNames(var_lvls) |
| 179 |
) %>% |
|
| 180 | 374x |
stats::setNames(.stats) %>% |
| 181 | 374x |
.unlist_keep_nulls() |
| 182 | ||
| 183 | 374x |
.nms <- if ("biomarker" %in% names(dots_extra_args)) var_lvls else names(.labels)
|
| 184 | ||
| 185 |
# Auto format handling |
|
| 186 | 374x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 187 | ||
| 188 |
# Get and check statistical names |
|
| 189 | 374x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 190 | ||
| 191 | 374x |
in_rows( |
| 192 | 374x |
.list = x_stats, |
| 193 | 374x |
.formats = .formats, |
| 194 | 374x |
.names = .nms, |
| 195 | 374x |
.stat_names = .stat_names, |
| 196 | 374x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 197 | 374x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 198 |
) |
|
| 199 |
} |
|
| 200 | ||
| 201 |
#' @describeIn response_subgroups Table-creating function which creates a table |
|
| 202 |
#' summarizing binary response by subgroup. This function is a wrapper for [rtables::analyze_colvars()] |
|
| 203 |
#' and [rtables::summarize_row_groups()]. |
|
| 204 |
#' |
|
| 205 |
#' @param df (`list`)\cr a list of data frames containing all analysis variables. List should be |
|
| 206 |
#' created using [extract_rsp_subgroups()]. |
|
| 207 |
#' @param vars (`character`)\cr the names of statistics to be reported among: |
|
| 208 |
#' * `n`: Total number of observations per group. |
|
| 209 |
#' * `n_rsp`: Number of responders per group. |
|
| 210 |
#' * `prop`: Proportion of responders. |
|
| 211 |
#' * `n_tot`: Total number of observations. |
|
| 212 |
#' * `or`: Odds ratio. |
|
| 213 |
#' * `ci` : Confidence interval of odds ratio. |
|
| 214 |
#' * `pval`: p-value of the effect. |
|
| 215 |
#' Note, the statistics `n_tot`, `or`, and `ci` are required. |
|
| 216 |
#' @param riskdiff (`list`)\cr if a risk (proportion) difference column should be added, a list of settings to apply |
|
| 217 |
#' within the column. See [control_riskdiff()] for details. If `NULL`, no risk difference column will be added. If |
|
| 218 |
#' `riskdiff$arm_x` and `riskdiff$arm_y` are `NULL`, the first level of `df$prop$arm` will be used as `arm_x` and |
|
| 219 |
#' the second level as `arm_y`. |
|
| 220 |
#' |
|
| 221 |
#' @return An `rtables` table summarizing binary response by subgroup. |
|
| 222 |
#' |
|
| 223 |
#' @examples |
|
| 224 |
#' # Table with default columns |
|
| 225 |
#' basic_table() %>% |
|
| 226 |
#' tabulate_rsp_subgroups(df) |
|
| 227 |
#' |
|
| 228 |
#' # Table with selected columns |
|
| 229 |
#' basic_table() %>% |
|
| 230 |
#' tabulate_rsp_subgroups( |
|
| 231 |
#' df = df, |
|
| 232 |
#' vars = c("n_tot", "n", "n_rsp", "prop", "or", "ci")
|
|
| 233 |
#' ) |
|
| 234 |
#' |
|
| 235 |
#' # Table with risk difference column added |
|
| 236 |
#' basic_table() %>% |
|
| 237 |
#' tabulate_rsp_subgroups( |
|
| 238 |
#' df, |
|
| 239 |
#' riskdiff = control_riskdiff( |
|
| 240 |
#' arm_x = levels(df$prop$arm)[1], |
|
| 241 |
#' arm_y = levels(df$prop$arm)[2] |
|
| 242 |
#' ) |
|
| 243 |
#' ) |
|
| 244 |
#' |
|
| 245 |
#' @export |
|
| 246 |
#' @order 2 |
|
| 247 |
tabulate_rsp_subgroups <- function(lyt, |
|
| 248 |
df, |
|
| 249 |
vars = c("n_tot", "n", "prop", "or", "ci"),
|
|
| 250 |
groups_lists = list(), |
|
| 251 |
label_all = lifecycle::deprecated(), |
|
| 252 |
riskdiff = NULL, |
|
| 253 |
na_str = default_na_str(), |
|
| 254 |
..., |
|
| 255 |
.stat_names = NULL, |
|
| 256 |
.formats = NULL, |
|
| 257 |
.labels = NULL, |
|
| 258 |
.indent_mods = NULL) {
|
|
| 259 | 14x |
checkmate::assert_list(riskdiff, null.ok = TRUE) |
| 260 | 14x |
checkmate::assert_true(all(c("n_tot", "or", "ci") %in% vars))
|
| 261 | 14x |
if ("pval" %in% vars && !"pval" %in% names(df$or)) {
|
| 262 | 1x |
warning( |
| 263 | 1x |
'The "pval" statistic has been selected but is not present in "df" so it will not be included in the output ', |
| 264 | 1x |
'table. To include the "pval" statistic, please specify a p-value test when generating "df" via ', |
| 265 | 1x |
'the "method" argument to `extract_rsp_subgroups()`. If method = "cmh", strata must also be specified via the ', |
| 266 | 1x |
'"variables" argument to `extract_rsp_subgroups()`.' |
| 267 |
) |
|
| 268 |
} |
|
| 269 | ||
| 270 | 14x |
if (lifecycle::is_present(label_all)) {
|
| 271 | ! |
lifecycle::deprecate_warn( |
| 272 | ! |
"0.9.8", "tabulate_rsp_subgroups(label_all)", |
| 273 | ! |
details = |
| 274 | ! |
"Please assign the `label_all` parameter within the `extract_rsp_subgroups()` function when creating `df`." |
| 275 |
) |
|
| 276 |
} |
|
| 277 | ||
| 278 |
# Process standard extra arguments |
|
| 279 | 14x |
extra_args <- list(".stats" = vars)
|
| 280 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 281 | 1x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 282 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 283 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 284 | ||
| 285 |
# Create "ci" column from "lcl" and "ucl" |
|
| 286 | 14x |
df$or$ci <- combine_vectors(df$or$lcl, df$or$ucl) |
| 287 | ||
| 288 |
# Extract additional parameters from df |
|
| 289 | 14x |
conf_level <- df$or$conf_level[1] |
| 290 | 14x |
method <- if ("pval_label" %in% names(df$or)) df$or$pval_label[1] else NULL
|
| 291 | 14x |
colvars <- d_rsp_subgroups_colvars(vars, conf_level = conf_level, method = method) |
| 292 | 14x |
prop_vars <- intersect(colvars$vars, c("n", "prop", "n_rsp"))
|
| 293 | 14x |
or_vars <- intersect(names(colvars$labels), c("n_tot", "or", "ci", "pval"))
|
| 294 | 14x |
colvars_prop <- list(vars = prop_vars, labels = colvars$labels[prop_vars]) |
| 295 | 14x |
colvars_or <- list(vars = or_vars, labels = colvars$labels[or_vars]) |
| 296 | ||
| 297 |
# Process additional arguments to the statistic function |
|
| 298 | 14x |
extra_args <- c( |
| 299 | 14x |
extra_args, |
| 300 | 14x |
groups_lists = list(groups_lists), conf_level = conf_level, method = method, |
| 301 |
... |
|
| 302 |
) |
|
| 303 | ||
| 304 |
# Adding additional info from layout to analysis function |
|
| 305 | 14x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 306 | 14x |
formals(a_response_subgroups) <- c(formals(a_response_subgroups), extra_args[[".additional_fun_parameters"]]) |
| 307 | ||
| 308 |
# Add risk difference column |
|
| 309 | 14x |
if (!is.null(riskdiff)) {
|
| 310 | ! |
if (is.null(riskdiff$arm_x)) riskdiff$arm_x <- levels(df$prop$arm)[1] |
| 311 | ! |
if (is.null(riskdiff$arm_y)) riskdiff$arm_y <- levels(df$prop$arm)[2] |
| 312 | 2x |
colvars_or$vars <- c(colvars_or$vars, "riskdiff") |
| 313 | 2x |
colvars_or$labels <- c(colvars_or$labels, riskdiff = riskdiff$col_label) |
| 314 | 2x |
arm_cols <- paste(rep(c("n_rsp", "n_rsp", "n", "n")), c(riskdiff$arm_x, riskdiff$arm_y), sep = "_")
|
| 315 | ||
| 316 | 2x |
df_prop_diff <- df$prop %>% |
| 317 | 2x |
dplyr::select(-"prop") %>% |
| 318 | 2x |
tidyr::pivot_wider( |
| 319 | 2x |
id_cols = c("subgroup", "var", "var_label", "row_type"),
|
| 320 | 2x |
names_from = "arm", |
| 321 | 2x |
values_from = c("n", "n_rsp")
|
| 322 |
) %>% |
|
| 323 | 2x |
dplyr::rowwise() %>% |
| 324 | 2x |
dplyr::mutate( |
| 325 | 2x |
riskdiff = stat_propdiff_ci( |
| 326 | 2x |
x = as.list(.data[[arm_cols[1]]]), |
| 327 | 2x |
y = as.list(.data[[arm_cols[2]]]), |
| 328 | 2x |
N_x = .data[[arm_cols[3]]], |
| 329 | 2x |
N_y = .data[[arm_cols[4]]], |
| 330 | 2x |
pct = riskdiff$pct |
| 331 |
) |
|
| 332 |
) %>% |
|
| 333 | 2x |
dplyr::select(-dplyr::all_of(arm_cols)) |
| 334 | ||
| 335 | 2x |
df$or <- df$or %>% |
| 336 | 2x |
dplyr::left_join( |
| 337 | 2x |
df_prop_diff, |
| 338 | 2x |
by = c("subgroup", "var", "var_label", "row_type")
|
| 339 |
) |
|
| 340 |
} |
|
| 341 | ||
| 342 |
# Add columns from table_prop (optional) |
|
| 343 | 14x |
if (length(colvars_prop$vars) > 0) {
|
| 344 | 13x |
lyt_prop <- split_cols_by(lyt = lyt, var = "arm") |
| 345 | 13x |
lyt_prop <- split_cols_by_multivar( |
| 346 | 13x |
lyt = lyt_prop, |
| 347 | 13x |
vars = colvars_prop$vars, |
| 348 | 13x |
varlabels = colvars_prop$labels |
| 349 |
) |
|
| 350 | ||
| 351 |
# Add "All Patients" row |
|
| 352 | 13x |
lyt_prop <- split_rows_by( |
| 353 | 13x |
lyt = lyt_prop, |
| 354 | 13x |
var = "row_type", |
| 355 | 13x |
split_fun = keep_split_levels("content"),
|
| 356 | 13x |
nested = FALSE, |
| 357 | 13x |
child_labels = "hidden" |
| 358 |
) |
|
| 359 | 13x |
lyt_prop <- analyze_colvars( |
| 360 | 13x |
lyt = lyt_prop, |
| 361 | 13x |
afun = a_response_subgroups, |
| 362 | 13x |
na_str = na_str, |
| 363 | 13x |
extra_args = extra_args |
| 364 |
) |
|
| 365 | ||
| 366 |
# Add analysis rows |
|
| 367 | 13x |
if ("analysis" %in% df$prop$row_type) {
|
| 368 | 12x |
lyt_prop <- split_rows_by( |
| 369 | 12x |
lyt = lyt_prop, |
| 370 | 12x |
var = "row_type", |
| 371 | 12x |
split_fun = keep_split_levels("analysis"),
|
| 372 | 12x |
nested = FALSE, |
| 373 | 12x |
child_labels = "hidden" |
| 374 |
) |
|
| 375 | 12x |
lyt_prop <- split_rows_by(lyt = lyt_prop, var = "var_label", nested = TRUE) |
| 376 | 12x |
lyt_prop <- analyze_colvars( |
| 377 | 12x |
lyt = lyt_prop, |
| 378 | 12x |
afun = a_response_subgroups, |
| 379 | 12x |
na_str = na_str, |
| 380 | 12x |
inclNAs = TRUE, |
| 381 | 12x |
extra_args = extra_args |
| 382 |
) |
|
| 383 |
} |
|
| 384 | ||
| 385 | 13x |
table_prop <- build_table(lyt_prop, df = df$prop) |
| 386 |
} else {
|
|
| 387 | 1x |
table_prop <- NULL |
| 388 |
} |
|
| 389 | ||
| 390 |
# Add columns from table_or ("n_tot", "or", and "ci" required)
|
|
| 391 | 14x |
lyt_or <- split_cols_by(lyt = lyt, var = "arm") |
| 392 | 14x |
lyt_or <- split_cols_by_multivar( |
| 393 | 14x |
lyt = lyt_or, |
| 394 | 14x |
vars = colvars_or$vars, |
| 395 | 14x |
varlabels = colvars_or$labels |
| 396 |
) |
|
| 397 | ||
| 398 |
# Add "All Patients" row |
|
| 399 | 14x |
lyt_or <- split_rows_by( |
| 400 | 14x |
lyt = lyt_or, |
| 401 | 14x |
var = "row_type", |
| 402 | 14x |
split_fun = keep_split_levels("content"),
|
| 403 | 14x |
nested = FALSE, |
| 404 | 14x |
child_labels = "hidden" |
| 405 |
) |
|
| 406 | 14x |
lyt_or <- analyze_colvars( |
| 407 | 14x |
lyt = lyt_or, |
| 408 | 14x |
afun = a_response_subgroups, |
| 409 | 14x |
na_str = na_str, |
| 410 | 14x |
extra_args = extra_args |
| 411 |
) %>% |
|
| 412 | 14x |
append_topleft("Baseline Risk Factors")
|
| 413 | ||
| 414 |
# Add analysis rows |
|
| 415 | 14x |
if ("analysis" %in% df$or$row_type) {
|
| 416 | 13x |
lyt_or <- split_rows_by( |
| 417 | 13x |
lyt = lyt_or, |
| 418 | 13x |
var = "row_type", |
| 419 | 13x |
split_fun = keep_split_levels("analysis"),
|
| 420 | 13x |
nested = FALSE, |
| 421 | 13x |
child_labels = "hidden" |
| 422 |
) |
|
| 423 | 13x |
lyt_or <- split_rows_by(lyt = lyt_or, var = "var_label", nested = TRUE) |
| 424 | 13x |
lyt_or <- analyze_colvars( |
| 425 | 13x |
lyt = lyt_or, |
| 426 | 13x |
afun = a_response_subgroups, |
| 427 | 13x |
na_str = na_str, |
| 428 | 13x |
inclNAs = TRUE, |
| 429 | 13x |
extra_args = extra_args |
| 430 |
) |
|
| 431 |
} |
|
| 432 | ||
| 433 | 14x |
table_or <- build_table(lyt_or, df = df$or) |
| 434 | ||
| 435 |
# Join tables, add forest plot attributes |
|
| 436 | 14x |
n_tot_id <- match("n_tot", colvars_or$vars)
|
| 437 | 14x |
if (is.null(table_prop)) {
|
| 438 | 1x |
result <- table_or |
| 439 | 1x |
or_id <- match("or", colvars_or$vars)
|
| 440 | 1x |
ci_id <- match("ci", colvars_or$vars)
|
| 441 |
} else {
|
|
| 442 | 13x |
result <- cbind_rtables(table_or[, n_tot_id], table_prop, table_or[, -n_tot_id]) |
| 443 | 13x |
or_id <- 1L + ncol(table_prop) + match("or", colvars_or$vars[-n_tot_id])
|
| 444 | 13x |
ci_id <- 1L + ncol(table_prop) + match("ci", colvars_or$vars[-n_tot_id])
|
| 445 | 13x |
n_tot_id <- 1L |
| 446 |
} |
|
| 447 | 14x |
structure( |
| 448 | 14x |
result, |
| 449 | 14x |
forest_header = paste0(levels(df$prop$arm), "\nBetter"), |
| 450 | 14x |
col_x = or_id, |
| 451 | 14x |
col_ci = ci_id, |
| 452 | 14x |
col_symbol_size = n_tot_id |
| 453 |
) |
|
| 454 |
} |
|
| 455 | ||
| 456 |
#' Labels for column variables in binary response by subgroup table |
|
| 457 |
#' |
|
| 458 |
#' @description `r lifecycle::badge("stable")`
|
|
| 459 |
#' |
|
| 460 |
#' Internal function to check variables included in [tabulate_rsp_subgroups()] and create column labels. |
|
| 461 |
#' |
|
| 462 |
#' @inheritParams argument_convention |
|
| 463 |
#' @inheritParams tabulate_rsp_subgroups |
|
| 464 |
#' |
|
| 465 |
#' @return A `list` of variables to tabulate and their labels. |
|
| 466 |
#' |
|
| 467 |
#' @export |
|
| 468 |
d_rsp_subgroups_colvars <- function(vars, |
|
| 469 |
conf_level = NULL, |
|
| 470 |
method = NULL) {
|
|
| 471 | 20x |
checkmate::assert_character(vars) |
| 472 | 20x |
checkmate::assert_subset(c("n_tot", "or", "ci"), vars)
|
| 473 | 20x |
checkmate::assert_subset( |
| 474 | 20x |
vars, |
| 475 | 20x |
c("n", "n_rsp", "prop", "n_tot", "or", "ci", "pval")
|
| 476 |
) |
|
| 477 | ||
| 478 | 20x |
varlabels <- c( |
| 479 | 20x |
n = "n", |
| 480 | 20x |
n_rsp = "Responders", |
| 481 | 20x |
prop = "Response (%)", |
| 482 | 20x |
n_tot = "Total n", |
| 483 | 20x |
or = "Odds Ratio" |
| 484 |
) |
|
| 485 | 20x |
colvars <- vars |
| 486 | ||
| 487 | 20x |
if ("ci" %in% colvars) {
|
| 488 | 20x |
checkmate::assert_false(is.null(conf_level)) |
| 489 | ||
| 490 | 20x |
varlabels <- c( |
| 491 | 20x |
varlabels, |
| 492 | 20x |
ci = paste0(100 * conf_level, "% CI") |
| 493 |
) |
|
| 494 |
} |
|
| 495 | ||
| 496 | 20x |
if ("pval" %in% colvars) {
|
| 497 | 14x |
varlabels <- c( |
| 498 | 14x |
varlabels, |
| 499 | 14x |
pval = method |
| 500 |
) |
|
| 501 |
} |
|
| 502 | ||
| 503 | 20x |
list( |
| 504 | 20x |
vars = colvars, |
| 505 | 20x |
labels = varlabels[vars] |
| 506 |
) |
|
| 507 |
} |
| 1 |
#' Helper functions for accessing information from `rtables` |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' These are a couple of functions that help with accessing the data in `rtables` objects. |
|
| 6 |
#' Currently these work for occurrence tables, which are defined as having a count as the first |
|
| 7 |
#' element and a fraction as the second element in each cell. |
|
| 8 |
#' |
|
| 9 |
#' @seealso [prune_occurrences] for usage of these functions. |
|
| 10 |
#' |
|
| 11 |
#' @name rtables_access |
|
| 12 |
NULL |
|
| 13 | ||
| 14 |
#' @describeIn rtables_access Helper function to extract the first values from each content |
|
| 15 |
#' cell and from specified columns in a `TableRow`. Defaults to all columns. |
|
| 16 |
#' |
|
| 17 |
#' @param table_row (`TableRow`)\cr an analysis row in a occurrence table. |
|
| 18 |
#' @param col_names (`character`)\cr the names of the columns to extract from. |
|
| 19 |
#' @param col_indices (`integer`)\cr the indices of the columns to extract from. If `col_names` are provided, |
|
| 20 |
#' then these are inferred from the names of `table_row`. Note that this currently only works well with a single |
|
| 21 |
#' column split. |
|
| 22 |
#' |
|
| 23 |
#' @return |
|
| 24 |
#' * `h_row_first_values()` returns a `vector` of numeric values. |
|
| 25 |
#' |
|
| 26 |
#' @examples |
|
| 27 |
#' tbl <- basic_table() %>% |
|
| 28 |
#' split_cols_by("ARM") %>%
|
|
| 29 |
#' split_rows_by("RACE") %>%
|
|
| 30 |
#' analyze("AGE", function(x) {
|
|
| 31 |
#' list( |
|
| 32 |
#' "mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.x (xx.x)"), |
|
| 33 |
#' "n" = length(x), |
|
| 34 |
#' "frac" = rcell(c(0.1, 0.1), format = "xx (xx)") |
|
| 35 |
#' ) |
|
| 36 |
#' }) %>% |
|
| 37 |
#' build_table(tern_ex_adsl) %>% |
|
| 38 |
#' prune_table() |
|
| 39 |
#' tree_row_elem <- collect_leaves(tbl[2, ])[[1]] |
|
| 40 |
#' result <- max(h_row_first_values(tree_row_elem)) |
|
| 41 |
#' result |
|
| 42 |
#' |
|
| 43 |
#' @export |
|
| 44 |
h_row_first_values <- function(table_row, |
|
| 45 |
col_names = NULL, |
|
| 46 |
col_indices = NULL) {
|
|
| 47 | 745x |
col_indices <- check_names_indices(table_row, col_names, col_indices) |
| 48 | 744x |
checkmate::assert_integerish(col_indices) |
| 49 | 744x |
checkmate::assert_subset(col_indices, seq_len(ncol(table_row))) |
| 50 | ||
| 51 |
# Main values are extracted |
|
| 52 | 744x |
row_vals <- row_values(table_row)[col_indices] |
| 53 | ||
| 54 |
# Main return |
|
| 55 | 744x |
vapply(row_vals, function(rv) {
|
| 56 | 2096x |
if (is.null(rv)) {
|
| 57 | 744x |
NA_real_ |
| 58 |
} else {
|
|
| 59 | 2093x |
rv[1L] |
| 60 |
} |
|
| 61 | 744x |
}, FUN.VALUE = numeric(1)) |
| 62 |
} |
|
| 63 | ||
| 64 |
#' @describeIn rtables_access Helper function that extracts row values and checks if they are |
|
| 65 |
#' convertible to integers (`integerish` values). |
|
| 66 |
#' |
|
| 67 |
#' @return |
|
| 68 |
#' * `h_row_counts()` returns a `vector` of numeric values. |
|
| 69 |
#' |
|
| 70 |
#' @examples |
|
| 71 |
#' # Row counts (integer values) |
|
| 72 |
#' # h_row_counts(tree_row_elem) # Fails because there are no integers |
|
| 73 |
#' # Using values with integers |
|
| 74 |
#' tree_row_elem <- collect_leaves(tbl[3, ])[[1]] |
|
| 75 |
#' result <- h_row_counts(tree_row_elem) |
|
| 76 |
#' # result |
|
| 77 |
#' |
|
| 78 |
#' @export |
|
| 79 |
h_row_counts <- function(table_row, |
|
| 80 |
col_names = NULL, |
|
| 81 |
col_indices = NULL) {
|
|
| 82 | 741x |
counts <- h_row_first_values(table_row, col_names, col_indices) |
| 83 | 741x |
checkmate::assert_integerish(counts) |
| 84 | 741x |
counts |
| 85 |
} |
|
| 86 | ||
| 87 |
#' @describeIn rtables_access Helper function to extract fractions from specified columns in a `TableRow`. |
|
| 88 |
#' More specifically it extracts the second values from each content cell and checks it is a fraction. |
|
| 89 |
#' |
|
| 90 |
#' @return |
|
| 91 |
#' * `h_row_fractions()` returns a `vector` of proportions. |
|
| 92 |
#' |
|
| 93 |
#' @examples |
|
| 94 |
#' # Row fractions |
|
| 95 |
#' tree_row_elem <- collect_leaves(tbl[4, ])[[1]] |
|
| 96 |
#' h_row_fractions(tree_row_elem) |
|
| 97 |
#' |
|
| 98 |
#' @export |
|
| 99 |
h_row_fractions <- function(table_row, |
|
| 100 |
col_names = NULL, |
|
| 101 |
col_indices = NULL) {
|
|
| 102 | 250x |
col_indices <- check_names_indices(table_row, col_names, col_indices) |
| 103 | 250x |
row_vals <- row_values(table_row)[col_indices] |
| 104 | 250x |
fractions <- sapply(row_vals, "[", 2L) |
| 105 | 250x |
checkmate::assert_numeric(fractions, lower = 0, upper = 1) |
| 106 | 250x |
fractions |
| 107 |
} |
|
| 108 | ||
| 109 |
#' @describeIn rtables_access Helper function to extract column counts from specified columns in a table. |
|
| 110 |
#' |
|
| 111 |
#' @param table (`VTableNodeInfo`)\cr an occurrence table or row. |
|
| 112 |
#' |
|
| 113 |
#' @return |
|
| 114 |
#' * `h_col_counts()` returns a `vector` of column counts. |
|
| 115 |
#' |
|
| 116 |
#' @export |
|
| 117 |
h_col_counts <- function(table, |
|
| 118 |
col_names = NULL, |
|
| 119 |
col_indices = NULL) {
|
|
| 120 | 307x |
col_indices <- check_names_indices(table, col_names, col_indices) |
| 121 | 307x |
counts <- col_counts(table)[col_indices] |
| 122 | 307x |
stats::setNames(counts, col_names) |
| 123 |
} |
|
| 124 | ||
| 125 |
#' @describeIn rtables_access Helper function to get first row of content table of current table. |
|
| 126 |
#' |
|
| 127 |
#' @return |
|
| 128 |
#' * `h_content_first_row()` returns a row from an `rtables` table. |
|
| 129 |
#' |
|
| 130 |
#' @export |
|
| 131 |
h_content_first_row <- function(table) {
|
|
| 132 | 27x |
ct <- content_table(table) |
| 133 | 27x |
tree_children(ct)[[1]] |
| 134 |
} |
|
| 135 | ||
| 136 |
#' @describeIn rtables_access Helper function which says whether current table is a leaf in the tree. |
|
| 137 |
#' |
|
| 138 |
#' @return |
|
| 139 |
#' * `is_leaf_table()` returns a `logical` value indicating whether current table is a leaf. |
|
| 140 |
#' |
|
| 141 |
#' @keywords internal |
|
| 142 |
is_leaf_table <- function(table) {
|
|
| 143 | 168x |
children <- tree_children(table) |
| 144 | 168x |
child_classes <- unique(sapply(children, class)) |
| 145 | 168x |
identical(child_classes, "ElementaryTable") |
| 146 |
} |
|
| 147 | ||
| 148 |
#' @describeIn rtables_access Internal helper function that tests standard inputs for column indices. |
|
| 149 |
#' |
|
| 150 |
#' @return |
|
| 151 |
#' * `check_names_indices` returns column indices. |
|
| 152 |
#' |
|
| 153 |
#' @keywords internal |
|
| 154 |
check_names_indices <- function(table_row, |
|
| 155 |
col_names = NULL, |
|
| 156 |
col_indices = NULL) {
|
|
| 157 | 1302x |
if (!is.null(col_names)) {
|
| 158 | 1256x |
if (!is.null(col_indices)) {
|
| 159 | 1x |
stop( |
| 160 | 1x |
"Inserted both col_names and col_indices when selecting row values. ", |
| 161 | 1x |
"Please choose one." |
| 162 |
) |
|
| 163 |
} |
|
| 164 | 1255x |
col_indices <- h_col_indices(table_row, col_names) |
| 165 |
} |
|
| 166 | 1301x |
if (is.null(col_indices)) {
|
| 167 | 39x |
ll <- ifelse(is.null(ncol(table_row)), length(table_row), ncol(table_row)) |
| 168 | 39x |
col_indices <- seq_len(ll) |
| 169 |
} |
|
| 170 | ||
| 171 | 1301x |
return(col_indices) |
| 172 |
} |
| 1 |
#' Count patients with abnormal analysis range values by baseline status |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_abnormal_by_baseline()] creates a layout element to count patients with abnormal |
|
| 6 |
#' analysis range values, categorized by baseline status. |
|
| 7 |
#' |
|
| 8 |
#' This function analyzes primary analysis variable `var` which indicates abnormal range results. Additional |
|
| 9 |
#' analysis variables that can be supplied as a list via the `variables` parameter are `id` (defaults to |
|
| 10 |
#' `USUBJID`), a variable to indicate unique subject identifiers, and `baseline` (defaults to `BNRIND`), a |
|
| 11 |
#' variable to indicate baseline reference ranges. |
|
| 12 |
#' |
|
| 13 |
#' For each direction specified via the `abnormal` parameter (e.g. High or Low), we condition on baseline |
|
| 14 |
#' range result and count patients in the numerator and denominator as follows for each of the following |
|
| 15 |
#' categories: |
|
| 16 |
#' * `Not <abnormality>` |
|
| 17 |
#' * `num`: The number of patients without abnormality at baseline (excluding those with missing baseline) |
|
| 18 |
#' and with at least one abnormality post-baseline. |
|
| 19 |
#' * `denom`: The number of patients without abnormality at baseline (excluding those with missing baseline). |
|
| 20 |
#' * `<Abnormality>` |
|
| 21 |
#' * `num`: The number of patients with abnormality as baseline and at least one abnormality post-baseline. |
|
| 22 |
#' * `denom`: The number of patients with abnormality at baseline. |
|
| 23 |
#' * `Total` |
|
| 24 |
#' * `num`: The number of patients with at least one post-baseline record and at least one abnormality |
|
| 25 |
#' post-baseline. |
|
| 26 |
#' * `denom`: The number of patients with at least one post-baseline record. |
|
| 27 |
#' |
|
| 28 |
#' This function assumes that `df` has been filtered to only include post-baseline records. |
|
| 29 |
#' |
|
| 30 |
#' @inheritParams argument_convention |
|
| 31 |
#' @param abnormal (`character`)\cr values identifying the abnormal range level(s) in `.var`. |
|
| 32 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 33 |
#' |
|
| 34 |
#' Options are: ``r shQuote(get_stats("abnormal_by_baseline"), type = "sh")``
|
|
| 35 |
#' |
|
| 36 |
#' @note |
|
| 37 |
#' * `df` should be filtered to include only post-baseline records. |
|
| 38 |
#' * If the baseline variable or analysis variable contains `NA` records, it is expected that `df` has been |
|
| 39 |
#' pre-processed using [df_explicit_na()] or [explicit_na()]. |
|
| 40 |
#' |
|
| 41 |
#' @seealso Relevant description function [d_count_abnormal_by_baseline()]. |
|
| 42 |
#' |
|
| 43 |
#' @name abnormal_by_baseline |
|
| 44 |
#' @order 1 |
|
| 45 |
NULL |
|
| 46 | ||
| 47 |
#' @describeIn abnormal_by_baseline Statistics function for a single `abnormal` level. |
|
| 48 |
#' |
|
| 49 |
#' @param na_str (`string`)\cr the explicit `na_level` argument you used in the pre-processing steps (maybe with |
|
| 50 |
#' [df_explicit_na()]). The default is `"<Missing>"`. |
|
| 51 |
#' |
|
| 52 |
#' @return |
|
| 53 |
#' * `s_count_abnormal_by_baseline()` returns statistic `fraction` which is a named list with 3 labeled elements: |
|
| 54 |
#' `not_abnormal`, `abnormal`, and `total`. Each element contains a vector with `num` and `denom` patient counts. |
|
| 55 |
#' |
|
| 56 |
#' @keywords internal |
|
| 57 |
s_count_abnormal_by_baseline <- function(df, |
|
| 58 |
.var, |
|
| 59 |
abnormal, |
|
| 60 |
na_str = "<Missing>", |
|
| 61 |
variables = list(id = "USUBJID", baseline = "BNRIND"), |
|
| 62 |
...) {
|
|
| 63 | 11x |
checkmate::assert_string(.var) |
| 64 | 11x |
checkmate::assert_string(abnormal) |
| 65 | 11x |
checkmate::assert_string(na_str) |
| 66 | 11x |
assert_df_with_variables(df, c(range = .var, variables)) |
| 67 | 11x |
checkmate::assert_subset(names(variables), c("id", "baseline"))
|
| 68 | 11x |
checkmate::assert_multi_class(df[[variables$id]], classes = c("factor", "character"))
|
| 69 | 11x |
checkmate::assert_multi_class(df[[variables$baseline]], classes = c("factor", "character"))
|
| 70 | 11x |
checkmate::assert_multi_class(df[[.var]], classes = c("factor", "character"))
|
| 71 | ||
| 72 |
# If input is passed as character, changed to factor |
|
| 73 | 11x |
df[[.var]] <- as_factor_keep_attributes(df[[.var]], na_level = na_str) |
| 74 | 11x |
df[[variables$baseline]] <- as_factor_keep_attributes(df[[variables$baseline]], na_level = na_str) |
| 75 | ||
| 76 | 11x |
assert_valid_factor(df[[.var]], any.missing = FALSE) |
| 77 | 10x |
assert_valid_factor(df[[variables$baseline]], any.missing = FALSE) |
| 78 | ||
| 79 |
# Keep only records with valid analysis value. |
|
| 80 | 9x |
df <- df[df[[.var]] != na_str, ] |
| 81 | ||
| 82 | 9x |
anl <- data.frame( |
| 83 | 9x |
id = df[[variables$id]], |
| 84 | 9x |
var = df[[.var]], |
| 85 | 9x |
baseline = df[[variables$baseline]], |
| 86 | 9x |
stringsAsFactors = FALSE |
| 87 |
) |
|
| 88 | ||
| 89 |
# Total: |
|
| 90 |
# - Patients in denominator: have at least one valid measurement post-baseline. |
|
| 91 |
# - Patients in numerator: have at least one abnormality. |
|
| 92 | 9x |
total_denom <- length(unique(anl$id)) |
| 93 | 9x |
total_num <- length(unique(anl$id[anl$var == abnormal])) |
| 94 | ||
| 95 |
# Baseline NA records are counted only in total rows. |
|
| 96 | 9x |
anl <- anl[anl$baseline != na_str, ] |
| 97 | ||
| 98 |
# Abnormal: |
|
| 99 |
# - Patients in denominator: have abnormality at baseline. |
|
| 100 |
# - Patients in numerator: have abnormality at baseline AND |
|
| 101 |
# have at least one abnormality post-baseline. |
|
| 102 | 9x |
abn_denom <- length(unique(anl$id[anl$baseline == abnormal])) |
| 103 | 9x |
abn_num <- length(unique(anl$id[anl$baseline == abnormal & anl$var == abnormal])) |
| 104 | ||
| 105 |
# Not abnormal: |
|
| 106 |
# - Patients in denominator: do not have abnormality at baseline. |
|
| 107 |
# - Patients in numerator: do not have abnormality at baseline AND |
|
| 108 |
# have at least one abnormality post-baseline. |
|
| 109 | 9x |
not_abn_denom <- length(unique(anl$id[anl$baseline != abnormal])) |
| 110 | 9x |
not_abn_num <- length(unique(anl$id[anl$baseline != abnormal & anl$var == abnormal])) |
| 111 | ||
| 112 | 9x |
labels <- d_count_abnormal_by_baseline(abnormal) |
| 113 | 9x |
list(fraction = list( |
| 114 | 9x |
not_abnormal = formatters::with_label(c(num = not_abn_num, denom = not_abn_denom), labels$not_abnormal), |
| 115 | 9x |
abnormal = formatters::with_label(c(num = abn_num, denom = abn_denom), labels$abnormal), |
| 116 | 9x |
total = formatters::with_label(c(num = total_num, denom = total_denom), labels$total) |
| 117 |
)) |
|
| 118 |
} |
|
| 119 | ||
| 120 |
#' @describeIn abnormal_by_baseline Formatted analysis function which is used as `afun` |
|
| 121 |
#' in `count_abnormal_by_baseline()`. |
|
| 122 |
#' |
|
| 123 |
#' @return |
|
| 124 |
#' * `a_count_abnormal_by_baseline()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 125 |
#' |
|
| 126 |
#' @keywords internal |
|
| 127 |
a_count_abnormal_by_baseline <- function(df, |
|
| 128 |
..., |
|
| 129 |
.stats = NULL, |
|
| 130 |
.stat_names = NULL, |
|
| 131 |
.formats = NULL, |
|
| 132 |
.labels = NULL, |
|
| 133 |
.indent_mods = NULL) {
|
|
| 134 |
# Check for additional parameters to the statistics function |
|
| 135 | 4x |
dots_extra_args <- list(...) |
| 136 | 4x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 137 | 4x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 138 | ||
| 139 |
# Check for user-defined functions |
|
| 140 | 4x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 141 | 4x |
.stats <- default_and_custom_stats_list$all_stats |
| 142 | 4x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 143 | ||
| 144 |
# Apply statistics function |
|
| 145 | 4x |
x_stats <- .apply_stat_functions( |
| 146 | 4x |
default_stat_fnc = s_count_abnormal_by_baseline, |
| 147 | 4x |
custom_stat_fnc_list = custom_stat_functions, |
| 148 | 4x |
args_list = c( |
| 149 | 4x |
df = list(df), |
| 150 | 4x |
extra_afun_params, |
| 151 | 4x |
dots_extra_args |
| 152 |
) |
|
| 153 |
) |
|
| 154 | ||
| 155 |
# Fill in formatting defaults |
|
| 156 | 4x |
.stats <- get_stats("abnormal_by_baseline", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 157 | 4x |
levels_per_stats <- lapply(x_stats, names) |
| 158 | 4x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 159 | 4x |
.labels <- get_labels_from_stats( |
| 160 | 4x |
.stats, .labels, levels_per_stats, d_count_abnormal_by_baseline(dots_extra_args$abnormal) |
| 161 |
) |
|
| 162 | 4x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 163 | ||
| 164 | 4x |
x_stats <- x_stats[.stats] %>% |
| 165 | 4x |
.unlist_keep_nulls() %>% |
| 166 | 4x |
setNames(names(.formats)) |
| 167 | ||
| 168 |
# Auto format handling |
|
| 169 | 4x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 170 | ||
| 171 |
# Get and check statistical names |
|
| 172 | 4x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 173 | ||
| 174 | 4x |
in_rows( |
| 175 | 4x |
.list = x_stats, |
| 176 | 4x |
.formats = .formats, |
| 177 | 4x |
.names = .labels %>% .unlist_keep_nulls(), |
| 178 | 4x |
.stat_names = .stat_names, |
| 179 | 4x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 180 | 4x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 181 |
) |
|
| 182 |
} |
|
| 183 | ||
| 184 |
#' @describeIn abnormal_by_baseline Layout-creating function which can take statistics function arguments |
|
| 185 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 186 |
#' |
|
| 187 |
#' @return |
|
| 188 |
#' * `count_abnormal_by_baseline()` returns a layout object suitable for passing to further layouting functions, |
|
| 189 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 190 |
#' the statistics from `s_count_abnormal_by_baseline()` to the table layout. |
|
| 191 |
#' |
|
| 192 |
#' @examples |
|
| 193 |
#' df <- data.frame( |
|
| 194 |
#' USUBJID = as.character(c(1:6)), |
|
| 195 |
#' ANRIND = factor(c(rep("LOW", 4), "NORMAL", "HIGH")),
|
|
| 196 |
#' BNRIND = factor(c("LOW", "NORMAL", "HIGH", NA, "LOW", "NORMAL"))
|
|
| 197 |
#' ) |
|
| 198 |
#' df <- df_explicit_na(df) |
|
| 199 |
#' |
|
| 200 |
#' # Layout creating function. |
|
| 201 |
#' basic_table() %>% |
|
| 202 |
#' count_abnormal_by_baseline(var = "ANRIND", abnormal = c(High = "HIGH")) %>% |
|
| 203 |
#' build_table(df) |
|
| 204 |
#' |
|
| 205 |
#' # Passing of statistics function and formatting arguments. |
|
| 206 |
#' df2 <- data.frame( |
|
| 207 |
#' ID = as.character(c(1, 2, 3, 4)), |
|
| 208 |
#' RANGE = factor(c("NORMAL", "LOW", "HIGH", "HIGH")),
|
|
| 209 |
#' BLRANGE = factor(c("LOW", "HIGH", "HIGH", "NORMAL"))
|
|
| 210 |
#' ) |
|
| 211 |
#' |
|
| 212 |
#' basic_table() %>% |
|
| 213 |
#' count_abnormal_by_baseline( |
|
| 214 |
#' var = "RANGE", |
|
| 215 |
#' abnormal = c(Low = "LOW"), |
|
| 216 |
#' variables = list(id = "ID", baseline = "BLRANGE"), |
|
| 217 |
#' .formats = c(fraction = "xx / xx"), |
|
| 218 |
#' .indent_mods = c(fraction = 2L) |
|
| 219 |
#' ) %>% |
|
| 220 |
#' build_table(df2) |
|
| 221 |
#' |
|
| 222 |
#' @export |
|
| 223 |
#' @order 2 |
|
| 224 |
count_abnormal_by_baseline <- function(lyt, |
|
| 225 |
var, |
|
| 226 |
abnormal, |
|
| 227 |
variables = list(id = "USUBJID", baseline = "BNRIND"), |
|
| 228 |
na_str = "<Missing>", |
|
| 229 |
nested = TRUE, |
|
| 230 |
..., |
|
| 231 |
table_names = abnormal, |
|
| 232 |
.stats = "fraction", |
|
| 233 |
.stat_names = NULL, |
|
| 234 |
.formats = list(fraction = format_fraction), |
|
| 235 |
.labels = NULL, |
|
| 236 |
.indent_mods = NULL) {
|
|
| 237 | 2x |
checkmate::assert_character(abnormal, len = length(table_names), names = "named") |
| 238 | 2x |
checkmate::assert_string(var) |
| 239 | ||
| 240 |
# Process standard extra arguments |
|
| 241 | 2x |
extra_args <- list(".stats" = .stats)
|
| 242 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 243 | 2x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 244 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 245 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 246 | ||
| 247 |
# Process additional arguments to the statistic function |
|
| 248 | 2x |
extra_args <- c(extra_args, "variables" = list(variables), ...) |
| 249 | ||
| 250 |
# Append additional info from layout to the analysis function |
|
| 251 | 2x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 252 | 2x |
formals(a_count_abnormal_by_baseline) <- c( |
| 253 | 2x |
formals(a_count_abnormal_by_baseline), extra_args[[".additional_fun_parameters"]] |
| 254 |
) |
|
| 255 | ||
| 256 |
# Add a new table section with label for each value in abnormal |
|
| 257 | 2x |
for (i in seq_along(abnormal)) {
|
| 258 | 4x |
extra_args[["abnormal"]] <- abnormal[i] |
| 259 | ||
| 260 | 4x |
lyt <- analyze( |
| 261 | 4x |
lyt = lyt, |
| 262 | 4x |
vars = var, |
| 263 | 4x |
afun = a_count_abnormal_by_baseline, |
| 264 | 4x |
var_labels = names(abnormal)[i], |
| 265 | 4x |
na_str = na_str, |
| 266 | 4x |
nested = nested, |
| 267 | 4x |
extra_args = extra_args, |
| 268 | 4x |
show_labels = "visible", |
| 269 | 4x |
table_names = table_names[i] |
| 270 |
) |
|
| 271 |
} |
|
| 272 | ||
| 273 | 2x |
lyt |
| 274 |
} |
|
| 275 | ||
| 276 |
#' Description function for `s_count_abnormal_by_baseline()` |
|
| 277 |
#' |
|
| 278 |
#' @description `r lifecycle::badge("stable")`
|
|
| 279 |
#' |
|
| 280 |
#' Description function that produces the labels for [s_count_abnormal_by_baseline()]. |
|
| 281 |
#' |
|
| 282 |
#' @inheritParams abnormal_by_baseline |
|
| 283 |
#' |
|
| 284 |
#' @return Abnormal category labels for [s_count_abnormal_by_baseline()]. |
|
| 285 |
#' |
|
| 286 |
#' @examples |
|
| 287 |
#' d_count_abnormal_by_baseline("LOW")
|
|
| 288 |
#' |
|
| 289 |
#' @export |
|
| 290 |
d_count_abnormal_by_baseline <- function(abnormal) {
|
|
| 291 | 13x |
not_abn_name <- paste("Not", tolower(abnormal))
|
| 292 | 13x |
abn_name <- paste0(toupper(substr(abnormal, 1, 1)), tolower(substring(abnormal, 2))) |
| 293 | 13x |
total_name <- "Total" |
| 294 | ||
| 295 | 13x |
list( |
| 296 | 13x |
not_abnormal = not_abn_name, |
| 297 | 13x |
abnormal = abn_name, |
| 298 | 13x |
total = total_name |
| 299 |
) |
|
| 300 |
} |
| 1 |
#' Convert list of groups to a data frame |
|
| 2 |
#' |
|
| 3 |
#' This converts a list of group levels into a data frame format which is expected by [rtables::add_combo_levels()]. |
|
| 4 |
#' |
|
| 5 |
#' @param groups_list (named `list` of `character`)\cr specifies the new group levels via the names and the |
|
| 6 |
#' levels that belong to it in the character vectors that are elements of the list. |
|
| 7 |
#' |
|
| 8 |
#' @return A `tibble` in the required format. |
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' grade_groups <- list( |
|
| 12 |
#' "Any Grade (%)" = c("1", "2", "3", "4", "5"),
|
|
| 13 |
#' "Grade 3-4 (%)" = c("3", "4"),
|
|
| 14 |
#' "Grade 5 (%)" = "5" |
|
| 15 |
#' ) |
|
| 16 |
#' groups_list_to_df(grade_groups) |
|
| 17 |
#' |
|
| 18 |
#' @export |
|
| 19 |
groups_list_to_df <- function(groups_list) {
|
|
| 20 | 5x |
checkmate::assert_list(groups_list, names = "named") |
| 21 | 5x |
lapply(groups_list, checkmate::assert_character) |
| 22 | 5x |
tibble::tibble( |
| 23 | 5x |
valname = make_names(names(groups_list)), |
| 24 | 5x |
label = names(groups_list), |
| 25 | 5x |
levelcombo = unname(groups_list), |
| 26 | 5x |
exargs = replicate(length(groups_list), list()) |
| 27 |
) |
|
| 28 |
} |
|
| 29 | ||
| 30 |
#' Reference and treatment group combination |
|
| 31 |
#' |
|
| 32 |
#' @description `r lifecycle::badge("stable")`
|
|
| 33 |
#' |
|
| 34 |
#' Facilitate the re-combination of groups divided as reference and treatment groups; it helps in arranging groups of |
|
| 35 |
#' columns in the `rtables` framework and teal modules. |
|
| 36 |
#' |
|
| 37 |
#' @param fct (`factor`)\cr the variable with levels which needs to be grouped. |
|
| 38 |
#' @param ref (`character`)\cr the reference level(s). |
|
| 39 |
#' @param collapse (`string`)\cr a character string to separate `fct` and `ref`. |
|
| 40 |
#' |
|
| 41 |
#' @return A `list` with first item `ref` (reference) and second item `trt` (treatment). |
|
| 42 |
#' |
|
| 43 |
#' @examples |
|
| 44 |
#' groups <- combine_groups( |
|
| 45 |
#' fct = DM$ARM, |
|
| 46 |
#' ref = c("B: Placebo")
|
|
| 47 |
#' ) |
|
| 48 |
#' |
|
| 49 |
#' basic_table() %>% |
|
| 50 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
| 51 |
#' add_colcounts() %>% |
|
| 52 |
#' analyze_vars("AGE") %>%
|
|
| 53 |
#' build_table(DM) |
|
| 54 |
#' |
|
| 55 |
#' @export |
|
| 56 |
combine_groups <- function(fct, |
|
| 57 |
ref = NULL, |
|
| 58 |
collapse = "/") {
|
|
| 59 | 10x |
checkmate::assert_string(collapse) |
| 60 | 10x |
checkmate::assert_character(ref, min.chars = 1, any.missing = FALSE, null.ok = TRUE) |
| 61 | 10x |
checkmate::assert_multi_class(fct, classes = c("factor", "character"))
|
| 62 | ||
| 63 | 10x |
fct <- as_factor_keep_attributes(fct) |
| 64 | ||
| 65 | 10x |
group_levels <- levels(fct) |
| 66 | 10x |
if (is.null(ref)) {
|
| 67 | 6x |
ref <- group_levels[1] |
| 68 |
} else {
|
|
| 69 | 4x |
checkmate::assert_subset(ref, group_levels) |
| 70 |
} |
|
| 71 | ||
| 72 | 10x |
groups <- list( |
| 73 | 10x |
ref = group_levels[group_levels %in% ref], |
| 74 | 10x |
trt = group_levels[!group_levels %in% ref] |
| 75 |
) |
|
| 76 | 10x |
stats::setNames(groups, nm = lapply(groups, paste, collapse = collapse)) |
| 77 |
} |
|
| 78 | ||
| 79 |
#' Split columns by groups of levels |
|
| 80 |
#' |
|
| 81 |
#' @description `r lifecycle::badge("stable")`
|
|
| 82 |
#' |
|
| 83 |
#' @inheritParams argument_convention |
|
| 84 |
#' @inheritParams groups_list_to_df |
|
| 85 |
#' @param ... additional arguments to [rtables::split_cols_by()] in order. For instance, to |
|
| 86 |
#' control formats (`format`), add a joint column for all groups (`incl_all`). |
|
| 87 |
#' |
|
| 88 |
#' @return A layout object suitable for passing to further layouting functions. Adding |
|
| 89 |
#' this function to an `rtable` layout will add a column split including the given |
|
| 90 |
#' groups to the table layout. |
|
| 91 |
#' |
|
| 92 |
#' @seealso [rtables::split_cols_by()] |
|
| 93 |
#' |
|
| 94 |
#' @examples |
|
| 95 |
#' # 1 - Basic use |
|
| 96 |
#' |
|
| 97 |
#' # Without group combination `split_cols_by_groups` is |
|
| 98 |
#' # equivalent to [rtables::split_cols_by()]. |
|
| 99 |
#' basic_table() %>% |
|
| 100 |
#' split_cols_by_groups("ARM") %>%
|
|
| 101 |
#' add_colcounts() %>% |
|
| 102 |
#' analyze("AGE") %>%
|
|
| 103 |
#' build_table(DM) |
|
| 104 |
#' |
|
| 105 |
#' # Add a reference column. |
|
| 106 |
#' basic_table() %>% |
|
| 107 |
#' split_cols_by_groups("ARM", ref_group = "B: Placebo") %>%
|
|
| 108 |
#' add_colcounts() %>% |
|
| 109 |
#' analyze( |
|
| 110 |
#' "AGE", |
|
| 111 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
| 112 |
#' if (.in_ref_col) {
|
|
| 113 |
#' in_rows("Diff Mean" = rcell(NULL))
|
|
| 114 |
#' } else {
|
|
| 115 |
#' in_rows("Diff Mean" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
| 116 |
#' } |
|
| 117 |
#' } |
|
| 118 |
#' ) %>% |
|
| 119 |
#' build_table(DM) |
|
| 120 |
#' |
|
| 121 |
#' # 2 - Adding group specification |
|
| 122 |
#' |
|
| 123 |
#' # Manual preparation of the groups. |
|
| 124 |
#' groups <- list( |
|
| 125 |
#' "Arms A+B" = c("A: Drug X", "B: Placebo"),
|
|
| 126 |
#' "Arms A+C" = c("A: Drug X", "C: Combination")
|
|
| 127 |
#' ) |
|
| 128 |
#' |
|
| 129 |
#' # Use of split_cols_by_groups without reference column. |
|
| 130 |
#' basic_table() %>% |
|
| 131 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
| 132 |
#' add_colcounts() %>% |
|
| 133 |
#' analyze("AGE") %>%
|
|
| 134 |
#' build_table(DM) |
|
| 135 |
#' |
|
| 136 |
#' # Including differentiated output in the reference column. |
|
| 137 |
#' basic_table() %>% |
|
| 138 |
#' split_cols_by_groups("ARM", groups_list = groups, ref_group = "Arms A+B") %>%
|
|
| 139 |
#' analyze( |
|
| 140 |
#' "AGE", |
|
| 141 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
| 142 |
#' if (.in_ref_col) {
|
|
| 143 |
#' in_rows("Diff. of Averages" = rcell(NULL))
|
|
| 144 |
#' } else {
|
|
| 145 |
#' in_rows("Diff. of Averages" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
| 146 |
#' } |
|
| 147 |
#' } |
|
| 148 |
#' ) %>% |
|
| 149 |
#' build_table(DM) |
|
| 150 |
#' |
|
| 151 |
#' # 3 - Binary list dividing factor levels into reference and treatment |
|
| 152 |
#' |
|
| 153 |
#' # `combine_groups` defines reference and treatment. |
|
| 154 |
#' groups <- combine_groups( |
|
| 155 |
#' fct = DM$ARM, |
|
| 156 |
#' ref = c("A: Drug X", "B: Placebo")
|
|
| 157 |
#' ) |
|
| 158 |
#' groups |
|
| 159 |
#' |
|
| 160 |
#' # Use group definition without reference column. |
|
| 161 |
#' basic_table() %>% |
|
| 162 |
#' split_cols_by_groups("ARM", groups_list = groups) %>%
|
|
| 163 |
#' add_colcounts() %>% |
|
| 164 |
#' analyze("AGE") %>%
|
|
| 165 |
#' build_table(DM) |
|
| 166 |
#' |
|
| 167 |
#' # Use group definition with reference column (first item of groups). |
|
| 168 |
#' basic_table() %>% |
|
| 169 |
#' split_cols_by_groups("ARM", groups, ref_group = names(groups)[1]) %>%
|
|
| 170 |
#' add_colcounts() %>% |
|
| 171 |
#' analyze( |
|
| 172 |
#' "AGE", |
|
| 173 |
#' afun = function(x, .ref_group, .in_ref_col) {
|
|
| 174 |
#' if (.in_ref_col) {
|
|
| 175 |
#' in_rows("Diff Mean" = rcell(NULL))
|
|
| 176 |
#' } else {
|
|
| 177 |
#' in_rows("Diff Mean" = rcell(mean(x) - mean(.ref_group), format = "xx.xx"))
|
|
| 178 |
#' } |
|
| 179 |
#' } |
|
| 180 |
#' ) %>% |
|
| 181 |
#' build_table(DM) |
|
| 182 |
#' |
|
| 183 |
#' @export |
|
| 184 |
split_cols_by_groups <- function(lyt, |
|
| 185 |
var, |
|
| 186 |
groups_list = NULL, |
|
| 187 |
ref_group = NULL, |
|
| 188 |
...) {
|
|
| 189 | 6x |
if (is.null(groups_list)) {
|
| 190 | 2x |
split_cols_by( |
| 191 | 2x |
lyt = lyt, |
| 192 | 2x |
var = var, |
| 193 | 2x |
ref_group = ref_group, |
| 194 |
... |
|
| 195 |
) |
|
| 196 |
} else {
|
|
| 197 | 4x |
groups_df <- groups_list_to_df(groups_list) |
| 198 | 4x |
if (!is.null(ref_group)) {
|
| 199 | 3x |
ref_group <- groups_df$valname[groups_df$label == ref_group] |
| 200 |
} |
|
| 201 | 4x |
split_cols_by( |
| 202 | 4x |
lyt = lyt, |
| 203 | 4x |
var = var, |
| 204 | 4x |
split_fun = add_combo_levels(groups_df, keep_levels = groups_df$valname), |
| 205 | 4x |
ref_group = ref_group, |
| 206 |
... |
|
| 207 |
) |
|
| 208 |
} |
|
| 209 |
} |
|
| 210 | ||
| 211 |
#' Combine counts |
|
| 212 |
#' |
|
| 213 |
#' Simplifies the estimation of column counts, especially when group combination is required. |
|
| 214 |
#' |
|
| 215 |
#' @inheritParams combine_groups |
|
| 216 |
#' @inheritParams groups_list_to_df |
|
| 217 |
#' |
|
| 218 |
#' @return A `vector` of column counts. |
|
| 219 |
#' |
|
| 220 |
#' @seealso [combine_groups()] |
|
| 221 |
#' |
|
| 222 |
#' @examples |
|
| 223 |
#' ref <- c("A: Drug X", "B: Placebo")
|
|
| 224 |
#' groups <- combine_groups(fct = DM$ARM, ref = ref) |
|
| 225 |
#' |
|
| 226 |
#' col_counts <- combine_counts( |
|
| 227 |
#' fct = DM$ARM, |
|
| 228 |
#' groups_list = groups |
|
| 229 |
#' ) |
|
| 230 |
#' |
|
| 231 |
#' basic_table() %>% |
|
| 232 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
| 233 |
#' add_colcounts() %>% |
|
| 234 |
#' analyze_vars("AGE") %>%
|
|
| 235 |
#' build_table(DM, col_counts = col_counts) |
|
| 236 |
#' |
|
| 237 |
#' ref <- "A: Drug X" |
|
| 238 |
#' groups <- combine_groups(fct = DM$ARM, ref = ref) |
|
| 239 |
#' col_counts <- combine_counts( |
|
| 240 |
#' fct = DM$ARM, |
|
| 241 |
#' groups_list = groups |
|
| 242 |
#' ) |
|
| 243 |
#' |
|
| 244 |
#' basic_table() %>% |
|
| 245 |
#' split_cols_by_groups("ARM", groups) %>%
|
|
| 246 |
#' add_colcounts() %>% |
|
| 247 |
#' analyze_vars("AGE") %>%
|
|
| 248 |
#' build_table(DM, col_counts = col_counts) |
|
| 249 |
#' |
|
| 250 |
#' @export |
|
| 251 |
combine_counts <- function(fct, groups_list = NULL) {
|
|
| 252 | 4x |
checkmate::assert_multi_class(fct, classes = c("factor", "character"))
|
| 253 | ||
| 254 | 4x |
fct <- as_factor_keep_attributes(fct) |
| 255 | ||
| 256 | 4x |
if (is.null(groups_list)) {
|
| 257 | 1x |
y <- table(fct) |
| 258 | 1x |
y <- stats::setNames(as.numeric(y), nm = dimnames(y)[[1]]) |
| 259 |
} else {
|
|
| 260 | 3x |
y <- vapply( |
| 261 | 3x |
X = groups_list, |
| 262 | 3x |
FUN = function(x) sum(table(fct)[x]), |
| 263 | 3x |
FUN.VALUE = 1 |
| 264 |
) |
|
| 265 |
} |
|
| 266 | 4x |
y |
| 267 |
} |
| 1 |
#' Helper functions for tabulating biomarker effects on binary response by subgroup |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Helper functions which are documented here separately to not confuse the user |
|
| 6 |
#' when reading about the user-facing functions. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams response_biomarkers_subgroups |
|
| 9 |
#' @inheritParams extract_rsp_biomarkers |
|
| 10 |
#' @inheritParams argument_convention |
|
| 11 |
#' |
|
| 12 |
#' @examples |
|
| 13 |
#' library(dplyr) |
|
| 14 |
#' library(forcats) |
|
| 15 |
#' |
|
| 16 |
#' adrs <- tern_ex_adrs |
|
| 17 |
#' adrs_labels <- formatters::var_labels(adrs) |
|
| 18 |
#' |
|
| 19 |
#' adrs_f <- adrs %>% |
|
| 20 |
#' filter(PARAMCD == "BESRSPI") %>% |
|
| 21 |
#' mutate(rsp = AVALC == "CR") |
|
| 22 |
#' formatters::var_labels(adrs_f) <- c(adrs_labels, "Response") |
|
| 23 |
#' |
|
| 24 |
#' @name h_response_biomarkers_subgroups |
|
| 25 |
NULL |
|
| 26 | ||
| 27 |
#' @describeIn h_response_biomarkers_subgroups helps with converting the "response" function variable list |
|
| 28 |
#' to the "logistic regression" variable list. The reason is that currently there is an |
|
| 29 |
#' inconsistency between the variable names accepted by `extract_rsp_subgroups()` and `fit_logistic()`. |
|
| 30 |
#' |
|
| 31 |
#' @param biomarker (`string`)\cr the name of the biomarker variable. |
|
| 32 |
#' |
|
| 33 |
#' @return |
|
| 34 |
#' * `h_rsp_to_logistic_variables()` returns a named `list` of elements `response`, `arm`, `covariates`, and `strata`. |
|
| 35 |
#' |
|
| 36 |
#' @examples |
|
| 37 |
#' # This is how the variable list is converted internally. |
|
| 38 |
#' h_rsp_to_logistic_variables( |
|
| 39 |
#' variables = list( |
|
| 40 |
#' rsp = "RSP", |
|
| 41 |
#' covariates = c("A", "B"),
|
|
| 42 |
#' strata = "D" |
|
| 43 |
#' ), |
|
| 44 |
#' biomarker = "AGE" |
|
| 45 |
#' ) |
|
| 46 |
#' |
|
| 47 |
#' @export |
|
| 48 |
h_rsp_to_logistic_variables <- function(variables, biomarker) {
|
|
| 49 | 49x |
if ("strat" %in% names(variables)) {
|
| 50 | ! |
warning( |
| 51 | ! |
"Warning: the `strat` element name of the `variables` list argument to `h_rsp_to_logistic_variables() ", |
| 52 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 53 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 54 |
) |
|
| 55 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 56 |
} |
|
| 57 | 49x |
checkmate::assert_list(variables) |
| 58 | 49x |
checkmate::assert_string(variables$rsp) |
| 59 | 49x |
checkmate::assert_string(biomarker) |
| 60 | 49x |
list( |
| 61 | 49x |
response = variables$rsp, |
| 62 | 49x |
arm = biomarker, |
| 63 | 49x |
covariates = variables$covariates, |
| 64 | 49x |
strata = variables$strata |
| 65 |
) |
|
| 66 |
} |
|
| 67 | ||
| 68 |
#' @describeIn h_response_biomarkers_subgroups prepares estimates for number of responses, patients and |
|
| 69 |
#' overall response rate, as well as odds ratio estimates, confidence intervals and p-values, for multiple |
|
| 70 |
#' biomarkers in a given single data set. |
|
| 71 |
#' `variables` corresponds to names of variables found in `data`, passed as a named list and requires elements |
|
| 72 |
#' `rsp` and `biomarkers` (vector of continuous biomarker variables) and optionally `covariates` |
|
| 73 |
#' and `strata`. |
|
| 74 |
#' |
|
| 75 |
#' @return |
|
| 76 |
#' * `h_logistic_mult_cont_df()` returns a `data.frame` containing estimates and statistics for the selected biomarkers. |
|
| 77 |
#' |
|
| 78 |
#' @examples |
|
| 79 |
#' # For a single population, estimate separately the effects |
|
| 80 |
#' # of two biomarkers. |
|
| 81 |
#' df <- h_logistic_mult_cont_df( |
|
| 82 |
#' variables = list( |
|
| 83 |
#' rsp = "rsp", |
|
| 84 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
| 85 |
#' covariates = "SEX" |
|
| 86 |
#' ), |
|
| 87 |
#' data = adrs_f |
|
| 88 |
#' ) |
|
| 89 |
#' df |
|
| 90 |
#' |
|
| 91 |
#' # If the data set is empty, still the corresponding rows with missings are returned. |
|
| 92 |
#' h_coxreg_mult_cont_df( |
|
| 93 |
#' variables = list( |
|
| 94 |
#' rsp = "rsp", |
|
| 95 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
| 96 |
#' covariates = "SEX", |
|
| 97 |
#' strata = "STRATA1" |
|
| 98 |
#' ), |
|
| 99 |
#' data = adrs_f[NULL, ] |
|
| 100 |
#' ) |
|
| 101 |
#' |
|
| 102 |
#' @export |
|
| 103 |
h_logistic_mult_cont_df <- function(variables, |
|
| 104 |
data, |
|
| 105 |
control = control_logistic()) {
|
|
| 106 | 28x |
if ("strat" %in% names(variables)) {
|
| 107 | ! |
warning( |
| 108 | ! |
"Warning: the `strat` element name of the `variables` list argument to `h_logistic_mult_cont_df() ", |
| 109 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 110 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 111 |
) |
|
| 112 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 113 |
} |
|
| 114 | 28x |
assert_df_with_variables(data, variables) |
| 115 | ||
| 116 | 28x |
checkmate::assert_character(variables$biomarkers, min.len = 1, any.missing = FALSE) |
| 117 | 28x |
checkmate::assert_list(control, names = "named") |
| 118 | ||
| 119 | 28x |
conf_level <- control[["conf_level"]] |
| 120 | 28x |
pval_label <- "p-value (Wald)" |
| 121 | ||
| 122 |
# If there is any data, run model, otherwise return empty results. |
|
| 123 | 28x |
if (nrow(data) > 0) {
|
| 124 | 27x |
bm_cols <- match(variables$biomarkers, names(data)) |
| 125 | 27x |
l_result <- lapply(variables$biomarkers, function(bm) {
|
| 126 | 48x |
model_fit <- fit_logistic( |
| 127 | 48x |
variables = h_rsp_to_logistic_variables(variables, bm), |
| 128 | 48x |
data = data, |
| 129 | 48x |
response_definition = control$response_definition |
| 130 |
) |
|
| 131 | 48x |
result <- h_logistic_simple_terms( |
| 132 | 48x |
x = bm, |
| 133 | 48x |
fit_glm = model_fit, |
| 134 | 48x |
conf_level = control$conf_level |
| 135 |
) |
|
| 136 | 48x |
resp_vector <- if (inherits(model_fit, "glm")) {
|
| 137 | 38x |
model_fit$model[[variables$rsp]] |
| 138 |
} else {
|
|
| 139 | 10x |
as.logical(as.matrix(model_fit$y)[, "status"]) |
| 140 |
} |
|
| 141 | 48x |
data.frame( |
| 142 |
# Dummy column needed downstream to create a nested header. |
|
| 143 | 48x |
biomarker = bm, |
| 144 | 48x |
biomarker_label = formatters::var_labels(data[bm], fill = TRUE), |
| 145 | 48x |
n_tot = length(resp_vector), |
| 146 | 48x |
n_rsp = sum(resp_vector), |
| 147 | 48x |
prop = mean(resp_vector), |
| 148 | 48x |
or = as.numeric(result[1L, "odds_ratio"]), |
| 149 | 48x |
lcl = as.numeric(result[1L, "lcl"]), |
| 150 | 48x |
ucl = as.numeric(result[1L, "ucl"]), |
| 151 | 48x |
conf_level = conf_level, |
| 152 | 48x |
pval = as.numeric(result[1L, "pvalue"]), |
| 153 | 48x |
pval_label = pval_label, |
| 154 | 48x |
stringsAsFactors = FALSE |
| 155 |
) |
|
| 156 |
}) |
|
| 157 | 27x |
do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
| 158 |
} else {
|
|
| 159 | 1x |
data.frame( |
| 160 | 1x |
biomarker = variables$biomarkers, |
| 161 | 1x |
biomarker_label = formatters::var_labels(data[variables$biomarkers], fill = TRUE), |
| 162 | 1x |
n_tot = 0L, |
| 163 | 1x |
n_rsp = 0L, |
| 164 | 1x |
prop = NA, |
| 165 | 1x |
or = NA, |
| 166 | 1x |
lcl = NA, |
| 167 | 1x |
ucl = NA, |
| 168 | 1x |
conf_level = conf_level, |
| 169 | 1x |
pval = NA, |
| 170 | 1x |
pval_label = pval_label, |
| 171 | 1x |
row.names = seq_along(variables$biomarkers), |
| 172 | 1x |
stringsAsFactors = FALSE |
| 173 |
) |
|
| 174 |
} |
|
| 175 |
} |
| 1 |
#' Count number of patients |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [analyze_num_patients()] creates a layout element to count total numbers of unique or |
|
| 6 |
#' non-unique patients. The primary analysis variable `vars` is used to uniquely identify patients. |
|
| 7 |
#' |
|
| 8 |
#' The `count_by` variable can be used to identify non-unique patients such that the number of patients with a unique |
|
| 9 |
#' combination of values in `vars` and `count_by` will be returned instead as the `nonunique` statistic. The `required` |
|
| 10 |
#' variable can be used to specify a variable required to be non-missing for the record to be included in the counts. |
|
| 11 |
#' |
|
| 12 |
#' The summarize function [summarize_num_patients()] performs the same function as [analyze_num_patients()] except it |
|
| 13 |
#' creates content rows, not data rows, to summarize the current table row/column context and operates on the level of |
|
| 14 |
#' the latest row split or the root of the table if no row splits have occurred. |
|
| 15 |
#' |
|
| 16 |
#' @inheritParams argument_convention |
|
| 17 |
#' @param required (`character` or `NULL`)\cr name of a variable that is required to be non-missing. |
|
| 18 |
#' @param count_by (`character` or `NULL`)\cr name of a variable to be combined with `vars` when counting |
|
| 19 |
#' `nonunique` records. |
|
| 20 |
#' @param unique_count_suffix (`flag`)\cr whether the `"(n)"` suffix should be added to `unique_count` labels. |
|
| 21 |
#' Defaults to `TRUE`. |
|
| 22 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 23 |
#' |
|
| 24 |
#' Options are: ``r shQuote(get_stats("summarize_num_patients"), type = "sh")``
|
|
| 25 |
#' |
|
| 26 |
#' @name summarize_num_patients |
|
| 27 |
#' @order 1 |
|
| 28 |
NULL |
|
| 29 | ||
| 30 |
#' @describeIn summarize_num_patients Statistics function which counts the number of |
|
| 31 |
#' unique patients, the corresponding percentage taken with respect to the |
|
| 32 |
#' total number of patients, and the number of non-unique patients. |
|
| 33 |
#' |
|
| 34 |
#' @param x (`character` or `factor`)\cr vector of patient IDs. |
|
| 35 |
#' |
|
| 36 |
#' @return |
|
| 37 |
#' * `s_num_patients()` returns a named `list` of 3 statistics: |
|
| 38 |
#' * `unique`: Vector of counts and percentages. |
|
| 39 |
#' * `nonunique`: Vector of counts. |
|
| 40 |
#' * `unique_count`: Counts. |
|
| 41 |
#' |
|
| 42 |
#' @examples |
|
| 43 |
#' # Use the statistics function to count number of unique and nonunique patients. |
|
| 44 |
#' s_num_patients(x = as.character(c(1, 1, 1, 2, 4, NA)), labelstr = "", .N_col = 6L) |
|
| 45 |
#' s_num_patients( |
|
| 46 |
#' x = as.character(c(1, 1, 1, 2, 4, NA)), |
|
| 47 |
#' labelstr = "", |
|
| 48 |
#' .N_col = 6L, |
|
| 49 |
#' count_by = c(1, 1, 2, 1, 1, 1) |
|
| 50 |
#' ) |
|
| 51 |
#' |
|
| 52 |
#' @export |
|
| 53 |
s_num_patients <- function(x, |
|
| 54 |
labelstr, |
|
| 55 |
.N_col, # nolint |
|
| 56 |
..., |
|
| 57 |
count_by = NULL, |
|
| 58 |
unique_count_suffix = TRUE) {
|
|
| 59 | 181x |
checkmate::assert_string(labelstr) |
| 60 | 181x |
checkmate::assert_count(.N_col) |
| 61 | 181x |
checkmate::assert_multi_class(x, classes = c("factor", "character"))
|
| 62 | 181x |
checkmate::assert_flag(unique_count_suffix) |
| 63 | ||
| 64 | 181x |
count1 <- n_available(unique(x)) |
| 65 | 181x |
count2 <- n_available(x) |
| 66 | ||
| 67 | 181x |
if (!is.null(count_by)) {
|
| 68 | 16x |
checkmate::assert_vector(count_by, len = length(x)) |
| 69 | 16x |
count2 <- n_available(unique(interaction(x, count_by))) |
| 70 |
} |
|
| 71 | ||
| 72 | 181x |
out <- list( |
| 73 | 181x |
unique = formatters::with_label(c(count1, ifelse(count1 == 0 && .N_col == 0, 0, count1 / .N_col)), labelstr), |
| 74 | 181x |
nonunique = formatters::with_label(count2, labelstr), |
| 75 | 181x |
unique_count = formatters::with_label( |
| 76 | 181x |
count1, ifelse(unique_count_suffix, paste0(labelstr, if (nzchar(labelstr)) " ", "(n)"), labelstr) |
| 77 |
) |
|
| 78 |
) |
|
| 79 | ||
| 80 | 181x |
out |
| 81 |
} |
|
| 82 | ||
| 83 |
#' @describeIn summarize_num_patients Statistics function which counts the number of unique patients |
|
| 84 |
#' in a column (variable), the corresponding percentage taken with respect to the total number of |
|
| 85 |
#' patients, and the number of non-unique patients in the column. |
|
| 86 |
#' |
|
| 87 |
#' @return |
|
| 88 |
#' * `s_num_patients_content()` returns the same values as `s_num_patients()`. |
|
| 89 |
#' |
|
| 90 |
#' @examples |
|
| 91 |
#' # Count number of unique and non-unique patients. |
|
| 92 |
#' |
|
| 93 |
#' df <- data.frame( |
|
| 94 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA)), |
|
| 95 |
#' EVENT = as.character(c(10, 15, 10, 17, 8)) |
|
| 96 |
#' ) |
|
| 97 |
#' s_num_patients_content(df, .N_col = 5, .var = "USUBJID") |
|
| 98 |
#' |
|
| 99 |
#' df_by_event <- data.frame( |
|
| 100 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA)), |
|
| 101 |
#' EVENT = c(10, 15, 10, 17, 8) |
|
| 102 |
#' ) |
|
| 103 |
#' s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID", count_by = "EVENT") |
|
| 104 |
#' |
|
| 105 |
#' @export |
|
| 106 |
s_num_patients_content <- function(df, |
|
| 107 |
labelstr = "", |
|
| 108 |
.N_col, # nolint |
|
| 109 |
.var, |
|
| 110 |
..., |
|
| 111 |
required = NULL, |
|
| 112 |
count_by = NULL, |
|
| 113 |
unique_count_suffix = TRUE) {
|
|
| 114 | 175x |
checkmate::assert_string(.var) |
| 115 | 175x |
checkmate::assert_data_frame(df) |
| 116 | 175x |
if (is.null(count_by)) {
|
| 117 | 162x |
assert_df_with_variables(df, list(id = .var)) |
| 118 |
} else {
|
|
| 119 | 13x |
assert_df_with_variables(df, list(id = .var, count_by = count_by)) |
| 120 |
} |
|
| 121 | 175x |
if (!is.null(required)) {
|
| 122 | ! |
checkmate::assert_string(required) |
| 123 | ! |
assert_df_with_variables(df, list(required = required)) |
| 124 | ! |
df <- df[!is.na(df[[required]]), , drop = FALSE] |
| 125 |
} |
|
| 126 | ||
| 127 | 175x |
x <- df[[.var]] |
| 128 | 175x |
y <- if (is.null(count_by)) NULL else df[[count_by]] |
| 129 | ||
| 130 | 175x |
s_num_patients( |
| 131 | 175x |
x = x, |
| 132 | 175x |
labelstr = labelstr, |
| 133 | 175x |
.N_col = .N_col, |
| 134 | 175x |
count_by = y, |
| 135 | 175x |
unique_count_suffix = unique_count_suffix |
| 136 |
) |
|
| 137 |
} |
|
| 138 | ||
| 139 |
#' @describeIn summarize_num_patients Formatted analysis function which is used as `afun` |
|
| 140 |
#' in `analyze_num_patients()` and as `cfun` in `summarize_num_patients()`. |
|
| 141 |
#' |
|
| 142 |
#' @return |
|
| 143 |
#' * `a_num_patients()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 144 |
#' |
|
| 145 |
#' @keywords internal |
|
| 146 |
a_num_patients <- function(df, |
|
| 147 |
labelstr = "", |
|
| 148 |
..., |
|
| 149 |
.stats = NULL, |
|
| 150 |
.stat_names = NULL, |
|
| 151 |
.formats = NULL, |
|
| 152 |
.labels = NULL, |
|
| 153 |
.indent_mods = NULL) {
|
|
| 154 |
# Check for additional parameters to the statistics function |
|
| 155 | 86x |
dots_extra_args <- list(...) |
| 156 | 86x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 157 | 86x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 158 | ||
| 159 |
# Check for user-defined functions |
|
| 160 | 86x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 161 | 86x |
.stats <- default_and_custom_stats_list$all_stats |
| 162 | 86x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 163 | ||
| 164 |
# Apply statistics function |
|
| 165 | 86x |
x_stats <- .apply_stat_functions( |
| 166 | 86x |
default_stat_fnc = s_num_patients_content, |
| 167 | 86x |
custom_stat_fnc_list = custom_stat_functions, |
| 168 | 86x |
args_list = c( |
| 169 | 86x |
df = list(df), |
| 170 | 86x |
labelstr = list(labelstr), |
| 171 | 86x |
extra_afun_params, |
| 172 | 86x |
dots_extra_args |
| 173 |
) |
|
| 174 |
) |
|
| 175 | ||
| 176 |
# Fill in formatting defaults |
|
| 177 | 86x |
.stats <- get_stats("summarize_num_patients", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 178 | 86x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 179 | 86x |
.labels <- get_labels_from_stats( |
| 180 | 86x |
.stats, .labels, |
| 181 | 86x |
tern_defaults = c(lapply(x_stats, attr, "label")[nchar(lapply(x_stats, attr, "label")) > 0], tern_default_labels) |
| 182 |
) |
|
| 183 | 86x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 184 | ||
| 185 | 86x |
x_stats <- x_stats[.stats] |
| 186 | ||
| 187 |
# Auto format handling |
|
| 188 | 86x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 189 | ||
| 190 |
# Get and check statistical names |
|
| 191 | 86x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 192 | ||
| 193 | 86x |
in_rows( |
| 194 | 86x |
.list = x_stats, |
| 195 | 86x |
.formats = .formats, |
| 196 | 86x |
.names = .labels %>% .unlist_keep_nulls(), |
| 197 | 86x |
.stat_names = .stat_names, |
| 198 | 86x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 199 | 86x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 200 |
) |
|
| 201 |
} |
|
| 202 | ||
| 203 |
#' @describeIn summarize_num_patients Layout-creating function which can take statistics function arguments |
|
| 204 |
#' and additional format arguments. This function is a wrapper for [rtables::summarize_row_groups()]. |
|
| 205 |
#' |
|
| 206 |
#' @return |
|
| 207 |
#' * `summarize_num_patients()` returns a layout object suitable for passing to further layouting functions, |
|
| 208 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 209 |
#' the statistics from `s_num_patients_content()` to the table layout. |
|
| 210 |
#' |
|
| 211 |
#' @examples |
|
| 212 |
#' # summarize_num_patients |
|
| 213 |
#' tbl <- basic_table() %>% |
|
| 214 |
#' split_cols_by("ARM") %>%
|
|
| 215 |
#' split_rows_by("SEX") %>%
|
|
| 216 |
#' summarize_num_patients("USUBJID", .stats = "unique_count") %>%
|
|
| 217 |
#' build_table(df) |
|
| 218 |
#' |
|
| 219 |
#' tbl |
|
| 220 |
#' |
|
| 221 |
#' @export |
|
| 222 |
#' @order 3 |
|
| 223 |
summarize_num_patients <- function(lyt, |
|
| 224 |
var, |
|
| 225 |
required = NULL, |
|
| 226 |
count_by = NULL, |
|
| 227 |
unique_count_suffix = TRUE, |
|
| 228 |
na_str = default_na_str(), |
|
| 229 |
riskdiff = FALSE, |
|
| 230 |
..., |
|
| 231 |
.stats = c("unique", "nonunique", "unique_count"),
|
|
| 232 |
.stat_names = NULL, |
|
| 233 |
.formats = NULL, |
|
| 234 |
.labels = list( |
|
| 235 |
unique = "Number of patients with at least one event", |
|
| 236 |
nonunique = "Number of events" |
|
| 237 |
), |
|
| 238 |
.indent_mods = 0L) {
|
|
| 239 | 17x |
checkmate::assert_flag(riskdiff) |
| 240 | 17x |
afun <- if (isFALSE(riskdiff)) a_num_patients else afun_riskdiff |
| 241 | ||
| 242 |
# Process standard extra arguments |
|
| 243 | 17x |
extra_args <- list(".stats" = .stats)
|
| 244 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 245 | 1x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 246 | 17x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 247 | 17x |
if (is.null(.indent_mods)) {
|
| 248 | ! |
indent_mod <- 0L |
| 249 | 17x |
} else if (length(.indent_mods) == 1) {
|
| 250 | 17x |
indent_mod <- .indent_mods |
| 251 |
} else {
|
|
| 252 | ! |
indent_mod <- 0L |
| 253 | ! |
extra_args[[".indent_mods"]] <- .indent_mods |
| 254 |
} |
|
| 255 | ||
| 256 |
# Process additional arguments to the statistic function |
|
| 257 | 17x |
extra_args <- c( |
| 258 | 17x |
extra_args, |
| 259 | 17x |
required = required, count_by = count_by, unique_count_suffix = unique_count_suffix, |
| 260 | 17x |
if (!isFALSE(riskdiff)) list(afun = list("s_num_patients_content" = a_num_patients)),
|
| 261 |
... |
|
| 262 |
) |
|
| 263 | ||
| 264 |
# Append additional info from layout to the analysis function |
|
| 265 | 17x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 266 | 17x |
formals(afun) <- c(formals(afun), extra_args[[".additional_fun_parameters"]]) |
| 267 | ||
| 268 | 17x |
summarize_row_groups( |
| 269 | 17x |
lyt = lyt, |
| 270 | 17x |
var = var, |
| 271 | 17x |
cfun = afun, |
| 272 | 17x |
na_str = na_str, |
| 273 | 17x |
extra_args = extra_args, |
| 274 | 17x |
indent_mod = indent_mod |
| 275 |
) |
|
| 276 |
} |
|
| 277 | ||
| 278 |
#' @describeIn summarize_num_patients Layout-creating function which can take statistics function arguments |
|
| 279 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 280 |
#' |
|
| 281 |
#' @return |
|
| 282 |
#' * `analyze_num_patients()` returns a layout object suitable for passing to further layouting functions, |
|
| 283 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 284 |
#' the statistics from `s_num_patients_content()` to the table layout. |
|
| 285 |
#' |
|
| 286 |
#' @details In general, functions that starts with `analyze*` are expected to |
|
| 287 |
#' work like [rtables::analyze()], while functions that starts with `summarize*` |
|
| 288 |
#' are based upon [rtables::summarize_row_groups()]. The latter provides a |
|
| 289 |
#' value for each dividing split in the row and column space, but, being it |
|
| 290 |
#' bound to the fundamental splits, it is repeated by design in every page |
|
| 291 |
#' when pagination is involved. |
|
| 292 |
#' |
|
| 293 |
#' @note As opposed to [summarize_num_patients()], this function does not repeat the produced rows. |
|
| 294 |
#' |
|
| 295 |
#' @examples |
|
| 296 |
#' df <- data.frame( |
|
| 297 |
#' USUBJID = as.character(c(1, 2, 1, 4, NA, 6, 6, 8, 9)), |
|
| 298 |
#' ARM = c("A", "A", "A", "A", "A", "B", "B", "B", "B"),
|
|
| 299 |
#' AGE = c(10, 15, 10, 17, 8, 11, 11, 19, 17), |
|
| 300 |
#' SEX = c("M", "M", "M", "F", "F", "F", "M", "F", "M")
|
|
| 301 |
#' ) |
|
| 302 |
#' |
|
| 303 |
#' # analyze_num_patients |
|
| 304 |
#' tbl <- basic_table() %>% |
|
| 305 |
#' split_cols_by("ARM") %>%
|
|
| 306 |
#' add_colcounts() %>% |
|
| 307 |
#' analyze_num_patients("USUBJID", .stats = c("unique")) %>%
|
|
| 308 |
#' build_table(df) |
|
| 309 |
#' |
|
| 310 |
#' tbl |
|
| 311 |
#' |
|
| 312 |
#' @export |
|
| 313 |
#' @order 2 |
|
| 314 |
analyze_num_patients <- function(lyt, |
|
| 315 |
vars, |
|
| 316 |
required = NULL, |
|
| 317 |
count_by = NULL, |
|
| 318 |
unique_count_suffix = TRUE, |
|
| 319 |
na_str = default_na_str(), |
|
| 320 |
nested = TRUE, |
|
| 321 |
show_labels = c("default", "visible", "hidden"),
|
|
| 322 |
riskdiff = FALSE, |
|
| 323 |
..., |
|
| 324 |
.stats = c("unique", "nonunique", "unique_count"),
|
|
| 325 |
.stat_names = NULL, |
|
| 326 |
.formats = NULL, |
|
| 327 |
.labels = list( |
|
| 328 |
unique = "Number of patients with at least one event", |
|
| 329 |
nonunique = "Number of events" |
|
| 330 |
), |
|
| 331 |
.indent_mods = NULL) {
|
|
| 332 | 4x |
checkmate::assert_flag(riskdiff) |
| 333 | 4x |
afun <- if (isFALSE(riskdiff)) a_num_patients else afun_riskdiff |
| 334 | ||
| 335 |
# Process standard extra arguments |
|
| 336 | 4x |
extra_args <- list(".stats" = .stats)
|
| 337 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 338 | ! |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 339 | 4x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 340 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 341 | ||
| 342 |
# Process additional arguments to the statistic function |
|
| 343 | 4x |
extra_args <- c( |
| 344 | 4x |
extra_args, |
| 345 | 4x |
required = required, count_by = count_by, unique_count_suffix = unique_count_suffix, |
| 346 | 4x |
if (!isFALSE(riskdiff)) list(afun = list("s_num_patients_content" = a_num_patients)),
|
| 347 |
... |
|
| 348 |
) |
|
| 349 | ||
| 350 |
# Append additional info from layout to the analysis function |
|
| 351 | 4x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 352 | 4x |
formals(afun) <- c(formals(afun), extra_args[[".additional_fun_parameters"]]) |
| 353 | ||
| 354 | 4x |
analyze( |
| 355 | 4x |
lyt = lyt, |
| 356 | 4x |
vars = vars, |
| 357 | 4x |
afun = afun, |
| 358 | 4x |
na_str = na_str, |
| 359 | 4x |
nested = nested, |
| 360 | 4x |
extra_args = extra_args, |
| 361 | 4x |
show_labels = show_labels |
| 362 |
) |
|
| 363 |
} |
| 1 |
#' Summarize change from baseline values or absolute baseline values |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [summarize_change()] creates a layout element to summarize the change from baseline or absolute |
|
| 6 |
#' baseline values. The primary analysis variable `vars` indicates the numerical change from baseline results. |
|
| 7 |
#' |
|
| 8 |
#' Required secondary analysis variables `value` and `baseline_flag` can be supplied to the function via |
|
| 9 |
#' the `variables` argument. The `value` element should be the name of the analysis value variable, and the |
|
| 10 |
#' `baseline_flag` element should be the name of the flag variable that indicates whether or not records contain |
|
| 11 |
#' baseline values. Depending on the baseline flag given, either the absolute baseline values (at baseline) |
|
| 12 |
#' or the change from baseline values (post-baseline) are then summarized. |
|
| 13 |
#' |
|
| 14 |
#' @inheritParams argument_convention |
|
| 15 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 16 |
#' |
|
| 17 |
#' Options are: ``r shQuote(get_stats("analyze_vars_numeric"), type = "sh")``
|
|
| 18 |
#' |
|
| 19 |
#' @name summarize_change |
|
| 20 |
#' @order 1 |
|
| 21 |
NULL |
|
| 22 | ||
| 23 |
#' @describeIn summarize_change Statistics function that summarizes baseline or post-baseline visits. |
|
| 24 |
#' |
|
| 25 |
#' @return |
|
| 26 |
#' * `s_change_from_baseline()` returns the same values returned by [s_summary.numeric()]. |
|
| 27 |
#' |
|
| 28 |
#' @note The data in `df` must be either all be from baseline or post-baseline visits. Otherwise |
|
| 29 |
#' an error will be thrown. |
|
| 30 |
#' |
|
| 31 |
#' @keywords internal |
|
| 32 |
s_change_from_baseline <- function(df, ...) {
|
|
| 33 | 10x |
args_list <- list(...) |
| 34 | 10x |
.var <- args_list[[".var"]] |
| 35 | 10x |
variables <- args_list[["variables"]] |
| 36 | ||
| 37 | 10x |
checkmate::assert_numeric(df[[variables$value]]) |
| 38 | 10x |
checkmate::assert_numeric(df[[.var]]) |
| 39 | 10x |
checkmate::assert_logical(df[[variables$baseline_flag]]) |
| 40 | 10x |
checkmate::assert_vector(unique(df[[variables$baseline_flag]]), max.len = 1) |
| 41 | 10x |
assert_df_with_variables(df, c(variables, list(chg = .var))) |
| 42 | ||
| 43 | 10x |
combined <- ifelse( |
| 44 | 10x |
df[[variables$baseline_flag]], |
| 45 | 10x |
df[[variables$value]], |
| 46 | 10x |
df[[.var]] |
| 47 |
) |
|
| 48 | 10x |
if (is.logical(combined) && identical(length(combined), 0L)) {
|
| 49 | 1x |
combined <- numeric(0) |
| 50 |
} |
|
| 51 | 10x |
s_summary(combined, ...) |
| 52 |
} |
|
| 53 | ||
| 54 |
#' @describeIn summarize_change Formatted analysis function which is used as `afun` in `summarize_change()`. |
|
| 55 |
#' |
|
| 56 |
#' @return |
|
| 57 |
#' * `a_change_from_baseline()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 58 |
#' |
|
| 59 |
#' @keywords internal |
|
| 60 |
a_change_from_baseline <- function(df, |
|
| 61 |
..., |
|
| 62 |
.stats = NULL, |
|
| 63 |
.stat_names = NULL, |
|
| 64 |
.formats = NULL, |
|
| 65 |
.labels = NULL, |
|
| 66 |
.indent_mods = NULL) {
|
|
| 67 |
# Check for additional parameters to the statistics function |
|
| 68 | 8x |
dots_extra_args <- list(...) |
| 69 | 8x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 70 | 8x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 71 | ||
| 72 |
# Check for user-defined functions |
|
| 73 | 8x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 74 | 8x |
.stats <- default_and_custom_stats_list$all_stats |
| 75 | 8x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 76 | ||
| 77 |
# Apply statistics function |
|
| 78 | 8x |
x_stats <- .apply_stat_functions( |
| 79 | 8x |
default_stat_fnc = s_change_from_baseline, |
| 80 | 8x |
custom_stat_fnc_list = custom_stat_functions, |
| 81 | 8x |
args_list = c( |
| 82 | 8x |
df = list(df), |
| 83 | 8x |
extra_afun_params, |
| 84 | 8x |
dots_extra_args |
| 85 |
) |
|
| 86 |
) |
|
| 87 | ||
| 88 |
# Fill in with formatting defaults |
|
| 89 | 6x |
.stats <- get_stats("analyze_vars_numeric", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 90 | 6x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 91 | 6x |
.labels <- get_labels_from_stats(.stats, .labels) |
| 92 | 6x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 93 | ||
| 94 | 6x |
x_stats <- x_stats[.stats] |
| 95 | ||
| 96 |
# Auto format handling |
|
| 97 | 6x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 98 | ||
| 99 |
# Get and check statistical names |
|
| 100 | 6x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 101 | ||
| 102 | 6x |
in_rows( |
| 103 | 6x |
.list = x_stats, |
| 104 | 6x |
.formats = .formats, |
| 105 | 6x |
.names = names(.labels), |
| 106 | 6x |
.stat_names = .stat_names, |
| 107 | 6x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 108 | 6x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 109 |
) |
|
| 110 |
} |
|
| 111 | ||
| 112 |
#' @describeIn summarize_change Layout-creating function which can take statistics function arguments |
|
| 113 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 114 |
#' |
|
| 115 |
#' @return |
|
| 116 |
#' * `summarize_change()` returns a layout object suitable for passing to further layouting functions, |
|
| 117 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 118 |
#' the statistics from `s_change_from_baseline()` to the table layout. |
|
| 119 |
#' |
|
| 120 |
#' @note To be used after a split on visits in the layout, such that each data subset only contains |
|
| 121 |
#' either baseline or post-baseline data. |
|
| 122 |
#' |
|
| 123 |
#' @examples |
|
| 124 |
#' library(dplyr) |
|
| 125 |
#' |
|
| 126 |
#' # Fabricate dataset |
|
| 127 |
#' dta_test <- data.frame( |
|
| 128 |
#' USUBJID = rep(1:6, each = 3), |
|
| 129 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
| 130 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)), |
|
| 131 |
#' AVAL = c(9:1, rep(NA, 9)) |
|
| 132 |
#' ) %>% |
|
| 133 |
#' mutate(ABLFLL = AVISIT == "V1") %>% |
|
| 134 |
#' group_by(USUBJID) %>% |
|
| 135 |
#' mutate( |
|
| 136 |
#' BLVAL = AVAL[ABLFLL], |
|
| 137 |
#' CHG = AVAL - BLVAL |
|
| 138 |
#' ) %>% |
|
| 139 |
#' ungroup() |
|
| 140 |
#' |
|
| 141 |
#' results <- basic_table() %>% |
|
| 142 |
#' split_cols_by("ARM") %>%
|
|
| 143 |
#' split_rows_by("AVISIT") %>%
|
|
| 144 |
#' summarize_change("CHG", variables = list(value = "AVAL", baseline_flag = "ABLFLL")) %>%
|
|
| 145 |
#' build_table(dta_test) |
|
| 146 |
#' |
|
| 147 |
#' results |
|
| 148 |
#' |
|
| 149 |
#' @export |
|
| 150 |
#' @order 2 |
|
| 151 |
summarize_change <- function(lyt, |
|
| 152 |
vars, |
|
| 153 |
variables, |
|
| 154 |
var_labels = vars, |
|
| 155 |
na_str = default_na_str(), |
|
| 156 |
na_rm = TRUE, |
|
| 157 |
nested = TRUE, |
|
| 158 |
show_labels = "default", |
|
| 159 |
table_names = vars, |
|
| 160 |
section_div = NA_character_, |
|
| 161 |
..., |
|
| 162 |
.stats = c("n", "mean_sd", "median", "range"),
|
|
| 163 |
.stat_names = NULL, |
|
| 164 |
.formats = c( |
|
| 165 |
mean_sd = "xx.xx (xx.xx)", |
|
| 166 |
mean_se = "xx.xx (xx.xx)", |
|
| 167 |
median = "xx.xx", |
|
| 168 |
range = "xx.xx - xx.xx", |
|
| 169 |
mean_pval = "xx.xx" |
|
| 170 |
), |
|
| 171 |
.labels = NULL, |
|
| 172 |
.indent_mods = NULL) {
|
|
| 173 |
# Process standard extra arguments |
|
| 174 | 4x |
extra_args <- list(".stats" = .stats)
|
| 175 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 176 | 4x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 177 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 178 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 179 | ||
| 180 |
# Process additional arguments to the statistic function |
|
| 181 | 4x |
extra_args <- c( |
| 182 | 4x |
extra_args, |
| 183 | 4x |
variables = list(variables), |
| 184 | 4x |
na_rm = na_rm, |
| 185 |
... |
|
| 186 |
) |
|
| 187 | ||
| 188 |
# Append additional info from layout to the analysis function |
|
| 189 | 4x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 190 | 4x |
formals(a_change_from_baseline) <- c(formals(a_change_from_baseline), extra_args[[".additional_fun_parameters"]]) |
| 191 | ||
| 192 | 4x |
analyze( |
| 193 | 4x |
lyt = lyt, |
| 194 | 4x |
vars = vars, |
| 195 | 4x |
afun = a_change_from_baseline, |
| 196 | 4x |
na_str = na_str, |
| 197 | 4x |
nested = nested, |
| 198 | 4x |
extra_args = extra_args, |
| 199 | 4x |
var_labels = var_labels, |
| 200 | 4x |
show_labels = show_labels, |
| 201 | 4x |
table_names = table_names, |
| 202 | 4x |
inclNAs = !na_rm, |
| 203 | 4x |
section_div = section_div |
| 204 |
) |
|
| 205 |
} |
| 1 |
#' Count patients with toxicity grades that have worsened from baseline by highest grade post-baseline |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_abnormal_lab_worsen_by_baseline()] creates a layout element to count patients with |
|
| 6 |
#' analysis toxicity grades which have worsened from baseline, categorized by highest (worst) grade post-baseline. |
|
| 7 |
#' |
|
| 8 |
#' This function analyzes primary analysis variable `var` which indicates analysis toxicity grades. Additional |
|
| 9 |
#' analysis variables that can be supplied as a list via the `variables` parameter are `id` (defaults to `USUBJID`), |
|
| 10 |
#' a variable to indicate unique subject identifiers, `baseline_var` (defaults to `BTOXGR`), a variable to indicate |
|
| 11 |
#' baseline toxicity grades, and `direction_var` (defaults to `GRADDIR`), a variable to indicate toxicity grade |
|
| 12 |
#' directions of interest to include (e.g. `"H"` (high), `"L"` (low), or `"B"` (both)). |
|
| 13 |
#' |
|
| 14 |
#' For the direction(s) specified in `direction_var`, patient counts by worst grade for patients who have |
|
| 15 |
#' worsened from baseline are calculated as follows: |
|
| 16 |
#' * `1` to `4`: The number of patients who have worsened from their baseline grades with worst |
|
| 17 |
#' grades 1-4, respectively. |
|
| 18 |
#' * `Any`: The total number of patients who have worsened from their baseline grades. |
|
| 19 |
#' |
|
| 20 |
#' Fractions are calculated by dividing the above counts by the number of patients who's analysis toxicity grades |
|
| 21 |
#' have worsened from baseline toxicity grades during treatment. |
|
| 22 |
#' |
|
| 23 |
#' Prior to using this function in your table layout you must use [rtables::split_rows_by()] to create a row |
|
| 24 |
#' split on variable `direction_var`. |
|
| 25 |
#' |
|
| 26 |
#' @inheritParams argument_convention |
|
| 27 |
#' @param variables (named `list` of `string`)\cr list of additional analysis variables including: |
|
| 28 |
#' * `id` (`string`)\cr subject variable name. |
|
| 29 |
#' * `baseline_var` (`string`)\cr name of the data column containing baseline toxicity variable. |
|
| 30 |
#' * `direction_var` (`string`)\cr see `direction_var` for more details. |
|
| 31 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 32 |
#' @param table_names `r lifecycle::badge("deprecated")` this parameter has no effect.
|
|
| 33 |
#' |
|
| 34 |
#' Options are: ``r shQuote(get_stats("abnormal_lab_worsen_by_baseline"), type = "sh")``
|
|
| 35 |
#' |
|
| 36 |
#' @seealso Relevant helper functions [h_adlb_worsen()] and [h_worsen_counter()] which are used within |
|
| 37 |
#' [s_count_abnormal_lab_worsen_by_baseline()] to process input data. |
|
| 38 |
#' |
|
| 39 |
#' @name abnormal_lab_worsen_by_baseline |
|
| 40 |
#' @order 1 |
|
| 41 |
NULL |
|
| 42 | ||
| 43 |
#' @describeIn abnormal_lab_worsen_by_baseline Statistics function for patients whose worst post-baseline |
|
| 44 |
#' lab grades are worse than their baseline grades. |
|
| 45 |
#' |
|
| 46 |
#' @return |
|
| 47 |
#' * `s_count_abnormal_lab_worsen_by_baseline()` returns the counts and fraction of patients whose worst |
|
| 48 |
#' post-baseline lab grades are worse than their baseline grades, for post-baseline worst grades |
|
| 49 |
#' "1", "2", "3", "4" and "Any". |
|
| 50 |
#' |
|
| 51 |
#' @keywords internal |
|
| 52 |
s_count_abnormal_lab_worsen_by_baseline <- function(df, |
|
| 53 |
.var = "ATOXGR", |
|
| 54 |
variables = list( |
|
| 55 |
id = "USUBJID", |
|
| 56 |
baseline_var = "BTOXGR", |
|
| 57 |
direction_var = "GRADDR" |
|
| 58 |
), |
|
| 59 |
...) {
|
|
| 60 | 13x |
checkmate::assert_string(.var) |
| 61 | 13x |
checkmate::assert_set_equal(names(variables), c("id", "baseline_var", "direction_var"))
|
| 62 | 13x |
checkmate::assert_string(variables$id) |
| 63 | 13x |
checkmate::assert_string(variables$baseline_var) |
| 64 | 13x |
checkmate::assert_string(variables$direction_var) |
| 65 | 13x |
assert_df_with_variables(df, c(aval = .var, variables[1:3])) |
| 66 | 13x |
assert_list_of_variables(variables) |
| 67 | ||
| 68 | 13x |
h_worsen_counter(df, variables$id, .var, variables$baseline_var, variables$direction_var) |
| 69 |
} |
|
| 70 | ||
| 71 |
#' @describeIn abnormal_lab_worsen_by_baseline Formatted analysis function which is used as `afun` |
|
| 72 |
#' in `count_abnormal_lab_worsen_by_baseline()`. |
|
| 73 |
#' |
|
| 74 |
#' @return |
|
| 75 |
#' * `a_count_abnormal_lab_worsen_by_baseline()` returns the corresponding list with |
|
| 76 |
#' formatted [rtables::CellValue()]. |
|
| 77 |
#' |
|
| 78 |
#' @keywords internal |
|
| 79 |
a_count_abnormal_lab_worsen_by_baseline <- function(df, |
|
| 80 |
..., |
|
| 81 |
.stats = NULL, |
|
| 82 |
.stat_names = NULL, |
|
| 83 |
.formats = NULL, |
|
| 84 |
.labels = NULL, |
|
| 85 |
.indent_mods = NULL) {
|
|
| 86 |
# Check for additional parameters to the statistics function |
|
| 87 | 12x |
dots_extra_args <- list(...) |
| 88 | 12x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 89 | 12x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 90 | ||
| 91 |
# Check for user-defined functions |
|
| 92 | 12x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 93 | 12x |
.stats <- default_and_custom_stats_list$all_stats |
| 94 | 12x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 95 | ||
| 96 |
# Apply statistics function |
|
| 97 | 12x |
x_stats <- .apply_stat_functions( |
| 98 | 12x |
default_stat_fnc = s_count_abnormal_lab_worsen_by_baseline, |
| 99 | 12x |
custom_stat_fnc_list = custom_stat_functions, |
| 100 | 12x |
args_list = c( |
| 101 | 12x |
df = list(df), |
| 102 | 12x |
extra_afun_params, |
| 103 | 12x |
dots_extra_args |
| 104 |
) |
|
| 105 |
) |
|
| 106 | ||
| 107 |
# Fill in formatting defaults |
|
| 108 | 12x |
.stats <- get_stats( |
| 109 | 12x |
"abnormal_lab_worsen_by_baseline", |
| 110 | 12x |
stats_in = .stats, |
| 111 | 12x |
custom_stats_in = names(custom_stat_functions) |
| 112 |
) |
|
| 113 | 12x |
levels_per_stats <- lapply(x_stats, names) |
| 114 | 12x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 115 | 12x |
.labels <- get_labels_from_stats(.stats, .labels, levels_per_stats) |
| 116 | 12x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 117 | ||
| 118 | 12x |
x_stats <- x_stats[.stats] %>% |
| 119 | 12x |
.unlist_keep_nulls() %>% |
| 120 | 12x |
setNames(names(.formats)) |
| 121 | ||
| 122 |
# Auto format handling |
|
| 123 | 12x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 124 | ||
| 125 |
# Get and check statistical names |
|
| 126 | 12x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 127 | ||
| 128 | 12x |
in_rows( |
| 129 | 12x |
.list = x_stats, |
| 130 | 12x |
.formats = .formats, |
| 131 | 12x |
.names = .labels %>% .unlist_keep_nulls(), |
| 132 | 12x |
.stat_names = .stat_names, |
| 133 | 12x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 134 | 12x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 135 |
) |
|
| 136 |
} |
|
| 137 | ||
| 138 |
#' @describeIn abnormal_lab_worsen_by_baseline Layout-creating function which can take statistics function |
|
| 139 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 140 |
#' |
|
| 141 |
#' @return |
|
| 142 |
#' * `count_abnormal_lab_worsen_by_baseline()` returns a layout object suitable for passing to further layouting |
|
| 143 |
#' functions, or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted |
|
| 144 |
#' rows containing the statistics from `s_count_abnormal_lab_worsen_by_baseline()` to the table layout. |
|
| 145 |
#' |
|
| 146 |
#' @examples |
|
| 147 |
#' library(dplyr) |
|
| 148 |
#' |
|
| 149 |
#' # The direction variable, GRADDR, is based on metadata |
|
| 150 |
#' adlb <- tern_ex_adlb %>% |
|
| 151 |
#' mutate( |
|
| 152 |
#' GRADDR = case_when( |
|
| 153 |
#' PARAMCD == "ALT" ~ "B", |
|
| 154 |
#' PARAMCD == "CRP" ~ "L", |
|
| 155 |
#' PARAMCD == "IGA" ~ "H" |
|
| 156 |
#' ) |
|
| 157 |
#' ) %>% |
|
| 158 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "") |
|
| 159 |
#' |
|
| 160 |
#' df <- h_adlb_worsen( |
|
| 161 |
#' adlb, |
|
| 162 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
| 163 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
| 164 |
#' direction_var = "GRADDR" |
|
| 165 |
#' ) |
|
| 166 |
#' |
|
| 167 |
#' basic_table() %>% |
|
| 168 |
#' split_cols_by("ARMCD") %>%
|
|
| 169 |
#' add_colcounts() %>% |
|
| 170 |
#' split_rows_by("PARAMCD") %>%
|
|
| 171 |
#' split_rows_by("GRADDR") %>%
|
|
| 172 |
#' count_abnormal_lab_worsen_by_baseline( |
|
| 173 |
#' var = "ATOXGR", |
|
| 174 |
#' variables = list( |
|
| 175 |
#' id = "USUBJID", |
|
| 176 |
#' baseline_var = "BTOXGR", |
|
| 177 |
#' direction_var = "GRADDR" |
|
| 178 |
#' ) |
|
| 179 |
#' ) %>% |
|
| 180 |
#' append_topleft("Direction of Abnormality") %>%
|
|
| 181 |
#' build_table(df = df, alt_counts_df = tern_ex_adsl) |
|
| 182 |
#' |
|
| 183 |
#' @export |
|
| 184 |
#' @order 2 |
|
| 185 |
count_abnormal_lab_worsen_by_baseline <- function(lyt, |
|
| 186 |
var, |
|
| 187 |
variables = list( |
|
| 188 |
id = "USUBJID", |
|
| 189 |
baseline_var = "BTOXGR", |
|
| 190 |
direction_var = "GRADDR" |
|
| 191 |
), |
|
| 192 |
na_str = default_na_str(), |
|
| 193 |
nested = TRUE, |
|
| 194 |
..., |
|
| 195 |
table_names = lifecycle::deprecated(), |
|
| 196 |
.stats = "fraction", |
|
| 197 |
.stat_names = NULL, |
|
| 198 |
.formats = list(fraction = format_fraction), |
|
| 199 |
.labels = NULL, |
|
| 200 |
.indent_mods = NULL) {
|
|
| 201 | 1x |
checkmate::assert_string(var) |
| 202 | ||
| 203 |
# Deprecated argument warning |
|
| 204 | 1x |
if (lifecycle::is_present(table_names)) {
|
| 205 | ! |
lifecycle::deprecate_warn( |
| 206 | ! |
"0.9.8", "count_abnormal_lab_worsen_by_baseline(table_names)", |
| 207 | ! |
details = "The argument has no effect on the output." |
| 208 |
) |
|
| 209 |
} |
|
| 210 | ||
| 211 |
# Process standard extra arguments |
|
| 212 | 1x |
extra_args <- list(".stats" = .stats)
|
| 213 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 214 | 1x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 215 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 216 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 217 | ||
| 218 |
# Process additional arguments to the statistic function |
|
| 219 | 1x |
extra_args <- c(extra_args, "variables" = list(variables), ...) |
| 220 | ||
| 221 |
# Append additional info from layout to the analysis function |
|
| 222 | 1x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 223 | 1x |
formals(a_count_abnormal_lab_worsen_by_baseline) <- c( |
| 224 | 1x |
formals(a_count_abnormal_lab_worsen_by_baseline), extra_args[[".additional_fun_parameters"]] |
| 225 |
) |
|
| 226 | ||
| 227 | 1x |
analyze( |
| 228 | 1x |
lyt = lyt, |
| 229 | 1x |
vars = var, |
| 230 | 1x |
afun = a_count_abnormal_lab_worsen_by_baseline, |
| 231 | 1x |
na_str = na_str, |
| 232 | 1x |
nested = nested, |
| 233 | 1x |
extra_args = extra_args, |
| 234 | 1x |
show_labels = "hidden" |
| 235 |
) |
|
| 236 |
} |
|
| 237 | ||
| 238 |
#' Helper function to prepare ADLB with worst labs |
|
| 239 |
#' |
|
| 240 |
#' @description `r lifecycle::badge("stable")`
|
|
| 241 |
#' |
|
| 242 |
#' Helper function to prepare a `df` for generate the patient count shift table. |
|
| 243 |
#' |
|
| 244 |
#' @param adlb (`data.frame`)\cr ADLB data frame. |
|
| 245 |
#' @param worst_flag_low (named `vector`)\cr worst low post-baseline lab grade flag variable. See how this is |
|
| 246 |
#' implemented in the following examples. |
|
| 247 |
#' @param worst_flag_high (named `vector`)\cr worst high post-baseline lab grade flag variable. See how this is |
|
| 248 |
#' implemented in the following examples. |
|
| 249 |
#' @param direction_var (`string`)\cr name of the direction variable specifying the direction of the shift table of |
|
| 250 |
#' interest. Only lab records flagged by `L`, `H` or `B` are included in the shift table. |
|
| 251 |
#' * `L`: low direction only |
|
| 252 |
#' * `H`: high direction only |
|
| 253 |
#' * `B`: both low and high directions |
|
| 254 |
#' |
|
| 255 |
#' @return `h_adlb_worsen()` returns the `adlb` `data.frame` containing only the |
|
| 256 |
#' worst labs specified according to `worst_flag_low` or `worst_flag_high` for the |
|
| 257 |
#' direction specified according to `direction_var`. For instance, for a lab that is |
|
| 258 |
#' needed for the low direction only, only records flagged by `worst_flag_low` are |
|
| 259 |
#' selected. For a lab that is needed for both low and high directions, the worst |
|
| 260 |
#' low records are selected for the low direction, and the worst high record are selected |
|
| 261 |
#' for the high direction. |
|
| 262 |
#' |
|
| 263 |
#' @seealso [abnormal_lab_worsen_by_baseline] |
|
| 264 |
#' |
|
| 265 |
#' @examples |
|
| 266 |
#' library(dplyr) |
|
| 267 |
#' |
|
| 268 |
#' # The direction variable, GRADDR, is based on metadata |
|
| 269 |
#' adlb <- tern_ex_adlb %>% |
|
| 270 |
#' mutate( |
|
| 271 |
#' GRADDR = case_when( |
|
| 272 |
#' PARAMCD == "ALT" ~ "B", |
|
| 273 |
#' PARAMCD == "CRP" ~ "L", |
|
| 274 |
#' PARAMCD == "IGA" ~ "H" |
|
| 275 |
#' ) |
|
| 276 |
#' ) %>% |
|
| 277 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "") |
|
| 278 |
#' |
|
| 279 |
#' df <- h_adlb_worsen( |
|
| 280 |
#' adlb, |
|
| 281 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
| 282 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
| 283 |
#' direction_var = "GRADDR" |
|
| 284 |
#' ) |
|
| 285 |
#' |
|
| 286 |
#' @export |
|
| 287 |
h_adlb_worsen <- function(adlb, |
|
| 288 |
worst_flag_low = NULL, |
|
| 289 |
worst_flag_high = NULL, |
|
| 290 |
direction_var) {
|
|
| 291 | 5x |
checkmate::assert_string(direction_var) |
| 292 | 5x |
checkmate::assert_subset(as.character(unique(adlb[[direction_var]])), c("B", "L", "H"))
|
| 293 | 5x |
assert_df_with_variables(adlb, list("Col" = direction_var))
|
| 294 | ||
| 295 | 5x |
if (any(unique(adlb[[direction_var]]) == "H")) {
|
| 296 | 4x |
assert_df_with_variables(adlb, list("High" = names(worst_flag_high)))
|
| 297 |
} |
|
| 298 | ||
| 299 | 5x |
if (any(unique(adlb[[direction_var]]) == "L")) {
|
| 300 | 4x |
assert_df_with_variables(adlb, list("Low" = names(worst_flag_low)))
|
| 301 |
} |
|
| 302 | ||
| 303 | 5x |
if (any(unique(adlb[[direction_var]]) == "B")) {
|
| 304 | 3x |
assert_df_with_variables( |
| 305 | 3x |
adlb, |
| 306 | 3x |
list( |
| 307 | 3x |
"Low" = names(worst_flag_low), |
| 308 | 3x |
"High" = names(worst_flag_high) |
| 309 |
) |
|
| 310 |
) |
|
| 311 |
} |
|
| 312 | ||
| 313 |
# extract patients with worst post-baseline lab, either low or high or both |
|
| 314 | 5x |
worst_flag <- c(worst_flag_low, worst_flag_high) |
| 315 | 5x |
col_names <- names(worst_flag) |
| 316 | 5x |
filter_values <- worst_flag |
| 317 | 5x |
temp <- Map( |
| 318 | 5x |
function(x, y) which(adlb[[x]] == y), |
| 319 | 5x |
col_names, |
| 320 | 5x |
filter_values |
| 321 |
) |
|
| 322 | 5x |
position_satisfy_filters <- Reduce(union, temp) |
| 323 | ||
| 324 |
# select variables of interest |
|
| 325 | 5x |
adlb_f <- adlb[position_satisfy_filters, ] |
| 326 | ||
| 327 |
# generate subsets for different directionality |
|
| 328 | 5x |
adlb_f_h <- adlb_f[which(adlb_f[[direction_var]] == "H"), ] |
| 329 | 5x |
adlb_f_l <- adlb_f[which(adlb_f[[direction_var]] == "L"), ] |
| 330 | 5x |
adlb_f_b <- adlb_f[which(adlb_f[[direction_var]] == "B"), ] |
| 331 | ||
| 332 |
# for labs requiring both high and low, data is duplicated and will be stacked on top of each other |
|
| 333 | 5x |
adlb_f_b_h <- adlb_f_b |
| 334 | 5x |
adlb_f_b_l <- adlb_f_b |
| 335 | ||
| 336 |
# extract data with worst lab |
|
| 337 | 5x |
if (!is.null(worst_flag_high) && !is.null(worst_flag_low)) {
|
| 338 |
# change H to High, L to Low |
|
| 339 | 3x |
adlb_f_h[[direction_var]] <- rep("High", nrow(adlb_f_h))
|
| 340 | 3x |
adlb_f_l[[direction_var]] <- rep("Low", nrow(adlb_f_l))
|
| 341 | ||
| 342 |
# change, B to High and Low |
|
| 343 | 3x |
adlb_f_b_h[[direction_var]] <- rep("High", nrow(adlb_f_b_h))
|
| 344 | 3x |
adlb_f_b_l[[direction_var]] <- rep("Low", nrow(adlb_f_b_l))
|
| 345 | ||
| 346 | 3x |
adlb_out_h <- adlb_f_h[which(adlb_f_h[[names(worst_flag_high)]] == worst_flag_high), ] |
| 347 | 3x |
adlb_out_b_h <- adlb_f_b_h[which(adlb_f_b_h[[names(worst_flag_high)]] == worst_flag_high), ] |
| 348 | 3x |
adlb_out_l <- adlb_f_l[which(adlb_f_l[[names(worst_flag_low)]] == worst_flag_low), ] |
| 349 | 3x |
adlb_out_b_l <- adlb_f_b_l[which(adlb_f_b_l[[names(worst_flag_low)]] == worst_flag_low), ] |
| 350 | ||
| 351 | 3x |
out <- rbind(adlb_out_h, adlb_out_b_h, adlb_out_l, adlb_out_b_l) |
| 352 | 2x |
} else if (!is.null(worst_flag_high)) {
|
| 353 | 1x |
adlb_f_h[[direction_var]] <- rep("High", nrow(adlb_f_h))
|
| 354 | 1x |
adlb_f_b_h[[direction_var]] <- rep("High", nrow(adlb_f_b_h))
|
| 355 | ||
| 356 | 1x |
adlb_out_h <- adlb_f_h[which(adlb_f_h[[names(worst_flag_high)]] == worst_flag_high), ] |
| 357 | 1x |
adlb_out_b_h <- adlb_f_b_h[which(adlb_f_b_h[[names(worst_flag_high)]] == worst_flag_high), ] |
| 358 | ||
| 359 | 1x |
out <- rbind(adlb_out_h, adlb_out_b_h) |
| 360 | 1x |
} else if (!is.null(worst_flag_low)) {
|
| 361 | 1x |
adlb_f_l[[direction_var]] <- rep("Low", nrow(adlb_f_l))
|
| 362 | 1x |
adlb_f_b_l[[direction_var]] <- rep("Low", nrow(adlb_f_b_l))
|
| 363 | ||
| 364 | 1x |
adlb_out_l <- adlb_f_l[which(adlb_f_l[[names(worst_flag_low)]] == worst_flag_low), ] |
| 365 | 1x |
adlb_out_b_l <- adlb_f_b_l[which(adlb_f_b_l[[names(worst_flag_low)]] == worst_flag_low), ] |
| 366 | ||
| 367 | 1x |
out <- rbind(adlb_out_l, adlb_out_b_l) |
| 368 |
} |
|
| 369 | ||
| 370 |
# label |
|
| 371 | 5x |
formatters::var_labels(out) <- formatters::var_labels(adlb_f, fill = FALSE) |
| 372 | ||
| 373 | 5x |
out |
| 374 |
} |
|
| 375 | ||
| 376 |
#' Helper function to analyze patients for `s_count_abnormal_lab_worsen_by_baseline()` |
|
| 377 |
#' |
|
| 378 |
#' @description `r lifecycle::badge("stable")`
|
|
| 379 |
#' |
|
| 380 |
#' Helper function to count the number of patients and the fraction of patients according to |
|
| 381 |
#' highest post-baseline lab grade variable `.var`, baseline lab grade variable `baseline_var`, |
|
| 382 |
#' and the direction of interest specified in `direction_var`. |
|
| 383 |
#' |
|
| 384 |
#' @inheritParams argument_convention |
|
| 385 |
#' @inheritParams h_adlb_worsen |
|
| 386 |
#' @param baseline_var (`string`)\cr name of the baseline lab grade variable. |
|
| 387 |
#' |
|
| 388 |
#' @return The counts and fraction of patients |
|
| 389 |
#' whose worst post-baseline lab grades are worse than their baseline grades, for |
|
| 390 |
#' post-baseline worst grades "1", "2", "3", "4" and "Any". |
|
| 391 |
#' |
|
| 392 |
#' @seealso [abnormal_lab_worsen_by_baseline] |
|
| 393 |
#' |
|
| 394 |
#' @examples |
|
| 395 |
#' library(dplyr) |
|
| 396 |
#' |
|
| 397 |
#' # The direction variable, GRADDR, is based on metadata |
|
| 398 |
#' adlb <- tern_ex_adlb %>% |
|
| 399 |
#' mutate( |
|
| 400 |
#' GRADDR = case_when( |
|
| 401 |
#' PARAMCD == "ALT" ~ "B", |
|
| 402 |
#' PARAMCD == "CRP" ~ "L", |
|
| 403 |
#' PARAMCD == "IGA" ~ "H" |
|
| 404 |
#' ) |
|
| 405 |
#' ) %>% |
|
| 406 |
#' filter(SAFFL == "Y" & ONTRTFL == "Y" & GRADDR != "") |
|
| 407 |
#' |
|
| 408 |
#' df <- h_adlb_worsen( |
|
| 409 |
#' adlb, |
|
| 410 |
#' worst_flag_low = c("WGRLOFL" = "Y"),
|
|
| 411 |
#' worst_flag_high = c("WGRHIFL" = "Y"),
|
|
| 412 |
#' direction_var = "GRADDR" |
|
| 413 |
#' ) |
|
| 414 |
#' |
|
| 415 |
#' # `h_worsen_counter` |
|
| 416 |
#' h_worsen_counter( |
|
| 417 |
#' df %>% filter(PARAMCD == "CRP" & GRADDR == "Low"), |
|
| 418 |
#' id = "USUBJID", |
|
| 419 |
#' .var = "ATOXGR", |
|
| 420 |
#' baseline_var = "BTOXGR", |
|
| 421 |
#' direction_var = "GRADDR" |
|
| 422 |
#' ) |
|
| 423 |
#' |
|
| 424 |
#' @export |
|
| 425 |
h_worsen_counter <- function(df, id, .var, baseline_var, direction_var) {
|
|
| 426 | 17x |
checkmate::assert_string(id) |
| 427 | 17x |
checkmate::assert_string(.var) |
| 428 | 17x |
checkmate::assert_string(baseline_var) |
| 429 | 17x |
checkmate::assert_scalar(unique(df[[direction_var]])) |
| 430 | 17x |
checkmate::assert_subset(unique(df[[direction_var]]), c("High", "Low"))
|
| 431 | 17x |
assert_df_with_variables(df, list(val = c(id, .var, baseline_var, direction_var))) |
| 432 | ||
| 433 |
# remove post-baseline missing |
|
| 434 | 17x |
df <- df[df[[.var]] != "<Missing>", ] |
| 435 | ||
| 436 |
# obtain directionality |
|
| 437 | 17x |
direction <- unique(df[[direction_var]]) |
| 438 | ||
| 439 | 17x |
if (direction == "Low") {
|
| 440 | 10x |
grade <- -1:-4 |
| 441 | 10x |
worst_grade <- -4 |
| 442 | 7x |
} else if (direction == "High") {
|
| 443 | 7x |
grade <- 1:4 |
| 444 | 7x |
worst_grade <- 4 |
| 445 |
} |
|
| 446 | ||
| 447 | 17x |
if (nrow(df) > 0) {
|
| 448 | 17x |
by_grade <- lapply(grade, function(i) {
|
| 449 |
# filter baseline values that is less than i or <Missing> |
|
| 450 | 68x |
df_temp <- df[df[[baseline_var]] %in% c((i + sign(i) * -1):(-1 * worst_grade), "<Missing>"), ] |
| 451 |
# num: number of patients with post-baseline worst lab equal to i |
|
| 452 | 68x |
num <- length(unique(df_temp[df_temp[[.var]] %in% i, id, drop = TRUE])) |
| 453 |
# denom: number of patients with baseline values less than i or <missing> and post-baseline in the same direction |
|
| 454 | 68x |
denom <- length(unique(df_temp[[id]])) |
| 455 | 68x |
rm(df_temp) |
| 456 | 68x |
c(num = num, denom = denom) |
| 457 |
}) |
|
| 458 |
} else {
|
|
| 459 | ! |
by_grade <- lapply(1, function(i) {
|
| 460 | ! |
c(num = 0, denom = 0) |
| 461 |
}) |
|
| 462 |
} |
|
| 463 | ||
| 464 | 17x |
names(by_grade) <- as.character(seq_along(by_grade)) |
| 465 | ||
| 466 |
# baseline grade less 4 or missing |
|
| 467 | 17x |
df_temp <- df[!df[[baseline_var]] %in% worst_grade, ] |
| 468 | ||
| 469 |
# denom: number of patients with baseline values less than 4 or <missing> and post-baseline in the same direction |
|
| 470 | 17x |
denom <- length(unique(df_temp[, id, drop = TRUE])) |
| 471 | ||
| 472 |
# condition 1: missing baseline and in the direction of abnormality |
|
| 473 | 17x |
con1 <- which(df_temp[[baseline_var]] == "<Missing>" & df_temp[[.var]] %in% grade) |
| 474 | 17x |
df_temp_nm <- df_temp[which(df_temp[[baseline_var]] != "<Missing>" & df_temp[[.var]] %in% grade), ] |
| 475 | ||
| 476 |
# condition 2: if post-baseline values are present then post-baseline values must be worse than baseline |
|
| 477 | 17x |
if (direction == "Low") {
|
| 478 | 10x |
con2 <- which(as.numeric(as.character(df_temp_nm[[.var]])) < as.numeric(as.character(df_temp_nm[[baseline_var]]))) |
| 479 |
} else {
|
|
| 480 | 7x |
con2 <- which(as.numeric(as.character(df_temp_nm[[.var]])) > as.numeric(as.character(df_temp_nm[[baseline_var]]))) |
| 481 |
} |
|
| 482 | ||
| 483 |
# number of patients satisfy either conditions 1 or 2 |
|
| 484 | 17x |
num <- length(unique(df_temp[union(con1, con2), id, drop = TRUE])) |
| 485 | ||
| 486 | 17x |
list(fraction = c(by_grade, list("Any" = c(num = num, denom = denom))))
|
| 487 |
} |
| 1 |
#' Odds ratio estimation |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [estimate_odds_ratio()] creates a layout element to compare bivariate responses between |
|
| 6 |
#' two groups by estimating an odds ratio and its confidence interval. |
|
| 7 |
#' |
|
| 8 |
#' The primary analysis variable specified by `vars` is the group variable. Additional variables can be included in the |
|
| 9 |
#' analysis via the `variables` argument, which accepts `arm`, an arm variable, and `strata`, a stratification variable. |
|
| 10 |
#' If more than two arm levels are present, they can be combined into two groups using the `groups_list` argument. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams split_cols_by_groups |
|
| 13 |
#' @inheritParams argument_convention |
|
| 14 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 15 |
#' |
|
| 16 |
#' Options are: ``r shQuote(get_stats("estimate_odds_ratio"), type = "sh")``
|
|
| 17 |
#' @param method (`string`)\cr whether to use the correct (`"exact"`) calculation in the conditional likelihood or one |
|
| 18 |
#' of the approximations. See [survival::clogit()] for details. |
|
| 19 |
#' |
|
| 20 |
#' @note |
|
| 21 |
#' * This function uses logistic regression for unstratified analyses, and conditional logistic regression for |
|
| 22 |
#' stratified analyses. The Wald confidence interval is calculated with the specified confidence level. |
|
| 23 |
#' * For stratified analyses, there is currently no implementation for conditional likelihood confidence intervals, |
|
| 24 |
#' therefore the likelihood confidence interval is not available as an option. |
|
| 25 |
#' * When `vars` contains only responders or non-responders no odds ratio estimation is possible so the returned |
|
| 26 |
#' values will be `NA`. |
|
| 27 |
#' |
|
| 28 |
#' @seealso Relevant helper function [h_odds_ratio()]. |
|
| 29 |
#' |
|
| 30 |
#' @name odds_ratio |
|
| 31 |
#' @order 1 |
|
| 32 |
NULL |
|
| 33 | ||
| 34 |
#' @describeIn odds_ratio Statistics function which estimates the odds ratio |
|
| 35 |
#' between a treatment and a control. A `variables` list with `arm` and `strata` |
|
| 36 |
#' variable names must be passed if a stratified analysis is required. |
|
| 37 |
#' |
|
| 38 |
#' @return |
|
| 39 |
#' * `s_odds_ratio()` returns a named list with the statistics `or_ci` |
|
| 40 |
#' (containing `est`, `lcl`, and `ucl`) and `n_tot`. |
|
| 41 |
#' |
|
| 42 |
#' @examples |
|
| 43 |
#' # Unstratified analysis. |
|
| 44 |
#' s_odds_ratio( |
|
| 45 |
#' df = subset(dta, grp == "A"), |
|
| 46 |
#' .var = "rsp", |
|
| 47 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 48 |
#' .in_ref_col = FALSE, |
|
| 49 |
#' .df_row = dta |
|
| 50 |
#' ) |
|
| 51 |
#' |
|
| 52 |
#' # Stratified analysis. |
|
| 53 |
#' s_odds_ratio( |
|
| 54 |
#' df = subset(dta, grp == "A"), |
|
| 55 |
#' .var = "rsp", |
|
| 56 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 57 |
#' .in_ref_col = FALSE, |
|
| 58 |
#' .df_row = dta, |
|
| 59 |
#' variables = list(arm = "grp", strata = "strata") |
|
| 60 |
#' ) |
|
| 61 |
#' |
|
| 62 |
#' @export |
|
| 63 |
s_odds_ratio <- function(df, |
|
| 64 |
.var, |
|
| 65 |
.ref_group, |
|
| 66 |
.in_ref_col, |
|
| 67 |
.df_row, |
|
| 68 |
variables = list(arm = NULL, strata = NULL), |
|
| 69 |
conf_level = 0.95, |
|
| 70 |
groups_list = NULL, |
|
| 71 |
method = "exact", |
|
| 72 |
...) {
|
|
| 73 | 99x |
y <- list(or_ci = numeric(), n_tot = numeric()) |
| 74 | ||
| 75 | 99x |
if (!.in_ref_col) {
|
| 76 | 94x |
assert_proportion_value(conf_level) |
| 77 | 94x |
assert_df_with_variables(df, list(rsp = .var)) |
| 78 | 94x |
assert_df_with_variables(.ref_group, list(rsp = .var)) |
| 79 | ||
| 80 | 94x |
if (is.null(variables$strata)) {
|
| 81 | 76x |
data <- data.frame( |
| 82 | 76x |
rsp = c(.ref_group[[.var]], df[[.var]]), |
| 83 | 76x |
grp = factor( |
| 84 | 76x |
rep(c("ref", "Not-ref"), c(nrow(.ref_group), nrow(df))),
|
| 85 | 76x |
levels = c("ref", "Not-ref")
|
| 86 |
) |
|
| 87 |
) |
|
| 88 | 76x |
y <- or_glm(data, conf_level = conf_level) |
| 89 |
} else {
|
|
| 90 | 18x |
assert_df_with_variables(.df_row, c(list(rsp = .var), variables)) |
| 91 | 18x |
checkmate::assert_subset(method, c("exact", "approximate", "efron", "breslow"), empty.ok = FALSE)
|
| 92 | ||
| 93 |
# The group variable prepared for clogit must be synchronised with combination groups definition. |
|
| 94 | 18x |
if (is.null(groups_list)) {
|
| 95 | 16x |
ref_grp <- as.character(unique(.ref_group[[variables$arm]])) |
| 96 | 16x |
trt_grp <- as.character(unique(df[[variables$arm]])) |
| 97 | 16x |
grp <- stats::relevel(factor(.df_row[[variables$arm]]), ref = ref_grp) |
| 98 |
} else {
|
|
| 99 |
# If more than one level in reference col. |
|
| 100 | 2x |
reference <- as.character(unique(.ref_group[[variables$arm]])) |
| 101 | 2x |
grp_ref_flag <- vapply( |
| 102 | 2x |
X = groups_list, |
| 103 | 2x |
FUN.VALUE = TRUE, |
| 104 | 2x |
FUN = function(x) all(reference %in% x) |
| 105 |
) |
|
| 106 | 2x |
ref_grp <- names(groups_list)[grp_ref_flag] |
| 107 | ||
| 108 |
# If more than one level in treatment col. |
|
| 109 | 2x |
treatment <- as.character(unique(df[[variables$arm]])) |
| 110 | 2x |
grp_trt_flag <- vapply( |
| 111 | 2x |
X = groups_list, |
| 112 | 2x |
FUN.VALUE = TRUE, |
| 113 | 2x |
FUN = function(x) all(treatment %in% x) |
| 114 |
) |
|
| 115 | 2x |
trt_grp <- names(groups_list)[grp_trt_flag] |
| 116 | ||
| 117 | 2x |
grp <- combine_levels(.df_row[[variables$arm]], levels = reference, new_level = ref_grp) |
| 118 | 2x |
grp <- combine_levels(grp, levels = treatment, new_level = trt_grp) |
| 119 |
} |
|
| 120 | ||
| 121 |
# The reference level in `grp` must be the same as in the `rtables` column split. |
|
| 122 | 18x |
data <- data.frame( |
| 123 | 18x |
rsp = .df_row[[.var]], |
| 124 | 18x |
grp = grp, |
| 125 | 18x |
strata = interaction(.df_row[variables$strata]) |
| 126 |
) |
|
| 127 | 18x |
y_all <- or_clogit(data, conf_level = conf_level, method = method) |
| 128 | 18x |
checkmate::assert_string(trt_grp) |
| 129 | 18x |
checkmate::assert_subset(trt_grp, names(y_all$or_ci)) |
| 130 | 17x |
y$or_ci <- y_all$or_ci[[trt_grp]] |
| 131 | 17x |
y$n_tot <- y_all$n_tot |
| 132 |
} |
|
| 133 |
} |
|
| 134 | ||
| 135 | 98x |
if ("est" %in% names(y$or_ci) && is.na(y$or_ci[["est"]]) && method != "approximate") {
|
| 136 | 1x |
warning( |
| 137 | 1x |
"Unable to compute the odds ratio estimate. Please try re-running the function with ", |
| 138 | 1x |
'parameter `method` set to "approximate".' |
| 139 |
) |
|
| 140 |
} |
|
| 141 | ||
| 142 | 98x |
y$or_ci <- formatters::with_label( |
| 143 | 98x |
x = y$or_ci, |
| 144 | 98x |
label = paste0("Odds Ratio (", 100 * conf_level, "% CI)")
|
| 145 |
) |
|
| 146 | ||
| 147 | 98x |
y$n_tot <- formatters::with_label( |
| 148 | 98x |
x = y$n_tot, |
| 149 | 98x |
label = "Total n" |
| 150 |
) |
|
| 151 | ||
| 152 | 98x |
y |
| 153 |
} |
|
| 154 | ||
| 155 |
#' @describeIn odds_ratio Formatted analysis function which is used as `afun` in `estimate_odds_ratio()`. |
|
| 156 |
#' |
|
| 157 |
#' @return |
|
| 158 |
#' * `a_odds_ratio()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 159 |
#' |
|
| 160 |
#' @examples |
|
| 161 |
#' a_odds_ratio( |
|
| 162 |
#' df = subset(dta, grp == "A"), |
|
| 163 |
#' .var = "rsp", |
|
| 164 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 165 |
#' .in_ref_col = FALSE, |
|
| 166 |
#' .df_row = dta |
|
| 167 |
#' ) |
|
| 168 |
#' |
|
| 169 |
#' @export |
|
| 170 |
a_odds_ratio <- function(df, |
|
| 171 |
..., |
|
| 172 |
.stats = NULL, |
|
| 173 |
.stat_names = NULL, |
|
| 174 |
.formats = NULL, |
|
| 175 |
.labels = NULL, |
|
| 176 |
.indent_mods = NULL) {
|
|
| 177 |
# Check for additional parameters to the statistics function |
|
| 178 | 12x |
dots_extra_args <- list(...) |
| 179 | 12x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 180 | 12x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 181 | ||
| 182 |
# Check for user-defined functions |
|
| 183 | 12x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 184 | 12x |
.stats <- default_and_custom_stats_list$all_stats |
| 185 | 12x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 186 | ||
| 187 |
# Apply statistics function |
|
| 188 | 12x |
x_stats <- .apply_stat_functions( |
| 189 | 12x |
default_stat_fnc = s_odds_ratio, |
| 190 | 12x |
custom_stat_fnc_list = custom_stat_functions, |
| 191 | 12x |
args_list = c( |
| 192 | 12x |
df = list(df), |
| 193 | 12x |
extra_afun_params, |
| 194 | 12x |
dots_extra_args |
| 195 |
) |
|
| 196 |
) |
|
| 197 | ||
| 198 |
# Fill in formatting defaults |
|
| 199 | 12x |
.stats <- get_stats("estimate_odds_ratio",
|
| 200 | 12x |
stats_in = .stats, |
| 201 | 12x |
custom_stats_in = names(custom_stat_functions) |
| 202 |
) |
|
| 203 | 12x |
x_stats <- x_stats[.stats] |
| 204 | 12x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 205 | 12x |
.labels <- get_labels_from_stats( |
| 206 | 12x |
.stats, .labels, |
| 207 | 12x |
tern_defaults = c(lapply(x_stats, attr, "label"), tern_default_labels) |
| 208 |
) |
|
| 209 | 12x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 210 | ||
| 211 |
# Auto format handling |
|
| 212 | 12x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 213 | ||
| 214 |
# Get and check statistical names |
|
| 215 | 12x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 216 | ||
| 217 | 12x |
in_rows( |
| 218 | 12x |
.list = x_stats, |
| 219 | 12x |
.formats = .formats, |
| 220 | 12x |
.names = .labels %>% .unlist_keep_nulls(), |
| 221 | 12x |
.stat_names = .stat_names, |
| 222 | 12x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 223 | 12x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 224 |
) |
|
| 225 |
} |
|
| 226 | ||
| 227 |
#' @describeIn odds_ratio Layout-creating function which can take statistics function arguments |
|
| 228 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 229 |
#' |
|
| 230 |
#' @return |
|
| 231 |
#' * `estimate_odds_ratio()` returns a layout object suitable for passing to further layouting functions, |
|
| 232 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 233 |
#' the statistics from `s_odds_ratio()` to the table layout. |
|
| 234 |
#' |
|
| 235 |
#' @examples |
|
| 236 |
#' set.seed(12) |
|
| 237 |
#' dta <- data.frame( |
|
| 238 |
#' rsp = sample(c(TRUE, FALSE), 100, TRUE), |
|
| 239 |
#' grp = factor(rep(c("A", "B"), each = 50), levels = c("A", "B")),
|
|
| 240 |
#' strata = factor(sample(c("C", "D"), 100, TRUE))
|
|
| 241 |
#' ) |
|
| 242 |
#' |
|
| 243 |
#' l <- basic_table() %>% |
|
| 244 |
#' split_cols_by(var = "grp", ref_group = "B") %>% |
|
| 245 |
#' estimate_odds_ratio(vars = "rsp") |
|
| 246 |
#' |
|
| 247 |
#' build_table(l, df = dta) |
|
| 248 |
#' |
|
| 249 |
#' @export |
|
| 250 |
#' @order 2 |
|
| 251 |
estimate_odds_ratio <- function(lyt, |
|
| 252 |
vars, |
|
| 253 |
variables = list(arm = NULL, strata = NULL), |
|
| 254 |
conf_level = 0.95, |
|
| 255 |
groups_list = NULL, |
|
| 256 |
method = "exact", |
|
| 257 |
na_str = default_na_str(), |
|
| 258 |
nested = TRUE, |
|
| 259 |
..., |
|
| 260 |
table_names = vars, |
|
| 261 |
show_labels = "hidden", |
|
| 262 |
var_labels = vars, |
|
| 263 |
.stats = "or_ci", |
|
| 264 |
.stat_names = NULL, |
|
| 265 |
.formats = NULL, |
|
| 266 |
.labels = NULL, |
|
| 267 |
.indent_mods = NULL) {
|
|
| 268 |
# Process standard extra arguments |
|
| 269 | 5x |
extra_args <- list(".stats" = .stats)
|
| 270 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 271 | ! |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 272 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 273 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 274 | ||
| 275 |
# Process additional arguments to the statistic function |
|
| 276 | 5x |
extra_args <- c( |
| 277 | 5x |
extra_args, |
| 278 | 5x |
variables = list(variables), conf_level = list(conf_level), groups_list = list(groups_list), method = list(method), |
| 279 |
... |
|
| 280 |
) |
|
| 281 | ||
| 282 |
# Append additional info from layout to the analysis function |
|
| 283 | 5x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 284 | 5x |
formals(a_odds_ratio) <- c(formals(a_odds_ratio), extra_args[[".additional_fun_parameters"]]) |
| 285 | ||
| 286 | 5x |
analyze( |
| 287 | 5x |
lyt = lyt, |
| 288 | 5x |
vars = vars, |
| 289 | 5x |
afun = a_odds_ratio, |
| 290 | 5x |
na_str = na_str, |
| 291 | 5x |
nested = nested, |
| 292 | 5x |
extra_args = extra_args, |
| 293 | 5x |
var_labels = var_labels, |
| 294 | 5x |
show_labels = show_labels, |
| 295 | 5x |
table_names = table_names |
| 296 |
) |
|
| 297 |
} |
|
| 298 | ||
| 299 |
#' Helper functions for odds ratio estimation |
|
| 300 |
#' |
|
| 301 |
#' @description `r lifecycle::badge("stable")`
|
|
| 302 |
#' |
|
| 303 |
#' Functions to calculate odds ratios in [estimate_odds_ratio()]. |
|
| 304 |
#' |
|
| 305 |
#' @inheritParams odds_ratio |
|
| 306 |
#' @inheritParams argument_convention |
|
| 307 |
#' @param data (`data.frame`)\cr data frame containing at least the variables `rsp` and `grp`, and optionally |
|
| 308 |
#' `strata` for [or_clogit()]. |
|
| 309 |
#' |
|
| 310 |
#' @return A named `list` of elements `or_ci` and `n_tot`. |
|
| 311 |
#' |
|
| 312 |
#' @seealso [odds_ratio] |
|
| 313 |
#' |
|
| 314 |
#' @name h_odds_ratio |
|
| 315 |
NULL |
|
| 316 | ||
| 317 |
#' @describeIn h_odds_ratio Estimates the odds ratio based on [stats::glm()]. Note that there must be |
|
| 318 |
#' exactly 2 groups in `data` as specified by the `grp` variable. |
|
| 319 |
#' |
|
| 320 |
#' @examples |
|
| 321 |
#' # Data with 2 groups. |
|
| 322 |
#' data <- data.frame( |
|
| 323 |
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1)), |
|
| 324 |
#' grp = letters[c(1, 1, 1, 2, 2, 2, 1, 2)], |
|
| 325 |
#' strata = letters[c(1, 2, 1, 2, 2, 2, 1, 2)], |
|
| 326 |
#' stringsAsFactors = TRUE |
|
| 327 |
#' ) |
|
| 328 |
#' |
|
| 329 |
#' # Odds ratio based on glm. |
|
| 330 |
#' or_glm(data, conf_level = 0.95) |
|
| 331 |
#' |
|
| 332 |
#' @export |
|
| 333 |
or_glm <- function(data, conf_level) {
|
|
| 334 | 77x |
checkmate::assert_logical(data$rsp) |
| 335 | 77x |
assert_proportion_value(conf_level) |
| 336 | 77x |
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp")) |
| 337 | 77x |
checkmate::assert_multi_class(data$grp, classes = c("factor", "character"))
|
| 338 | ||
| 339 | 77x |
data$grp <- as_factor_keep_attributes(data$grp) |
| 340 | 77x |
assert_df_with_factors(data, list(val = "grp"), min.levels = 2, max.levels = 2) |
| 341 | 77x |
formula <- stats::as.formula("rsp ~ grp")
|
| 342 | 77x |
model_fit <- stats::glm( |
| 343 | 77x |
formula = formula, data = data, |
| 344 | 77x |
family = stats::binomial(link = "logit") |
| 345 |
) |
|
| 346 | ||
| 347 |
# Note that here we need to discard the intercept. |
|
| 348 | 77x |
or <- exp(stats::coef(model_fit)[-1]) |
| 349 | 77x |
or_ci <- exp( |
| 350 | 77x |
stats::confint.default(model_fit, level = conf_level)[-1, , drop = FALSE] |
| 351 |
) |
|
| 352 | ||
| 353 | 77x |
values <- stats::setNames(c(or, or_ci), c("est", "lcl", "ucl"))
|
| 354 | 77x |
n_tot <- stats::setNames(nrow(model_fit$model), "n_tot") |
| 355 | ||
| 356 | 77x |
list(or_ci = values, n_tot = n_tot) |
| 357 |
} |
|
| 358 | ||
| 359 |
#' @describeIn h_odds_ratio Estimates the odds ratio based on [survival::clogit()]. This is done for |
|
| 360 |
#' the whole data set including all groups, since the results are not the same as when doing |
|
| 361 |
#' pairwise comparisons between the groups. |
|
| 362 |
#' |
|
| 363 |
#' @examples |
|
| 364 |
#' # Data with 3 groups. |
|
| 365 |
#' data <- data.frame( |
|
| 366 |
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0)), |
|
| 367 |
#' grp = letters[c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3)], |
|
| 368 |
#' strata = LETTERS[c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)], |
|
| 369 |
#' stringsAsFactors = TRUE |
|
| 370 |
#' ) |
|
| 371 |
#' |
|
| 372 |
#' # Odds ratio based on stratified estimation by conditional logistic regression. |
|
| 373 |
#' or_clogit(data, conf_level = 0.95) |
|
| 374 |
#' |
|
| 375 |
#' @export |
|
| 376 |
or_clogit <- function(data, conf_level, method = "exact") {
|
|
| 377 | 19x |
checkmate::assert_logical(data$rsp) |
| 378 | 19x |
assert_proportion_value(conf_level) |
| 379 | 19x |
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp", strata = "strata")) |
| 380 | 19x |
checkmate::assert_multi_class(data$grp, classes = c("factor", "character"))
|
| 381 | 19x |
checkmate::assert_multi_class(data$strata, classes = c("factor", "character"))
|
| 382 | 19x |
checkmate::assert_subset(method, c("exact", "approximate", "efron", "breslow"), empty.ok = FALSE)
|
| 383 | ||
| 384 | 19x |
data$grp <- as_factor_keep_attributes(data$grp) |
| 385 | 19x |
data$strata <- as_factor_keep_attributes(data$strata) |
| 386 | ||
| 387 |
# Deviation from convention: `survival::strata` must be simply `strata`. |
|
| 388 | 19x |
formula <- stats::as.formula("rsp ~ grp + strata(strata)")
|
| 389 | 19x |
model_fit <- clogit_with_tryCatch(formula = formula, data = data, method = method) |
| 390 | ||
| 391 |
# Create a list with one set of OR estimates and CI per coefficient, i.e. |
|
| 392 |
# comparison of one group vs. the reference group. |
|
| 393 | 19x |
coef_est <- stats::coef(model_fit) |
| 394 | 19x |
ci_est <- stats::confint(model_fit, level = conf_level) |
| 395 | 19x |
or_ci <- list() |
| 396 | 19x |
for (coef_name in names(coef_est)) {
|
| 397 | 21x |
grp_name <- gsub("^grp", "", x = coef_name)
|
| 398 | 21x |
or_ci[[grp_name]] <- stats::setNames( |
| 399 | 21x |
object = exp(c(coef_est[coef_name], ci_est[coef_name, , drop = TRUE])), |
| 400 | 21x |
nm = c("est", "lcl", "ucl")
|
| 401 |
) |
|
| 402 |
} |
|
| 403 | 19x |
list(or_ci = or_ci, n_tot = c(n_tot = model_fit$n)) |
| 404 |
} |
| 1 |
#' Summarize analysis of covariance (ANCOVA) results |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [summarize_ancova()] creates a layout element to summarize ANCOVA results. |
|
| 6 |
#' |
|
| 7 |
#' This function can be used to analyze multiple endpoints and/or multiple timepoints within the response variable(s) |
|
| 8 |
#' specified as `vars`. |
|
| 9 |
#' |
|
| 10 |
#' Additional variables for the analysis, namely an arm (grouping) variable and covariate variables, can be defined |
|
| 11 |
#' via the `variables` argument. See below for more details on how to specify `variables`. An interaction term can |
|
| 12 |
#' be implemented in the model if needed. The interaction variable that should interact with the arm variable is |
|
| 13 |
#' specified via the `interaction_term` parameter, and the specific value of `interaction_term` for which to extract |
|
| 14 |
#' the ANCOVA results via the `interaction_y` parameter. |
|
| 15 |
#' |
|
| 16 |
#' @inheritParams h_ancova |
|
| 17 |
#' @inheritParams argument_convention |
|
| 18 |
#' @param interaction_y (`string` or `flag`)\cr a selected item inside of the `interaction_item` variable which will be |
|
| 19 |
#' used to select the specific ANCOVA results. if the interaction is not needed, the default option is `FALSE`. |
|
| 20 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 21 |
#' |
|
| 22 |
#' Options are: ``r shQuote(get_stats("summarize_ancova"), type = "sh")``
|
|
| 23 |
#' |
|
| 24 |
#' @name summarize_ancova |
|
| 25 |
#' @order 1 |
|
| 26 |
NULL |
|
| 27 | ||
| 28 |
#' Helper function to return results of a linear model |
|
| 29 |
#' |
|
| 30 |
#' @description `r lifecycle::badge("stable")`
|
|
| 31 |
#' |
|
| 32 |
#' @inheritParams argument_convention |
|
| 33 |
#' @param .df_row (`data.frame`)\cr data set that includes all the variables that are called in `.var` and `variables`. |
|
| 34 |
#' @param variables (named `list` of `string`)\cr list of additional analysis variables, with expected elements: |
|
| 35 |
#' * `arm` (`string`)\cr group variable, for which the covariate adjusted means of multiple groups will be |
|
| 36 |
#' summarized. Specifically, the first level of `arm` variable is taken as the reference group. |
|
| 37 |
#' * `covariates` (`character`)\cr a vector that can contain single variable names (such as `"X1"`), and/or |
|
| 38 |
#' interaction terms indicated by `"X1 * X2"`. |
|
| 39 |
#' @param interaction_item (`string` or `NULL`)\cr name of the variable that should have interactions |
|
| 40 |
#' with arm. if the interaction is not needed, the default option is `NULL`. |
|
| 41 |
#' @param weights_emmeans (`string` or `NULL`)\cr argument from [emmeans::emmeans()] |
|
| 42 |
#' |
|
| 43 |
#' @return The summary of a linear model. |
|
| 44 |
#' |
|
| 45 |
#' @examples |
|
| 46 |
#' h_ancova( |
|
| 47 |
#' .var = "Sepal.Length", |
|
| 48 |
#' .df_row = iris, |
|
| 49 |
#' variables = list(arm = "Species", covariates = c("Petal.Length * Petal.Width", "Sepal.Width"))
|
|
| 50 |
#' ) |
|
| 51 |
#' |
|
| 52 |
#' @export |
|
| 53 |
h_ancova <- function(.var, |
|
| 54 |
.df_row, |
|
| 55 |
variables, |
|
| 56 |
interaction_item = NULL, |
|
| 57 |
weights_emmeans = NULL) {
|
|
| 58 | 27x |
checkmate::assert_string(.var) |
| 59 | 27x |
checkmate::assert_list(variables) |
| 60 | 27x |
checkmate::assert_subset(names(variables), c("arm", "covariates"))
|
| 61 | 27x |
assert_df_with_variables(.df_row, list(rsp = .var)) |
| 62 | ||
| 63 | 26x |
arm <- variables$arm |
| 64 | 26x |
covariates <- variables$covariates |
| 65 | 26x |
if (!is.null(covariates) && length(covariates) > 0) {
|
| 66 |
# Get all covariate variable names in the model. |
|
| 67 | 11x |
var_list <- get_covariates(covariates) |
| 68 | 11x |
assert_df_with_variables(.df_row, var_list) |
| 69 |
} |
|
| 70 | ||
| 71 | 25x |
covariates_part <- paste(covariates, collapse = " + ") |
| 72 | 25x |
if (covariates_part != "") {
|
| 73 | 10x |
formula <- stats::as.formula(paste0(.var, " ~ ", covariates_part, " + ", arm)) |
| 74 |
} else {
|
|
| 75 | 15x |
formula <- stats::as.formula(paste0(.var, " ~ ", arm)) |
| 76 |
} |
|
| 77 | ||
| 78 | 25x |
if (is.null(interaction_item)) {
|
| 79 | 21x |
specs <- arm |
| 80 |
} else {
|
|
| 81 | 4x |
specs <- c(arm, interaction_item) |
| 82 |
} |
|
| 83 | ||
| 84 | 25x |
lm_fit <- stats::lm( |
| 85 | 25x |
formula = formula, |
| 86 | 25x |
data = .df_row |
| 87 |
) |
|
| 88 | 25x |
emmeans_fit <- emmeans::emmeans( |
| 89 | 25x |
lm_fit, |
| 90 |
# Specify here the group variable over which EMM are desired. |
|
| 91 | 25x |
specs = specs, |
| 92 |
# Pass the data again so that the factor levels of the arm variable can be inferred. |
|
| 93 | 25x |
data = .df_row, |
| 94 | 25x |
weights = weights_emmeans |
| 95 |
) |
|
| 96 | ||
| 97 | 25x |
emmeans_fit |
| 98 |
} |
|
| 99 | ||
| 100 |
#' @describeIn summarize_ancova Statistics function that produces a named list of results |
|
| 101 |
#' of the investigated linear model. |
|
| 102 |
#' |
|
| 103 |
#' @return |
|
| 104 |
#' * `s_ancova()` returns a named list of 5 statistics: |
|
| 105 |
#' * `n`: Count of complete sample size for the group. |
|
| 106 |
#' * `lsmean`: Estimated marginal means in the group. |
|
| 107 |
#' * `lsmean_diff`: Difference in estimated marginal means in comparison to the reference group. |
|
| 108 |
#' If working with the reference group, this will be empty. |
|
| 109 |
#' * `lsmean_diff_ci`: Confidence level for difference in estimated marginal means in comparison |
|
| 110 |
#' to the reference group. |
|
| 111 |
#' * `pval`: p-value (not adjusted for multiple comparisons). |
|
| 112 |
#' |
|
| 113 |
#' @keywords internal |
|
| 114 |
s_ancova <- function(df, |
|
| 115 |
.var, |
|
| 116 |
.df_row, |
|
| 117 |
.ref_group, |
|
| 118 |
.in_ref_col, |
|
| 119 |
variables, |
|
| 120 |
conf_level, |
|
| 121 |
interaction_y = FALSE, |
|
| 122 |
interaction_item = NULL, |
|
| 123 |
weights_emmeans = NULL, |
|
| 124 |
...) {
|
|
| 125 | 24x |
emmeans_fit <- h_ancova( |
| 126 | 24x |
.var = .var, |
| 127 | 24x |
variables = variables, |
| 128 | 24x |
.df_row = .df_row, |
| 129 | 24x |
interaction_item = interaction_item, |
| 130 | 24x |
weights_emmeans = weights_emmeans |
| 131 |
) |
|
| 132 | ||
| 133 | 24x |
sum_fit <- summary( |
| 134 | 24x |
emmeans_fit, |
| 135 | 24x |
level = conf_level |
| 136 |
) |
|
| 137 | ||
| 138 | 24x |
arm <- variables$arm |
| 139 | ||
| 140 | 24x |
sum_level <- as.character(unique(df[[arm]])) |
| 141 | ||
| 142 |
# Ensure that there is only one element in sum_level. |
|
| 143 | 24x |
checkmate::assert_scalar(sum_level) |
| 144 | ||
| 145 | 23x |
sum_fit_level <- sum_fit[sum_fit[[arm]] == sum_level, ] |
| 146 | ||
| 147 |
# Get the index of the ref arm |
|
| 148 | 23x |
if (interaction_y != FALSE) {
|
| 149 | 4x |
y <- unlist(df[(df[[interaction_item]] == interaction_y), .var]) |
| 150 |
# convert characters selected in interaction_y into the numeric order |
|
| 151 | 4x |
interaction_y <- which(sum_fit_level[[interaction_item]] == interaction_y) |
| 152 | 4x |
sum_fit_level <- sum_fit_level[interaction_y, ] |
| 153 |
# if interaction is called, reset the index |
|
| 154 | 4x |
ref_key <- seq(sum_fit[[arm]][unique(.ref_group[[arm]])]) |
| 155 | 4x |
ref_key <- tail(ref_key, n = 1) |
| 156 | 4x |
ref_key <- (interaction_y - 1) * length(unique(.df_row[[arm]])) + ref_key |
| 157 |
} else {
|
|
| 158 | 19x |
y <- df[[.var]] |
| 159 |
# Get the index of the ref arm when interaction is not called |
|
| 160 | 19x |
ref_key <- seq(sum_fit[[arm]][unique(.ref_group[[arm]])]) |
| 161 | 19x |
ref_key <- tail(ref_key, n = 1) |
| 162 |
} |
|
| 163 | ||
| 164 | 23x |
if (.in_ref_col) {
|
| 165 | 8x |
list( |
| 166 | 8x |
n = length(y[!is.na(y)]), |
| 167 | 8x |
lsmean = formatters::with_label(sum_fit_level$emmean, "Adjusted Mean"), |
| 168 | 8x |
lsmean_diff = formatters::with_label(numeric(), "Difference in Adjusted Means"), |
| 169 | 8x |
lsmean_diff_ci = formatters::with_label(numeric(), f_conf_level(conf_level)), |
| 170 | 8x |
pval = formatters::with_label(numeric(), "p-value") |
| 171 |
) |
|
| 172 |
} else {
|
|
| 173 |
# Estimate the differences between the marginal means. |
|
| 174 | 15x |
emmeans_contrasts <- emmeans::contrast( |
| 175 | 15x |
emmeans_fit, |
| 176 |
# Compare all arms versus the control arm. |
|
| 177 | 15x |
method = "trt.vs.ctrl", |
| 178 |
# Take the arm factor from .ref_group as the control arm. |
|
| 179 | 15x |
ref = ref_key, |
| 180 | 15x |
level = conf_level |
| 181 |
) |
|
| 182 | 15x |
sum_contrasts <- summary( |
| 183 | 15x |
emmeans_contrasts, |
| 184 |
# Derive confidence intervals, t-tests and p-values. |
|
| 185 | 15x |
infer = TRUE, |
| 186 |
# Do not adjust the p-values for multiplicity. |
|
| 187 | 15x |
adjust = "none" |
| 188 |
) |
|
| 189 | ||
| 190 | 15x |
contrast_lvls <- gsub( |
| 191 | 15x |
"^\\(|\\)$", "", gsub(paste0(" - \\(*", .ref_group[[arm]][1], ".*"), "", sum_contrasts$contrast)
|
| 192 |
) |
|
| 193 | 15x |
if (!is.null(interaction_item)) {
|
| 194 | 2x |
sum_contrasts_level <- sum_contrasts[grepl(sum_level, contrast_lvls, fixed = TRUE), ] |
| 195 |
} else {
|
|
| 196 | 13x |
sum_contrasts_level <- sum_contrasts[sum_level == contrast_lvls, ] |
| 197 |
} |
|
| 198 | 15x |
if (interaction_y != FALSE) {
|
| 199 | 2x |
sum_contrasts_level <- sum_contrasts_level[interaction_y, ] |
| 200 |
} |
|
| 201 | ||
| 202 | 15x |
list( |
| 203 | 15x |
n = length(y[!is.na(y)]), |
| 204 | 15x |
lsmean = formatters::with_label(sum_fit_level$emmean, "Adjusted Mean"), |
| 205 | 15x |
lsmean_diff = formatters::with_label(sum_contrasts_level$estimate, "Difference in Adjusted Means"), |
| 206 | 15x |
lsmean_diff_ci = formatters::with_label( |
| 207 | 15x |
c(sum_contrasts_level$lower.CL, sum_contrasts_level$upper.CL), |
| 208 | 15x |
f_conf_level(conf_level) |
| 209 |
), |
|
| 210 | 15x |
pval = formatters::with_label(sum_contrasts_level$p.value, "p-value") |
| 211 |
) |
|
| 212 |
} |
|
| 213 |
} |
|
| 214 | ||
| 215 |
#' @describeIn summarize_ancova Formatted analysis function which is used as `afun` in `summarize_ancova()`. |
|
| 216 |
#' |
|
| 217 |
#' @return |
|
| 218 |
#' * `a_ancova()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 219 |
#' |
|
| 220 |
#' @keywords internal |
|
| 221 |
a_ancova <- function(df, |
|
| 222 |
..., |
|
| 223 |
.stats = NULL, |
|
| 224 |
.stat_names = NULL, |
|
| 225 |
.formats = NULL, |
|
| 226 |
.labels = NULL, |
|
| 227 |
.indent_mods = NULL) {
|
|
| 228 |
# Check for additional parameters to the statistics function |
|
| 229 | 21x |
dots_extra_args <- list(...) |
| 230 | 21x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 231 | 21x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 232 | ||
| 233 |
# Check for user-defined functions |
|
| 234 | 21x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 235 | 21x |
.stats <- default_and_custom_stats_list$all_stats |
| 236 | 21x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 237 | ||
| 238 |
# Apply statistics function |
|
| 239 | 21x |
x_stats <- .apply_stat_functions( |
| 240 | 21x |
default_stat_fnc = s_ancova, |
| 241 | 21x |
custom_stat_fnc_list = custom_stat_functions, |
| 242 | 21x |
args_list = c( |
| 243 | 21x |
df = list(df), |
| 244 | 21x |
extra_afun_params, |
| 245 | 21x |
dots_extra_args |
| 246 |
) |
|
| 247 |
) |
|
| 248 | ||
| 249 |
# Fill in formatting defaults |
|
| 250 | 21x |
.stats <- get_stats("summarize_ancova",
|
| 251 | 21x |
stats_in = .stats, |
| 252 | 21x |
custom_stats_in = names(custom_stat_functions) |
| 253 |
) |
|
| 254 | 21x |
x_stats <- x_stats[.stats] |
| 255 | 21x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 256 | 21x |
.labels <- get_labels_from_stats( |
| 257 | 21x |
.stats, .labels, |
| 258 | 21x |
tern_defaults = c(lapply(x_stats[names(x_stats) != "n"], attr, "label"), tern_default_labels) |
| 259 |
) |
|
| 260 | 21x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 261 | ||
| 262 |
# Auto format handling |
|
| 263 | 21x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 264 | ||
| 265 |
# Get and check statistical names |
|
| 266 | 21x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 267 | ||
| 268 | 21x |
in_rows( |
| 269 | 21x |
.list = x_stats, |
| 270 | 21x |
.formats = .formats, |
| 271 | 21x |
.names = .labels %>% .unlist_keep_nulls(), |
| 272 | 21x |
.stat_names = .stat_names, |
| 273 | 21x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 274 | 21x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 275 |
) |
|
| 276 |
} |
|
| 277 | ||
| 278 |
#' @describeIn summarize_ancova Layout-creating function which can take statistics function arguments |
|
| 279 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 280 |
#' |
|
| 281 |
#' @return |
|
| 282 |
#' * `summarize_ancova()` returns a layout object suitable for passing to further layouting functions, |
|
| 283 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 284 |
#' the statistics from `s_ancova()` to the table layout. |
|
| 285 |
#' |
|
| 286 |
#' @examples |
|
| 287 |
#' basic_table() %>% |
|
| 288 |
#' split_cols_by("Species", ref_group = "setosa") %>%
|
|
| 289 |
#' add_colcounts() %>% |
|
| 290 |
#' summarize_ancova( |
|
| 291 |
#' vars = "Petal.Length", |
|
| 292 |
#' variables = list(arm = "Species", covariates = NULL), |
|
| 293 |
#' table_names = "unadj", |
|
| 294 |
#' conf_level = 0.95, var_labels = "Unadjusted comparison", |
|
| 295 |
#' .labels = c(lsmean = "Mean", lsmean_diff = "Difference in Means") |
|
| 296 |
#' ) %>% |
|
| 297 |
#' summarize_ancova( |
|
| 298 |
#' vars = "Petal.Length", |
|
| 299 |
#' variables = list(arm = "Species", covariates = c("Sepal.Length", "Sepal.Width")),
|
|
| 300 |
#' table_names = "adj", |
|
| 301 |
#' conf_level = 0.95, var_labels = "Adjusted comparison (covariates: Sepal.Length and Sepal.Width)" |
|
| 302 |
#' ) %>% |
|
| 303 |
#' build_table(iris) |
|
| 304 |
#' |
|
| 305 |
#' @export |
|
| 306 |
#' @order 2 |
|
| 307 |
summarize_ancova <- function(lyt, |
|
| 308 |
vars, |
|
| 309 |
variables, |
|
| 310 |
conf_level, |
|
| 311 |
interaction_y = FALSE, |
|
| 312 |
interaction_item = NULL, |
|
| 313 |
weights_emmeans = NULL, |
|
| 314 |
var_labels, |
|
| 315 |
na_str = default_na_str(), |
|
| 316 |
nested = TRUE, |
|
| 317 |
..., |
|
| 318 |
show_labels = "visible", |
|
| 319 |
table_names = vars, |
|
| 320 |
.stats = c("n", "lsmean", "lsmean_diff", "lsmean_diff_ci", "pval"),
|
|
| 321 |
.stat_names = NULL, |
|
| 322 |
.formats = NULL, |
|
| 323 |
.labels = NULL, |
|
| 324 |
.indent_mods = list("lsmean_diff_ci" = 1L, "pval" = 1L)) {
|
|
| 325 |
# Process standard extra arguments |
|
| 326 | 7x |
extra_args <- list(".stats" = .stats)
|
| 327 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 328 | ! |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 329 | 3x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 330 | 7x |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 331 | ||
| 332 |
# Process additional arguments to the statistic function |
|
| 333 | 7x |
extra_args <- c( |
| 334 | 7x |
extra_args, |
| 335 | 7x |
variables = list(variables), conf_level = list(conf_level), interaction_y = list(interaction_y), |
| 336 | 7x |
interaction_item = list(interaction_item), |
| 337 | 7x |
weights_emmeans = weights_emmeans, |
| 338 |
... |
|
| 339 |
) |
|
| 340 | ||
| 341 |
# Append additional info from layout to the analysis function |
|
| 342 | 7x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 343 | 7x |
formals(a_ancova) <- c(formals(a_ancova), extra_args[[".additional_fun_parameters"]]) |
| 344 | ||
| 345 | 7x |
analyze( |
| 346 | 7x |
lyt = lyt, |
| 347 | 7x |
vars = vars, |
| 348 | 7x |
afun = a_ancova, |
| 349 | 7x |
na_str = na_str, |
| 350 | 7x |
nested = nested, |
| 351 | 7x |
extra_args = extra_args, |
| 352 | 7x |
var_labels = var_labels, |
| 353 | 7x |
show_labels = show_labels, |
| 354 | 7x |
table_names = table_names |
| 355 |
) |
|
| 356 |
} |
| 1 |
#' Count specific values |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_values()] creates a layout element to calculate counts of specific values within a |
|
| 6 |
#' variable of interest. |
|
| 7 |
#' |
|
| 8 |
#' This function analyzes one or more variables of interest supplied as a vector to `vars`. Values to |
|
| 9 |
#' count for variable(s) in `vars` can be given as a vector via the `values` argument. One row of |
|
| 10 |
#' counts will be generated for each variable. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams argument_convention |
|
| 13 |
#' @param values (`character`)\cr specific values that should be counted. |
|
| 14 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 15 |
#' |
|
| 16 |
#' Options are: ``r shQuote(get_stats("count_values"), type = "sh")``
|
|
| 17 |
#' |
|
| 18 |
#' @note |
|
| 19 |
#' * For `factor` variables, `s_count_values` checks whether `values` are all included in the levels of `x` |
|
| 20 |
#' and fails otherwise. |
|
| 21 |
#' * For `count_values()`, variable labels are shown when there is more than one element in `vars`, |
|
| 22 |
#' otherwise they are hidden. |
|
| 23 |
#' |
|
| 24 |
#' @name count_values |
|
| 25 |
#' @order 1 |
|
| 26 |
NULL |
|
| 27 | ||
| 28 |
#' @describeIn count_values S3 generic function to count values. |
|
| 29 |
#' |
|
| 30 |
#' @inheritParams s_summary.logical |
|
| 31 |
#' |
|
| 32 |
#' @return |
|
| 33 |
#' * `s_count_values()` returns output of [s_summary()] for specified values of a non-numeric variable. |
|
| 34 |
#' |
|
| 35 |
#' @export |
|
| 36 |
s_count_values <- function(x, |
|
| 37 |
values, |
|
| 38 |
na.rm = TRUE, # nolint |
|
| 39 |
denom = c("n", "N_col", "N_row"),
|
|
| 40 |
...) {
|
|
| 41 | 207x |
UseMethod("s_count_values", x)
|
| 42 |
} |
|
| 43 | ||
| 44 |
#' @describeIn count_values Method for `character` class. |
|
| 45 |
#' |
|
| 46 |
#' @method s_count_values character |
|
| 47 |
#' |
|
| 48 |
#' @examples |
|
| 49 |
#' # `s_count_values.character` |
|
| 50 |
#' s_count_values(x = c("a", "b", "a"), values = "a")
|
|
| 51 |
#' s_count_values(x = c("a", "b", "a", NA, NA), values = "b", na.rm = FALSE)
|
|
| 52 |
#' |
|
| 53 |
#' @export |
|
| 54 |
s_count_values.character <- function(x, |
|
| 55 |
values = "Y", |
|
| 56 |
na.rm = TRUE, # nolint |
|
| 57 |
...) {
|
|
| 58 | 200x |
checkmate::assert_character(values) |
| 59 | ||
| 60 | 200x |
if (na.rm) {
|
| 61 | 199x |
x <- x[!is.na(x)] |
| 62 |
} |
|
| 63 | ||
| 64 | 200x |
is_in_values <- x %in% values |
| 65 | ||
| 66 | 200x |
s_summary(is_in_values, na_rm = na.rm, ...) |
| 67 |
} |
|
| 68 | ||
| 69 |
#' @describeIn count_values Method for `factor` class. This makes an automatic |
|
| 70 |
#' conversion to `character` and then forwards to the method for characters. |
|
| 71 |
#' |
|
| 72 |
#' @method s_count_values factor |
|
| 73 |
#' |
|
| 74 |
#' @examples |
|
| 75 |
#' # `s_count_values.factor` |
|
| 76 |
#' s_count_values(x = factor(c("a", "b", "a")), values = "a")
|
|
| 77 |
#' |
|
| 78 |
#' @export |
|
| 79 |
s_count_values.factor <- function(x, |
|
| 80 |
values = "Y", |
|
| 81 |
...) {
|
|
| 82 | 4x |
s_count_values(as.character(x), values = as.character(values), ...) |
| 83 |
} |
|
| 84 | ||
| 85 |
#' @describeIn count_values Method for `logical` class. |
|
| 86 |
#' |
|
| 87 |
#' @method s_count_values logical |
|
| 88 |
#' |
|
| 89 |
#' @examples |
|
| 90 |
#' # `s_count_values.logical` |
|
| 91 |
#' s_count_values(x = c(TRUE, FALSE, TRUE)) |
|
| 92 |
#' |
|
| 93 |
#' @export |
|
| 94 |
s_count_values.logical <- function(x, values = TRUE, ...) {
|
|
| 95 | 3x |
checkmate::assert_logical(values) |
| 96 | 3x |
s_count_values(as.character(x), values = as.character(values), ...) |
| 97 |
} |
|
| 98 | ||
| 99 |
#' @describeIn count_values Formatted analysis function which is used as `afun` |
|
| 100 |
#' in `count_values()`. |
|
| 101 |
#' |
|
| 102 |
#' @return |
|
| 103 |
#' * `a_count_values()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 104 |
#' |
|
| 105 |
#' @examples |
|
| 106 |
#' # `a_count_values` |
|
| 107 |
#' a_count_values(x = factor(c("a", "b", "a")), values = "a", .N_col = 10, .N_row = 10)
|
|
| 108 |
#' |
|
| 109 |
#' @export |
|
| 110 |
a_count_values <- function(x, |
|
| 111 |
..., |
|
| 112 |
.stats = NULL, |
|
| 113 |
.stat_names = NULL, |
|
| 114 |
.formats = NULL, |
|
| 115 |
.labels = NULL, |
|
| 116 |
.indent_mods = NULL) {
|
|
| 117 |
# Check for additional parameters to the statistics function |
|
| 118 | 17x |
dots_extra_args <- list(...) |
| 119 | 17x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 120 | 17x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 121 | ||
| 122 |
# Check for user-defined functions |
|
| 123 | 17x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 124 | 17x |
.stats <- default_and_custom_stats_list$all_stats |
| 125 | 17x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 126 | ||
| 127 |
# Main statistic calculations |
|
| 128 | 17x |
x_stats <- .apply_stat_functions( |
| 129 | 17x |
default_stat_fnc = s_count_values, |
| 130 | 17x |
custom_stat_fnc_list = custom_stat_functions, |
| 131 | 17x |
args_list = c( |
| 132 | 17x |
x = list(x), |
| 133 | 17x |
extra_afun_params, |
| 134 | 17x |
dots_extra_args |
| 135 |
) |
|
| 136 |
) |
|
| 137 | ||
| 138 |
# Fill in formatting defaults |
|
| 139 | 17x |
.stats <- get_stats("analyze_vars_counts", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 140 | 17x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 141 | 17x |
.labels <- get_labels_from_stats(.stats, .labels) |
| 142 | 17x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 143 | ||
| 144 | 17x |
x_stats <- x_stats[.stats] |
| 145 | ||
| 146 |
# Auto format handling |
|
| 147 | 17x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 148 | ||
| 149 |
# Get and check statistical names |
|
| 150 | 17x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 151 | ||
| 152 | 17x |
in_rows( |
| 153 | 17x |
.list = x_stats, |
| 154 | 17x |
.formats = .formats, |
| 155 | 17x |
.names = names(.labels), |
| 156 | 17x |
.stat_names = .stat_names, |
| 157 | 17x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 158 | 17x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 159 |
) |
|
| 160 |
} |
|
| 161 | ||
| 162 |
#' @describeIn count_values Layout-creating function which can take statistics function arguments |
|
| 163 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 164 |
#' |
|
| 165 |
#' @return |
|
| 166 |
#' * `count_values()` returns a layout object suitable for passing to further layouting functions, |
|
| 167 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 168 |
#' the statistics from `s_count_values()` to the table layout. |
|
| 169 |
#' |
|
| 170 |
#' @examples |
|
| 171 |
#' # `count_values` |
|
| 172 |
#' basic_table() %>% |
|
| 173 |
#' count_values("Species", values = "setosa") %>%
|
|
| 174 |
#' build_table(iris) |
|
| 175 |
#' |
|
| 176 |
#' @export |
|
| 177 |
#' @order 2 |
|
| 178 |
count_values <- function(lyt, |
|
| 179 |
vars, |
|
| 180 |
values, |
|
| 181 |
na_str = default_na_str(), |
|
| 182 |
na_rm = TRUE, |
|
| 183 |
nested = TRUE, |
|
| 184 |
..., |
|
| 185 |
table_names = vars, |
|
| 186 |
.stats = "count_fraction", |
|
| 187 |
.stat_names = NULL, |
|
| 188 |
.formats = c(count_fraction = "xx (xx.xx%)", count = "xx"), |
|
| 189 |
.labels = c(count_fraction = paste(values, collapse = ", ")), |
|
| 190 |
.indent_mods = NULL) {
|
|
| 191 |
# Process standard extra arguments |
|
| 192 | 8x |
extra_args <- list(".stats" = .stats)
|
| 193 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 194 | 8x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 195 | 8x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 196 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 197 | ||
| 198 |
# Process additional arguments to the statistic function |
|
| 199 | 8x |
extra_args <- c( |
| 200 | 8x |
extra_args, |
| 201 | 8x |
na_rm = na_rm, values = list(values), |
| 202 |
... |
|
| 203 |
) |
|
| 204 | ||
| 205 |
# Adding additional info from layout to analysis function |
|
| 206 | 8x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 207 | 8x |
formals(a_count_values) <- c(formals(a_count_values), extra_args[[".additional_fun_parameters"]]) |
| 208 | ||
| 209 | 8x |
analyze( |
| 210 | 8x |
lyt, |
| 211 | 8x |
vars, |
| 212 | 8x |
afun = a_count_values, |
| 213 | 8x |
na_str = na_str, |
| 214 | 8x |
nested = nested, |
| 215 | 8x |
extra_args = extra_args, |
| 216 | 8x |
show_labels = ifelse(length(vars) > 1, "visible", "hidden"), |
| 217 | 8x |
table_names = table_names |
| 218 |
) |
|
| 219 |
} |
| 1 |
#' Control functions for Kaplan-Meier plot annotation tables |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Auxiliary functions for controlling arguments for formatting the annotation tables that can be added to plots |
|
| 6 |
#' generated via [g_km()]. |
|
| 7 |
#' |
|
| 8 |
#' @param x (`proportion`)\cr x-coordinate for center of annotation table. |
|
| 9 |
#' @param y (`proportion`)\cr y-coordinate for center of annotation table. |
|
| 10 |
#' @param w (`proportion`)\cr relative width of the annotation table. |
|
| 11 |
#' @param h (`proportion`)\cr relative height of the annotation table. |
|
| 12 |
#' @param fill (`flag` or `character`)\cr whether the annotation table should have a background fill color. |
|
| 13 |
#' Can also be a color code to use as the background fill color. If `TRUE`, color code defaults to `"#00000020"`. |
|
| 14 |
#' |
|
| 15 |
#' @return A list of components with the same names as the arguments. |
|
| 16 |
#' |
|
| 17 |
#' @seealso [g_km()] |
|
| 18 |
#' |
|
| 19 |
#' @name control_annot |
|
| 20 |
NULL |
|
| 21 | ||
| 22 |
#' @describeIn control_annot Control function for formatting the median survival time annotation table. This annotation |
|
| 23 |
#' table can be added in [g_km()] by setting `annot_surv_med=TRUE`, and can be configured using the |
|
| 24 |
#' `control_surv_med_annot()` function by setting it as the `control_annot_surv_med` argument. |
|
| 25 |
#' |
|
| 26 |
#' @examples |
|
| 27 |
#' control_surv_med_annot() |
|
| 28 |
#' |
|
| 29 |
#' @export |
|
| 30 |
control_surv_med_annot <- function(x = 0.8, y = 0.85, w = 0.32, h = 0.16, fill = TRUE) {
|
|
| 31 | 22x |
assert_proportion_value(x) |
| 32 | 22x |
assert_proportion_value(y) |
| 33 | 22x |
assert_proportion_value(w) |
| 34 | 22x |
assert_proportion_value(h) |
| 35 | ||
| 36 | 22x |
list(x = x, y = y, w = w, h = h, fill = fill) |
| 37 |
} |
|
| 38 | ||
| 39 |
#' @describeIn control_annot Control function for formatting the Cox-PH annotation table. This annotation table can be |
|
| 40 |
#' added in [g_km()] by setting `annot_coxph=TRUE`, and can be configured using the `control_coxph_annot()` function |
|
| 41 |
#' by setting it as the `control_annot_coxph` argument. |
|
| 42 |
#' |
|
| 43 |
#' @param ref_lbls (`flag`)\cr whether the reference group should be explicitly printed in labels for the |
|
| 44 |
#' annotation table. If `FALSE` (default), only comparison groups will be printed in the table labels. |
|
| 45 |
#' |
|
| 46 |
#' @examples |
|
| 47 |
#' control_coxph_annot() |
|
| 48 |
#' |
|
| 49 |
#' @export |
|
| 50 |
control_coxph_annot <- function(x = 0.29, y = 0.51, w = 0.4, h = 0.125, fill = TRUE, ref_lbls = FALSE) {
|
|
| 51 | 11x |
checkmate::assert_logical(ref_lbls, any.missing = FALSE) |
| 52 | ||
| 53 | 11x |
res <- c(control_surv_med_annot(x = x, y = y, w = w, h = h), list(ref_lbls = ref_lbls)) |
| 54 | 11x |
res |
| 55 |
} |
|
| 56 | ||
| 57 |
#' Helper function to calculate x-tick positions |
|
| 58 |
#' |
|
| 59 |
#' @description `r lifecycle::badge("stable")`
|
|
| 60 |
#' |
|
| 61 |
#' Calculate the positions of ticks on the x-axis. However, if `xticks` already |
|
| 62 |
#' exists it is kept as is. It is based on the same function `ggplot2` relies on, |
|
| 63 |
#' and is required in the graphic and the patient-at-risk annotation table. |
|
| 64 |
#' |
|
| 65 |
#' @inheritParams g_km |
|
| 66 |
#' @inheritParams h_ggkm |
|
| 67 |
#' |
|
| 68 |
#' @return A vector of positions to use for x-axis ticks on a `ggplot` object. |
|
| 69 |
#' |
|
| 70 |
#' @examples |
|
| 71 |
#' library(dplyr) |
|
| 72 |
#' library(survival) |
|
| 73 |
#' |
|
| 74 |
#' data <- tern_ex_adtte %>% |
|
| 75 |
#' filter(PARAMCD == "OS") %>% |
|
| 76 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>% |
|
| 77 |
#' h_data_plot() |
|
| 78 |
#' |
|
| 79 |
#' h_xticks(data) |
|
| 80 |
#' h_xticks(data, xticks = seq(0, 3000, 500)) |
|
| 81 |
#' h_xticks(data, xticks = 500) |
|
| 82 |
#' h_xticks(data, xticks = 500, max_time = 6000) |
|
| 83 |
#' h_xticks(data, xticks = c(0, 500), max_time = 300) |
|
| 84 |
#' h_xticks(data, xticks = 500, max_time = 300) |
|
| 85 |
#' |
|
| 86 |
#' @export |
|
| 87 |
h_xticks <- function(data, xticks = NULL, max_time = NULL) {
|
|
| 88 | 18x |
if (is.null(xticks)) {
|
| 89 | 13x |
if (is.null(max_time)) {
|
| 90 | 11x |
labeling::extended(range(data$time)[1], range(data$time)[2], m = 5) |
| 91 |
} else {
|
|
| 92 | 2x |
labeling::extended(range(data$time)[1], max(range(data$time)[2], max_time), m = 5) |
| 93 |
} |
|
| 94 | 5x |
} else if (checkmate::test_number(xticks)) {
|
| 95 | 2x |
if (is.null(max_time)) {
|
| 96 | 1x |
seq(0, max(data$time), xticks) |
| 97 |
} else {
|
|
| 98 | 1x |
seq(0, max(data$time, max_time), xticks) |
| 99 |
} |
|
| 100 | 3x |
} else if (is.numeric(xticks)) {
|
| 101 | 2x |
xticks |
| 102 |
} else {
|
|
| 103 | 1x |
stop( |
| 104 | 1x |
paste( |
| 105 | 1x |
"xticks should be either `NULL`", |
| 106 | 1x |
"or a single number (interval between x ticks)", |
| 107 | 1x |
"or a numeric vector (position of ticks on the x axis)" |
| 108 |
) |
|
| 109 |
) |
|
| 110 |
} |
|
| 111 |
} |
|
| 112 | ||
| 113 |
#' Helper function for survival estimations |
|
| 114 |
#' |
|
| 115 |
#' @description `r lifecycle::badge("stable")`
|
|
| 116 |
#' |
|
| 117 |
#' Transform a survival fit to a table with groups in rows characterized by N, median and confidence interval. |
|
| 118 |
#' |
|
| 119 |
#' @inheritParams h_data_plot |
|
| 120 |
#' |
|
| 121 |
#' @return A summary table with statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`). |
|
| 122 |
#' |
|
| 123 |
#' @examples |
|
| 124 |
#' library(dplyr) |
|
| 125 |
#' library(survival) |
|
| 126 |
#' |
|
| 127 |
#' adtte <- tern_ex_adtte %>% filter(PARAMCD == "OS") |
|
| 128 |
#' fit <- survfit( |
|
| 129 |
#' formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, |
|
| 130 |
#' data = adtte |
|
| 131 |
#' ) |
|
| 132 |
#' h_tbl_median_surv(fit_km = fit) |
|
| 133 |
#' |
|
| 134 |
#' @export |
|
| 135 |
h_tbl_median_surv <- function(fit_km, armval = "All") {
|
|
| 136 | 10x |
y <- if (is.null(fit_km$strata)) {
|
| 137 | ! |
as.data.frame(t(summary(fit_km)$table), row.names = armval) |
| 138 |
} else {
|
|
| 139 | 10x |
tbl <- summary(fit_km)$table |
| 140 | 10x |
rownames_lst <- strsplit(sub("=", "equals", rownames(tbl)), "equals")
|
| 141 | 10x |
rownames(tbl) <- matrix(unlist(rownames_lst), ncol = 2, byrow = TRUE)[, 2] |
| 142 | 10x |
as.data.frame(tbl) |
| 143 |
} |
|
| 144 | 10x |
conf.int <- summary(fit_km)$conf.int # nolint |
| 145 | 10x |
y$records <- round(y$records) |
| 146 | 10x |
y$median <- signif(y$median, 4) |
| 147 | 10x |
y$`CI` <- paste0( |
| 148 | 10x |
"(", signif(y[[paste0(conf.int, "LCL")]], 4), ", ", signif(y[[paste0(conf.int, "UCL")]], 4), ")"
|
| 149 |
) |
|
| 150 | 10x |
stats::setNames( |
| 151 | 10x |
y[c("records", "median", "CI")],
|
| 152 | 10x |
c("N", "Median", f_conf_level(conf.int))
|
| 153 |
) |
|
| 154 |
} |
|
| 155 | ||
| 156 |
#' Helper function for generating a pairwise Cox-PH table |
|
| 157 |
#' |
|
| 158 |
#' @description `r lifecycle::badge("stable")`
|
|
| 159 |
#' |
|
| 160 |
#' Create a `data.frame` of pairwise stratified or unstratified Cox-PH analysis results. |
|
| 161 |
#' |
|
| 162 |
#' @inheritParams g_km |
|
| 163 |
#' @param annot_coxph_ref_lbls (`flag`)\cr whether the reference group should be explicitly printed in labels for the |
|
| 164 |
#' `annot_coxph` table. If `FALSE` (default), only comparison groups will be printed in `annot_coxph` table labels. |
|
| 165 |
#' |
|
| 166 |
#' @return A `data.frame` containing statistics `HR`, `XX% CI` (`XX` taken from `control_coxph_pw`), |
|
| 167 |
#' and `p-value (log-rank)`. |
|
| 168 |
#' |
|
| 169 |
#' @examples |
|
| 170 |
#' library(dplyr) |
|
| 171 |
#' |
|
| 172 |
#' adtte <- tern_ex_adtte %>% |
|
| 173 |
#' filter(PARAMCD == "OS") %>% |
|
| 174 |
#' mutate(is_event = CNSR == 0) |
|
| 175 |
#' |
|
| 176 |
#' h_tbl_coxph_pairwise( |
|
| 177 |
#' df = adtte, |
|
| 178 |
#' variables = list(tte = "AVAL", is_event = "is_event", arm = "ARM"), |
|
| 179 |
#' control_coxph_pw = control_coxph(conf_level = 0.9) |
|
| 180 |
#' ) |
|
| 181 |
#' |
|
| 182 |
#' @export |
|
| 183 |
h_tbl_coxph_pairwise <- function(df, |
|
| 184 |
variables, |
|
| 185 |
ref_group_coxph = NULL, |
|
| 186 |
control_coxph_pw = control_coxph(), |
|
| 187 |
annot_coxph_ref_lbls = FALSE) {
|
|
| 188 | 4x |
if ("strat" %in% names(variables)) {
|
| 189 | ! |
warning( |
| 190 | ! |
"Warning: the `strat` element name of the `variables` list argument to `h_tbl_coxph_pairwise() ", |
| 191 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 192 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 193 |
) |
|
| 194 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 195 |
} |
|
| 196 | ||
| 197 | 4x |
assert_df_with_variables(df, variables) |
| 198 | 4x |
checkmate::assert_choice(ref_group_coxph, levels(df[[variables$arm]]), null.ok = TRUE) |
| 199 | 4x |
checkmate::assert_flag(annot_coxph_ref_lbls) |
| 200 | ||
| 201 | 4x |
arm <- variables$arm |
| 202 | 4x |
df[[arm]] <- factor(df[[arm]]) |
| 203 | ||
| 204 | 4x |
ref_group <- if (!is.null(ref_group_coxph)) ref_group_coxph else levels(df[[variables$arm]])[1] |
| 205 | 4x |
comp_group <- setdiff(levels(df[[arm]]), ref_group) |
| 206 | ||
| 207 | 4x |
results <- Map(function(comp) {
|
| 208 | 8x |
res <- s_coxph_pairwise( |
| 209 | 8x |
df = df[df[[arm]] == comp, , drop = FALSE], |
| 210 | 8x |
.ref_group = df[df[[arm]] == ref_group, , drop = FALSE], |
| 211 | 8x |
.in_ref_col = FALSE, |
| 212 | 8x |
.var = variables$tte, |
| 213 | 8x |
is_event = variables$is_event, |
| 214 | 8x |
strata = variables$strata, |
| 215 | 8x |
control = control_coxph_pw |
| 216 |
) |
|
| 217 | 8x |
res_df <- data.frame( |
| 218 | 8x |
hr = format(round(res$hr, 2), nsmall = 2), |
| 219 | 8x |
hr_ci = paste0( |
| 220 | 8x |
"(", format(round(res$hr_ci[1], 2), nsmall = 2), ", ",
|
| 221 | 8x |
format(round(res$hr_ci[2], 2), nsmall = 2), ")" |
| 222 |
), |
|
| 223 | 8x |
pvalue = if (res$pvalue < 0.0001) "<0.0001" else format(round(res$pvalue, 4), 4), |
| 224 | 8x |
stringsAsFactors = FALSE |
| 225 |
) |
|
| 226 | 8x |
colnames(res_df) <- c("HR", vapply(res[c("hr_ci", "pvalue")], obj_label, FUN.VALUE = "character"))
|
| 227 | 8x |
row.names(res_df) <- comp |
| 228 | 8x |
res_df |
| 229 | 4x |
}, comp_group) |
| 230 | 1x |
if (annot_coxph_ref_lbls) names(results) <- paste(comp_group, "vs.", ref_group) |
| 231 | ||
| 232 | 4x |
do.call(rbind, results) |
| 233 |
} |
|
| 234 | ||
| 235 |
#' Helper function to tidy survival fit data |
|
| 236 |
#' |
|
| 237 |
#' @description `r lifecycle::badge("stable")`
|
|
| 238 |
#' |
|
| 239 |
#' Convert the survival fit data into a data frame designed for plotting |
|
| 240 |
#' within `g_km`. |
|
| 241 |
#' |
|
| 242 |
#' This starts from the [broom::tidy()] result, and then: |
|
| 243 |
#' * Post-processes the `strata` column into a factor. |
|
| 244 |
#' * Extends each stratum by an additional first row with time 0 and probability 1 so that |
|
| 245 |
#' downstream plot lines start at those coordinates. |
|
| 246 |
#' * Adds a `censor` column. |
|
| 247 |
#' * Filters the rows before `max_time`. |
|
| 248 |
#' |
|
| 249 |
#' @inheritParams g_km |
|
| 250 |
#' @param fit_km (`survfit`)\cr result of [survival::survfit()]. |
|
| 251 |
#' @param armval (`string`)\cr used as strata name when treatment arm variable only has one level. Default is `"All"`. |
|
| 252 |
#' |
|
| 253 |
#' @return A `tibble` with columns `time`, `n.risk`, `n.event`, `n.censor`, `estimate`, `std.error`, `conf.high`, |
|
| 254 |
#' `conf.low`, `strata`, and `censor`. |
|
| 255 |
#' |
|
| 256 |
#' @examples |
|
| 257 |
#' library(dplyr) |
|
| 258 |
#' library(survival) |
|
| 259 |
#' |
|
| 260 |
#' # Test with multiple arms |
|
| 261 |
#' tern_ex_adtte %>% |
|
| 262 |
#' filter(PARAMCD == "OS") %>% |
|
| 263 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>% |
|
| 264 |
#' h_data_plot() |
|
| 265 |
#' |
|
| 266 |
#' # Test with single arm |
|
| 267 |
#' tern_ex_adtte %>% |
|
| 268 |
#' filter(PARAMCD == "OS", ARMCD == "ARM B") %>% |
|
| 269 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>% |
|
| 270 |
#' h_data_plot(armval = "ARM B") |
|
| 271 |
#' |
|
| 272 |
#' @export |
|
| 273 |
h_data_plot <- function(fit_km, |
|
| 274 |
armval = "All", |
|
| 275 |
max_time = NULL) {
|
|
| 276 | 18x |
y <- broom::tidy(fit_km) |
| 277 | ||
| 278 | 18x |
if (!is.null(fit_km$strata)) {
|
| 279 | 18x |
fit_km_var_level <- strsplit(sub("=", "equals", names(fit_km$strata)), "equals")
|
| 280 | 18x |
strata_levels <- vapply(fit_km_var_level, FUN = "[", FUN.VALUE = "a", i = 2) |
| 281 | 18x |
strata_var_level <- strsplit(sub("=", "equals", y$strata), "equals")
|
| 282 | 18x |
y$strata <- factor( |
| 283 | 18x |
vapply(strata_var_level, FUN = "[", FUN.VALUE = "a", i = 2), |
| 284 | 18x |
levels = strata_levels |
| 285 |
) |
|
| 286 |
} else {
|
|
| 287 | ! |
y$strata <- armval |
| 288 |
} |
|
| 289 | ||
| 290 | 18x |
y_by_strata <- split(y, y$strata) |
| 291 | 18x |
y_by_strata_extended <- lapply( |
| 292 | 18x |
y_by_strata, |
| 293 | 18x |
FUN = function(tbl) {
|
| 294 | 53x |
first_row <- tbl[1L, ] |
| 295 | 53x |
first_row$time <- 0 |
| 296 | 53x |
first_row$n.risk <- sum(first_row[, c("n.risk", "n.event", "n.censor")])
|
| 297 | 53x |
first_row$n.event <- first_row$n.censor <- 0 |
| 298 | 53x |
first_row$estimate <- first_row$conf.high <- first_row$conf.low <- 1 |
| 299 | 53x |
first_row$std.error <- 0 |
| 300 | 53x |
rbind( |
| 301 | 53x |
first_row, |
| 302 | 53x |
tbl |
| 303 |
) |
|
| 304 |
} |
|
| 305 |
) |
|
| 306 | 18x |
y <- do.call(rbind, y_by_strata_extended) |
| 307 | ||
| 308 | 18x |
y$censor <- ifelse(y$n.censor > 0, y$estimate, NA) |
| 309 | 18x |
if (!is.null(max_time)) {
|
| 310 | 1x |
y <- y[y$time <= max(max_time), ] |
| 311 |
} |
|
| 312 | 18x |
y |
| 313 |
} |
|
| 314 | ||
| 315 |
## Deprecated Functions ---- |
|
| 316 | ||
| 317 |
#' Helper function to create a KM plot |
|
| 318 |
#' |
|
| 319 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 320 |
#' |
|
| 321 |
#' Draw the Kaplan-Meier plot using `ggplot2`. |
|
| 322 |
#' |
|
| 323 |
#' @inheritParams g_km |
|
| 324 |
#' @param data (`data.frame`)\cr survival data as pre-processed by `h_data_plot`. |
|
| 325 |
#' |
|
| 326 |
#' @return A `ggplot` object. |
|
| 327 |
#' |
|
| 328 |
#' @examples |
|
| 329 |
#' \donttest{
|
|
| 330 |
#' library(dplyr) |
|
| 331 |
#' library(survival) |
|
| 332 |
#' |
|
| 333 |
#' fit_km <- tern_ex_adtte %>% |
|
| 334 |
#' filter(PARAMCD == "OS") %>% |
|
| 335 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) |
|
| 336 |
#' data_plot <- h_data_plot(fit_km = fit_km) |
|
| 337 |
#' xticks <- h_xticks(data = data_plot) |
|
| 338 |
#' gg <- h_ggkm( |
|
| 339 |
#' data = data_plot, |
|
| 340 |
#' censor_show = TRUE, |
|
| 341 |
#' xticks = xticks, |
|
| 342 |
#' xlab = "Days", |
|
| 343 |
#' yval = "Survival", |
|
| 344 |
#' ylab = "Survival Probability", |
|
| 345 |
#' title = "Survival" |
|
| 346 |
#' ) |
|
| 347 |
#' gg |
|
| 348 |
#' } |
|
| 349 |
#' |
|
| 350 |
#' @export |
|
| 351 |
h_ggkm <- function(data, |
|
| 352 |
xticks = NULL, |
|
| 353 |
yval = "Survival", |
|
| 354 |
censor_show, |
|
| 355 |
xlab, |
|
| 356 |
ylab, |
|
| 357 |
ylim = NULL, |
|
| 358 |
title, |
|
| 359 |
footnotes = NULL, |
|
| 360 |
max_time = NULL, |
|
| 361 |
lwd = 1, |
|
| 362 |
lty = NULL, |
|
| 363 |
pch = 3, |
|
| 364 |
size = 2, |
|
| 365 |
col = NULL, |
|
| 366 |
ci_ribbon = FALSE, |
|
| 367 |
ggtheme = nestcolor::theme_nest()) {
|
|
| 368 | 1x |
lifecycle::deprecate_warn( |
| 369 | 1x |
"0.9.4", |
| 370 | 1x |
"h_ggkm()", |
| 371 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 372 |
) |
|
| 373 | 1x |
checkmate::assert_numeric(lty, null.ok = TRUE) |
| 374 | 1x |
checkmate::assert_character(col, null.ok = TRUE) |
| 375 | ||
| 376 | 1x |
if (is.null(ylim)) {
|
| 377 | 1x |
data_lims <- data |
| 378 | ! |
if (yval == "Failure") data_lims[["estimate"]] <- 1 - data_lims[["estimate"]] |
| 379 | 1x |
if (!is.null(max_time)) {
|
| 380 | ! |
y_lwr <- min(data_lims[data_lims$time < max_time, ][["estimate"]]) |
| 381 | ! |
y_upr <- max(data_lims[data_lims$time < max_time, ][["estimate"]]) |
| 382 |
} else {
|
|
| 383 | 1x |
y_lwr <- min(data_lims[["estimate"]]) |
| 384 | 1x |
y_upr <- max(data_lims[["estimate"]]) |
| 385 |
} |
|
| 386 | 1x |
ylim <- c(y_lwr, y_upr) |
| 387 |
} |
|
| 388 | 1x |
checkmate::assert_numeric(ylim, finite = TRUE, any.missing = FALSE, len = 2, sorted = TRUE) |
| 389 | ||
| 390 |
# change estimates of survival to estimates of failure (1 - survival) |
|
| 391 | 1x |
if (yval == "Failure") {
|
| 392 | ! |
data$estimate <- 1 - data$estimate |
| 393 | ! |
data[c("conf.high", "conf.low")] <- list(1 - data$conf.low, 1 - data$conf.high)
|
| 394 | ! |
data$censor <- 1 - data$censor |
| 395 |
} |
|
| 396 | ||
| 397 | 1x |
gg <- {
|
| 398 | 1x |
ggplot2::ggplot( |
| 399 | 1x |
data = data, |
| 400 | 1x |
mapping = ggplot2::aes( |
| 401 | 1x |
x = .data[["time"]], |
| 402 | 1x |
y = .data[["estimate"]], |
| 403 | 1x |
ymin = .data[["conf.low"]], |
| 404 | 1x |
ymax = .data[["conf.high"]], |
| 405 | 1x |
color = .data[["strata"]], |
| 406 | 1x |
fill = .data[["strata"]] |
| 407 |
) |
|
| 408 |
) + |
|
| 409 | 1x |
ggplot2::geom_hline(yintercept = 0) |
| 410 |
} |
|
| 411 | ||
| 412 | 1x |
if (ci_ribbon) {
|
| 413 | ! |
gg <- gg + ggplot2::geom_ribbon(alpha = .3, lty = 0) |
| 414 |
} |
|
| 415 | ||
| 416 | 1x |
gg <- if (is.null(lty)) {
|
| 417 | 1x |
gg + |
| 418 | 1x |
ggplot2::geom_step(linewidth = lwd) |
| 419 | 1x |
} else if (checkmate::test_number(lty)) {
|
| 420 | ! |
gg + |
| 421 | ! |
ggplot2::geom_step(linewidth = lwd, lty = lty) |
| 422 | 1x |
} else if (is.numeric(lty)) {
|
| 423 | ! |
gg + |
| 424 | ! |
ggplot2::geom_step(mapping = ggplot2::aes(linetype = .data[["strata"]]), linewidth = lwd) + |
| 425 | ! |
ggplot2::scale_linetype_manual(values = lty) |
| 426 |
} |
|
| 427 | ||
| 428 | 1x |
gg <- gg + |
| 429 | 1x |
ggplot2::coord_cartesian(ylim = ylim) + |
| 430 | 1x |
ggplot2::labs(x = xlab, y = ylab, title = title, caption = footnotes) |
| 431 | ||
| 432 | 1x |
if (!is.null(col)) {
|
| 433 | ! |
gg <- gg + |
| 434 | ! |
ggplot2::scale_color_manual(values = col) + |
| 435 | ! |
ggplot2::scale_fill_manual(values = col) |
| 436 |
} |
|
| 437 | 1x |
if (censor_show) {
|
| 438 | 1x |
dt <- data[data$n.censor != 0, ] |
| 439 | 1x |
dt$censor_lbl <- factor("Censored")
|
| 440 | ||
| 441 | 1x |
gg <- gg + ggplot2::geom_point( |
| 442 | 1x |
data = dt, |
| 443 | 1x |
ggplot2::aes( |
| 444 | 1x |
x = .data[["time"]], |
| 445 | 1x |
y = .data[["censor"]], |
| 446 | 1x |
shape = .data[["censor_lbl"]] |
| 447 |
), |
|
| 448 | 1x |
size = size, |
| 449 | 1x |
show.legend = TRUE, |
| 450 | 1x |
inherit.aes = TRUE |
| 451 |
) + |
|
| 452 | 1x |
ggplot2::scale_shape_manual(name = NULL, values = pch) + |
| 453 | 1x |
ggplot2::guides( |
| 454 | 1x |
shape = ggplot2::guide_legend(override.aes = list(linetype = NA)), |
| 455 | 1x |
fill = ggplot2::guide_legend(override.aes = list(shape = NA)) |
| 456 |
) |
|
| 457 |
} |
|
| 458 | ||
| 459 | 1x |
if (!is.null(max_time) && !is.null(xticks)) {
|
| 460 | ! |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks, limits = c(min(0, xticks), max(c(xticks, max_time)))) |
| 461 | 1x |
} else if (!is.null(xticks)) {
|
| 462 | 1x |
if (max(data$time) <= max(xticks)) {
|
| 463 | 1x |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks, limits = c(min(0, min(xticks)), max(xticks))) |
| 464 |
} else {
|
|
| 465 | ! |
gg <- gg + ggplot2::scale_x_continuous(breaks = xticks) |
| 466 |
} |
|
| 467 | ! |
} else if (!is.null(max_time)) {
|
| 468 | ! |
gg <- gg + ggplot2::scale_x_continuous(limits = c(0, max_time)) |
| 469 |
} |
|
| 470 | ||
| 471 | 1x |
if (!is.null(ggtheme)) {
|
| 472 | 1x |
gg <- gg + ggtheme |
| 473 |
} |
|
| 474 | ||
| 475 | 1x |
gg + ggplot2::theme( |
| 476 | 1x |
legend.position = "bottom", |
| 477 | 1x |
legend.title = ggplot2::element_blank(), |
| 478 | 1x |
legend.key.height = unit(0.02, "npc"), |
| 479 | 1x |
panel.grid.major.x = ggplot2::element_line(linewidth = 2) |
| 480 |
) |
|
| 481 |
} |
|
| 482 | ||
| 483 |
#' `ggplot` decomposition |
|
| 484 |
#' |
|
| 485 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 486 |
#' |
|
| 487 |
#' The elements composing the `ggplot` are extracted and organized in a `list`. |
|
| 488 |
#' |
|
| 489 |
#' @param gg (`ggplot`)\cr a graphic to decompose. |
|
| 490 |
#' |
|
| 491 |
#' @return A named `list` with elements: |
|
| 492 |
#' * `panel`: The panel. |
|
| 493 |
#' * `yaxis`: The y-axis. |
|
| 494 |
#' * `xaxis`: The x-axis. |
|
| 495 |
#' * `xlab`: The x-axis label. |
|
| 496 |
#' * `ylab`: The y-axis label. |
|
| 497 |
#' * `guide`: The legend. |
|
| 498 |
#' |
|
| 499 |
#' @examples |
|
| 500 |
#' \donttest{
|
|
| 501 |
#' library(dplyr) |
|
| 502 |
#' library(survival) |
|
| 503 |
#' library(grid) |
|
| 504 |
#' |
|
| 505 |
#' fit_km <- tern_ex_adtte %>% |
|
| 506 |
#' filter(PARAMCD == "OS") %>% |
|
| 507 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) |
|
| 508 |
#' data_plot <- h_data_plot(fit_km = fit_km) |
|
| 509 |
#' xticks <- h_xticks(data = data_plot) |
|
| 510 |
#' gg <- h_ggkm( |
|
| 511 |
#' data = data_plot, |
|
| 512 |
#' yval = "Survival", |
|
| 513 |
#' censor_show = TRUE, |
|
| 514 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability", |
|
| 515 |
#' title = "tt", |
|
| 516 |
#' footnotes = "ff" |
|
| 517 |
#' ) |
|
| 518 |
#' |
|
| 519 |
#' g_el <- h_decompose_gg(gg) |
|
| 520 |
#' grid::grid.newpage() |
|
| 521 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "red", fill = "gray85", lwd = 5)) |
|
| 522 |
#' grid::grid.draw(g_el$panel) |
|
| 523 |
#' |
|
| 524 |
#' grid::grid.newpage() |
|
| 525 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "royalblue", fill = "gray85", lwd = 5)) |
|
| 526 |
#' grid::grid.draw(with(g_el, cbind(ylab, yaxis))) |
|
| 527 |
#' } |
|
| 528 |
#' |
|
| 529 |
#' @export |
|
| 530 |
h_decompose_gg <- function(gg) {
|
|
| 531 | 1x |
lifecycle::deprecate_warn( |
| 532 | 1x |
"0.9.4", |
| 533 | 1x |
"h_decompose_gg()", |
| 534 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 535 |
) |
|
| 536 | 1x |
g_el <- ggplot2::ggplotGrob(gg) |
| 537 | 1x |
y <- c( |
| 538 | 1x |
panel = "panel", |
| 539 | 1x |
yaxis = "axis-l", |
| 540 | 1x |
xaxis = "axis-b", |
| 541 | 1x |
xlab = "xlab-b", |
| 542 | 1x |
ylab = "ylab-l", |
| 543 | 1x |
guide = "guide" |
| 544 |
) |
|
| 545 | 1x |
lapply(X = y, function(x) gtable::gtable_filter(g_el, x)) |
| 546 |
} |
|
| 547 | ||
| 548 |
#' Helper function to prepare a KM layout |
|
| 549 |
#' |
|
| 550 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 551 |
#' |
|
| 552 |
#' Prepares a (5 rows) x (2 cols) layout for the Kaplan-Meier curve. |
|
| 553 |
#' |
|
| 554 |
#' @inheritParams g_km |
|
| 555 |
#' @inheritParams h_ggkm |
|
| 556 |
#' @param g_el (`list` of `gtable`)\cr list as obtained by `h_decompose_gg()`. |
|
| 557 |
#' @param annot_at_risk (`flag`)\cr compute and add the annotation table reporting the number of |
|
| 558 |
#' patient at risk matching the main grid of the Kaplan-Meier curve. |
|
| 559 |
#' |
|
| 560 |
#' @return A grid layout. |
|
| 561 |
#' |
|
| 562 |
#' @details The layout corresponds to a grid of two columns and five rows of unequal dimensions. Most of the |
|
| 563 |
#' dimension are fixed, only the curve is flexible and will accommodate with the remaining free space. |
|
| 564 |
#' * The left column gets the annotation of the `ggplot` (y-axis) and the names of the strata for the patient |
|
| 565 |
#' at risk tabulation. The main constraint is about the width of the columns which must allow the writing of |
|
| 566 |
#' the strata name. |
|
| 567 |
#' * The right column receive the `ggplot`, the legend, the x-axis and the patient at risk table. |
|
| 568 |
#' |
|
| 569 |
#' @examples |
|
| 570 |
#' \donttest{
|
|
| 571 |
#' library(dplyr) |
|
| 572 |
#' library(survival) |
|
| 573 |
#' library(grid) |
|
| 574 |
#' |
|
| 575 |
#' fit_km <- tern_ex_adtte %>% |
|
| 576 |
#' filter(PARAMCD == "OS") %>% |
|
| 577 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) |
|
| 578 |
#' data_plot <- h_data_plot(fit_km = fit_km) |
|
| 579 |
#' xticks <- h_xticks(data = data_plot) |
|
| 580 |
#' gg <- h_ggkm( |
|
| 581 |
#' data = data_plot, |
|
| 582 |
#' censor_show = TRUE, |
|
| 583 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability", |
|
| 584 |
#' title = "tt", footnotes = "ff", yval = "Survival" |
|
| 585 |
#' ) |
|
| 586 |
#' g_el <- h_decompose_gg(gg) |
|
| 587 |
#' lyt <- h_km_layout(data = data_plot, g_el = g_el, title = "t", footnotes = "f") |
|
| 588 |
#' grid.show.layout(lyt) |
|
| 589 |
#' } |
|
| 590 |
#' |
|
| 591 |
#' @export |
|
| 592 |
h_km_layout <- function(data, g_el, title, footnotes, annot_at_risk = TRUE, annot_at_risk_title = TRUE) {
|
|
| 593 | 1x |
lifecycle::deprecate_warn( |
| 594 | 1x |
"0.9.4", |
| 595 | 1x |
"h_km_layout()", |
| 596 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 597 |
) |
|
| 598 | 1x |
txtlines <- levels(as.factor(data$strata)) |
| 599 | 1x |
nlines <- nlevels(as.factor(data$strata)) |
| 600 | 1x |
col_annot_width <- max( |
| 601 | 1x |
c( |
| 602 | 1x |
as.numeric(grid::convertX(g_el$yaxis$widths + g_el$ylab$widths, "pt")), |
| 603 | 1x |
as.numeric( |
| 604 | 1x |
grid::convertX( |
| 605 | 1x |
grid::stringWidth(txtlines) + grid::unit(7, "pt"), "pt" |
| 606 |
) |
|
| 607 |
) |
|
| 608 |
) |
|
| 609 |
) |
|
| 610 | ||
| 611 | 1x |
ttl_row <- as.numeric(!is.null(title)) |
| 612 | 1x |
foot_row <- as.numeric(!is.null(footnotes)) |
| 613 | 1x |
no_tbl_ind <- c() |
| 614 | 1x |
ht_x <- c() |
| 615 | 1x |
ht_units <- c() |
| 616 | ||
| 617 | 1x |
if (ttl_row == 1) {
|
| 618 | 1x |
no_tbl_ind <- c(no_tbl_ind, TRUE) |
| 619 | 1x |
ht_x <- c(ht_x, 2) |
| 620 | 1x |
ht_units <- c(ht_units, "lines") |
| 621 |
} |
|
| 622 | ||
| 623 | 1x |
no_tbl_ind <- c(no_tbl_ind, rep(TRUE, 3), rep(FALSE, 2)) |
| 624 | 1x |
ht_x <- c( |
| 625 | 1x |
ht_x, |
| 626 | 1x |
1, |
| 627 | 1x |
grid::convertX(with(g_el, xaxis$heights + ylab$widths), "pt") + grid::unit(5, "pt"), |
| 628 | 1x |
grid::convertX(g_el$guide$heights, "pt") + grid::unit(2, "pt"), |
| 629 | 1x |
1, |
| 630 | 1x |
nlines + 0.5, |
| 631 | 1x |
grid::convertX(with(g_el, xaxis$heights + ylab$widths), "pt") |
| 632 |
) |
|
| 633 | 1x |
ht_units <- c( |
| 634 | 1x |
ht_units, |
| 635 | 1x |
"null", |
| 636 | 1x |
"pt", |
| 637 | 1x |
"pt", |
| 638 | 1x |
"lines", |
| 639 | 1x |
"lines", |
| 640 | 1x |
"pt" |
| 641 |
) |
|
| 642 | ||
| 643 | 1x |
if (foot_row == 1) {
|
| 644 | 1x |
no_tbl_ind <- c(no_tbl_ind, TRUE) |
| 645 | 1x |
ht_x <- c(ht_x, 1) |
| 646 | 1x |
ht_units <- c(ht_units, "lines") |
| 647 |
} |
|
| 648 | 1x |
if (annot_at_risk) {
|
| 649 | 1x |
no_at_risk_tbl <- rep(TRUE, 6 + ttl_row + foot_row) |
| 650 | 1x |
if (!annot_at_risk_title) {
|
| 651 | ! |
no_at_risk_tbl[length(no_at_risk_tbl) - 2 - foot_row] <- FALSE |
| 652 |
} |
|
| 653 |
} else {
|
|
| 654 | ! |
no_at_risk_tbl <- no_tbl_ind |
| 655 |
} |
|
| 656 | ||
| 657 | 1x |
grid::grid.layout( |
| 658 | 1x |
nrow = sum(no_at_risk_tbl), ncol = 2, |
| 659 | 1x |
widths = grid::unit(c(col_annot_width, 1), c("pt", "null")),
|
| 660 | 1x |
heights = grid::unit( |
| 661 | 1x |
x = ht_x[no_at_risk_tbl], |
| 662 | 1x |
units = ht_units[no_at_risk_tbl] |
| 663 |
) |
|
| 664 |
) |
|
| 665 |
} |
|
| 666 | ||
| 667 |
#' Helper function to create patient-at-risk grobs |
|
| 668 |
#' |
|
| 669 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 670 |
#' |
|
| 671 |
#' Two graphical objects are obtained, one corresponding to row labeling and the second to the table of |
|
| 672 |
#' numbers of patients at risk. If `title = TRUE`, a third object corresponding to the table title is |
|
| 673 |
#' also obtained. |
|
| 674 |
#' |
|
| 675 |
#' @inheritParams g_km |
|
| 676 |
#' @inheritParams h_ggkm |
|
| 677 |
#' @param annot_tbl (`data.frame`)\cr annotation as prepared by [survival::summary.survfit()] which |
|
| 678 |
#' includes the number of patients at risk at given time points. |
|
| 679 |
#' @param xlim (`numeric(1)`)\cr the maximum value on the x-axis (used to ensure the at risk table aligns with the KM |
|
| 680 |
#' graph). |
|
| 681 |
#' @param title (`flag`)\cr whether the "Patients at Risk" title should be added above the `annot_at_risk` |
|
| 682 |
#' table. Has no effect if `annot_at_risk` is `FALSE`. Defaults to `TRUE`. |
|
| 683 |
#' |
|
| 684 |
#' @return A named `list` of two `gTree` objects if `title = FALSE`: `at_risk` and `label`, or three |
|
| 685 |
#' `gTree` objects if `title = TRUE`: `at_risk`, `label`, and `title`. |
|
| 686 |
#' |
|
| 687 |
#' @examples |
|
| 688 |
#' \donttest{
|
|
| 689 |
#' library(dplyr) |
|
| 690 |
#' library(survival) |
|
| 691 |
#' library(grid) |
|
| 692 |
#' |
|
| 693 |
#' fit_km <- tern_ex_adtte %>% |
|
| 694 |
#' filter(PARAMCD == "OS") %>% |
|
| 695 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) |
|
| 696 |
#' |
|
| 697 |
#' data_plot <- h_data_plot(fit_km = fit_km) |
|
| 698 |
#' |
|
| 699 |
#' xticks <- h_xticks(data = data_plot) |
|
| 700 |
#' |
|
| 701 |
#' gg <- h_ggkm( |
|
| 702 |
#' data = data_plot, |
|
| 703 |
#' censor_show = TRUE, |
|
| 704 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability", |
|
| 705 |
#' title = "tt", footnotes = "ff", yval = "Survival" |
|
| 706 |
#' ) |
|
| 707 |
#' |
|
| 708 |
#' # The annotation table reports the patient at risk for a given strata and |
|
| 709 |
#' # times (`xticks`). |
|
| 710 |
#' annot_tbl <- summary(fit_km, times = xticks) |
|
| 711 |
#' if (is.null(fit_km$strata)) {
|
|
| 712 |
#' annot_tbl <- with(annot_tbl, data.frame(n.risk = n.risk, time = time, strata = "All")) |
|
| 713 |
#' } else {
|
|
| 714 |
#' strata_lst <- strsplit(sub("=", "equals", levels(annot_tbl$strata)), "equals")
|
|
| 715 |
#' levels(annot_tbl$strata) <- matrix(unlist(strata_lst), ncol = 2, byrow = TRUE)[, 2] |
|
| 716 |
#' annot_tbl <- data.frame( |
|
| 717 |
#' n.risk = annot_tbl$n.risk, |
|
| 718 |
#' time = annot_tbl$time, |
|
| 719 |
#' strata = annot_tbl$strata |
|
| 720 |
#' ) |
|
| 721 |
#' } |
|
| 722 |
#' |
|
| 723 |
#' # The annotation table is transformed into a grob. |
|
| 724 |
#' tbl <- h_grob_tbl_at_risk(data = data_plot, annot_tbl = annot_tbl, xlim = max(xticks)) |
|
| 725 |
#' |
|
| 726 |
#' # For the representation, the layout is estimated for which the decomposition |
|
| 727 |
#' # of the graphic element is necessary. |
|
| 728 |
#' g_el <- h_decompose_gg(gg) |
|
| 729 |
#' lyt <- h_km_layout(data = data_plot, g_el = g_el, title = "t", footnotes = "f") |
|
| 730 |
#' |
|
| 731 |
#' grid::grid.newpage() |
|
| 732 |
#' pushViewport(viewport(layout = lyt, height = .95, width = .95)) |
|
| 733 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "purple", fill = "gray85", lwd = 1)) |
|
| 734 |
#' pushViewport(viewport(layout.pos.row = 3:4, layout.pos.col = 2)) |
|
| 735 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "orange", fill = "gray85", lwd = 1)) |
|
| 736 |
#' grid::grid.draw(tbl$at_risk) |
|
| 737 |
#' popViewport() |
|
| 738 |
#' pushViewport(viewport(layout.pos.row = 3:4, layout.pos.col = 1)) |
|
| 739 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "green3", fill = "gray85", lwd = 1)) |
|
| 740 |
#' grid::grid.draw(tbl$label) |
|
| 741 |
#' } |
|
| 742 |
#' |
|
| 743 |
#' @export |
|
| 744 |
h_grob_tbl_at_risk <- function(data, annot_tbl, xlim, title = TRUE) {
|
|
| 745 | 1x |
lifecycle::deprecate_warn( |
| 746 | 1x |
"0.9.4", |
| 747 | 1x |
"h_grob_tbl_at_risk()", |
| 748 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 749 |
) |
|
| 750 | 1x |
txtlines <- levels(as.factor(data$strata)) |
| 751 | 1x |
nlines <- nlevels(as.factor(data$strata)) |
| 752 | 1x |
y_int <- annot_tbl$time[2] - annot_tbl$time[1] |
| 753 | 1x |
annot_tbl <- expand.grid( |
| 754 | 1x |
time = seq(0, xlim, y_int), |
| 755 | 1x |
strata = unique(annot_tbl$strata) |
| 756 | 1x |
) %>% dplyr::left_join(annot_tbl, by = c("time", "strata"))
|
| 757 | 1x |
annot_tbl[is.na(annot_tbl)] <- 0 |
| 758 | 1x |
y_str_unit <- as.numeric(annot_tbl$strata) |
| 759 | 1x |
vp_table <- grid::plotViewport(margins = grid::unit(c(0, 0, 0, 0), "lines")) |
| 760 | 1x |
if (title) {
|
| 761 | 1x |
gb_table_title <- grid::gList( |
| 762 | 1x |
grid::textGrob( |
| 763 | 1x |
label = "Patients at Risk:", |
| 764 | 1x |
x = 1, |
| 765 | 1x |
y = grid::unit(0.2, "native"), |
| 766 | 1x |
gp = grid::gpar(fontface = "bold", fontsize = 10) |
| 767 |
) |
|
| 768 |
) |
|
| 769 |
} |
|
| 770 | 1x |
gb_table_left_annot <- grid::gList( |
| 771 | 1x |
grid::rectGrob( |
| 772 | 1x |
x = 0, y = grid::unit(c(1:nlines) - 1, "lines"), |
| 773 | 1x |
gp = grid::gpar(fill = c("gray95", "gray90"), alpha = 1, col = "white"),
|
| 774 | 1x |
height = grid::unit(1, "lines"), just = "bottom", hjust = 0 |
| 775 |
), |
|
| 776 | 1x |
grid::textGrob( |
| 777 | 1x |
label = unique(annot_tbl$strata), |
| 778 | 1x |
x = 0.5, |
| 779 | 1x |
y = grid::unit( |
| 780 | 1x |
(max(unique(y_str_unit)) - unique(y_str_unit)) + 0.75, |
| 781 | 1x |
"native" |
| 782 |
), |
|
| 783 | 1x |
gp = grid::gpar(fontface = "italic", fontsize = 10) |
| 784 |
) |
|
| 785 |
) |
|
| 786 | 1x |
gb_patient_at_risk <- grid::gList( |
| 787 | 1x |
grid::rectGrob( |
| 788 | 1x |
x = 0, y = grid::unit(c(1:nlines) - 1, "lines"), |
| 789 | 1x |
gp = grid::gpar(fill = c("gray95", "gray90"), alpha = 1, col = "white"),
|
| 790 | 1x |
height = grid::unit(1, "lines"), just = "bottom", hjust = 0 |
| 791 |
), |
|
| 792 | 1x |
grid::textGrob( |
| 793 | 1x |
label = annot_tbl$n.risk, |
| 794 | 1x |
x = grid::unit(annot_tbl$time, "native"), |
| 795 | 1x |
y = grid::unit( |
| 796 | 1x |
(max(y_str_unit) - y_str_unit) + .5, |
| 797 | 1x |
"line" |
| 798 | 1x |
) # maybe native |
| 799 |
) |
|
| 800 |
) |
|
| 801 | ||
| 802 | 1x |
ret <- list( |
| 803 | 1x |
at_risk = grid::gList( |
| 804 | 1x |
grid::gTree( |
| 805 | 1x |
vp = vp_table, |
| 806 | 1x |
children = grid::gList( |
| 807 | 1x |
grid::gTree( |
| 808 | 1x |
vp = grid::dataViewport( |
| 809 | 1x |
xscale = c(0, xlim) + c(-0.05, 0.05) * xlim, |
| 810 | 1x |
yscale = c(0, nlines + 1), |
| 811 | 1x |
extension = c(0.05, 0) |
| 812 |
), |
|
| 813 | 1x |
children = grid::gList(gb_patient_at_risk) |
| 814 |
) |
|
| 815 |
) |
|
| 816 |
) |
|
| 817 |
), |
|
| 818 | 1x |
label = grid::gList( |
| 819 | 1x |
grid::gTree( |
| 820 | 1x |
vp = grid::viewport(width = max(grid::stringWidth(txtlines))), |
| 821 | 1x |
children = grid::gList( |
| 822 | 1x |
grid::gTree( |
| 823 | 1x |
vp = grid::dataViewport( |
| 824 | 1x |
xscale = 0:1, |
| 825 | 1x |
yscale = c(0, nlines + 1), |
| 826 | 1x |
extension = c(0.0, 0) |
| 827 |
), |
|
| 828 | 1x |
children = grid::gList(gb_table_left_annot) |
| 829 |
) |
|
| 830 |
) |
|
| 831 |
) |
|
| 832 |
) |
|
| 833 |
) |
|
| 834 | ||
| 835 | 1x |
if (title) {
|
| 836 | 1x |
ret[["title"]] <- grid::gList( |
| 837 | 1x |
grid::gTree( |
| 838 | 1x |
vp = grid::viewport(width = max(grid::stringWidth(txtlines))), |
| 839 | 1x |
children = grid::gList( |
| 840 | 1x |
grid::gTree( |
| 841 | 1x |
vp = grid::dataViewport( |
| 842 | 1x |
xscale = 0:1, |
| 843 | 1x |
yscale = c(0, 1), |
| 844 | 1x |
extension = c(0, 0) |
| 845 |
), |
|
| 846 | 1x |
children = grid::gList(gb_table_title) |
| 847 |
) |
|
| 848 |
) |
|
| 849 |
) |
|
| 850 |
) |
|
| 851 |
} |
|
| 852 | ||
| 853 | 1x |
ret |
| 854 |
} |
|
| 855 | ||
| 856 |
#' Helper function to create survival estimation grobs |
|
| 857 |
#' |
|
| 858 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 859 |
#' |
|
| 860 |
#' The survival fit is transformed in a grob containing a table with groups in |
|
| 861 |
#' rows characterized by N, median and 95% confidence interval. |
|
| 862 |
#' |
|
| 863 |
#' @inheritParams g_km |
|
| 864 |
#' @inheritParams h_data_plot |
|
| 865 |
#' @param ttheme (`list`)\cr see [gridExtra::ttheme_default()]. |
|
| 866 |
#' @param x (`proportion`)\cr a value between 0 and 1 specifying x-location. |
|
| 867 |
#' @param y (`proportion`)\cr a value between 0 and 1 specifying y-location. |
|
| 868 |
#' @param width (`grid::unit`)\cr width (as a unit) to use when printing the grob. |
|
| 869 |
#' |
|
| 870 |
#' @return A `grob` of a table containing statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`). |
|
| 871 |
#' |
|
| 872 |
#' @examples |
|
| 873 |
#' \donttest{
|
|
| 874 |
#' library(dplyr) |
|
| 875 |
#' library(survival) |
|
| 876 |
#' library(grid) |
|
| 877 |
#' |
|
| 878 |
#' grid::grid.newpage() |
|
| 879 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "pink", fill = "gray85", lwd = 1)) |
|
| 880 |
#' tern_ex_adtte %>% |
|
| 881 |
#' filter(PARAMCD == "OS") %>% |
|
| 882 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) %>% |
|
| 883 |
#' h_grob_median_surv() %>% |
|
| 884 |
#' grid::grid.draw() |
|
| 885 |
#' } |
|
| 886 |
#' |
|
| 887 |
#' @export |
|
| 888 |
h_grob_median_surv <- function(fit_km, |
|
| 889 |
armval = "All", |
|
| 890 |
x = 0.9, |
|
| 891 |
y = 0.9, |
|
| 892 |
width = grid::unit(0.3, "npc"), |
|
| 893 |
ttheme = gridExtra::ttheme_default()) {
|
|
| 894 | 1x |
lifecycle::deprecate_warn( |
| 895 | 1x |
"0.9.4", |
| 896 | 1x |
"h_grob_median_surv()", |
| 897 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 898 |
) |
|
| 899 | 1x |
data <- h_tbl_median_surv(fit_km, armval = armval) |
| 900 | ||
| 901 | 1x |
width <- grid::convertUnit(grid::unit(as.numeric(width), grid::unitType(width)), "in") |
| 902 | 1x |
height <- width * (nrow(data) + 1) / 12 |
| 903 | ||
| 904 | 1x |
w <- paste(" ", c(
|
| 905 | 1x |
rownames(data)[which.max(nchar(rownames(data)))], |
| 906 | 1x |
sapply(names(data), function(x) c(x, data[[x]])[which.max(nchar(c(x, data[[x]])))]) |
| 907 |
)) |
|
| 908 | 1x |
w_unit <- grid::convertWidth(grid::stringWidth(w), "in", valueOnly = TRUE) |
| 909 | ||
| 910 | 1x |
w_txt <- sapply(1:64, function(x) {
|
| 911 | 64x |
graphics::par(ps = x) |
| 912 | 64x |
graphics::strwidth(w[4], units = "in") |
| 913 |
}) |
|
| 914 | 1x |
f_size_w <- which.max(w_txt[w_txt < as.numeric((w_unit / sum(w_unit)) * width)[4]]) |
| 915 | ||
| 916 | 1x |
h_txt <- sapply(1:64, function(x) {
|
| 917 | 64x |
graphics::par(ps = x) |
| 918 | 64x |
graphics::strheight(grid::stringHeight("X"), units = "in")
|
| 919 |
}) |
|
| 920 | 1x |
f_size_h <- which.max(h_txt[h_txt < as.numeric(grid::unit(as.numeric(height) / 4, grid::unitType(height)))]) |
| 921 | ||
| 922 | 1x |
if (ttheme$core$fg_params$fontsize == 12) {
|
| 923 | 1x |
ttheme$core$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 924 | 1x |
ttheme$colhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 925 | 1x |
ttheme$rowhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 926 |
} |
|
| 927 | ||
| 928 | 1x |
gt <- gridExtra::tableGrob( |
| 929 | 1x |
d = data, |
| 930 | 1x |
theme = ttheme |
| 931 |
) |
|
| 932 | 1x |
gt$widths <- ((w_unit / sum(w_unit)) * width) |
| 933 | 1x |
gt$heights <- rep(grid::unit(as.numeric(height) / 4, grid::unitType(height)), nrow(gt)) |
| 934 | ||
| 935 | 1x |
vp <- grid::viewport( |
| 936 | 1x |
x = grid::unit(x, "npc") + grid::unit(1, "lines"), |
| 937 | 1x |
y = grid::unit(y, "npc") + grid::unit(1.5, "lines"), |
| 938 | 1x |
height = height, |
| 939 | 1x |
width = width, |
| 940 | 1x |
just = c("right", "top")
|
| 941 |
) |
|
| 942 | ||
| 943 | 1x |
grid::gList( |
| 944 | 1x |
grid::gTree( |
| 945 | 1x |
vp = vp, |
| 946 | 1x |
children = grid::gList(gt) |
| 947 |
) |
|
| 948 |
) |
|
| 949 |
} |
|
| 950 | ||
| 951 |
#' Helper function to create grid object with y-axis annotation |
|
| 952 |
#' |
|
| 953 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 954 |
#' |
|
| 955 |
#' Build the y-axis annotation from a decomposed `ggplot`. |
|
| 956 |
#' |
|
| 957 |
#' @param ylab (`gtable`)\cr the y-lab as a graphical object derived from a `ggplot`. |
|
| 958 |
#' @param yaxis (`gtable`)\cr the y-axis as a graphical object derived from a `ggplot`. |
|
| 959 |
#' |
|
| 960 |
#' @return A `gTree` object containing the y-axis annotation from a `ggplot`. |
|
| 961 |
#' |
|
| 962 |
#' @examples |
|
| 963 |
#' \donttest{
|
|
| 964 |
#' library(dplyr) |
|
| 965 |
#' library(survival) |
|
| 966 |
#' library(grid) |
|
| 967 |
#' |
|
| 968 |
#' fit_km <- tern_ex_adtte %>% |
|
| 969 |
#' filter(PARAMCD == "OS") %>% |
|
| 970 |
#' survfit(formula = Surv(AVAL, 1 - CNSR) ~ ARMCD, data = .) |
|
| 971 |
#' data_plot <- h_data_plot(fit_km = fit_km) |
|
| 972 |
#' xticks <- h_xticks(data = data_plot) |
|
| 973 |
#' gg <- h_ggkm( |
|
| 974 |
#' data = data_plot, |
|
| 975 |
#' censor_show = TRUE, |
|
| 976 |
#' xticks = xticks, xlab = "Days", ylab = "Survival Probability", |
|
| 977 |
#' title = "title", footnotes = "footnotes", yval = "Survival" |
|
| 978 |
#' ) |
|
| 979 |
#' |
|
| 980 |
#' g_el <- h_decompose_gg(gg) |
|
| 981 |
#' |
|
| 982 |
#' grid::grid.newpage() |
|
| 983 |
#' pvp <- grid::plotViewport(margins = c(5, 4, 2, 20)) |
|
| 984 |
#' pushViewport(pvp) |
|
| 985 |
#' grid::grid.draw(h_grob_y_annot(ylab = g_el$ylab, yaxis = g_el$yaxis)) |
|
| 986 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "gray35", fill = NA)) |
|
| 987 |
#' } |
|
| 988 |
#' |
|
| 989 |
#' @export |
|
| 990 |
h_grob_y_annot <- function(ylab, yaxis) {
|
|
| 991 | 1x |
lifecycle::deprecate_warn( |
| 992 | 1x |
"0.9.4", |
| 993 | 1x |
"h_grob_y_annot()", |
| 994 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 995 |
) |
|
| 996 | 1x |
grid::gList( |
| 997 | 1x |
grid::gTree( |
| 998 | 1x |
vp = grid::viewport( |
| 999 | 1x |
width = grid::convertX(yaxis$widths + ylab$widths, "pt"), |
| 1000 | 1x |
x = grid::unit(1, "npc"), |
| 1001 | 1x |
just = "right" |
| 1002 |
), |
|
| 1003 | 1x |
children = grid::gList(cbind(ylab, yaxis)) |
| 1004 |
) |
|
| 1005 |
) |
|
| 1006 |
} |
|
| 1007 | ||
| 1008 |
#' Helper function to create Cox-PH grobs |
|
| 1009 |
#' |
|
| 1010 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 1011 |
#' |
|
| 1012 |
#' Grob of `rtable` output from [h_tbl_coxph_pairwise()] |
|
| 1013 |
#' |
|
| 1014 |
#' @inheritParams h_grob_median_surv |
|
| 1015 |
#' @param ... arguments to pass to [h_tbl_coxph_pairwise()]. |
|
| 1016 |
#' @param x (`proportion`)\cr a value between 0 and 1 specifying x-location. |
|
| 1017 |
#' @param y (`proportion`)\cr a value between 0 and 1 specifying y-location. |
|
| 1018 |
#' @param width (`grid::unit`)\cr width (as a unit) to use when printing the grob. |
|
| 1019 |
#' |
|
| 1020 |
#' @return A `grob` of a table containing statistics `HR`, `XX% CI` (`XX` taken from `control_coxph_pw`), |
|
| 1021 |
#' and `p-value (log-rank)`. |
|
| 1022 |
#' |
|
| 1023 |
#' @examples |
|
| 1024 |
#' \donttest{
|
|
| 1025 |
#' library(dplyr) |
|
| 1026 |
#' library(survival) |
|
| 1027 |
#' library(grid) |
|
| 1028 |
#' |
|
| 1029 |
#' grid::grid.newpage() |
|
| 1030 |
#' grid.rect(gp = grid::gpar(lty = 1, col = "pink", fill = "gray85", lwd = 1)) |
|
| 1031 |
#' data <- tern_ex_adtte %>% |
|
| 1032 |
#' filter(PARAMCD == "OS") %>% |
|
| 1033 |
#' mutate(is_event = CNSR == 0) |
|
| 1034 |
#' tbl_grob <- h_grob_coxph( |
|
| 1035 |
#' df = data, |
|
| 1036 |
#' variables = list(tte = "AVAL", is_event = "is_event", arm = "ARMCD"), |
|
| 1037 |
#' control_coxph_pw = control_coxph(conf_level = 0.9), x = 0.5, y = 0.5 |
|
| 1038 |
#' ) |
|
| 1039 |
#' grid::grid.draw(tbl_grob) |
|
| 1040 |
#' } |
|
| 1041 |
#' |
|
| 1042 |
#' @export |
|
| 1043 |
h_grob_coxph <- function(..., |
|
| 1044 |
x = 0, |
|
| 1045 |
y = 0, |
|
| 1046 |
width = grid::unit(0.4, "npc"), |
|
| 1047 |
ttheme = gridExtra::ttheme_default( |
|
| 1048 |
padding = grid::unit(c(1, .5), "lines"), |
|
| 1049 |
core = list(bg_params = list(fill = c("grey95", "grey90"), alpha = .5))
|
|
| 1050 |
)) {
|
|
| 1051 | 1x |
lifecycle::deprecate_warn( |
| 1052 | 1x |
"0.9.4", |
| 1053 | 1x |
"h_grob_coxph()", |
| 1054 | 1x |
details = "`g_km` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 1055 |
) |
|
| 1056 | 1x |
data <- h_tbl_coxph_pairwise(...) |
| 1057 | ||
| 1058 | 1x |
width <- grid::convertUnit(grid::unit(as.numeric(width), grid::unitType(width)), "in") |
| 1059 | 1x |
height <- width * (nrow(data) + 1) / 12 |
| 1060 | ||
| 1061 | 1x |
w <- paste(" ", c(
|
| 1062 | 1x |
rownames(data)[which.max(nchar(rownames(data)))], |
| 1063 | 1x |
sapply(names(data), function(x) c(x, data[[x]])[which.max(nchar(c(x, data[[x]])))]) |
| 1064 |
)) |
|
| 1065 | 1x |
w_unit <- grid::convertWidth(grid::stringWidth(w), "in", valueOnly = TRUE) |
| 1066 | ||
| 1067 | 1x |
w_txt <- sapply(1:64, function(x) {
|
| 1068 | 64x |
graphics::par(ps = x) |
| 1069 | 64x |
graphics::strwidth(w[4], units = "in") |
| 1070 |
}) |
|
| 1071 | 1x |
f_size_w <- which.max(w_txt[w_txt < as.numeric((w_unit / sum(w_unit)) * width)[4]]) |
| 1072 | ||
| 1073 | 1x |
h_txt <- sapply(1:64, function(x) {
|
| 1074 | 64x |
graphics::par(ps = x) |
| 1075 | 64x |
graphics::strheight(grid::stringHeight("X"), units = "in")
|
| 1076 |
}) |
|
| 1077 | 1x |
f_size_h <- which.max(h_txt[h_txt < as.numeric(grid::unit(as.numeric(height) / 4, grid::unitType(height)))]) |
| 1078 | ||
| 1079 | 1x |
if (ttheme$core$fg_params$fontsize == 12) {
|
| 1080 | 1x |
ttheme$core$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 1081 | 1x |
ttheme$colhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 1082 | 1x |
ttheme$rowhead$fg_params$fontsize <- min(f_size_w, f_size_h) |
| 1083 |
} |
|
| 1084 | ||
| 1085 | 1x |
tryCatch( |
| 1086 | 1x |
expr = {
|
| 1087 | 1x |
gt <- gridExtra::tableGrob( |
| 1088 | 1x |
d = data, |
| 1089 | 1x |
theme = ttheme |
| 1090 | 1x |
) # ERROR 'data' must be of a vector type, was 'NULL' |
| 1091 | 1x |
gt$widths <- ((w_unit / sum(w_unit)) * width) |
| 1092 | 1x |
gt$heights <- rep(grid::unit(as.numeric(height) / 4, grid::unitType(height)), nrow(gt)) |
| 1093 | 1x |
vp <- grid::viewport( |
| 1094 | 1x |
x = grid::unit(x, "npc") + grid::unit(1, "lines"), |
| 1095 | 1x |
y = grid::unit(y, "npc") + grid::unit(1.5, "lines"), |
| 1096 | 1x |
height = height, |
| 1097 | 1x |
width = width, |
| 1098 | 1x |
just = c("left", "bottom")
|
| 1099 |
) |
|
| 1100 | 1x |
grid::gList( |
| 1101 | 1x |
grid::gTree( |
| 1102 | 1x |
vp = vp, |
| 1103 | 1x |
children = grid::gList(gt) |
| 1104 |
) |
|
| 1105 |
) |
|
| 1106 |
}, |
|
| 1107 | 1x |
error = function(w) {
|
| 1108 | ! |
message(paste( |
| 1109 | ! |
"Warning: Cox table will not be displayed as there is", |
| 1110 | ! |
"not any level to be compared in the arm variable." |
| 1111 |
)) |
|
| 1112 | ! |
return( |
| 1113 | ! |
grid::gList( |
| 1114 | ! |
grid::gTree( |
| 1115 | ! |
vp = NULL, |
| 1116 | ! |
children = NULL |
| 1117 |
) |
|
| 1118 |
) |
|
| 1119 |
) |
|
| 1120 |
} |
|
| 1121 |
) |
|
| 1122 |
} |
| 1 |
#' Cox proportional hazards regression |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Fits a Cox regression model and estimates hazard ratio to describe the effect size in a survival analysis. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams argument_convention |
|
| 8 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 9 |
#' |
|
| 10 |
#' Options are: ``r shQuote(get_stats("summarize_coxreg"), type = "sh")``
|
|
| 11 |
#' |
|
| 12 |
#' @details Cox models are the most commonly used methods to estimate the magnitude of |
|
| 13 |
#' the effect in survival analysis. It assumes proportional hazards: the ratio |
|
| 14 |
#' of the hazards between groups (e.g., two arms) is constant over time. |
|
| 15 |
#' This ratio is referred to as the "hazard ratio" (HR) and is one of the |
|
| 16 |
#' most commonly reported metrics to describe the effect size in survival |
|
| 17 |
#' analysis (NEST Team, 2020). |
|
| 18 |
#' |
|
| 19 |
#' @seealso [fit_coxreg] for relevant fitting functions, [h_cox_regression] for relevant |
|
| 20 |
#' helper functions, and [tidy_coxreg] for custom tidy methods. |
|
| 21 |
#' |
|
| 22 |
#' @examples |
|
| 23 |
#' library(survival) |
|
| 24 |
#' |
|
| 25 |
#' # Testing dataset [survival::bladder]. |
|
| 26 |
#' set.seed(1, kind = "Mersenne-Twister") |
|
| 27 |
#' dta_bladder <- with( |
|
| 28 |
#' data = bladder[bladder$enum < 5, ], |
|
| 29 |
#' tibble::tibble( |
|
| 30 |
#' TIME = stop, |
|
| 31 |
#' STATUS = event, |
|
| 32 |
#' ARM = as.factor(rx), |
|
| 33 |
#' COVAR1 = as.factor(enum) %>% formatters::with_label("A Covariate Label"),
|
|
| 34 |
#' COVAR2 = factor( |
|
| 35 |
#' sample(as.factor(enum)), |
|
| 36 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
| 37 |
#' ) %>% formatters::with_label("Sex (F/M)")
|
|
| 38 |
#' ) |
|
| 39 |
#' ) |
|
| 40 |
#' dta_bladder$AGE <- sample(20:60, size = nrow(dta_bladder), replace = TRUE) |
|
| 41 |
#' dta_bladder$STUDYID <- factor("X")
|
|
| 42 |
#' |
|
| 43 |
#' u1_variables <- list( |
|
| 44 |
#' time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
|
|
| 45 |
#' ) |
|
| 46 |
#' |
|
| 47 |
#' u2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
|
|
| 48 |
#' |
|
| 49 |
#' m1_variables <- list( |
|
| 50 |
#' time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
|
|
| 51 |
#' ) |
|
| 52 |
#' |
|
| 53 |
#' m2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
|
|
| 54 |
#' |
|
| 55 |
#' @name cox_regression |
|
| 56 |
#' @order 1 |
|
| 57 |
NULL |
|
| 58 | ||
| 59 |
#' @describeIn cox_regression Statistics function that transforms results tabulated |
|
| 60 |
#' from [fit_coxreg_univar()] or [fit_coxreg_multivar()] into a list. |
|
| 61 |
#' |
|
| 62 |
#' @param model_df (`data.frame`)\cr contains the resulting model fit from a [fit_coxreg] |
|
| 63 |
#' function with tidying applied via [broom::tidy()]. |
|
| 64 |
#' @param .stats (`character`)\cr the names of statistics to be reported among: |
|
| 65 |
#' * `n`: number of observations (univariate only) |
|
| 66 |
#' * `hr`: hazard ratio |
|
| 67 |
#' * `ci`: confidence interval |
|
| 68 |
#' * `pval`: p-value of the treatment effect |
|
| 69 |
#' * `pval_inter`: p-value of the interaction effect between the treatment and the covariate (univariate only) |
|
| 70 |
#' @param .which_vars (`character`)\cr which rows should statistics be returned for from the given model. |
|
| 71 |
#' Defaults to `"all"`. Other options include `"var_main"` for main effects, `"inter"` for interaction effects, |
|
| 72 |
#' and `"multi_lvl"` for multivariate model covariate level rows. When `.which_vars` is `"all"`, specific |
|
| 73 |
#' variables can be selected by specifying `.var_nms`. |
|
| 74 |
#' @param .var_nms (`character`)\cr the `term` value of rows in `df` for which `.stats` should be returned. Typically |
|
| 75 |
#' this is the name of a variable. If using variable labels, `var` should be a vector of both the desired |
|
| 76 |
#' variable name and the variable label in that order to see all `.stats` related to that variable. When `.which_vars` |
|
| 77 |
#' is `"var_main"`, `.var_nms` should be only the variable name. |
|
| 78 |
#' |
|
| 79 |
#' @return |
|
| 80 |
#' * `s_coxreg()` returns the selected statistic for from the Cox regression model for the selected variable(s). |
|
| 81 |
#' |
|
| 82 |
#' @examples |
|
| 83 |
#' # s_coxreg |
|
| 84 |
#' |
|
| 85 |
#' # Univariate |
|
| 86 |
#' univar_model <- fit_coxreg_univar(variables = u1_variables, data = dta_bladder) |
|
| 87 |
#' df1 <- broom::tidy(univar_model) |
|
| 88 |
#' |
|
| 89 |
#' s_coxreg(model_df = df1, .stats = "hr") |
|
| 90 |
#' |
|
| 91 |
#' # Univariate with interactions |
|
| 92 |
#' univar_model_inter <- fit_coxreg_univar( |
|
| 93 |
#' variables = u1_variables, control = control_coxreg(interaction = TRUE), data = dta_bladder |
|
| 94 |
#' ) |
|
| 95 |
#' df1_inter <- broom::tidy(univar_model_inter) |
|
| 96 |
#' |
|
| 97 |
#' s_coxreg(model_df = df1_inter, .stats = "hr", .which_vars = "inter", .var_nms = "COVAR1") |
|
| 98 |
#' |
|
| 99 |
#' # Univariate without treatment arm - only "COVAR2" covariate effects |
|
| 100 |
#' univar_covs_model <- fit_coxreg_univar(variables = u2_variables, data = dta_bladder) |
|
| 101 |
#' df1_covs <- broom::tidy(univar_covs_model) |
|
| 102 |
#' |
|
| 103 |
#' s_coxreg(model_df = df1_covs, .stats = "hr", .var_nms = c("COVAR2", "Sex (F/M)"))
|
|
| 104 |
#' |
|
| 105 |
#' # Multivariate. |
|
| 106 |
#' multivar_model <- fit_coxreg_multivar(variables = m1_variables, data = dta_bladder) |
|
| 107 |
#' df2 <- broom::tidy(multivar_model) |
|
| 108 |
#' |
|
| 109 |
#' s_coxreg(model_df = df2, .stats = "pval", .which_vars = "var_main", .var_nms = "COVAR1") |
|
| 110 |
#' s_coxreg( |
|
| 111 |
#' model_df = df2, .stats = "pval", .which_vars = "multi_lvl", |
|
| 112 |
#' .var_nms = c("COVAR1", "A Covariate Label")
|
|
| 113 |
#' ) |
|
| 114 |
#' |
|
| 115 |
#' # Multivariate without treatment arm - only "COVAR1" main effect |
|
| 116 |
#' multivar_covs_model <- fit_coxreg_multivar(variables = m2_variables, data = dta_bladder) |
|
| 117 |
#' df2_covs <- broom::tidy(multivar_covs_model) |
|
| 118 |
#' |
|
| 119 |
#' s_coxreg(model_df = df2_covs, .stats = "hr") |
|
| 120 |
#' |
|
| 121 |
#' @export |
|
| 122 |
s_coxreg <- function(model_df, .stats, .which_vars = "all", .var_nms = NULL) {
|
|
| 123 | 291x |
assert_df_with_variables(model_df, list(term = "term", stat = .stats)) |
| 124 | 291x |
checkmate::assert_multi_class(model_df$term, classes = c("factor", "character"))
|
| 125 | 291x |
model_df$term <- as.character(model_df$term) |
| 126 | 291x |
.var_nms <- .var_nms[!is.na(.var_nms)] |
| 127 | ||
| 128 | 289x |
if (length(.var_nms) > 0) model_df <- model_df[model_df$term %in% .var_nms, ] |
| 129 | 69x |
if (.which_vars == "multi_lvl") model_df$term <- tail(.var_nms, 1) |
| 130 | ||
| 131 |
# We need a list with names corresponding to the stats to display of equal length to the list of stats. |
|
| 132 | 291x |
y <- split(model_df, f = model_df$term, drop = FALSE) |
| 133 | 291x |
y <- stats::setNames(y, nm = rep(.stats, length(y))) |
| 134 | ||
| 135 | 291x |
if (.which_vars == "var_main") {
|
| 136 | 128x |
y <- lapply(y, function(x) x[1, ]) # only main effect |
| 137 | 163x |
} else if (.which_vars %in% c("inter", "multi_lvl")) {
|
| 138 | 120x |
y <- lapply(y, function(x) if (nrow(y[[1]]) > 1) x[-1, ] else x) # exclude main effect |
| 139 |
} |
|
| 140 | ||
| 141 | 291x |
lapply( |
| 142 | 291x |
X = y, |
| 143 | 291x |
FUN = function(x) {
|
| 144 | 295x |
z <- as.list(x[[.stats]]) |
| 145 | 295x |
stats::setNames(z, nm = x$term_label) |
| 146 |
} |
|
| 147 |
) |
|
| 148 |
} |
|
| 149 | ||
| 150 |
#' @describeIn cox_regression Analysis function which is used as `afun` in [rtables::analyze()] |
|
| 151 |
#' and `cfun` in [rtables::summarize_row_groups()] within `summarize_coxreg()`. |
|
| 152 |
#' |
|
| 153 |
#' @param eff (`flag`)\cr whether treatment effect should be calculated. Defaults to `FALSE`. |
|
| 154 |
#' @param var_main (`flag`)\cr whether main effects should be calculated. Defaults to `FALSE`. |
|
| 155 |
#' @param na_str (`string`)\cr custom string to replace all `NA` values with. Defaults to `""`. |
|
| 156 |
#' @param cache_env (`environment`)\cr an environment object used to cache the regression model in order to |
|
| 157 |
#' avoid repeatedly fitting the same model for every row in the table. Defaults to `NULL` (no caching). |
|
| 158 |
#' @param varlabels (`list`)\cr a named list corresponds to the names of variables found in data, passed |
|
| 159 |
#' as a named list and corresponding to time, event, arm, strata, and covariates terms. If arm is missing |
|
| 160 |
#' from variables, then only Cox model(s) including the covariates will be fitted and the corresponding |
|
| 161 |
#' effect estimates will be tabulated later. |
|
| 162 |
#' |
|
| 163 |
#' @return |
|
| 164 |
#' * `a_coxreg()` returns formatted [rtables::CellValue()]. |
|
| 165 |
#' |
|
| 166 |
#' @examples |
|
| 167 |
#' a_coxreg( |
|
| 168 |
#' df = dta_bladder, |
|
| 169 |
#' labelstr = "Label 1", |
|
| 170 |
#' variables = u1_variables, |
|
| 171 |
#' .spl_context = list(value = "COVAR1"), |
|
| 172 |
#' .stats = "n", |
|
| 173 |
#' .formats = "xx" |
|
| 174 |
#' ) |
|
| 175 |
#' |
|
| 176 |
#' a_coxreg( |
|
| 177 |
#' df = dta_bladder, |
|
| 178 |
#' labelstr = "", |
|
| 179 |
#' variables = u1_variables, |
|
| 180 |
#' .spl_context = list(value = "COVAR2"), |
|
| 181 |
#' .stats = "pval", |
|
| 182 |
#' .formats = "xx.xxxx" |
|
| 183 |
#' ) |
|
| 184 |
#' |
|
| 185 |
#' @export |
|
| 186 |
a_coxreg <- function(df, |
|
| 187 |
labelstr, |
|
| 188 |
eff = FALSE, |
|
| 189 |
var_main = FALSE, |
|
| 190 |
multivar = FALSE, |
|
| 191 |
variables, |
|
| 192 |
at = list(), |
|
| 193 |
control = control_coxreg(), |
|
| 194 |
.spl_context, |
|
| 195 |
.stats, |
|
| 196 |
.formats, |
|
| 197 |
.indent_mods = NULL, |
|
| 198 |
na_str = "", |
|
| 199 |
cache_env = NULL) {
|
|
| 200 | 288x |
cov_no_arm <- !multivar && !"arm" %in% names(variables) && control$interaction # special case: univar no arm |
| 201 | 288x |
cov <- tail(.spl_context$value, 1) # current variable/covariate |
| 202 | 288x |
var_lbl <- formatters::var_labels(df)[cov] # check for df labels |
| 203 | 288x |
if (length(labelstr) > 1) {
|
| 204 | 8x |
labelstr <- if (cov %in% names(labelstr)) labelstr[[cov]] else var_lbl # use df labels if none |
| 205 | 280x |
} else if (!is.na(var_lbl) && labelstr == cov && cov %in% variables$covariates) {
|
| 206 | 67x |
labelstr <- var_lbl |
| 207 |
} |
|
| 208 | 288x |
if (eff || multivar || cov_no_arm) {
|
| 209 | 143x |
control$interaction <- FALSE |
| 210 |
} else {
|
|
| 211 | 145x |
variables$covariates <- cov |
| 212 | 50x |
if (var_main) control$interaction <- TRUE |
| 213 |
} |
|
| 214 | ||
| 215 | 288x |
if (is.null(cache_env[[cov]])) {
|
| 216 | 47x |
if (!multivar) {
|
| 217 | 32x |
model <- fit_coxreg_univar(variables = variables, data = df, at = at, control = control) %>% broom::tidy() |
| 218 |
} else {
|
|
| 219 | 15x |
model <- fit_coxreg_multivar(variables = variables, data = df, control = control) %>% broom::tidy() |
| 220 |
} |
|
| 221 | 47x |
cache_env[[cov]] <- model |
| 222 |
} else {
|
|
| 223 | 241x |
model <- cache_env[[cov]] |
| 224 |
} |
|
| 225 | 148x |
if (!multivar && !var_main) model[, "pval_inter"] <- NA_real_ |
| 226 | ||
| 227 | 288x |
if (cov_no_arm || (!cov_no_arm && !"arm" %in% names(variables) && is.numeric(df[[cov]]))) {
|
| 228 | 15x |
multivar <- TRUE |
| 229 | 3x |
if (!cov_no_arm) var_main <- TRUE |
| 230 |
} |
|
| 231 | ||
| 232 | 288x |
vars_coxreg <- list(which_vars = "all", var_nms = NULL) |
| 233 | 288x |
if (eff) {
|
| 234 | 65x |
if (multivar && !var_main) { # multivar treatment level
|
| 235 | 12x |
var_lbl_arm <- formatters::var_labels(df)[[variables$arm]] |
| 236 | 12x |
vars_coxreg[c("var_nms", "which_vars")] <- list(c(variables$arm, var_lbl_arm), "multi_lvl")
|
| 237 |
} else { # treatment effect
|
|
| 238 | 53x |
vars_coxreg["var_nms"] <- variables$arm |
| 239 | 12x |
if (var_main) vars_coxreg["which_vars"] <- "var_main" |
| 240 |
} |
|
| 241 |
} else {
|
|
| 242 | 223x |
if (!multivar || (multivar && var_main && !is.numeric(df[[cov]]))) { # covariate effect/level
|
| 243 | 166x |
vars_coxreg[c("var_nms", "which_vars")] <- list(cov, "var_main")
|
| 244 | 57x |
} else if (multivar) { # multivar covariate level
|
| 245 | 57x |
vars_coxreg[c("var_nms", "which_vars")] <- list(c(cov, var_lbl), "multi_lvl")
|
| 246 | 12x |
if (var_main) model[cov, .stats] <- NA_real_ |
| 247 |
} |
|
| 248 | 50x |
if (!multivar && !var_main && control$interaction) vars_coxreg["which_vars"] <- "inter" # interaction effect |
| 249 |
} |
|
| 250 | 288x |
var_vals <- s_coxreg(model, .stats, .which_vars = vars_coxreg$which_vars, .var_nms = vars_coxreg$var_nms)[[1]] |
| 251 | 288x |
var_names <- if (all(grepl("\\(reference = ", names(var_vals))) && labelstr != tail(.spl_context$value, 1)) {
|
| 252 | 27x |
paste(c(labelstr, tail(strsplit(names(var_vals), " ")[[1]], 3)), collapse = " ") # "reference" main effect labels |
| 253 | 288x |
} else if ((!multivar && !eff && !(!var_main && control$interaction) && nchar(labelstr) > 0) || |
| 254 | 288x |
(multivar && var_main && is.numeric(df[[cov]]))) { # nolint
|
| 255 | 71x |
labelstr # other main effect labels |
| 256 | 288x |
} else if (multivar && !eff && !var_main && is.numeric(df[[cov]])) {
|
| 257 | 12x |
"All" # multivar numeric covariate |
| 258 |
} else {
|
|
| 259 | 178x |
names(var_vals) |
| 260 |
} |
|
| 261 | 288x |
in_rows( |
| 262 | 288x |
.list = var_vals, .names = var_names, .labels = var_names, .indent_mods = .indent_mods, |
| 263 | 288x |
.formats = stats::setNames(rep(.formats, length(var_names)), var_names), |
| 264 | 288x |
.format_na_strs = stats::setNames(rep(na_str, length(var_names)), var_names) |
| 265 |
) |
|
| 266 |
} |
|
| 267 | ||
| 268 |
#' @describeIn cox_regression Layout-creating function which creates a Cox regression summary table |
|
| 269 |
#' layout. This function is a wrapper for several `rtables` layouting functions. This function |
|
| 270 |
#' is a wrapper for [rtables::analyze_colvars()] and [rtables::summarize_row_groups()]. |
|
| 271 |
#' |
|
| 272 |
#' @inheritParams fit_coxreg_univar |
|
| 273 |
#' @param multivar (`flag`)\cr whether multivariate Cox regression should run (defaults to `FALSE`), otherwise |
|
| 274 |
#' univariate Cox regression will run. |
|
| 275 |
#' @param common_var (`string`)\cr the name of a factor variable in the dataset which takes the same value |
|
| 276 |
#' for all rows. This should be created during pre-processing if no such variable currently exists. |
|
| 277 |
#' @param .section_div (`string` or `NA`)\cr string which should be repeated as a section divider between sections. |
|
| 278 |
#' Defaults to `NA` for no section divider. If a vector of two strings are given, the first will be used between |
|
| 279 |
#' treatment and covariate sections and the second between different covariates. |
|
| 280 |
#' |
|
| 281 |
#' @return |
|
| 282 |
#' * `summarize_coxreg()` returns a layout object suitable for passing to further layouting functions, |
|
| 283 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add a Cox regression table |
|
| 284 |
#' containing the chosen statistics to the table layout. |
|
| 285 |
#' |
|
| 286 |
#' @seealso [fit_coxreg_univar()] and [fit_coxreg_multivar()] which also take the `variables`, `data`, |
|
| 287 |
#' `at` (univariate only), and `control` arguments but return unformatted univariate and multivariate |
|
| 288 |
#' Cox regression models, respectively. |
|
| 289 |
#' |
|
| 290 |
#' @examples |
|
| 291 |
#' # summarize_coxreg |
|
| 292 |
#' |
|
| 293 |
#' result_univar <- basic_table() %>% |
|
| 294 |
#' summarize_coxreg(variables = u1_variables) %>% |
|
| 295 |
#' build_table(dta_bladder) |
|
| 296 |
#' result_univar |
|
| 297 |
#' |
|
| 298 |
#' result_univar_covs <- basic_table() %>% |
|
| 299 |
#' summarize_coxreg( |
|
| 300 |
#' variables = u2_variables, |
|
| 301 |
#' ) %>% |
|
| 302 |
#' build_table(dta_bladder) |
|
| 303 |
#' result_univar_covs |
|
| 304 |
#' |
|
| 305 |
#' result_multivar <- basic_table() %>% |
|
| 306 |
#' summarize_coxreg( |
|
| 307 |
#' variables = m1_variables, |
|
| 308 |
#' multivar = TRUE, |
|
| 309 |
#' ) %>% |
|
| 310 |
#' build_table(dta_bladder) |
|
| 311 |
#' result_multivar |
|
| 312 |
#' |
|
| 313 |
#' result_multivar_covs <- basic_table() %>% |
|
| 314 |
#' summarize_coxreg( |
|
| 315 |
#' variables = m2_variables, |
|
| 316 |
#' multivar = TRUE, |
|
| 317 |
#' varlabels = c("Covariate 1", "Covariate 2") # custom labels
|
|
| 318 |
#' ) %>% |
|
| 319 |
#' build_table(dta_bladder) |
|
| 320 |
#' result_multivar_covs |
|
| 321 |
#' |
|
| 322 |
#' @export |
|
| 323 |
#' @order 2 |
|
| 324 |
summarize_coxreg <- function(lyt, |
|
| 325 |
variables, |
|
| 326 |
control = control_coxreg(), |
|
| 327 |
at = list(), |
|
| 328 |
multivar = FALSE, |
|
| 329 |
common_var = "STUDYID", |
|
| 330 |
.stats = c("n", "hr", "ci", "pval", "pval_inter"),
|
|
| 331 |
.formats = c( |
|
| 332 |
n = "xx", hr = "xx.xx", ci = "(xx.xx, xx.xx)", |
|
| 333 |
pval = "x.xxxx | (<0.0001)", pval_inter = "x.xxxx | (<0.0001)" |
|
| 334 |
), |
|
| 335 |
varlabels = NULL, |
|
| 336 |
.indent_mods = NULL, |
|
| 337 |
na_str = "", |
|
| 338 |
.section_div = NA_character_) {
|
|
| 339 | 16x |
if (multivar && control$interaction) {
|
| 340 | 1x |
warning(paste( |
| 341 | 1x |
"Interactions are not available for multivariate cox regression using summarize_coxreg.", |
| 342 | 1x |
"The model will be calculated without interaction effects." |
| 343 |
)) |
|
| 344 |
} |
|
| 345 | 16x |
if (control$interaction && !"arm" %in% names(variables)) {
|
| 346 | 1x |
stop("To include interactions please specify 'arm' in variables.")
|
| 347 |
} |
|
| 348 | ||
| 349 | 15x |
.stats <- if (!"arm" %in% names(variables) || multivar) { # only valid statistics
|
| 350 | 6x |
intersect(c("hr", "ci", "pval"), .stats)
|
| 351 | 15x |
} else if (control$interaction) {
|
| 352 | 5x |
intersect(c("n", "hr", "ci", "pval", "pval_inter"), .stats)
|
| 353 |
} else {
|
|
| 354 | 4x |
intersect(c("n", "hr", "ci", "pval"), .stats)
|
| 355 |
} |
|
| 356 | 15x |
stat_labels <- c( |
| 357 | 15x |
n = "n", hr = "Hazard Ratio", ci = paste0(control$conf_level * 100, "% CI"), |
| 358 | 15x |
pval = "p-value", pval_inter = "Interaction p-value" |
| 359 |
) |
|
| 360 | 15x |
stat_labels <- stat_labels[names(stat_labels) %in% .stats] |
| 361 | 15x |
.formats <- .formats[names(.formats) %in% .stats] |
| 362 | 15x |
env <- new.env() # create caching environment |
| 363 | ||
| 364 | 15x |
lyt <- lyt %>% |
| 365 | 15x |
split_cols_by_multivar( |
| 366 | 15x |
vars = rep(common_var, length(.stats)), |
| 367 | 15x |
varlabels = stat_labels, |
| 368 | 15x |
extra_args = list( |
| 369 | 15x |
.stats = .stats, .formats = .formats, .indent_mods = .indent_mods, na_str = rep(na_str, length(.stats)), |
| 370 | 15x |
cache_env = replicate(length(.stats), list(env)) |
| 371 |
) |
|
| 372 |
) |
|
| 373 | ||
| 374 | 15x |
if ("arm" %in% names(variables)) { # treatment effect
|
| 375 | 13x |
lyt <- lyt %>% |
| 376 | 13x |
split_rows_by( |
| 377 | 13x |
common_var, |
| 378 | 13x |
split_label = "Treatment:", |
| 379 | 13x |
label_pos = "visible", |
| 380 | 13x |
child_labels = "hidden", |
| 381 | 13x |
section_div = head(.section_div, 1) |
| 382 |
) |
|
| 383 | 13x |
if (!multivar) {
|
| 384 | 9x |
lyt <- lyt %>% |
| 385 | 9x |
analyze_colvars( |
| 386 | 9x |
afun = a_coxreg, |
| 387 | 9x |
na_str = na_str, |
| 388 | 9x |
extra_args = list( |
| 389 | 9x |
variables = variables, control = control, multivar = multivar, eff = TRUE, var_main = multivar, |
| 390 | 9x |
labelstr = "" |
| 391 |
) |
|
| 392 |
) |
|
| 393 |
} else { # treatment level effects
|
|
| 394 | 4x |
lyt <- lyt %>% |
| 395 | 4x |
summarize_row_groups( |
| 396 | 4x |
cfun = a_coxreg, |
| 397 | 4x |
na_str = na_str, |
| 398 | 4x |
extra_args = list( |
| 399 | 4x |
variables = variables, control = control, multivar = multivar, eff = TRUE, var_main = multivar |
| 400 |
) |
|
| 401 |
) %>% |
|
| 402 | 4x |
analyze_colvars( |
| 403 | 4x |
afun = a_coxreg, |
| 404 | 4x |
na_str = na_str, |
| 405 | 4x |
extra_args = list(eff = TRUE, control = control, variables = variables, multivar = multivar, labelstr = "") |
| 406 |
) |
|
| 407 |
} |
|
| 408 |
} |
|
| 409 | ||
| 410 | 15x |
if ("covariates" %in% names(variables)) { # covariate main effects
|
| 411 | 15x |
lyt <- lyt %>% |
| 412 | 15x |
split_rows_by_multivar( |
| 413 | 15x |
vars = variables$covariates, |
| 414 | 15x |
varlabels = varlabels, |
| 415 | 15x |
split_label = "Covariate:", |
| 416 | 15x |
nested = FALSE, |
| 417 | 15x |
child_labels = if (multivar || control$interaction || !"arm" %in% names(variables)) "default" else "hidden", |
| 418 | 15x |
section_div = tail(.section_div, 1) |
| 419 |
) |
|
| 420 | 15x |
if (multivar || control$interaction || !"arm" %in% names(variables)) {
|
| 421 | 11x |
lyt <- lyt %>% |
| 422 | 11x |
summarize_row_groups( |
| 423 | 11x |
cfun = a_coxreg, |
| 424 | 11x |
na_str = na_str, |
| 425 | 11x |
extra_args = list( |
| 426 | 11x |
variables = variables, at = at, control = control, multivar = multivar, |
| 427 | 11x |
var_main = if (multivar) multivar else control$interaction |
| 428 |
) |
|
| 429 |
) |
|
| 430 |
} else {
|
|
| 431 | 1x |
if (!is.null(varlabels)) names(varlabels) <- variables$covariates |
| 432 | 4x |
lyt <- lyt %>% |
| 433 | 4x |
analyze_colvars( |
| 434 | 4x |
afun = a_coxreg, |
| 435 | 4x |
na_str = na_str, |
| 436 | 4x |
extra_args = list( |
| 437 | 4x |
variables = variables, at = at, control = control, multivar = multivar, |
| 438 | 4x |
var_main = if (multivar) multivar else control$interaction, |
| 439 | 4x |
labelstr = if (is.null(varlabels)) "" else varlabels |
| 440 |
) |
|
| 441 |
) |
|
| 442 |
} |
|
| 443 | ||
| 444 | 2x |
if (!"arm" %in% names(variables)) control$interaction <- TRUE # special case: univar no arm |
| 445 | 15x |
if (multivar || control$interaction) { # covariate level effects
|
| 446 | 11x |
lyt <- lyt %>% |
| 447 | 11x |
analyze_colvars( |
| 448 | 11x |
afun = a_coxreg, |
| 449 | 11x |
na_str = na_str, |
| 450 | 11x |
extra_args = list(variables = variables, at = at, control = control, multivar = multivar, labelstr = ""), |
| 451 | 11x |
indent_mod = if (!"arm" %in% names(variables) || multivar) 0L else -1L |
| 452 |
) |
|
| 453 |
} |
|
| 454 |
} |
|
| 455 | ||
| 456 | 15x |
lyt |
| 457 |
} |
| 1 |
#' Count occurrences by grade |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [count_occurrences_by_grade()] creates a layout element to calculate occurrence counts by grade. |
|
| 6 |
#' |
|
| 7 |
#' This function analyzes primary analysis variable `var` which indicates toxicity grades. The `id` variable |
|
| 8 |
#' is used to indicate unique subject identifiers (defaults to `USUBJID`). The user can also supply a list of |
|
| 9 |
#' custom groups of grades to analyze via the `grade_groups` parameter. The `remove_single` argument will |
|
| 10 |
#' remove single grades from the analysis so that *only* grade groups are analyzed. |
|
| 11 |
#' |
|
| 12 |
#' If there are multiple grades recorded for one patient only the highest grade level is counted. |
|
| 13 |
#' |
|
| 14 |
#' The summarize function [summarize_occurrences_by_grade()] performs the same function as |
|
| 15 |
#' [count_occurrences_by_grade()] except it creates content rows, not data rows, to summarize the current table |
|
| 16 |
#' row/column context and operates on the level of the latest row split or the root of the table if no row splits have |
|
| 17 |
#' occurred. |
|
| 18 |
#' |
|
| 19 |
#' @inheritParams count_occurrences |
|
| 20 |
#' @inheritParams argument_convention |
|
| 21 |
#' @param grade_groups (named `list` of `character`)\cr list containing groupings of grades. |
|
| 22 |
#' @param remove_single (`flag`)\cr `TRUE` to not include the elements of one-element grade groups |
|
| 23 |
#' in the the output list; in this case only the grade groups names will be included in the output. If |
|
| 24 |
#' `only_grade_groups` is set to `TRUE` this argument is ignored. |
|
| 25 |
#' @param only_grade_groups (`flag`)\cr whether only the specified grade groups should be |
|
| 26 |
#' included, with individual grade rows removed (`TRUE`), or all grades and grade groups |
|
| 27 |
#' should be displayed (`FALSE`). |
|
| 28 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 29 |
#' |
|
| 30 |
#' Options are: ``r shQuote(get_stats("count_occurrences_by_grade"), type = "sh")``
|
|
| 31 |
#' |
|
| 32 |
#' @seealso Relevant helper function [h_append_grade_groups()]. |
|
| 33 |
#' |
|
| 34 |
#' @name count_occurrences_by_grade |
|
| 35 |
#' @order 1 |
|
| 36 |
NULL |
|
| 37 | ||
| 38 |
#' Helper function for `s_count_occurrences_by_grade()` |
|
| 39 |
#' |
|
| 40 |
#' @description `r lifecycle::badge("stable")`
|
|
| 41 |
#' |
|
| 42 |
#' Helper function for [s_count_occurrences_by_grade()] to insert grade groupings into list with |
|
| 43 |
#' individual grade frequencies. The order of the final result follows the order of `grade_groups`. |
|
| 44 |
#' The elements under any-grade group (if any), i.e. the grade group equal to `refs` will be moved to |
|
| 45 |
#' the end. Grade groups names must be unique. |
|
| 46 |
#' |
|
| 47 |
#' @inheritParams count_occurrences_by_grade |
|
| 48 |
#' @param refs (named `list` of `numeric`)\cr named list where each name corresponds to a reference grade level |
|
| 49 |
#' and each entry represents a count. |
|
| 50 |
#' |
|
| 51 |
#' @return Formatted list of grade groupings. |
|
| 52 |
#' |
|
| 53 |
#' @examples |
|
| 54 |
#' h_append_grade_groups( |
|
| 55 |
#' list( |
|
| 56 |
#' "Any Grade" = as.character(1:5), |
|
| 57 |
#' "Grade 1-2" = c("1", "2"),
|
|
| 58 |
#' "Grade 3-4" = c("3", "4")
|
|
| 59 |
#' ), |
|
| 60 |
#' list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
|
|
| 61 |
#' ) |
|
| 62 |
#' |
|
| 63 |
#' h_append_grade_groups( |
|
| 64 |
#' list( |
|
| 65 |
#' "Any Grade" = as.character(5:1), |
|
| 66 |
#' "Grade A" = "5", |
|
| 67 |
#' "Grade B" = c("4", "3")
|
|
| 68 |
#' ), |
|
| 69 |
#' list("1" = 10, "2" = 20, "3" = 30, "4" = 40, "5" = 50)
|
|
| 70 |
#' ) |
|
| 71 |
#' |
|
| 72 |
#' h_append_grade_groups( |
|
| 73 |
#' list( |
|
| 74 |
#' "Any Grade" = as.character(1:5), |
|
| 75 |
#' "Grade 1-2" = c("1", "2"),
|
|
| 76 |
#' "Grade 3-4" = c("3", "4")
|
|
| 77 |
#' ), |
|
| 78 |
#' list("1" = 10, "2" = 5, "3" = 0)
|
|
| 79 |
#' ) |
|
| 80 |
#' |
|
| 81 |
#' @export |
|
| 82 |
h_append_grade_groups <- function(grade_groups, refs, remove_single = TRUE, only_grade_groups = FALSE) {
|
|
| 83 | 32x |
checkmate::assert_list(grade_groups) |
| 84 | 32x |
checkmate::assert_list(refs) |
| 85 | 32x |
refs_orig <- refs |
| 86 | 32x |
elements <- unique(unlist(grade_groups)) |
| 87 | ||
| 88 |
### compute sums in groups |
|
| 89 | 32x |
grp_sum <- lapply(grade_groups, function(i) do.call(sum, refs[i])) |
| 90 | 32x |
if (!checkmate::test_subset(elements, names(refs))) {
|
| 91 | 2x |
padding_el <- setdiff(elements, names(refs)) |
| 92 | 2x |
refs[padding_el] <- 0 |
| 93 |
} |
|
| 94 | 32x |
result <- c(grp_sum, refs) |
| 95 | ||
| 96 |
### order result while keeping grade_groups's ordering |
|
| 97 | 32x |
ordr <- grade_groups |
| 98 | ||
| 99 |
# elements of any-grade group (if any) will be moved to the end |
|
| 100 | 32x |
is_any <- sapply(grade_groups, setequal, y = names(refs)) |
| 101 | 32x |
ordr[is_any] <- list(character(0)) # hide elements under any-grade group |
| 102 | ||
| 103 |
# groups-elements combined sequence |
|
| 104 | 32x |
ordr <- c(lapply(names(ordr), function(g) c(g, ordr[[g]])), recursive = TRUE, use.names = FALSE) |
| 105 | 32x |
ordr <- ordr[!duplicated(ordr)] |
| 106 | ||
| 107 |
# append remaining elements (if any) |
|
| 108 | 32x |
ordr <- union(ordr, unlist(grade_groups[is_any])) # from any-grade group |
| 109 | 32x |
ordr <- union(ordr, names(refs)) # from refs |
| 110 | ||
| 111 |
# remove elements of single-element groups, if any |
|
| 112 | 32x |
if (only_grade_groups) {
|
| 113 | 3x |
ordr <- intersect(ordr, names(grade_groups)) |
| 114 | 29x |
} else if (remove_single) {
|
| 115 | 29x |
is_single <- sapply(grade_groups, length) == 1L |
| 116 | 29x |
ordr <- setdiff(ordr, unlist(grade_groups[is_single])) |
| 117 |
} |
|
| 118 | ||
| 119 |
# apply the order |
|
| 120 | 32x |
result <- result[ordr] |
| 121 | ||
| 122 |
# remove groups without any elements in the original refs |
|
| 123 |
# note: it's OK if groups have 0 value |
|
| 124 | 32x |
keep_grp <- vapply(grade_groups, function(x, rf) {
|
| 125 | 64x |
any(x %in% rf) |
| 126 | 32x |
}, rf = names(refs_orig), logical(1)) |
| 127 | ||
| 128 | 32x |
keep_el <- names(result) %in% names(refs_orig) | names(result) %in% names(keep_grp)[keep_grp] |
| 129 | 32x |
result <- result[keep_el] |
| 130 | ||
| 131 | 32x |
result |
| 132 |
} |
|
| 133 | ||
| 134 |
#' @describeIn count_occurrences_by_grade Statistics function which counts the |
|
| 135 |
#' number of patients by highest grade. |
|
| 136 |
#' |
|
| 137 |
#' @return |
|
| 138 |
#' * `s_count_occurrences_by_grade()` returns a list of counts and fractions with one element per grade level or |
|
| 139 |
#' grade level grouping. |
|
| 140 |
#' |
|
| 141 |
#' @examples |
|
| 142 |
#' s_count_occurrences_by_grade( |
|
| 143 |
#' df, |
|
| 144 |
#' .N_col = 10L, |
|
| 145 |
#' .var = "AETOXGR", |
|
| 146 |
#' id = "USUBJID", |
|
| 147 |
#' grade_groups = list("ANY" = levels(df$AETOXGR))
|
|
| 148 |
#' ) |
|
| 149 |
#' |
|
| 150 |
#' @export |
|
| 151 |
s_count_occurrences_by_grade <- function(df, |
|
| 152 |
labelstr = "", |
|
| 153 |
.var, |
|
| 154 |
.N_row, # nolint |
|
| 155 |
.N_col, # nolint |
|
| 156 |
..., |
|
| 157 |
id = "USUBJID", |
|
| 158 |
grade_groups = list(), |
|
| 159 |
remove_single = TRUE, |
|
| 160 |
only_grade_groups = FALSE, |
|
| 161 |
denom = c("N_col", "n", "N_row")) {
|
|
| 162 | 75x |
assert_valid_factor(df[[.var]]) |
| 163 | 75x |
assert_df_with_variables(df, list(grade = .var, id = id)) |
| 164 | ||
| 165 | 75x |
denom <- match.arg(denom) %>% |
| 166 | 75x |
switch( |
| 167 | 75x |
n = nlevels(factor(df[[id]])), |
| 168 | 75x |
N_row = .N_row, |
| 169 | 75x |
N_col = .N_col |
| 170 |
) |
|
| 171 | ||
| 172 | 75x |
if (nrow(df) < 1) {
|
| 173 | 5x |
grade_levels <- levels(df[[.var]]) |
| 174 | 5x |
l_count <- as.list(rep(0, length(grade_levels))) |
| 175 | 5x |
names(l_count) <- grade_levels |
| 176 |
} else {
|
|
| 177 | 70x |
if (isTRUE(is.factor(df[[id]]))) {
|
| 178 | ! |
assert_valid_factor(df[[id]], any.missing = FALSE) |
| 179 |
} else {
|
|
| 180 | 70x |
checkmate::assert_character(df[[id]], min.chars = 1, any.missing = FALSE) |
| 181 |
} |
|
| 182 | 70x |
checkmate::assert_count(.N_col) |
| 183 | ||
| 184 | 70x |
id <- df[[id]] |
| 185 | 70x |
grade <- df[[.var]] |
| 186 | ||
| 187 | 70x |
if (!is.ordered(grade)) {
|
| 188 | 70x |
grade_lbl <- obj_label(grade) |
| 189 | 70x |
lvls <- levels(grade) |
| 190 | 70x |
if (sum(grepl("^\\d+$", lvls)) %in% c(0, length(lvls))) {
|
| 191 | 69x |
lvl_ord <- lvls |
| 192 |
} else {
|
|
| 193 | 1x |
lvls[!grepl("^\\d+$", lvls)] <- min(as.numeric(lvls[grepl("^\\d+$", lvls)])) - 1
|
| 194 | 1x |
lvl_ord <- levels(grade)[order(as.numeric(lvls))] |
| 195 |
} |
|
| 196 | 70x |
grade <- formatters::with_label(factor(grade, levels = lvl_ord, ordered = TRUE), grade_lbl) |
| 197 |
} |
|
| 198 | ||
| 199 | 70x |
missing_lvl <- grepl("missing", tolower(levels(grade)))
|
| 200 | 70x |
if (any(missing_lvl)) {
|
| 201 | 1x |
grade <- factor( |
| 202 | 1x |
grade, |
| 203 | 1x |
levels = c(levels(grade)[!missing_lvl], levels(grade)[missing_lvl]), |
| 204 | 1x |
ordered = is.ordered(grade) |
| 205 |
) |
|
| 206 |
} |
|
| 207 | 70x |
df_max <- stats::aggregate(grade ~ id, FUN = max, drop = FALSE) |
| 208 | 70x |
l_count <- as.list(table(df_max$grade)) |
| 209 |
} |
|
| 210 | ||
| 211 | 75x |
if (length(grade_groups) > 0) {
|
| 212 | 30x |
l_count <- h_append_grade_groups(grade_groups, l_count, remove_single, only_grade_groups) |
| 213 |
} |
|
| 214 | ||
| 215 | 75x |
l_count_fraction <- lapply( |
| 216 | 75x |
l_count, |
| 217 | 75x |
function(i, denom) {
|
| 218 | 299x |
if (i == 0 && denom == 0) {
|
| 219 | 9x |
c(0, 0) |
| 220 |
} else {
|
|
| 221 | 290x |
c(i, i / denom) |
| 222 |
} |
|
| 223 |
}, |
|
| 224 | 75x |
denom = denom |
| 225 |
) |
|
| 226 | ||
| 227 | 75x |
list( |
| 228 | 75x |
count_fraction = l_count_fraction, |
| 229 | 75x |
count_fraction_fixed_dp = l_count_fraction |
| 230 |
) |
|
| 231 |
} |
|
| 232 | ||
| 233 |
#' @describeIn count_occurrences_by_grade Formatted analysis function which is used as `afun` |
|
| 234 |
#' in `count_occurrences_by_grade()`. |
|
| 235 |
#' |
|
| 236 |
#' @return |
|
| 237 |
#' * `a_count_occurrences_by_grade()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 238 |
#' |
|
| 239 |
#' @examples |
|
| 240 |
#' a_count_occurrences_by_grade( |
|
| 241 |
#' df, |
|
| 242 |
#' .N_col = 10L, |
|
| 243 |
#' .N_row = 10L, |
|
| 244 |
#' .var = "AETOXGR", |
|
| 245 |
#' id = "USUBJID", |
|
| 246 |
#' grade_groups = list("ANY" = levels(df$AETOXGR))
|
|
| 247 |
#' ) |
|
| 248 |
#' |
|
| 249 |
#' @export |
|
| 250 |
a_count_occurrences_by_grade <- function(df, |
|
| 251 |
labelstr = "", |
|
| 252 |
..., |
|
| 253 |
.stats = NULL, |
|
| 254 |
.stat_names = NULL, |
|
| 255 |
.formats = NULL, |
|
| 256 |
.labels = NULL, |
|
| 257 |
.indent_mods = NULL) {
|
|
| 258 |
# Check for additional parameters to the statistics function |
|
| 259 | 56x |
dots_extra_args <- list(...) |
| 260 | 56x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 261 | 56x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 262 | ||
| 263 |
# Check for user-defined functions |
|
| 264 | 56x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 265 | 56x |
.stats <- default_and_custom_stats_list$all_stats |
| 266 | 56x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 267 | ||
| 268 |
# Apply statistics function |
|
| 269 | 56x |
x_stats <- .apply_stat_functions( |
| 270 | 56x |
default_stat_fnc = s_count_occurrences_by_grade, |
| 271 | 56x |
custom_stat_fnc_list = custom_stat_functions, |
| 272 | 56x |
args_list = c( |
| 273 | 56x |
df = list(df), |
| 274 | 56x |
labelstr = list(labelstr), |
| 275 | 56x |
extra_afun_params, |
| 276 | 56x |
dots_extra_args |
| 277 |
) |
|
| 278 |
) |
|
| 279 | ||
| 280 |
# Fill in formatting defaults |
|
| 281 | 56x |
.stats <- get_stats("count_occurrences_by_grade", stats_in = .stats, custom_stats_in = names(custom_stat_functions))
|
| 282 | 56x |
x_stats <- x_stats[.stats] |
| 283 | 56x |
levels_per_stats <- lapply(x_stats, names) |
| 284 | 56x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 285 | 56x |
.labels <- get_labels_from_stats(.stats, .labels, levels_per_stats) |
| 286 | 56x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 287 | ||
| 288 | 56x |
x_stats <- x_stats[.stats] %>% |
| 289 | 56x |
.unlist_keep_nulls() %>% |
| 290 | 56x |
setNames(names(.formats)) |
| 291 | ||
| 292 |
# Auto format handling |
|
| 293 | 56x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 294 | ||
| 295 |
# Get and check statistical names |
|
| 296 | 56x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 297 | ||
| 298 | 56x |
in_rows( |
| 299 | 56x |
.list = x_stats, |
| 300 | 56x |
.formats = .formats, |
| 301 | 56x |
.names = .labels %>% .unlist_keep_nulls(), |
| 302 | 56x |
.stat_names = .stat_names, |
| 303 | 56x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 304 | 56x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 305 |
) |
|
| 306 |
} |
|
| 307 | ||
| 308 |
#' @describeIn count_occurrences_by_grade Layout-creating function which can take statistics function |
|
| 309 |
#' arguments and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 310 |
#' |
|
| 311 |
#' @return |
|
| 312 |
#' * `count_occurrences_by_grade()` returns a layout object suitable for passing to further layouting functions, |
|
| 313 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 314 |
#' the statistics from `s_count_occurrences_by_grade()` to the table layout. |
|
| 315 |
#' |
|
| 316 |
#' @examples |
|
| 317 |
#' library(dplyr) |
|
| 318 |
#' |
|
| 319 |
#' df <- data.frame( |
|
| 320 |
#' USUBJID = as.character(c(1:6, 1)), |
|
| 321 |
#' ARM = factor(c("A", "A", "A", "B", "B", "B", "A"), levels = c("A", "B")),
|
|
| 322 |
#' AETOXGR = factor(c(1, 2, 3, 4, 1, 2, 3), levels = c(1:5)), |
|
| 323 |
#' AESEV = factor( |
|
| 324 |
#' x = c("MILD", "MODERATE", "SEVERE", "MILD", "MILD", "MODERATE", "SEVERE"),
|
|
| 325 |
#' levels = c("MILD", "MODERATE", "SEVERE")
|
|
| 326 |
#' ), |
|
| 327 |
#' stringsAsFactors = FALSE |
|
| 328 |
#' ) |
|
| 329 |
#' |
|
| 330 |
#' df_adsl <- df %>% |
|
| 331 |
#' select(USUBJID, ARM) %>% |
|
| 332 |
#' unique() |
|
| 333 |
#' |
|
| 334 |
#' # Layout creating function with custom format. |
|
| 335 |
#' basic_table() %>% |
|
| 336 |
#' split_cols_by("ARM") %>%
|
|
| 337 |
#' add_colcounts() %>% |
|
| 338 |
#' count_occurrences_by_grade( |
|
| 339 |
#' var = "AESEV", |
|
| 340 |
#' .formats = c("count_fraction" = "xx.xx (xx.xx%)")
|
|
| 341 |
#' ) %>% |
|
| 342 |
#' build_table(df, alt_counts_df = df_adsl) |
|
| 343 |
#' |
|
| 344 |
#' # Define additional grade groupings. |
|
| 345 |
#' grade_groups <- list( |
|
| 346 |
#' "-Any-" = c("1", "2", "3", "4", "5"),
|
|
| 347 |
#' "Grade 1-2" = c("1", "2"),
|
|
| 348 |
#' "Grade 3-5" = c("3", "4", "5")
|
|
| 349 |
#' ) |
|
| 350 |
#' |
|
| 351 |
#' basic_table() %>% |
|
| 352 |
#' split_cols_by("ARM") %>%
|
|
| 353 |
#' add_colcounts() %>% |
|
| 354 |
#' count_occurrences_by_grade( |
|
| 355 |
#' var = "AETOXGR", |
|
| 356 |
#' grade_groups = grade_groups, |
|
| 357 |
#' only_grade_groups = TRUE |
|
| 358 |
#' ) %>% |
|
| 359 |
#' build_table(df, alt_counts_df = df_adsl) |
|
| 360 |
#' |
|
| 361 |
#' @export |
|
| 362 |
#' @order 2 |
|
| 363 |
count_occurrences_by_grade <- function(lyt, |
|
| 364 |
var, |
|
| 365 |
id = "USUBJID", |
|
| 366 |
grade_groups = list(), |
|
| 367 |
remove_single = TRUE, |
|
| 368 |
only_grade_groups = FALSE, |
|
| 369 |
var_labels = var, |
|
| 370 |
show_labels = "default", |
|
| 371 |
riskdiff = FALSE, |
|
| 372 |
na_str = default_na_str(), |
|
| 373 |
nested = TRUE, |
|
| 374 |
..., |
|
| 375 |
table_names = var, |
|
| 376 |
.stats = "count_fraction", |
|
| 377 |
.stat_names = NULL, |
|
| 378 |
.formats = list(count_fraction = format_count_fraction_fixed_dp), |
|
| 379 |
.labels = NULL, |
|
| 380 |
.indent_mods = NULL) {
|
|
| 381 | 12x |
checkmate::assert_flag(riskdiff) |
| 382 | 12x |
afun <- if (isFALSE(riskdiff)) a_count_occurrences_by_grade else afun_riskdiff |
| 383 | ||
| 384 |
# Process standard extra arguments |
|
| 385 | 12x |
extra_args <- list(".stats" = .stats)
|
| 386 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 387 | 12x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 388 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 389 | 1x |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 390 | ||
| 391 |
# Process additional arguments to the statistic function |
|
| 392 | 12x |
extra_args <- c( |
| 393 | 12x |
extra_args, |
| 394 | 12x |
id = id, grade_groups = list(grade_groups), remove_single = remove_single, only_grade_groups = only_grade_groups, |
| 395 | 12x |
if (!isFALSE(riskdiff)) list(afun = list("s_count_occurrences_by_grade" = a_count_occurrences_by_grade)),
|
| 396 |
... |
|
| 397 |
) |
|
| 398 | ||
| 399 |
# Append additional info from layout to the analysis function |
|
| 400 | 12x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 401 | 12x |
formals(afun) <- c(formals(afun), extra_args[[".additional_fun_parameters"]]) |
| 402 | ||
| 403 | 12x |
analyze( |
| 404 | 12x |
lyt = lyt, |
| 405 | 12x |
vars = var, |
| 406 | 12x |
afun = afun, |
| 407 | 12x |
na_str = na_str, |
| 408 | 12x |
nested = nested, |
| 409 | 12x |
extra_args = extra_args, |
| 410 | 12x |
var_labels = var_labels, |
| 411 | 12x |
show_labels = show_labels, |
| 412 | 12x |
table_names = table_names |
| 413 |
) |
|
| 414 |
} |
|
| 415 | ||
| 416 |
#' @describeIn count_occurrences_by_grade Layout-creating function which can take content function arguments |
|
| 417 |
#' and additional format arguments. This function is a wrapper for [rtables::summarize_row_groups()]. |
|
| 418 |
#' |
|
| 419 |
#' @return |
|
| 420 |
#' * `summarize_occurrences_by_grade()` returns a layout object suitable for passing to further layouting functions, |
|
| 421 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted content rows |
|
| 422 |
#' containing the statistics from `s_count_occurrences_by_grade()` to the table layout. |
|
| 423 |
#' |
|
| 424 |
#' @examples |
|
| 425 |
#' # Layout creating function with custom format. |
|
| 426 |
#' basic_table() %>% |
|
| 427 |
#' add_colcounts() %>% |
|
| 428 |
#' split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
|
|
| 429 |
#' summarize_occurrences_by_grade( |
|
| 430 |
#' var = "AESEV", |
|
| 431 |
#' .formats = c("count_fraction" = "xx.xx (xx.xx%)")
|
|
| 432 |
#' ) %>% |
|
| 433 |
#' build_table(df, alt_counts_df = df_adsl) |
|
| 434 |
#' |
|
| 435 |
#' basic_table() %>% |
|
| 436 |
#' add_colcounts() %>% |
|
| 437 |
#' split_rows_by("ARM", child_labels = "visible", nested = TRUE) %>%
|
|
| 438 |
#' summarize_occurrences_by_grade( |
|
| 439 |
#' var = "AETOXGR", |
|
| 440 |
#' grade_groups = grade_groups |
|
| 441 |
#' ) %>% |
|
| 442 |
#' build_table(df, alt_counts_df = df_adsl) |
|
| 443 |
#' |
|
| 444 |
#' @export |
|
| 445 |
#' @order 3 |
|
| 446 |
summarize_occurrences_by_grade <- function(lyt, |
|
| 447 |
var, |
|
| 448 |
id = "USUBJID", |
|
| 449 |
grade_groups = list(), |
|
| 450 |
remove_single = TRUE, |
|
| 451 |
only_grade_groups = FALSE, |
|
| 452 |
riskdiff = FALSE, |
|
| 453 |
na_str = default_na_str(), |
|
| 454 |
..., |
|
| 455 |
.stats = "count_fraction", |
|
| 456 |
.stat_names = NULL, |
|
| 457 |
.formats = list(count_fraction = format_count_fraction_fixed_dp), |
|
| 458 |
.labels = NULL, |
|
| 459 |
.indent_mods = 0L) {
|
|
| 460 | 6x |
checkmate::assert_flag(riskdiff) |
| 461 | 6x |
afun <- if (isFALSE(riskdiff)) a_count_occurrences_by_grade else afun_riskdiff |
| 462 | ||
| 463 |
# Process standard extra arguments |
|
| 464 | 6x |
extra_args <- list(".stats" = .stats)
|
| 465 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 466 | 6x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 467 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 468 | 6x |
if (is.null(.indent_mods)) {
|
| 469 | ! |
indent_mod <- 0L |
| 470 | 6x |
} else if (length(.indent_mods) == 1) {
|
| 471 | 6x |
indent_mod <- .indent_mods |
| 472 |
} else {
|
|
| 473 | ! |
indent_mod <- 0L |
| 474 | ! |
extra_args[[".indent_mods"]] <- .indent_mods |
| 475 |
} |
|
| 476 | ||
| 477 |
# Process additional arguments to the statistic function |
|
| 478 | 6x |
extra_args <- c( |
| 479 | 6x |
extra_args, |
| 480 | 6x |
id = id, grade_groups = list(grade_groups), remove_single = remove_single, only_grade_groups = only_grade_groups, |
| 481 | 6x |
if (!isFALSE(riskdiff)) list(afun = list("s_count_occurrences_by_grade" = a_count_occurrences_by_grade)),
|
| 482 |
... |
|
| 483 |
) |
|
| 484 | ||
| 485 |
# Append additional info from layout to the analysis function |
|
| 486 | 6x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 487 | 6x |
formals(afun) <- c(formals(afun), extra_args[[".additional_fun_parameters"]]) |
| 488 | ||
| 489 | 6x |
summarize_row_groups( |
| 490 | 6x |
lyt = lyt, |
| 491 | 6x |
var = var, |
| 492 | 6x |
cfun = afun, |
| 493 | 6x |
na_str = na_str, |
| 494 | 6x |
extra_args = extra_args, |
| 495 | 6x |
indent_mod = indent_mod |
| 496 |
) |
|
| 497 |
} |
| 1 |
#' Confidence intervals for a difference of binomials |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 4 |
#' |
|
| 5 |
#' Several confidence intervals for the difference between proportions. |
|
| 6 |
#' |
|
| 7 |
#' @name desctools_binom |
|
| 8 |
NULL |
|
| 9 | ||
| 10 |
#' Recycle list of parameters |
|
| 11 |
#' |
|
| 12 |
#' This function recycles all supplied elements to the maximal dimension. |
|
| 13 |
#' |
|
| 14 |
#' @param ... (`any`)\cr elements to recycle. |
|
| 15 |
#' |
|
| 16 |
#' @return A `list`. |
|
| 17 |
#' |
|
| 18 |
#' @keywords internal |
|
| 19 |
#' @noRd |
|
| 20 |
h_recycle <- function(...) {
|
|
| 21 | 78x |
lst <- list(...) |
| 22 | 78x |
maxdim <- max(lengths(lst)) |
| 23 | 78x |
res <- lapply(lst, rep, length.out = maxdim) |
| 24 | 78x |
attr(res, "maxdim") <- maxdim |
| 25 | 78x |
return(res) |
| 26 |
} |
|
| 27 | ||
| 28 |
#' @describeIn desctools_binom Several confidence intervals for the difference between proportions. |
|
| 29 |
#' |
|
| 30 |
#' @return A `matrix` of 3 values: |
|
| 31 |
#' * `est`: estimate of proportion difference. |
|
| 32 |
#' * `lwr.ci`: estimate of lower end of the confidence interval. |
|
| 33 |
#' * `upr.ci`: estimate of upper end of the confidence interval. |
|
| 34 |
#' |
|
| 35 |
#' @keywords internal |
|
| 36 |
desctools_binom <- function(x1, |
|
| 37 |
n1, |
|
| 38 |
x2, |
|
| 39 |
n2, |
|
| 40 |
conf.level = 0.95, # nolint |
|
| 41 |
sides = c("two.sided", "left", "right"),
|
|
| 42 |
method = c( |
|
| 43 |
"ac", "wald", "waldcc", "score", "scorecc", "mn", "mee", "blj", "ha", "hal", "jp" |
|
| 44 |
)) {
|
|
| 45 | 26x |
if (missing(sides)) {
|
| 46 | 26x |
sides <- match.arg(sides) |
| 47 |
} |
|
| 48 | 26x |
if (missing(method)) {
|
| 49 | 1x |
method <- match.arg(method) |
| 50 |
} |
|
| 51 | 26x |
iBinomDiffCI <- function(x1, n1, x2, n2, conf.level, sides, method) { # nolint
|
| 52 | 26x |
if (sides != "two.sided") {
|
| 53 | ! |
conf.level <- 1 - 2 * (1 - conf.level) # nolint |
| 54 |
} |
|
| 55 | 26x |
alpha <- 1 - conf.level |
| 56 | 26x |
kappa <- stats::qnorm(1 - alpha / 2) |
| 57 | 26x |
p1_hat <- x1 / n1 |
| 58 | 26x |
p2_hat <- x2 / n2 |
| 59 | 26x |
est <- p1_hat - p2_hat |
| 60 | 26x |
switch(method, |
| 61 | 26x |
wald = {
|
| 62 | 4x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
| 63 | 4x |
term2 <- kappa * sqrt(vd) |
| 64 | 4x |
ci_lwr <- max(-1, est - term2) |
| 65 | 4x |
ci_upr <- min(1, est + term2) |
| 66 |
}, |
|
| 67 | 26x |
waldcc = {
|
| 68 | 6x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
| 69 | 6x |
term2 <- kappa * sqrt(vd) |
| 70 | 6x |
term2 <- term2 + 0.5 * (1 / n1 + 1 / n2) |
| 71 | 6x |
ci_lwr <- max(-1, est - term2) |
| 72 | 6x |
ci_upr <- min(1, est + term2) |
| 73 |
}, |
|
| 74 | 26x |
ac = {
|
| 75 | 2x |
n1 <- n1 + 2 |
| 76 | 2x |
n2 <- n2 + 2 |
| 77 | 2x |
x1 <- x1 + 1 |
| 78 | 2x |
x2 <- x2 + 1 |
| 79 | 2x |
p1_hat <- x1 / n1 |
| 80 | 2x |
p2_hat <- x2 / n2 |
| 81 | 2x |
est1 <- p1_hat - p2_hat |
| 82 | 2x |
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2 |
| 83 | 2x |
term2 <- kappa * sqrt(vd) |
| 84 | 2x |
ci_lwr <- max(-1, est1 - term2) |
| 85 | 2x |
ci_upr <- min(1, est1 + term2) |
| 86 |
}, |
|
| 87 | 26x |
exact = {
|
| 88 | ! |
ci_lwr <- NA |
| 89 | ! |
ci_upr <- NA |
| 90 |
}, |
|
| 91 | 26x |
score = {
|
| 92 | 3x |
w1 <- desctools_binomci( |
| 93 | 3x |
x = x1, n = n1, conf.level = conf.level, |
| 94 | 3x |
method = "wilson" |
| 95 |
) |
|
| 96 | 3x |
w2 <- desctools_binomci( |
| 97 | 3x |
x = x2, n = n2, conf.level = conf.level, |
| 98 | 3x |
method = "wilson" |
| 99 |
) |
|
| 100 | 3x |
l1 <- w1[2] |
| 101 | 3x |
u1 <- w1[3] |
| 102 | 3x |
l2 <- w2[2] |
| 103 | 3x |
u2 <- w2[3] |
| 104 | 3x |
ci_lwr <- est - kappa * sqrt(l1 * (1 - l1) / n1 + u2 * (1 - u2) / n2) |
| 105 | 3x |
ci_upr <- est + kappa * sqrt(u1 * (1 - u1) / n1 + l2 * (1 - l2) / n2) |
| 106 |
}, |
|
| 107 | 26x |
scorecc = {
|
| 108 | 1x |
w1 <- desctools_binomci( |
| 109 | 1x |
x = x1, n = n1, conf.level = conf.level, |
| 110 | 1x |
method = "wilsoncc" |
| 111 |
) |
|
| 112 | 1x |
w2 <- desctools_binomci( |
| 113 | 1x |
x = x2, n = n2, conf.level = conf.level, |
| 114 | 1x |
method = "wilsoncc" |
| 115 |
) |
|
| 116 | 1x |
l1 <- w1[2] |
| 117 | 1x |
u1 <- w1[3] |
| 118 | 1x |
l2 <- w2[2] |
| 119 | 1x |
u2 <- w2[3] |
| 120 | 1x |
ci_lwr <- max(-1, est - sqrt((p1_hat - l1)^2 + (u2 - p2_hat)^2)) |
| 121 | 1x |
ci_upr <- min(1, est + sqrt((u1 - p1_hat)^2 + (p2_hat - l2)^2)) |
| 122 |
}, |
|
| 123 | 26x |
mee = {
|
| 124 | 1x |
.score <- function(p1, n1, p2, n2, dif) {
|
| 125 | ! |
if (dif > 1) dif <- 1 |
| 126 | ! |
if (dif < -1) dif <- -1 |
| 127 | 24x |
diff <- p1 - p2 - dif |
| 128 | 24x |
if (abs(diff) == 0) {
|
| 129 | ! |
res <- 0 |
| 130 |
} else {
|
|
| 131 | 24x |
t <- n2 / n1 |
| 132 | 24x |
a <- 1 + t |
| 133 | 24x |
b <- -(1 + t + p1 + t * p2 + dif * (t + 2)) |
| 134 | 24x |
c <- dif * dif + dif * (2 * p1 + t + 1) + p1 + t * p2 |
| 135 | 24x |
d <- -p1 * dif * (1 + dif) |
| 136 | 24x |
v <- (b / a / 3)^3 - b * c / (6 * a * a) + d / a / 2 |
| 137 | 24x |
if (abs(v) < .Machine$double.eps) v <- 0 |
| 138 | 24x |
s <- sqrt((b / a / 3)^2 - c / a / 3) |
| 139 | 24x |
u <- ifelse(v > 0, 1, -1) * s |
| 140 | 24x |
w <- (3.141592654 + acos(v / u^3)) / 3 |
| 141 | 24x |
p1d <- 2 * u * cos(w) - b / a / 3 |
| 142 | 24x |
p2d <- p1d - dif |
| 143 | 24x |
n <- n1 + n2 |
| 144 | 24x |
res <- (p1d * (1 - p1d) / n1 + p2d * (1 - p2d) / n2) |
| 145 |
} |
|
| 146 | 24x |
return(sqrt(res)) |
| 147 |
} |
|
| 148 | 1x |
pval <- function(delta) {
|
| 149 | 24x |
z <- (est - delta) / .score(p1_hat, n1, p2_hat, n2, delta) |
| 150 | 24x |
2 * min(stats::pnorm(z), 1 - stats::pnorm(z)) |
| 151 |
} |
|
| 152 | 1x |
ci_lwr <- max(-1, stats::uniroot(function(delta) {
|
| 153 | 12x |
pval(delta) - alpha |
| 154 | 1x |
}, interval = c(-1 + 1e-06, est - 1e-06))$root) |
| 155 | 1x |
ci_upr <- min(1, stats::uniroot(function(delta) {
|
| 156 | 12x |
pval(delta) - alpha |
| 157 | 1x |
}, interval = c(est + 1e-06, 1 - 1e-06))$root) |
| 158 |
}, |
|
| 159 | 26x |
blj = {
|
| 160 | 1x |
p1_dash <- (x1 + 0.5) / (n1 + 1) |
| 161 | 1x |
p2_dash <- (x2 + 0.5) / (n2 + 1) |
| 162 | 1x |
vd <- p1_dash * (1 - p1_dash) / n1 + p2_dash * (1 - p2_dash) / n2 |
| 163 | 1x |
term2 <- kappa * sqrt(vd) |
| 164 | 1x |
est_dash <- p1_dash - p2_dash |
| 165 | 1x |
ci_lwr <- max(-1, est_dash - term2) |
| 166 | 1x |
ci_upr <- min(1, est_dash + term2) |
| 167 |
}, |
|
| 168 | 26x |
ha = {
|
| 169 | 5x |
term2 <- 1 / |
| 170 | 5x |
(2 * min(n1, n2)) + kappa * sqrt(p1_hat * (1 - p1_hat) / (n1 - 1) + p2_hat * (1 - p2_hat) / (n2 - 1)) |
| 171 | 5x |
ci_lwr <- max(-1, est - term2) |
| 172 | 5x |
ci_upr <- min(1, est + term2) |
| 173 |
}, |
|
| 174 | 26x |
mn = {
|
| 175 | 1x |
.conf <- function(x1, n1, x2, n2, z, lower = FALSE) {
|
| 176 | 2x |
p1 <- x1 / n1 |
| 177 | 2x |
p2 <- x2 / n2 |
| 178 | 2x |
p_hat <- p1 - p2 |
| 179 | 2x |
dp <- 1 + ifelse(lower, 1, -1) * p_hat |
| 180 | 2x |
i <- 1 |
| 181 | 2x |
while (i <= 50) {
|
| 182 | 46x |
dp <- 0.5 * dp |
| 183 | 46x |
y <- p_hat + ifelse(lower, -1, 1) * dp |
| 184 | 46x |
score <- .score(p1, n1, p2, n2, y) |
| 185 | 46x |
if (score < z) {
|
| 186 | 20x |
p_hat <- y |
| 187 |
} |
|
| 188 | 46x |
if ((dp < 1e-07) || (abs(z - score) < 1e-06)) {
|
| 189 | 2x |
(break)() |
| 190 |
} else {
|
|
| 191 | 44x |
i <- i + 1 |
| 192 |
} |
|
| 193 |
} |
|
| 194 | 2x |
return(y) |
| 195 |
} |
|
| 196 | 1x |
.score <- function(p1, n1, p2, n2, dif) {
|
| 197 | 46x |
diff <- p1 - p2 - dif |
| 198 | 46x |
if (abs(diff) == 0) {
|
| 199 | ! |
res <- 0 |
| 200 |
} else {
|
|
| 201 | 46x |
t <- n2 / n1 |
| 202 | 46x |
a <- 1 + t |
| 203 | 46x |
b <- -(1 + t + p1 + t * p2 + dif * (t + 2)) |
| 204 | 46x |
c <- dif * dif + dif * (2 * p1 + t + 1) + p1 + t * p2 |
| 205 | 46x |
d <- -p1 * dif * (1 + dif) |
| 206 | 46x |
v <- (b / a / 3)^3 - b * c / (6 * a * a) + d / a / 2 |
| 207 | 46x |
s <- sqrt((b / a / 3)^2 - c / a / 3) |
| 208 | 46x |
u <- ifelse(v > 0, 1, -1) * s |
| 209 | 46x |
w <- (3.141592654 + acos(v / u^3)) / 3 |
| 210 | 46x |
p1d <- 2 * u * cos(w) - b / a / 3 |
| 211 | 46x |
p2d <- p1d - dif |
| 212 | 46x |
n <- n1 + n2 |
| 213 | 46x |
var <- (p1d * (1 - p1d) / n1 + p2d * (1 - p2d) / n2) * n / (n - 1) |
| 214 | 46x |
res <- diff^2 / var |
| 215 |
} |
|
| 216 | 46x |
return(res) |
| 217 |
} |
|
| 218 | 1x |
z <- stats::qchisq(conf.level, 1) |
| 219 | 1x |
ci_lwr <- max(-1, .conf(x1, n1, x2, n2, z, TRUE)) |
| 220 | 1x |
ci_upr <- min(1, .conf(x1, n1, x2, n2, z, FALSE)) |
| 221 |
}, |
|
| 222 | 26x |
beal = {
|
| 223 | ! |
a <- p1_hat + p2_hat |
| 224 | ! |
b <- p1_hat - p2_hat |
| 225 | ! |
u <- ((1 / n1) + (1 / n2)) / 4 |
| 226 | ! |
v <- ((1 / n1) - (1 / n2)) / 4 |
| 227 | ! |
V <- u * ((2 - a) * a - b^2) + 2 * v * (1 - a) * b # nolint |
| 228 | ! |
z <- stats::qchisq(p = 1 - alpha / 2, df = 1) |
| 229 | ! |
A <- sqrt(z * (V + z * u^2 * (2 - a) * a + z * v^2 * (1 - a)^2)) # nolint |
| 230 | ! |
B <- (b + z * v * (1 - a)) / (1 + z * u) # nolint |
| 231 | ! |
ci_lwr <- max(-1, B - A / (1 + z * u)) |
| 232 | ! |
ci_upr <- min(1, B + A / (1 + z * u)) |
| 233 |
}, |
|
| 234 | 26x |
hal = {
|
| 235 | 1x |
psi <- (p1_hat + p2_hat) / 2 |
| 236 | 1x |
u <- (1 / n1 + 1 / n2) / 4 |
| 237 | 1x |
v <- (1 / n1 - 1 / n2) / 4 |
| 238 | 1x |
z <- kappa |
| 239 | 1x |
theta <- ((p1_hat - p2_hat) + z^2 * v * (1 - 2 * psi)) / (1 + z^2 * u) |
| 240 | 1x |
w <- z / (1 + z^2 * u) * sqrt(u * (4 * psi * (1 - psi) - (p1_hat - p2_hat)^2) + 2 * v * (1 - 2 * psi) * |
| 241 | 1x |
(p1_hat - p2_hat) + 4 * z^2 * u^2 * (1 - psi) * psi + z^2 * v^2 * (1 - 2 * psi)^2) # nolint |
| 242 | 1x |
c(theta + w, theta - w) |
| 243 | 1x |
ci_lwr <- max(-1, theta - w) |
| 244 | 1x |
ci_upr <- min(1, theta + w) |
| 245 |
}, |
|
| 246 | 26x |
jp = {
|
| 247 | 1x |
psi <- 0.5 * ((x1 + 0.5) / (n1 + 1) + (x2 + 0.5) / (n2 + 1)) |
| 248 | 1x |
u <- (1 / n1 + 1 / n2) / 4 |
| 249 | 1x |
v <- (1 / n1 - 1 / n2) / 4 |
| 250 | 1x |
z <- kappa |
| 251 | 1x |
theta <- ((p1_hat - p2_hat) + z^2 * v * (1 - 2 * psi)) / (1 + z^2 * u) |
| 252 | 1x |
w <- z / (1 + z^2 * u) * sqrt(u * (4 * psi * (1 - psi) - (p1_hat - p2_hat)^2) + 2 * v * (1 - 2 * psi) * |
| 253 | 1x |
(p1_hat - p2_hat) + 4 * z^2 * u^2 * (1 - psi) * psi + z^2 * v^2 * (1 - 2 * psi)^2) # nolint |
| 254 | 1x |
c(theta + w, theta - w) |
| 255 | 1x |
ci_lwr <- max(-1, theta - w) |
| 256 | 1x |
ci_upr <- min(1, theta + w) |
| 257 |
}, |
|
| 258 |
) |
|
| 259 | 26x |
ci <- c( |
| 260 | 26x |
est = est, lwr.ci = min(ci_lwr, ci_upr), |
| 261 | 26x |
upr.ci = max(ci_lwr, ci_upr) |
| 262 |
) |
|
| 263 | 26x |
if (sides == "left") {
|
| 264 | ! |
ci[3] <- 1 |
| 265 | 26x |
} else if (sides == "right") {
|
| 266 | ! |
ci[2] <- -1 |
| 267 |
} |
|
| 268 | 26x |
return(ci) |
| 269 |
} |
|
| 270 | 26x |
method <- match.arg(arg = method, several.ok = TRUE) |
| 271 | 26x |
sides <- match.arg(arg = sides, several.ok = TRUE) |
| 272 | 26x |
lst <- h_recycle( |
| 273 | 26x |
x1 = x1, n1 = n1, x2 = x2, n2 = n2, conf.level = conf.level, |
| 274 | 26x |
sides = sides, method = method |
| 275 |
) |
|
| 276 | 26x |
res <- t(sapply(1:attr(lst, "maxdim"), function(i) {
|
| 277 | 26x |
iBinomDiffCI( |
| 278 | 26x |
x1 = lst$x1[i], |
| 279 | 26x |
n1 = lst$n1[i], x2 = lst$x2[i], n2 = lst$n2[i], conf.level = lst$conf.level[i], |
| 280 | 26x |
sides = lst$sides[i], method = lst$method[i] |
| 281 |
) |
|
| 282 |
})) |
|
| 283 | 26x |
lgn <- h_recycle(x1 = if (is.null(names(x1))) {
|
| 284 | 26x |
paste("x1", seq_along(x1), sep = ".")
|
| 285 |
} else {
|
|
| 286 | ! |
names(x1) |
| 287 | 26x |
}, n1 = if (is.null(names(n1))) {
|
| 288 | 26x |
paste("n1", seq_along(n1), sep = ".")
|
| 289 |
} else {
|
|
| 290 | ! |
names(n1) |
| 291 | 26x |
}, x2 = if (is.null(names(x2))) {
|
| 292 | 26x |
paste("x2", seq_along(x2), sep = ".")
|
| 293 |
} else {
|
|
| 294 | ! |
names(x2) |
| 295 | 26x |
}, n2 = if (is.null(names(n2))) {
|
| 296 | 26x |
paste("n2", seq_along(n2), sep = ".")
|
| 297 |
} else {
|
|
| 298 | ! |
names(n2) |
| 299 | 26x |
}, conf.level = conf.level, sides = sides, method = method) |
| 300 | 26x |
xn <- apply(as.data.frame(lgn[sapply(lgn, function(x) {
|
| 301 | 182x |
length(unique(x)) != |
| 302 | 182x |
1 |
| 303 | 26x |
})]), 1, paste, collapse = ":") |
| 304 | 26x |
rownames(res) <- xn |
| 305 | 26x |
return(res) |
| 306 |
} |
|
| 307 | ||
| 308 |
#' @describeIn desctools_binom Compute confidence intervals for binomial proportions. |
|
| 309 |
#' |
|
| 310 |
#' @param x (`integer(1)`)\cr number of successes. |
|
| 311 |
#' @param n (`integer(1)`)\cr number of trials. |
|
| 312 |
#' @param conf.level (`proportion`)\cr confidence level, defaults to 0.95. |
|
| 313 |
#' @param sides (`string`)\cr side of the confidence interval to compute. Must be one of `"two-sided"` (default), |
|
| 314 |
#' `"left"`, or `"right"`. |
|
| 315 |
#' @param method (`string`)\cr method to use. Can be one out of: `"wald"`, `"wilson"`, `"wilsoncc"`, |
|
| 316 |
#' `"agresti-coull"`, `"jeffreys"`, `"modified wilson"`, `"modified jeffreys"`, `"clopper-pearson"`, `"arcsine"`, |
|
| 317 |
#' `"logit"`, `"witting"`, `"pratt"`, `"midp"`, `"lik"`, and `"blaker"`. |
|
| 318 |
#' |
|
| 319 |
#' @return A `matrix` with 3 columns containing: |
|
| 320 |
#' * `est`: estimate of proportion difference. |
|
| 321 |
#' * `lwr.ci`: lower end of the confidence interval. |
|
| 322 |
#' * `upr.ci`: upper end of the confidence interval. |
|
| 323 |
#' |
|
| 324 |
#' @keywords internal |
|
| 325 |
desctools_binomci <- function(x, |
|
| 326 |
n, |
|
| 327 |
conf.level = 0.95, # nolint |
|
| 328 |
sides = c("two.sided", "left", "right"),
|
|
| 329 |
method = c( |
|
| 330 |
"wilson", "wald", "waldcc", "agresti-coull", |
|
| 331 |
"jeffreys", "modified wilson", "wilsoncc", "modified jeffreys", |
|
| 332 |
"clopper-pearson", "arcsine", "logit", "witting", "pratt", |
|
| 333 |
"midp", "lik", "blaker" |
|
| 334 |
), |
|
| 335 |
rand = 123, |
|
| 336 |
tol = 1e-05) {
|
|
| 337 | 26x |
if (missing(method)) {
|
| 338 | 1x |
method <- "wilson" |
| 339 |
} |
|
| 340 | 26x |
if (missing(sides)) {
|
| 341 | 25x |
sides <- "two.sided" |
| 342 |
} |
|
| 343 | 26x |
iBinomCI <- function(x, n, conf.level = 0.95, sides = c("two.sided", "left", "right"), # nolint
|
| 344 | 26x |
method = c( |
| 345 | 26x |
"wilson", "wilsoncc", "wald", |
| 346 | 26x |
"waldcc", "agresti-coull", "jeffreys", "modified wilson", |
| 347 | 26x |
"modified jeffreys", "clopper-pearson", "arcsine", "logit", |
| 348 | 26x |
"witting", "pratt", "midp", "lik", "blaker" |
| 349 |
), |
|
| 350 | 26x |
rand = 123, |
| 351 | 26x |
tol = 1e-05) {
|
| 352 | 26x |
if (length(x) != 1) {
|
| 353 | ! |
stop("'x' has to be of length 1 (number of successes)")
|
| 354 |
} |
|
| 355 | 26x |
if (length(n) != 1) {
|
| 356 | ! |
stop("'n' has to be of length 1 (number of trials)")
|
| 357 |
} |
|
| 358 | 26x |
if (length(conf.level) != 1) {
|
| 359 | ! |
stop("'conf.level' has to be of length 1 (confidence level)")
|
| 360 |
} |
|
| 361 | 26x |
if (conf.level < 0.5 || conf.level > 1) {
|
| 362 | ! |
stop("'conf.level' has to be in [0.5, 1]")
|
| 363 |
} |
|
| 364 | 26x |
sides <- match.arg(sides, choices = c( |
| 365 | 26x |
"two.sided", "left", |
| 366 | 26x |
"right" |
| 367 | 26x |
), several.ok = FALSE) |
| 368 | 26x |
if (sides != "two.sided") {
|
| 369 | 1x |
conf.level <- 1 - 2 * (1 - conf.level) # nolint |
| 370 |
} |
|
| 371 | 26x |
alpha <- 1 - conf.level |
| 372 | 26x |
kappa <- stats::qnorm(1 - alpha / 2) |
| 373 | 26x |
p_hat <- x / n |
| 374 | 26x |
q_hat <- 1 - p_hat |
| 375 | 26x |
est <- p_hat |
| 376 | 26x |
switch(match.arg(arg = method, choices = c( |
| 377 | 26x |
"wilson", |
| 378 | 26x |
"wald", "waldcc", "wilsoncc", "agresti-coull", "jeffreys", |
| 379 | 26x |
"modified wilson", "modified jeffreys", "clopper-pearson", |
| 380 | 26x |
"arcsine", "logit", "witting", "pratt", "midp", "lik", |
| 381 | 26x |
"blaker" |
| 382 |
)), |
|
| 383 | 26x |
wald = {
|
| 384 | 1x |
term2 <- kappa * sqrt(p_hat * q_hat) / sqrt(n) |
| 385 | 1x |
ci_lwr <- max(0, p_hat - term2) |
| 386 | 1x |
ci_upr <- min(1, p_hat + term2) |
| 387 |
}, |
|
| 388 | 26x |
waldcc = {
|
| 389 | 1x |
term2 <- kappa * sqrt(p_hat * q_hat) / sqrt(n) |
| 390 | 1x |
term2 <- term2 + 1 / (2 * n) |
| 391 | 1x |
ci_lwr <- max(0, p_hat - term2) |
| 392 | 1x |
ci_upr <- min(1, p_hat + term2) |
| 393 |
}, |
|
| 394 | 26x |
wilson = {
|
| 395 | 8x |
term1 <- (x + kappa^2 / 2) / (n + kappa^2) |
| 396 | 8x |
term2 <- kappa * sqrt(n) / (n + kappa^2) * sqrt(p_hat * q_hat + kappa^2 / (4 * n)) |
| 397 | 8x |
ci_lwr <- max(0, term1 - term2) |
| 398 | 8x |
ci_upr <- min(1, term1 + term2) |
| 399 |
}, |
|
| 400 | 26x |
wilsoncc = {
|
| 401 | 3x |
lci <- ( |
| 402 | 3x |
2 * x + kappa^2 - 1 - kappa * sqrt(kappa^2 - 2 - 1 / n + 4 * p_hat * (n * q_hat + 1)) |
| 403 | 3x |
) / (2 * (n + kappa^2)) |
| 404 | 3x |
uci <- ( |
| 405 | 3x |
2 * x + kappa^2 + 1 + kappa * sqrt(kappa^2 + 2 - 1 / n + 4 * p_hat * (n * q_hat - 1)) |
| 406 | 3x |
) / (2 * (n + kappa^2)) |
| 407 | 3x |
ci_lwr <- max(0, ifelse(p_hat == 0, 0, lci)) |
| 408 | 3x |
ci_upr <- min(1, ifelse(p_hat == 1, 1, uci)) |
| 409 |
}, |
|
| 410 | 26x |
`agresti-coull` = {
|
| 411 | 1x |
x_tilde <- x + kappa^2 / 2 |
| 412 | 1x |
n_tilde <- n + kappa^2 |
| 413 | 1x |
p_tilde <- x_tilde / n_tilde |
| 414 | 1x |
q_tilde <- 1 - p_tilde |
| 415 | 1x |
est <- p_tilde |
| 416 | 1x |
term2 <- kappa * sqrt(p_tilde * q_tilde) / sqrt(n_tilde) |
| 417 | 1x |
ci_lwr <- max(0, p_tilde - term2) |
| 418 | 1x |
ci_upr <- min(1, p_tilde + term2) |
| 419 |
}, |
|
| 420 | 26x |
jeffreys = {
|
| 421 | 1x |
if (x == 0) {
|
| 422 | ! |
ci_lwr <- 0 |
| 423 |
} else {
|
|
| 424 | 1x |
ci_lwr <- stats::qbeta( |
| 425 | 1x |
alpha / 2, |
| 426 | 1x |
x + 0.5, n - x + 0.5 |
| 427 |
) |
|
| 428 |
} |
|
| 429 | 1x |
if (x == n) {
|
| 430 | ! |
ci_upr <- 1 |
| 431 |
} else {
|
|
| 432 | 1x |
ci_upr <- stats::qbeta(1 - alpha / 2, x + 0.5, n - x + 0.5) |
| 433 |
} |
|
| 434 |
}, |
|
| 435 | 26x |
`modified wilson` = {
|
| 436 | 1x |
term1 <- (x + kappa^2 / 2) / (n + kappa^2) |
| 437 | 1x |
term2 <- kappa * sqrt(n) / (n + kappa^2) * sqrt(p_hat * q_hat + kappa^2 / (4 * n)) |
| 438 | 1x |
if ((n <= 50 & x %in% c(1, 2)) | (n >= 51 & x %in% c(1:3))) {
|
| 439 | ! |
ci_lwr <- 0.5 * stats::qchisq(alpha, 2 * x) / n |
| 440 |
} else {
|
|
| 441 | 1x |
ci_lwr <- max(0, term1 - term2) |
| 442 |
} |
|
| 443 | 1x |
if ((n <= 50 & x %in% c(n - 1, n - 2)) | (n >= 51 & x %in% c(n - (1:3)))) {
|
| 444 | ! |
ci_upr <- 1 - 0.5 * stats::qchisq( |
| 445 | ! |
alpha, |
| 446 | ! |
2 * (n - x) |
| 447 | ! |
) / n |
| 448 |
} else {
|
|
| 449 | 1x |
ci_upr <- min(1, term1 + term2) |
| 450 |
} |
|
| 451 |
}, |
|
| 452 | 26x |
`modified jeffreys` = {
|
| 453 | 1x |
if (x == n) {
|
| 454 | ! |
ci_lwr <- (alpha / 2)^(1 / n) |
| 455 |
} else {
|
|
| 456 | 1x |
if (x <= 1) {
|
| 457 | ! |
ci_lwr <- 0 |
| 458 |
} else {
|
|
| 459 | 1x |
ci_lwr <- stats::qbeta( |
| 460 | 1x |
alpha / 2, |
| 461 | 1x |
x + 0.5, n - x + 0.5 |
| 462 |
) |
|
| 463 |
} |
|
| 464 |
} |
|
| 465 | 1x |
if (x == 0) {
|
| 466 | ! |
ci_upr <- 1 - (alpha / 2)^(1 / n) |
| 467 |
} else {
|
|
| 468 | 1x |
if (x >= n - 1) {
|
| 469 | ! |
ci_upr <- 1 |
| 470 |
} else {
|
|
| 471 | 1x |
ci_upr <- stats::qbeta(1 - alpha / 2, x + 0.5, n - x + 0.5) |
| 472 |
} |
|
| 473 |
} |
|
| 474 |
}, |
|
| 475 | 26x |
`clopper-pearson` = {
|
| 476 | 1x |
ci_lwr <- stats::qbeta(alpha / 2, x, n - x + 1) |
| 477 | 1x |
ci_upr <- stats::qbeta(1 - alpha / 2, x + 1, n - x) |
| 478 |
}, |
|
| 479 | 26x |
arcsine = {
|
| 480 | 1x |
p_tilde <- (x + 0.375) / (n + 0.75) |
| 481 | 1x |
est <- p_tilde |
| 482 | 1x |
ci_lwr <- sin(asin(sqrt(p_tilde)) - 0.5 * kappa / sqrt(n))^2 |
| 483 | 1x |
ci_upr <- sin(asin(sqrt(p_tilde)) + 0.5 * kappa / sqrt(n))^2 |
| 484 |
}, |
|
| 485 | 26x |
logit = {
|
| 486 | 1x |
lambda_hat <- log(x / (n - x)) |
| 487 | 1x |
V_hat <- n / (x * (n - x)) # nolint |
| 488 | 1x |
lambda_lower <- lambda_hat - kappa * sqrt(V_hat) |
| 489 | 1x |
lambda_upper <- lambda_hat + kappa * sqrt(V_hat) |
| 490 | 1x |
ci_lwr <- exp(lambda_lower) / (1 + exp(lambda_lower)) |
| 491 | 1x |
ci_upr <- exp(lambda_upper) / (1 + exp(lambda_upper)) |
| 492 |
}, |
|
| 493 | 26x |
witting = {
|
| 494 | 1x |
set.seed(rand) |
| 495 | 1x |
x_tilde <- x + stats::runif(1, min = 0, max = 1) |
| 496 | 1x |
pbinom_abscont <- function(q, size, prob) {
|
| 497 | 22x |
v <- trunc(q) |
| 498 | 22x |
term1 <- stats::pbinom(v - 1, size = size, prob = prob) |
| 499 | 22x |
term2 <- (q - v) * stats::dbinom(v, size = size, prob = prob) |
| 500 | 22x |
return(term1 + term2) |
| 501 |
} |
|
| 502 | 1x |
qbinom_abscont <- function(p, size, x) {
|
| 503 | 2x |
fun <- function(prob, size, x, p) {
|
| 504 | 22x |
pbinom_abscont(x, size, prob) - p |
| 505 |
} |
|
| 506 | 2x |
stats::uniroot(fun, |
| 507 | 2x |
interval = c(0, 1), size = size, |
| 508 | 2x |
x = x, p = p |
| 509 | 2x |
)$root |
| 510 |
} |
|
| 511 | 1x |
ci_lwr <- qbinom_abscont(1 - alpha, size = n, x = x_tilde) |
| 512 | 1x |
ci_upr <- qbinom_abscont(alpha, size = n, x = x_tilde) |
| 513 |
}, |
|
| 514 | 26x |
pratt = {
|
| 515 | 1x |
if (x == 0) {
|
| 516 | ! |
ci_lwr <- 0 |
| 517 | ! |
ci_upr <- 1 - alpha^(1 / n) |
| 518 | 1x |
} else if (x == 1) {
|
| 519 | ! |
ci_lwr <- 1 - (1 - alpha / 2)^(1 / n) |
| 520 | ! |
ci_upr <- 1 - (alpha / 2)^(1 / n) |
| 521 | 1x |
} else if (x == (n - 1)) {
|
| 522 | ! |
ci_lwr <- (alpha / 2)^(1 / n) |
| 523 | ! |
ci_upr <- (1 - alpha / 2)^(1 / n) |
| 524 | 1x |
} else if (x == n) {
|
| 525 | ! |
ci_lwr <- alpha^(1 / n) |
| 526 | ! |
ci_upr <- 1 |
| 527 |
} else {
|
|
| 528 | 1x |
z <- stats::qnorm(1 - alpha / 2) |
| 529 | 1x |
A <- ((x + 1) / (n - x))^2 # nolint |
| 530 | 1x |
B <- 81 * (x + 1) * (n - x) - 9 * n - 8 # nolint |
| 531 | 1x |
C <- (0 - 3) * z * sqrt(9 * (x + 1) * (n - x) * (9 * n + 5 - z^2) + n + 1) # nolint |
| 532 | 1x |
D <- 81 * (x + 1)^2 - 9 * (x + 1) * (2 + z^2) + 1 # nolint |
| 533 | 1x |
E <- 1 + A * ((B + C) / D)^3 # nolint |
| 534 | 1x |
ci_upr <- 1 / E |
| 535 | 1x |
A <- (x / (n - x - 1))^2 # nolint |
| 536 | 1x |
B <- 81 * x * (n - x - 1) - 9 * n - 8 # nolint |
| 537 | 1x |
C <- 3 * z * sqrt(9 * x * (n - x - 1) * (9 * n + 5 - z^2) + n + 1) # nolint |
| 538 | 1x |
D <- 81 * x^2 - 9 * x * (2 + z^2) + 1 # nolint |
| 539 | 1x |
E <- 1 + A * ((B + C) / D)^3 # nolint |
| 540 | 1x |
ci_lwr <- 1 / E |
| 541 |
} |
|
| 542 |
}, |
|
| 543 | 26x |
midp = {
|
| 544 | 1x |
f_low <- function(pi, x, n) {
|
| 545 | 12x |
1 / 2 * stats::dbinom(x, size = n, prob = pi) + stats::pbinom(x, |
| 546 | 12x |
size = n, prob = pi, lower.tail = FALSE |
| 547 |
) - |
|
| 548 | 12x |
(1 - conf.level) / 2 |
| 549 |
} |
|
| 550 | 1x |
f_up <- function(pi, x, n) {
|
| 551 | 12x |
1 / 2 * stats::dbinom(x, size = n, prob = pi) + stats::pbinom(x - 1, size = n, prob = pi) - (1 - conf.level) / 2 |
| 552 |
} |
|
| 553 | 1x |
ci_lwr <- 0 |
| 554 | 1x |
ci_upr <- 1 |
| 555 | 1x |
if (x != 0) {
|
| 556 | 1x |
ci_lwr <- stats::uniroot(f_low, |
| 557 | 1x |
interval = c(0, p_hat), |
| 558 | 1x |
x = x, n = n |
| 559 | 1x |
)$root |
| 560 |
} |
|
| 561 | 1x |
if (x != n) {
|
| 562 | 1x |
ci_upr <- stats::uniroot(f_up, interval = c( |
| 563 | 1x |
p_hat, |
| 564 | 1x |
1 |
| 565 | 1x |
), x = x, n = n)$root |
| 566 |
} |
|
| 567 |
}, |
|
| 568 | 26x |
lik = {
|
| 569 | 2x |
ci_lwr <- 0 |
| 570 | 2x |
ci_upr <- 1 |
| 571 | 2x |
z <- stats::qnorm(1 - alpha * 0.5) |
| 572 | 2x |
tol <- .Machine$double.eps^0.5 |
| 573 | 2x |
BinDev <- function(y, x, mu, wt, bound = 0, tol = .Machine$double.eps^0.5, # nolint |
| 574 |
...) {
|
|
| 575 | 40x |
ll_y <- ifelse(y %in% c(0, 1), 0, stats::dbinom(x, wt, |
| 576 | 40x |
y, |
| 577 | 40x |
log = TRUE |
| 578 |
)) |
|
| 579 | 40x |
ll_mu <- ifelse(mu %in% c(0, 1), 0, stats::dbinom(x, |
| 580 | 40x |
wt, mu, |
| 581 | 40x |
log = TRUE |
| 582 |
)) |
|
| 583 | 40x |
res <- ifelse(abs(y - mu) < tol, 0, sign(y - mu) * sqrt(-2 * (ll_y - ll_mu))) |
| 584 | 40x |
return(res - bound) |
| 585 |
} |
|
| 586 | 2x |
if (x != 0 && tol < p_hat) {
|
| 587 | 2x |
ci_lwr <- if (BinDev( |
| 588 | 2x |
tol, x, p_hat, n, -z, |
| 589 | 2x |
tol |
| 590 | 2x |
) <= 0) {
|
| 591 | 2x |
stats::uniroot( |
| 592 | 2x |
f = BinDev, interval = c(tol, if (p_hat < tol || p_hat == 1) {
|
| 593 | ! |
1 - tol |
| 594 |
} else {
|
|
| 595 | 2x |
p_hat |
| 596 | 2x |
}), bound = -z, |
| 597 | 2x |
x = x, mu = p_hat, wt = n |
| 598 | 2x |
)$root |
| 599 |
} |
|
| 600 |
} |
|
| 601 | 2x |
if (x != n && p_hat < (1 - tol)) {
|
| 602 | 2x |
ci_upr <- if ( |
| 603 | 2x |
BinDev(y = 1 - tol, x = x, mu = ifelse(p_hat > 1 - tol, tol, p_hat), wt = n, bound = z, tol = tol) < 0) { # nolint
|
| 604 | ! |
ci_lwr <- if (BinDev( |
| 605 | ! |
tol, x, if (p_hat < tol || p_hat == 1) {
|
| 606 | ! |
1 - tol |
| 607 |
} else {
|
|
| 608 | ! |
p_hat |
| 609 | ! |
}, n, |
| 610 | ! |
-z, tol |
| 611 | ! |
) <= 0) {
|
| 612 | ! |
stats::uniroot( |
| 613 | ! |
f = BinDev, interval = c(tol, p_hat), |
| 614 | ! |
bound = -z, x = x, mu = p_hat, wt = n |
| 615 | ! |
)$root |
| 616 |
} |
|
| 617 |
} else {
|
|
| 618 | 2x |
stats::uniroot( |
| 619 | 2x |
f = BinDev, interval = c(if (p_hat > 1 - tol) {
|
| 620 | ! |
tol |
| 621 |
} else {
|
|
| 622 | 2x |
p_hat |
| 623 | 2x |
}, 1 - tol), bound = z, |
| 624 | 2x |
x = x, mu = p_hat, wt = n |
| 625 | 2x |
)$root |
| 626 |
} |
|
| 627 |
} |
|
| 628 |
}, |
|
| 629 | 26x |
blaker = {
|
| 630 | 1x |
acceptbin <- function(x, n, p) {
|
| 631 | 3954x |
p1 <- 1 - stats::pbinom(x - 1, n, p) |
| 632 | 3954x |
p2 <- stats::pbinom(x, n, p) |
| 633 | 3954x |
a1 <- p1 + stats::pbinom(stats::qbinom(p1, n, p) - 1, n, p) |
| 634 | 3954x |
a2 <- p2 + 1 - stats::pbinom( |
| 635 | 3954x |
stats::qbinom(1 - p2, n, p), n, |
| 636 | 3954x |
p |
| 637 |
) |
|
| 638 | 3954x |
return(min(a1, a2)) |
| 639 |
} |
|
| 640 | 1x |
ci_lwr <- 0 |
| 641 | 1x |
ci_upr <- 1 |
| 642 | 1x |
if (x != 0) {
|
| 643 | 1x |
ci_lwr <- stats::qbeta((1 - conf.level) / 2, x, n - x + 1) |
| 644 | 1x |
while (acceptbin(x, n, ci_lwr + tol) < (1 - conf.level)) {
|
| 645 | 1976x |
ci_lwr <- ci_lwr + tol |
| 646 |
} |
|
| 647 |
} |
|
| 648 | 1x |
if (x != n) {
|
| 649 | 1x |
ci_upr <- stats::qbeta(1 - (1 - conf.level) / 2, x + 1, n - x) |
| 650 | 1x |
while (acceptbin(x, n, ci_upr - tol) < (1 - conf.level)) {
|
| 651 | 1976x |
ci_upr <- ci_upr - tol |
| 652 |
} |
|
| 653 |
} |
|
| 654 |
} |
|
| 655 |
) |
|
| 656 | 26x |
ci <- c(est = est, lwr.ci = max(0, ci_lwr), upr.ci = min( |
| 657 | 26x |
1, |
| 658 | 26x |
ci_upr |
| 659 |
)) |
|
| 660 | 26x |
if (sides == "left") {
|
| 661 | 1x |
ci[3] <- 1 |
| 662 | 25x |
} else if (sides == "right") {
|
| 663 | ! |
ci[2] <- 0 |
| 664 |
} |
|
| 665 | 26x |
return(ci) |
| 666 |
} |
|
| 667 | 26x |
lst <- list( |
| 668 | 26x |
x = x, n = n, conf.level = conf.level, sides = sides, |
| 669 | 26x |
method = method, rand = rand |
| 670 |
) |
|
| 671 | 26x |
maxdim <- max(unlist(lapply(lst, length))) |
| 672 | 26x |
lgp <- lapply(lst, rep, length.out = maxdim) |
| 673 | 26x |
lgn <- h_recycle(x = if (is.null(names(x))) {
|
| 674 | 26x |
paste("x", seq_along(x), sep = ".")
|
| 675 |
} else {
|
|
| 676 | ! |
names(x) |
| 677 | 26x |
}, n = if (is.null(names(n))) {
|
| 678 | 26x |
paste("n", seq_along(n), sep = ".")
|
| 679 |
} else {
|
|
| 680 | ! |
names(n) |
| 681 | 26x |
}, conf.level = conf.level, sides = sides, method = method) |
| 682 | 26x |
xn <- apply(as.data.frame(lgn[sapply(lgn, function(x) {
|
| 683 | 130x |
length(unique(x)) != |
| 684 | 130x |
1 |
| 685 | 26x |
})]), 1, paste, collapse = ":") |
| 686 | 26x |
res <- t(sapply(1:maxdim, function(i) {
|
| 687 | 26x |
iBinomCI( |
| 688 | 26x |
x = lgp$x[i], |
| 689 | 26x |
n = lgp$n[i], conf.level = lgp$conf.level[i], sides = lgp$sides[i], |
| 690 | 26x |
method = lgp$method[i], rand = lgp$rand[i] |
| 691 |
) |
|
| 692 |
})) |
|
| 693 | 26x |
colnames(res)[1] <- c("est")
|
| 694 | 26x |
rownames(res) <- xn |
| 695 | 26x |
return(res) |
| 696 |
} |
| 1 |
#' Tabulate biomarker effects on survival by subgroup |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The [tabulate_survival_biomarkers()] function creates a layout element to tabulate the estimated effects of multiple |
|
| 6 |
#' continuous biomarker variables on survival across subgroups, returning statistics including median survival time and |
|
| 7 |
#' hazard ratio for each population subgroup. The table is created from `df`, a list of data frames returned by |
|
| 8 |
#' [extract_survival_biomarkers()], with the statistics to include specified via the `vars` parameter. |
|
| 9 |
#' |
|
| 10 |
#' A forest plot can be created from the resulting table using the [g_forest()] function. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams fit_coxreg_multivar |
|
| 13 |
#' @inheritParams survival_duration_subgroups |
|
| 14 |
#' @inheritParams argument_convention |
|
| 15 |
#' @param df (`data.frame`)\cr containing all analysis variables, as returned by |
|
| 16 |
#' [extract_survival_biomarkers()]. |
|
| 17 |
#' @param vars (`character`)\cr the names of statistics to be reported among: |
|
| 18 |
#' * `n_tot_events`: Total number of events per group. |
|
| 19 |
#' * `n_tot`: Total number of observations per group. |
|
| 20 |
#' * `median`: Median survival time. |
|
| 21 |
#' * `hr`: Hazard ratio. |
|
| 22 |
#' * `ci`: Confidence interval of hazard ratio. |
|
| 23 |
#' * `pval`: p-value of the effect. |
|
| 24 |
#' Note, one of the statistics `n_tot` and `n_tot_events`, as well as both `hr` and `ci` are required. |
|
| 25 |
#' |
|
| 26 |
#' @details These functions create a layout starting from a data frame which contains |
|
| 27 |
#' the required statistics. The tables are then typically used as input for forest plots. |
|
| 28 |
#' |
|
| 29 |
#' @examples |
|
| 30 |
#' library(dplyr) |
|
| 31 |
#' |
|
| 32 |
#' adtte <- tern_ex_adtte |
|
| 33 |
#' |
|
| 34 |
#' # Save variable labels before data processing steps. |
|
| 35 |
#' adtte_labels <- formatters::var_labels(adtte) |
|
| 36 |
#' |
|
| 37 |
#' adtte_f <- adtte %>% |
|
| 38 |
#' filter(PARAMCD == "OS") %>% |
|
| 39 |
#' mutate( |
|
| 40 |
#' AVALU = as.character(AVALU), |
|
| 41 |
#' is_event = CNSR == 0 |
|
| 42 |
#' ) |
|
| 43 |
#' labels <- c("AVALU" = adtte_labels[["AVALU"]], "is_event" = "Event Flag")
|
|
| 44 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels |
|
| 45 |
#' |
|
| 46 |
#' # Typical analysis of two continuous biomarkers `BMRKR1` and `AGE`, |
|
| 47 |
#' # in multiple regression models containing one covariate `RACE`, |
|
| 48 |
#' # as well as one stratification variable `STRATA1`. The subgroups |
|
| 49 |
#' # are defined by the levels of `BMRKR2`. |
|
| 50 |
#' |
|
| 51 |
#' df <- extract_survival_biomarkers( |
|
| 52 |
#' variables = list( |
|
| 53 |
#' tte = "AVAL", |
|
| 54 |
#' is_event = "is_event", |
|
| 55 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
| 56 |
#' strata = "STRATA1", |
|
| 57 |
#' covariates = "SEX", |
|
| 58 |
#' subgroups = "BMRKR2" |
|
| 59 |
#' ), |
|
| 60 |
#' label_all = "Total Patients", |
|
| 61 |
#' data = adtte_f |
|
| 62 |
#' ) |
|
| 63 |
#' df |
|
| 64 |
#' |
|
| 65 |
#' # Here we group the levels of `BMRKR2` manually. |
|
| 66 |
#' df_grouped <- extract_survival_biomarkers( |
|
| 67 |
#' variables = list( |
|
| 68 |
#' tte = "AVAL", |
|
| 69 |
#' is_event = "is_event", |
|
| 70 |
#' biomarkers = c("BMRKR1", "AGE"),
|
|
| 71 |
#' strata = "STRATA1", |
|
| 72 |
#' covariates = "SEX", |
|
| 73 |
#' subgroups = "BMRKR2" |
|
| 74 |
#' ), |
|
| 75 |
#' data = adtte_f, |
|
| 76 |
#' groups_lists = list( |
|
| 77 |
#' BMRKR2 = list( |
|
| 78 |
#' "low" = "LOW", |
|
| 79 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
| 80 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
| 81 |
#' ) |
|
| 82 |
#' ) |
|
| 83 |
#' ) |
|
| 84 |
#' df_grouped |
|
| 85 |
#' |
|
| 86 |
#' @name survival_biomarkers_subgroups |
|
| 87 |
#' @order 1 |
|
| 88 |
NULL |
|
| 89 | ||
| 90 |
#' Prepare survival data estimates for multiple biomarkers in a single data frame |
|
| 91 |
#' |
|
| 92 |
#' @description `r lifecycle::badge("stable")`
|
|
| 93 |
#' |
|
| 94 |
#' Prepares estimates for number of events, patients and median survival times, as well as hazard ratio estimates, |
|
| 95 |
#' confidence intervals and p-values, for multiple biomarkers across population subgroups in a single data frame. |
|
| 96 |
#' `variables` corresponds to the names of variables found in `data`, passed as a named `list` and requires elements |
|
| 97 |
#' `tte`, `is_event`, `biomarkers` (vector of continuous biomarker variables), and optionally `subgroups` and `strata`. |
|
| 98 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables. |
|
| 99 |
#' |
|
| 100 |
#' @inheritParams argument_convention |
|
| 101 |
#' @inheritParams fit_coxreg_multivar |
|
| 102 |
#' @inheritParams survival_duration_subgroups |
|
| 103 |
#' |
|
| 104 |
#' @return A `data.frame` with columns `biomarker`, `biomarker_label`, `n_tot`, `n_tot_events`, |
|
| 105 |
#' `median`, `hr`, `lcl`, `ucl`, `conf_level`, `pval`, `pval_label`, `subgroup`, `var`, |
|
| 106 |
#' `var_label`, and `row_type`. |
|
| 107 |
#' |
|
| 108 |
#' @seealso [h_coxreg_mult_cont_df()] which is used internally, [tabulate_survival_biomarkers()]. |
|
| 109 |
#' |
|
| 110 |
#' @export |
|
| 111 |
extract_survival_biomarkers <- function(variables, |
|
| 112 |
data, |
|
| 113 |
groups_lists = list(), |
|
| 114 |
control = control_coxreg(), |
|
| 115 |
label_all = "All Patients") {
|
|
| 116 | 6x |
if ("strat" %in% names(variables)) {
|
| 117 | ! |
warning( |
| 118 | ! |
"Warning: the `strat` element name of the `variables` list argument to `extract_survival_biomarkers() ", |
| 119 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 120 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 121 |
) |
|
| 122 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 123 |
} |
|
| 124 | ||
| 125 | 6x |
checkmate::assert_list(variables) |
| 126 | 6x |
checkmate::assert_character(variables$subgroups, null.ok = TRUE) |
| 127 | 6x |
checkmate::assert_string(label_all) |
| 128 | ||
| 129 |
# Start with all patients. |
|
| 130 | 6x |
result_all <- h_coxreg_mult_cont_df( |
| 131 | 6x |
variables = variables, |
| 132 | 6x |
data = data, |
| 133 | 6x |
control = control |
| 134 |
) |
|
| 135 | 6x |
result_all$subgroup <- label_all |
| 136 | 6x |
result_all$var <- "ALL" |
| 137 | 6x |
result_all$var_label <- label_all |
| 138 | 6x |
result_all$row_type <- "content" |
| 139 | 6x |
if (is.null(variables$subgroups)) {
|
| 140 |
# Only return result for all patients. |
|
| 141 | 1x |
result_all |
| 142 |
} else {
|
|
| 143 |
# Add subgroups results. |
|
| 144 | 5x |
l_data <- h_split_by_subgroups( |
| 145 | 5x |
data, |
| 146 | 5x |
variables$subgroups, |
| 147 | 5x |
groups_lists = groups_lists |
| 148 |
) |
|
| 149 | 5x |
l_result <- lapply(l_data, function(grp) {
|
| 150 | 25x |
result <- h_coxreg_mult_cont_df( |
| 151 | 25x |
variables = variables, |
| 152 | 25x |
data = grp$df, |
| 153 | 25x |
control = control |
| 154 |
) |
|
| 155 | 25x |
result_labels <- grp$df_labels[rep(1, times = nrow(result)), ] |
| 156 | 25x |
cbind(result, result_labels) |
| 157 |
}) |
|
| 158 | 5x |
result_subgroups <- do.call(rbind, args = c(l_result, make.row.names = FALSE)) |
| 159 | 5x |
result_subgroups$row_type <- "analysis" |
| 160 | 5x |
rbind( |
| 161 | 5x |
result_all, |
| 162 | 5x |
result_subgroups |
| 163 |
) |
|
| 164 |
} |
|
| 165 |
} |
|
| 166 | ||
| 167 |
#' @describeIn survival_biomarkers_subgroups Table-creating function which creates a table |
|
| 168 |
#' summarizing biomarker effects on survival by subgroup. |
|
| 169 |
#' |
|
| 170 |
#' @param label_all `r lifecycle::badge("deprecated")`\cr please assign the `label_all` parameter within the
|
|
| 171 |
#' [extract_survival_biomarkers()] function when creating `df`. |
|
| 172 |
#' |
|
| 173 |
#' @return An `rtables` table summarizing biomarker effects on survival by subgroup. |
|
| 174 |
#' |
|
| 175 |
#' @note In contrast to [tabulate_survival_subgroups()] this tabulation function does |
|
| 176 |
#' not start from an input layout `lyt`. This is because internally the table is |
|
| 177 |
#' created by combining multiple subtables. |
|
| 178 |
#' |
|
| 179 |
#' @seealso [extract_survival_biomarkers()] |
|
| 180 |
#' |
|
| 181 |
#' @examples |
|
| 182 |
#' ## Table with default columns. |
|
| 183 |
#' tabulate_survival_biomarkers(df) |
|
| 184 |
#' |
|
| 185 |
#' ## Table with a manually chosen set of columns: leave out "pval", reorder. |
|
| 186 |
#' tab <- tabulate_survival_biomarkers( |
|
| 187 |
#' df = df, |
|
| 188 |
#' vars = c("n_tot_events", "ci", "n_tot", "median", "hr"),
|
|
| 189 |
#' time_unit = as.character(adtte_f$AVALU[1]) |
|
| 190 |
#' ) |
|
| 191 |
#' |
|
| 192 |
#' ## Finally produce the forest plot. |
|
| 193 |
#' \donttest{
|
|
| 194 |
#' g_forest(tab, xlim = c(0.8, 1.2)) |
|
| 195 |
#' } |
|
| 196 |
#' |
|
| 197 |
#' @export |
|
| 198 |
#' @order 2 |
|
| 199 |
tabulate_survival_biomarkers <- function(df, |
|
| 200 |
vars = c("n_tot", "n_tot_events", "median", "hr", "ci", "pval"),
|
|
| 201 |
groups_lists = list(), |
|
| 202 |
control = control_coxreg(), |
|
| 203 |
label_all = lifecycle::deprecated(), |
|
| 204 |
time_unit = NULL, |
|
| 205 |
na_str = default_na_str(), |
|
| 206 |
..., |
|
| 207 |
.stat_names = NULL, |
|
| 208 |
.formats = NULL, |
|
| 209 |
.labels = NULL, |
|
| 210 |
.indent_mods = NULL) {
|
|
| 211 | 5x |
if (lifecycle::is_present(label_all)) {
|
| 212 | 1x |
lifecycle::deprecate_warn( |
| 213 | 1x |
"0.9.5", "tabulate_survival_biomarkers(label_all)", |
| 214 | 1x |
details = paste( |
| 215 | 1x |
"Please assign the `label_all` parameter within the", |
| 216 | 1x |
"`extract_survival_biomarkers()` function when creating `df`." |
| 217 |
) |
|
| 218 |
) |
|
| 219 |
} |
|
| 220 | ||
| 221 | 5x |
checkmate::assert_data_frame(df) |
| 222 | 5x |
checkmate::assert_character(df$biomarker) |
| 223 | 5x |
checkmate::assert_character(df$biomarker_label) |
| 224 | 5x |
checkmate::assert_subset(vars, get_stats("tabulate_survival_biomarkers"))
|
| 225 | ||
| 226 |
# Process standard extra arguments |
|
| 227 | 5x |
extra_args <- list(".stats" = vars)
|
| 228 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 229 | ! |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 230 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 231 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 232 | ||
| 233 | 5x |
colvars <- d_survival_subgroups_colvars( |
| 234 | 5x |
vars, |
| 235 | 5x |
conf_level = df$conf_level[1], |
| 236 | 5x |
method = df$pval_label[1], |
| 237 | 5x |
time_unit = time_unit |
| 238 |
) |
|
| 239 | ||
| 240 |
# Process additional arguments to the statistic function |
|
| 241 | 5x |
extra_args <- c( |
| 242 | 5x |
extra_args, |
| 243 | 5x |
groups_lists = list(groups_lists), control = list(control), biomarker = TRUE, |
| 244 |
... |
|
| 245 |
) |
|
| 246 | ||
| 247 |
# Adding additional info from layout to analysis function |
|
| 248 | 5x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 249 | 5x |
formals(a_survival_subgroups) <- c(formals(a_survival_subgroups), extra_args[[".additional_fun_parameters"]]) |
| 250 | ||
| 251 |
# Create "ci" column from "lcl" and "ucl" |
|
| 252 | 5x |
df$ci <- combine_vectors(df$lcl, df$ucl) |
| 253 | ||
| 254 | 5x |
df_subs <- split(df, f = df$biomarker) |
| 255 | 5x |
tbls <- lapply( |
| 256 | 5x |
df_subs, |
| 257 | 5x |
function(df) {
|
| 258 | 9x |
lyt <- basic_table() |
| 259 | ||
| 260 |
# Split cols by the multiple variables to populate into columns. |
|
| 261 | 9x |
lyt <- split_cols_by_multivar( |
| 262 | 9x |
lyt = lyt, |
| 263 | 9x |
vars = colvars$vars, |
| 264 | 9x |
varlabels = colvars$labels |
| 265 |
) |
|
| 266 | ||
| 267 |
# Row split by biomarker |
|
| 268 | 9x |
lyt <- split_rows_by( |
| 269 | 9x |
lyt = lyt, |
| 270 | 9x |
var = "biomarker_label", |
| 271 | 9x |
nested = FALSE |
| 272 |
) |
|
| 273 | ||
| 274 |
# Add "All Patients" row |
|
| 275 | 9x |
lyt <- split_rows_by( |
| 276 | 9x |
lyt = lyt, |
| 277 | 9x |
var = "row_type", |
| 278 | 9x |
split_fun = keep_split_levels("content"),
|
| 279 | 9x |
nested = TRUE, |
| 280 | 9x |
child_labels = "hidden" |
| 281 |
) |
|
| 282 | 9x |
lyt <- analyze_colvars( |
| 283 | 9x |
lyt = lyt, |
| 284 | 9x |
afun = a_survival_subgroups, |
| 285 | 9x |
na_str = na_str, |
| 286 | 9x |
extra_args = c(extra_args, overall = TRUE) |
| 287 |
) |
|
| 288 | ||
| 289 |
# Add analysis rows |
|
| 290 | 9x |
if ("analysis" %in% df$row_type) {
|
| 291 | 6x |
lyt <- split_rows_by( |
| 292 | 6x |
lyt = lyt, |
| 293 | 6x |
var = "row_type", |
| 294 | 6x |
split_fun = keep_split_levels("analysis"),
|
| 295 | 6x |
nested = TRUE, |
| 296 | 6x |
child_labels = "hidden" |
| 297 |
) |
|
| 298 | 6x |
lyt <- split_rows_by( |
| 299 | 6x |
lyt = lyt, |
| 300 | 6x |
var = "var_label", |
| 301 | 6x |
nested = TRUE, |
| 302 | 6x |
indent_mod = 1L |
| 303 |
) |
|
| 304 | 6x |
lyt <- analyze_colvars( |
| 305 | 6x |
lyt = lyt, |
| 306 | 6x |
afun = a_survival_subgroups, |
| 307 | 6x |
na_str = na_str, |
| 308 | 6x |
inclNAs = TRUE, |
| 309 | 6x |
extra_args = extra_args |
| 310 |
) |
|
| 311 |
} |
|
| 312 | 9x |
build_table(lyt, df = df) |
| 313 |
} |
|
| 314 |
) |
|
| 315 | ||
| 316 | 5x |
result <- do.call(rbind, tbls) |
| 317 | ||
| 318 | 5x |
n_tot_ids <- grep("^n_tot", vars)
|
| 319 | 5x |
hr_id <- match("hr", vars)
|
| 320 | 5x |
ci_id <- match("ci", vars)
|
| 321 | 5x |
structure( |
| 322 | 5x |
result, |
| 323 | 5x |
forest_header = paste0(c("Higher", "Lower"), "\nBetter"),
|
| 324 | 5x |
col_x = hr_id, |
| 325 | 5x |
col_ci = ci_id, |
| 326 | 5x |
col_symbol_size = n_tot_ids[1] |
| 327 |
) |
|
| 328 |
} |
| 1 |
#' Control function for Cox regression |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Sets a list of parameters for Cox regression fit. Used internally. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams argument_convention |
|
| 8 |
#' @param pval_method (`string`)\cr the method used for estimation of p.values; `wald` (default) or `likelihood`. |
|
| 9 |
#' @param interaction (`flag`)\cr if `TRUE`, the model includes the interaction between the studied |
|
| 10 |
#' treatment and candidate covariate. Note that for univariate models without treatment arm, and |
|
| 11 |
#' multivariate models, no interaction can be used so that this needs to be `FALSE`. |
|
| 12 |
#' @param ties (`string`)\cr among `exact` (equivalent to `DISCRETE` in SAS), `efron` and `breslow`, |
|
| 13 |
#' see [survival::coxph()]. Note: there is no equivalent of SAS `EXACT` method in R. |
|
| 14 |
#' |
|
| 15 |
#' @return A `list` of items with names corresponding to the arguments. |
|
| 16 |
#' |
|
| 17 |
#' @seealso [fit_coxreg_univar()] and [fit_coxreg_multivar()]. |
|
| 18 |
#' |
|
| 19 |
#' @examples |
|
| 20 |
#' control_coxreg() |
|
| 21 |
#' |
|
| 22 |
#' @export |
|
| 23 |
control_coxreg <- function(pval_method = c("wald", "likelihood"),
|
|
| 24 |
ties = c("exact", "efron", "breslow"),
|
|
| 25 |
conf_level = 0.95, |
|
| 26 |
interaction = FALSE) {
|
|
| 27 | 55x |
pval_method <- match.arg(pval_method) |
| 28 | 55x |
ties <- match.arg(ties) |
| 29 | 55x |
checkmate::assert_flag(interaction) |
| 30 | 55x |
assert_proportion_value(conf_level) |
| 31 | 55x |
list( |
| 32 | 55x |
pval_method = pval_method, |
| 33 | 55x |
ties = ties, |
| 34 | 55x |
conf_level = conf_level, |
| 35 | 55x |
interaction = interaction |
| 36 |
) |
|
| 37 |
} |
|
| 38 | ||
| 39 |
#' Custom tidy methods for Cox regression |
|
| 40 |
#' |
|
| 41 |
#' @description `r lifecycle::badge("stable")`
|
|
| 42 |
#' |
|
| 43 |
#' @inheritParams argument_convention |
|
| 44 |
#' @param x (`list`)\cr result of the Cox regression model fitted by [fit_coxreg_univar()] (for univariate models) |
|
| 45 |
#' or [fit_coxreg_multivar()] (for multivariate models). |
|
| 46 |
#' |
|
| 47 |
#' @return [broom::tidy()] returns: |
|
| 48 |
#' * For `summary.coxph` objects, a `data.frame` with columns: `Pr(>|z|)`, `exp(coef)`, `exp(-coef)`, `lower .95`, |
|
| 49 |
#' `upper .95`, `level`, and `n`. |
|
| 50 |
#' * For `coxreg.univar` objects, a `data.frame` with columns: `effect`, `term`, `term_label`, `level`, `n`, `hr`, |
|
| 51 |
#' `lcl`, `ucl`, `pval`, and `ci`. |
|
| 52 |
#' * For `coxreg.multivar` objects, a `data.frame` with columns: `term`, `pval`, `term_label`, `hr`, `lcl`, `ucl`, |
|
| 53 |
#' `level`, and `ci`. |
|
| 54 |
#' |
|
| 55 |
#' @seealso [cox_regression] |
|
| 56 |
#' |
|
| 57 |
#' @name tidy_coxreg |
|
| 58 |
NULL |
|
| 59 | ||
| 60 |
#' @describeIn tidy_coxreg Custom tidy method for [survival::coxph()] summary results. |
|
| 61 |
#' |
|
| 62 |
#' Tidy the [survival::coxph()] results into a `data.frame` to extract model results. |
|
| 63 |
#' |
|
| 64 |
#' @method tidy summary.coxph |
|
| 65 |
#' |
|
| 66 |
#' @examples |
|
| 67 |
#' library(survival) |
|
| 68 |
#' library(broom) |
|
| 69 |
#' |
|
| 70 |
#' set.seed(1, kind = "Mersenne-Twister") |
|
| 71 |
#' |
|
| 72 |
#' dta_bladder <- with( |
|
| 73 |
#' data = bladder[bladder$enum < 5, ], |
|
| 74 |
#' data.frame( |
|
| 75 |
#' time = stop, |
|
| 76 |
#' status = event, |
|
| 77 |
#' armcd = as.factor(rx), |
|
| 78 |
#' covar1 = as.factor(enum), |
|
| 79 |
#' covar2 = factor( |
|
| 80 |
#' sample(as.factor(enum)), |
|
| 81 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
| 82 |
#' ) |
|
| 83 |
#' ) |
|
| 84 |
#' ) |
|
| 85 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
| 86 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels |
|
| 87 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE) |
|
| 88 |
#' |
|
| 89 |
#' formula <- "survival::Surv(time, status) ~ armcd + covar1" |
|
| 90 |
#' msum <- summary(coxph(stats::as.formula(formula), data = dta_bladder)) |
|
| 91 |
#' tidy(msum) |
|
| 92 |
#' |
|
| 93 |
#' @export |
|
| 94 |
tidy.summary.coxph <- function(x, # nolint |
|
| 95 |
...) {
|
|
| 96 | 199x |
checkmate::assert_class(x, "summary.coxph") |
| 97 | 199x |
pval <- x$coefficients |
| 98 | 199x |
confint <- x$conf.int |
| 99 | 199x |
levels <- rownames(pval) |
| 100 | ||
| 101 | 199x |
pval <- tibble::as_tibble(pval) |
| 102 | 199x |
confint <- tibble::as_tibble(confint) |
| 103 | ||
| 104 | 199x |
ret <- cbind(pval[, grepl("Pr", names(pval))], confint)
|
| 105 | 199x |
ret$level <- levels |
| 106 | 199x |
ret$n <- x[["n"]] |
| 107 | 199x |
ret |
| 108 |
} |
|
| 109 | ||
| 110 |
#' @describeIn tidy_coxreg Custom tidy method for a univariate Cox regression. |
|
| 111 |
#' |
|
| 112 |
#' Tidy up the result of a Cox regression model fitted by [fit_coxreg_univar()]. |
|
| 113 |
#' |
|
| 114 |
#' @method tidy coxreg.univar |
|
| 115 |
#' |
|
| 116 |
#' @examples |
|
| 117 |
#' ## Cox regression: arm + 1 covariate. |
|
| 118 |
#' mod1 <- fit_coxreg_univar( |
|
| 119 |
#' variables = list( |
|
| 120 |
#' time = "time", event = "status", arm = "armcd", |
|
| 121 |
#' covariates = "covar1" |
|
| 122 |
#' ), |
|
| 123 |
#' data = dta_bladder, |
|
| 124 |
#' control = control_coxreg(conf_level = 0.91) |
|
| 125 |
#' ) |
|
| 126 |
#' |
|
| 127 |
#' ## Cox regression: arm + 1 covariate + interaction, 2 candidate covariates. |
|
| 128 |
#' mod2 <- fit_coxreg_univar( |
|
| 129 |
#' variables = list( |
|
| 130 |
#' time = "time", event = "status", arm = "armcd", |
|
| 131 |
#' covariates = c("covar1", "covar2")
|
|
| 132 |
#' ), |
|
| 133 |
#' data = dta_bladder, |
|
| 134 |
#' control = control_coxreg(conf_level = 0.91, interaction = TRUE) |
|
| 135 |
#' ) |
|
| 136 |
#' |
|
| 137 |
#' tidy(mod1) |
|
| 138 |
#' tidy(mod2) |
|
| 139 |
#' |
|
| 140 |
#' @export |
|
| 141 |
tidy.coxreg.univar <- function(x, # nolint |
|
| 142 |
...) {
|
|
| 143 | 38x |
checkmate::assert_class(x, "coxreg.univar") |
| 144 | 38x |
mod <- x$mod |
| 145 | 38x |
vars <- c(x$vars$arm, x$vars$covariates) |
| 146 | 38x |
has_arm <- "arm" %in% names(x$vars) |
| 147 | ||
| 148 | 38x |
result <- if (!has_arm) {
|
| 149 | 5x |
Map( |
| 150 | 5x |
mod = mod, vars = vars, |
| 151 | 5x |
f = function(mod, vars) {
|
| 152 | 6x |
h_coxreg_multivar_extract( |
| 153 | 6x |
var = vars, |
| 154 | 6x |
data = x$data, |
| 155 | 6x |
mod = mod, |
| 156 | 6x |
control = x$control |
| 157 |
) |
|
| 158 |
} |
|
| 159 |
) |
|
| 160 | 38x |
} else if (x$control$interaction) {
|
| 161 | 12x |
Map( |
| 162 | 12x |
mod = mod, covar = vars, |
| 163 | 12x |
f = function(mod, covar) {
|
| 164 | 26x |
h_coxreg_extract_interaction( |
| 165 | 26x |
effect = x$vars$arm, covar = covar, mod = mod, data = x$data, |
| 166 | 26x |
at = x$at, control = x$control |
| 167 |
) |
|
| 168 |
} |
|
| 169 |
) |
|
| 170 |
} else {
|
|
| 171 | 21x |
Map( |
| 172 | 21x |
mod = mod, vars = vars, |
| 173 | 21x |
f = function(mod, vars) {
|
| 174 | 53x |
h_coxreg_univar_extract( |
| 175 | 53x |
effect = x$vars$arm, covar = vars, data = x$data, mod = mod, |
| 176 | 53x |
control = x$control |
| 177 |
) |
|
| 178 |
} |
|
| 179 |
) |
|
| 180 |
} |
|
| 181 | 38x |
result <- do.call(rbind, result) |
| 182 | ||
| 183 | 38x |
result$ci <- Map(lcl = result$lcl, ucl = result$ucl, f = function(lcl, ucl) c(lcl, ucl)) |
| 184 | 38x |
result$n <- lapply(result$n, empty_vector_if_na) |
| 185 | 38x |
result$ci <- lapply(result$ci, empty_vector_if_na) |
| 186 | 38x |
result$hr <- lapply(result$hr, empty_vector_if_na) |
| 187 | 38x |
if (x$control$interaction) {
|
| 188 | 12x |
result$pval_inter <- lapply(result$pval_inter, empty_vector_if_na) |
| 189 |
# Remove interaction p-values due to change in specifications. |
|
| 190 | 12x |
result$pval[result$effect != "Treatment:"] <- NA |
| 191 |
} |
|
| 192 | 38x |
result$pval <- lapply(result$pval, empty_vector_if_na) |
| 193 | 38x |
attr(result, "conf_level") <- x$control$conf_level |
| 194 | 38x |
result |
| 195 |
} |
|
| 196 | ||
| 197 |
#' @describeIn tidy_coxreg Custom tidy method for a multivariate Cox regression. |
|
| 198 |
#' |
|
| 199 |
#' Tidy up the result of a Cox regression model fitted by [fit_coxreg_multivar()]. |
|
| 200 |
#' |
|
| 201 |
#' @method tidy coxreg.multivar |
|
| 202 |
#' |
|
| 203 |
#' @examples |
|
| 204 |
#' multivar_model <- fit_coxreg_multivar( |
|
| 205 |
#' variables = list( |
|
| 206 |
#' time = "time", event = "status", arm = "armcd", |
|
| 207 |
#' covariates = c("covar1", "covar2")
|
|
| 208 |
#' ), |
|
| 209 |
#' data = dta_bladder |
|
| 210 |
#' ) |
|
| 211 |
#' broom::tidy(multivar_model) |
|
| 212 |
#' |
|
| 213 |
#' @export |
|
| 214 |
tidy.coxreg.multivar <- function(x, # nolint |
|
| 215 |
...) {
|
|
| 216 | 16x |
checkmate::assert_class(x, "coxreg.multivar") |
| 217 | 16x |
vars <- c(x$vars$arm, x$vars$covariates) |
| 218 | ||
| 219 |
# Convert the model summaries to data. |
|
| 220 | 16x |
result <- Map( |
| 221 | 16x |
vars = vars, |
| 222 | 16x |
f = function(vars) {
|
| 223 | 60x |
h_coxreg_multivar_extract( |
| 224 | 60x |
var = vars, data = x$data, |
| 225 | 60x |
mod = x$mod, control = x$control |
| 226 |
) |
|
| 227 |
} |
|
| 228 |
) |
|
| 229 | 16x |
result <- do.call(rbind, result) |
| 230 | ||
| 231 | 16x |
result$ci <- Map(lcl = result$lcl, ucl = result$ucl, f = function(lcl, ucl) c(lcl, ucl)) |
| 232 | 16x |
result$ci <- lapply(result$ci, empty_vector_if_na) |
| 233 | 16x |
result$hr <- lapply(result$hr, empty_vector_if_na) |
| 234 | 16x |
result$pval <- lapply(result$pval, empty_vector_if_na) |
| 235 | 16x |
result <- result[, names(result) != "n"] |
| 236 | 16x |
attr(result, "conf_level") <- x$control$conf_level |
| 237 | ||
| 238 | 16x |
result |
| 239 |
} |
|
| 240 | ||
| 241 |
#' Fitting functions for Cox proportional hazards regression |
|
| 242 |
#' |
|
| 243 |
#' @description `r lifecycle::badge("stable")`
|
|
| 244 |
#' |
|
| 245 |
#' Fitting functions for univariate and multivariate Cox regression models. |
|
| 246 |
#' |
|
| 247 |
#' @param variables (named `list`)\cr the names of the variables found in `data`, passed as a named list and |
|
| 248 |
#' corresponding to the `time`, `event`, `arm`, `strata`, and `covariates` terms. If `arm` is missing from |
|
| 249 |
#' `variables`, then only Cox model(s) including the `covariates` will be fitted and the corresponding effect |
|
| 250 |
#' estimates will be tabulated later. |
|
| 251 |
#' @param data (`data.frame`)\cr the dataset containing the variables to fit the models. |
|
| 252 |
#' @param at (`list` of `numeric`)\cr when the candidate covariate is a `numeric`, use `at` to specify |
|
| 253 |
#' the value of the covariate at which the effect should be estimated. |
|
| 254 |
#' @param control (`list`)\cr a list of parameters as returned by the helper function [control_coxreg()]. |
|
| 255 |
#' |
|
| 256 |
#' @seealso [h_cox_regression] for relevant helper functions, [cox_regression]. |
|
| 257 |
#' |
|
| 258 |
#' @examples |
|
| 259 |
#' library(survival) |
|
| 260 |
#' |
|
| 261 |
#' set.seed(1, kind = "Mersenne-Twister") |
|
| 262 |
#' |
|
| 263 |
#' # Testing dataset [survival::bladder]. |
|
| 264 |
#' dta_bladder <- with( |
|
| 265 |
#' data = bladder[bladder$enum < 5, ], |
|
| 266 |
#' data.frame( |
|
| 267 |
#' time = stop, |
|
| 268 |
#' status = event, |
|
| 269 |
#' armcd = as.factor(rx), |
|
| 270 |
#' covar1 = as.factor(enum), |
|
| 271 |
#' covar2 = factor( |
|
| 272 |
#' sample(as.factor(enum)), |
|
| 273 |
#' levels = 1:4, labels = c("F", "F", "M", "M")
|
|
| 274 |
#' ) |
|
| 275 |
#' ) |
|
| 276 |
#' ) |
|
| 277 |
#' labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
|
|
| 278 |
#' formatters::var_labels(dta_bladder)[names(labels)] <- labels |
|
| 279 |
#' dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE) |
|
| 280 |
#' |
|
| 281 |
#' plot( |
|
| 282 |
#' survfit(Surv(time, status) ~ armcd + covar1, data = dta_bladder), |
|
| 283 |
#' lty = 2:4, |
|
| 284 |
#' xlab = "Months", |
|
| 285 |
#' col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
|
|
| 286 |
#' ) |
|
| 287 |
#' |
|
| 288 |
#' @name fit_coxreg |
|
| 289 |
NULL |
|
| 290 | ||
| 291 |
#' @describeIn fit_coxreg Fit a series of univariate Cox regression models given the inputs. |
|
| 292 |
#' |
|
| 293 |
#' @return |
|
| 294 |
#' * `fit_coxreg_univar()` returns a `coxreg.univar` class object which is a named `list` |
|
| 295 |
#' with 5 elements: |
|
| 296 |
#' * `mod`: Cox regression models fitted by [survival::coxph()]. |
|
| 297 |
#' * `data`: The original data frame input. |
|
| 298 |
#' * `control`: The original control input. |
|
| 299 |
#' * `vars`: The variables used in the model. |
|
| 300 |
#' * `at`: Value of the covariate at which the effect should be estimated. |
|
| 301 |
#' |
|
| 302 |
#' @note When using `fit_coxreg_univar` there should be two study arms. |
|
| 303 |
#' |
|
| 304 |
#' @examples |
|
| 305 |
#' # fit_coxreg_univar |
|
| 306 |
#' |
|
| 307 |
#' ## Cox regression: arm + 1 covariate. |
|
| 308 |
#' mod1 <- fit_coxreg_univar( |
|
| 309 |
#' variables = list( |
|
| 310 |
#' time = "time", event = "status", arm = "armcd", |
|
| 311 |
#' covariates = "covar1" |
|
| 312 |
#' ), |
|
| 313 |
#' data = dta_bladder, |
|
| 314 |
#' control = control_coxreg(conf_level = 0.91) |
|
| 315 |
#' ) |
|
| 316 |
#' |
|
| 317 |
#' ## Cox regression: arm + 1 covariate + interaction, 2 candidate covariates. |
|
| 318 |
#' mod2 <- fit_coxreg_univar( |
|
| 319 |
#' variables = list( |
|
| 320 |
#' time = "time", event = "status", arm = "armcd", |
|
| 321 |
#' covariates = c("covar1", "covar2")
|
|
| 322 |
#' ), |
|
| 323 |
#' data = dta_bladder, |
|
| 324 |
#' control = control_coxreg(conf_level = 0.91, interaction = TRUE) |
|
| 325 |
#' ) |
|
| 326 |
#' |
|
| 327 |
#' ## Cox regression: arm + 1 covariate, stratified analysis. |
|
| 328 |
#' mod3 <- fit_coxreg_univar( |
|
| 329 |
#' variables = list( |
|
| 330 |
#' time = "time", event = "status", arm = "armcd", strata = "covar2", |
|
| 331 |
#' covariates = c("covar1")
|
|
| 332 |
#' ), |
|
| 333 |
#' data = dta_bladder, |
|
| 334 |
#' control = control_coxreg(conf_level = 0.91) |
|
| 335 |
#' ) |
|
| 336 |
#' |
|
| 337 |
#' ## Cox regression: no arm, only covariates. |
|
| 338 |
#' mod4 <- fit_coxreg_univar( |
|
| 339 |
#' variables = list( |
|
| 340 |
#' time = "time", event = "status", |
|
| 341 |
#' covariates = c("covar1", "covar2")
|
|
| 342 |
#' ), |
|
| 343 |
#' data = dta_bladder |
|
| 344 |
#' ) |
|
| 345 |
#' |
|
| 346 |
#' @export |
|
| 347 |
fit_coxreg_univar <- function(variables, |
|
| 348 |
data, |
|
| 349 |
at = list(), |
|
| 350 |
control = control_coxreg()) {
|
|
| 351 | 43x |
checkmate::assert_list(variables, names = "named") |
| 352 | 43x |
has_arm <- "arm" %in% names(variables) |
| 353 | 43x |
arm_name <- if (has_arm) "arm" else NULL |
| 354 | ||
| 355 | 43x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
| 356 | ||
| 357 | 43x |
assert_df_with_variables(data, variables) |
| 358 | 43x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
| 359 | ||
| 360 | 43x |
if (!is.null(variables$strata)) {
|
| 361 | 4x |
checkmate::assert_disjunct(control$pval_method, "likelihood") |
| 362 |
} |
|
| 363 | 42x |
if (has_arm) {
|
| 364 | 36x |
assert_df_with_factors(data, list(val = variables$arm), min.levels = 2, max.levels = 2) |
| 365 |
} |
|
| 366 | 41x |
vars <- unlist(variables[c(arm_name, "covariates", "strata")], use.names = FALSE) |
| 367 | 41x |
for (i in vars) {
|
| 368 | 94x |
if (is.factor(data[[i]])) {
|
| 369 | 82x |
attr(data[[i]], "levels") <- levels(droplevels(data[[i]])) |
| 370 |
} |
|
| 371 |
} |
|
| 372 | 41x |
forms <- h_coxreg_univar_formulas(variables, interaction = control$interaction) |
| 373 | 41x |
mod <- lapply( |
| 374 | 41x |
forms, function(x) {
|
| 375 | 90x |
survival::coxph(formula = stats::as.formula(x), data = data, ties = control$ties) |
| 376 |
} |
|
| 377 |
) |
|
| 378 | 41x |
structure( |
| 379 | 41x |
list( |
| 380 | 41x |
mod = mod, |
| 381 | 41x |
data = data, |
| 382 | 41x |
control = control, |
| 383 | 41x |
vars = variables, |
| 384 | 41x |
at = at |
| 385 |
), |
|
| 386 | 41x |
class = "coxreg.univar" |
| 387 |
) |
|
| 388 |
} |
|
| 389 | ||
| 390 |
#' @describeIn fit_coxreg Fit a multivariate Cox regression model. |
|
| 391 |
#' |
|
| 392 |
#' @return |
|
| 393 |
#' * `fit_coxreg_multivar()` returns a `coxreg.multivar` class object which is a named list |
|
| 394 |
#' with 4 elements: |
|
| 395 |
#' * `mod`: Cox regression model fitted by [survival::coxph()]. |
|
| 396 |
#' * `data`: The original data frame input. |
|
| 397 |
#' * `control`: The original control input. |
|
| 398 |
#' * `vars`: The variables used in the model. |
|
| 399 |
#' |
|
| 400 |
#' @examples |
|
| 401 |
#' # fit_coxreg_multivar |
|
| 402 |
#' |
|
| 403 |
#' ## Cox regression: multivariate Cox regression. |
|
| 404 |
#' multivar_model <- fit_coxreg_multivar( |
|
| 405 |
#' variables = list( |
|
| 406 |
#' time = "time", event = "status", arm = "armcd", |
|
| 407 |
#' covariates = c("covar1", "covar2")
|
|
| 408 |
#' ), |
|
| 409 |
#' data = dta_bladder |
|
| 410 |
#' ) |
|
| 411 |
#' |
|
| 412 |
#' # Example without treatment arm. |
|
| 413 |
#' multivar_covs_model <- fit_coxreg_multivar( |
|
| 414 |
#' variables = list( |
|
| 415 |
#' time = "time", event = "status", |
|
| 416 |
#' covariates = c("covar1", "covar2")
|
|
| 417 |
#' ), |
|
| 418 |
#' data = dta_bladder |
|
| 419 |
#' ) |
|
| 420 |
#' |
|
| 421 |
#' @export |
|
| 422 |
fit_coxreg_multivar <- function(variables, |
|
| 423 |
data, |
|
| 424 |
control = control_coxreg()) {
|
|
| 425 | 83x |
checkmate::assert_list(variables, names = "named") |
| 426 | 83x |
has_arm <- "arm" %in% names(variables) |
| 427 | 83x |
arm_name <- if (has_arm) "arm" else NULL |
| 428 | ||
| 429 | 83x |
if (!is.null(variables$covariates)) {
|
| 430 | 21x |
checkmate::assert_character(variables$covariates) |
| 431 |
} |
|
| 432 | ||
| 433 | 83x |
checkmate::assert_false(control$interaction) |
| 434 | 83x |
assert_df_with_variables(data, variables) |
| 435 | 83x |
assert_list_of_variables(variables[c(arm_name, "event", "time")]) |
| 436 | ||
| 437 | 83x |
if (!is.null(variables$strata)) {
|
| 438 | 3x |
checkmate::assert_disjunct(control$pval_method, "likelihood") |
| 439 |
} |
|
| 440 | ||
| 441 | 82x |
form <- h_coxreg_multivar_formula(variables) |
| 442 | 82x |
mod <- survival::coxph( |
| 443 | 82x |
formula = stats::as.formula(form), |
| 444 | 82x |
data = data, |
| 445 | 82x |
ties = control$ties |
| 446 |
) |
|
| 447 | 82x |
structure( |
| 448 | 82x |
list( |
| 449 | 82x |
mod = mod, |
| 450 | 82x |
data = data, |
| 451 | 82x |
control = control, |
| 452 | 82x |
vars = variables |
| 453 |
), |
|
| 454 | 82x |
class = "coxreg.multivar" |
| 455 |
) |
|
| 456 |
} |
|
| 457 | ||
| 458 |
#' Muffled `car::Anova` |
|
| 459 |
#' |
|
| 460 |
#' Applied on survival models, [car::Anova()] signal that the `strata` terms is dropped from the model formula when |
|
| 461 |
#' present, this function deliberately muffles this message. |
|
| 462 |
#' |
|
| 463 |
#' @param mod (`coxph`)\cr Cox regression model fitted by [survival::coxph()]. |
|
| 464 |
#' @param test_statistic (`string`)\cr the method used for estimation of p.values; `wald` (default) or `likelihood`. |
|
| 465 |
#' |
|
| 466 |
#' @return The output of [car::Anova()], with convergence message muffled. |
|
| 467 |
#' |
|
| 468 |
#' @keywords internal |
|
| 469 |
muffled_car_anova <- function(mod, test_statistic) {
|
|
| 470 | 219x |
tryCatch( |
| 471 | 219x |
withCallingHandlers( |
| 472 | 219x |
expr = {
|
| 473 | 219x |
car::Anova( |
| 474 | 219x |
mod, |
| 475 | 219x |
test.statistic = test_statistic, |
| 476 | 219x |
type = "III" |
| 477 |
) |
|
| 478 |
}, |
|
| 479 | 219x |
message = function(m) invokeRestart("muffleMessage"),
|
| 480 | 219x |
error = function(e) {
|
| 481 | 1x |
stop(paste( |
| 482 | 1x |
"the model seems to have convergence problems, please try to change", |
| 483 | 1x |
"the configuration of covariates or strata variables, e.g.", |
| 484 | 1x |
"- original error:", e |
| 485 |
)) |
|
| 486 |
} |
|
| 487 |
) |
|
| 488 |
) |
|
| 489 |
} |
| 1 |
#' Helper function to create a map data frame for `trim_levels_to_map()` |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Helper function to create a map data frame from the input dataset, which can be used as an argument in the |
|
| 6 |
#' `trim_levels_to_map` split function. Based on different method, the map is constructed differently. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams argument_convention |
|
| 9 |
#' @param abnormal (named `list`)\cr identifying the abnormal range level(s) in `df`. Based on the levels of |
|
| 10 |
#' abnormality of the input dataset, it can be something like `list(Low = "LOW LOW", High = "HIGH HIGH")` or |
|
| 11 |
#' `abnormal = list(Low = "LOW", High = "HIGH"))` |
|
| 12 |
#' @param method (`string`)\cr indicates how the returned map will be constructed. Can be `"default"` or `"range"`. |
|
| 13 |
#' |
|
| 14 |
#' @return A map `data.frame`. |
|
| 15 |
#' |
|
| 16 |
#' @note If method is `"default"`, the returned map will only have the abnormal directions that are observed in the |
|
| 17 |
#' `df`, and records with all normal values will be excluded to avoid error in creating layout. If method is |
|
| 18 |
#' `"range"`, the returned map will be based on the rule that at least one observation with low range > 0 |
|
| 19 |
#' for low direction and at least one observation with high range is not missing for high direction. |
|
| 20 |
#' |
|
| 21 |
#' @examples |
|
| 22 |
#' adlb <- df_explicit_na(tern_ex_adlb) |
|
| 23 |
#' |
|
| 24 |
#' h_map_for_count_abnormal( |
|
| 25 |
#' df = adlb, |
|
| 26 |
#' variables = list(anl = "ANRIND", split_rows = c("LBCAT", "PARAM")),
|
|
| 27 |
#' abnormal = list(low = c("LOW"), high = c("HIGH")),
|
|
| 28 |
#' method = "default", |
|
| 29 |
#' na_str = "<Missing>" |
|
| 30 |
#' ) |
|
| 31 |
#' |
|
| 32 |
#' df <- data.frame( |
|
| 33 |
#' USUBJID = c(rep("1", 4), rep("2", 4), rep("3", 4)),
|
|
| 34 |
#' AVISIT = c( |
|
| 35 |
#' rep("WEEK 1", 2),
|
|
| 36 |
#' rep("WEEK 2", 2),
|
|
| 37 |
#' rep("WEEK 1", 2),
|
|
| 38 |
#' rep("WEEK 2", 2),
|
|
| 39 |
#' rep("WEEK 1", 2),
|
|
| 40 |
#' rep("WEEK 2", 2)
|
|
| 41 |
#' ), |
|
| 42 |
#' PARAM = rep(c("ALT", "CPR"), 6),
|
|
| 43 |
#' ANRIND = c( |
|
| 44 |
#' "NORMAL", "NORMAL", "LOW", |
|
| 45 |
#' "HIGH", "LOW", "LOW", "HIGH", "HIGH", rep("NORMAL", 4)
|
|
| 46 |
#' ), |
|
| 47 |
#' ANRLO = rep(5, 12), |
|
| 48 |
#' ANRHI = rep(20, 12) |
|
| 49 |
#' ) |
|
| 50 |
#' df$ANRIND <- factor(df$ANRIND, levels = c("LOW", "HIGH", "NORMAL"))
|
|
| 51 |
#' h_map_for_count_abnormal( |
|
| 52 |
#' df = df, |
|
| 53 |
#' variables = list( |
|
| 54 |
#' anl = "ANRIND", |
|
| 55 |
#' split_rows = c("PARAM"),
|
|
| 56 |
#' range_low = "ANRLO", |
|
| 57 |
#' range_high = "ANRHI" |
|
| 58 |
#' ), |
|
| 59 |
#' abnormal = list(low = c("LOW"), high = c("HIGH")),
|
|
| 60 |
#' method = "range", |
|
| 61 |
#' na_str = "<Missing>" |
|
| 62 |
#' ) |
|
| 63 |
#' |
|
| 64 |
#' @export |
|
| 65 |
h_map_for_count_abnormal <- function(df, |
|
| 66 |
variables = list( |
|
| 67 |
anl = "ANRIND", |
|
| 68 |
split_rows = c("PARAM"),
|
|
| 69 |
range_low = "ANRLO", |
|
| 70 |
range_high = "ANRHI" |
|
| 71 |
), |
|
| 72 |
abnormal = list(low = c("LOW", "LOW LOW"), high = c("HIGH", "HIGH HIGH")),
|
|
| 73 |
method = c("default", "range"),
|
|
| 74 |
na_str = "<Missing>") {
|
|
| 75 | 7x |
method <- match.arg(method) |
| 76 | 7x |
checkmate::assert_subset(c("anl", "split_rows"), names(variables))
|
| 77 | 7x |
checkmate::assert_false(anyNA(df[variables$split_rows])) |
| 78 | 7x |
assert_df_with_variables(df, |
| 79 | 7x |
variables = list(anl = variables$anl, split_rows = variables$split_rows), |
| 80 | 7x |
na_level = na_str |
| 81 |
) |
|
| 82 | 7x |
assert_df_with_factors(df, list(val = variables$anl)) |
| 83 | 7x |
assert_valid_factor(df[[variables$anl]], any.missing = FALSE) |
| 84 | 7x |
assert_list_of_variables(variables) |
| 85 | 7x |
checkmate::assert_list(abnormal, types = "character", len = 2) |
| 86 | ||
| 87 |
# Drop usued levels from df as they are not supposed to be in the final map |
|
| 88 | 7x |
df <- droplevels(df) |
| 89 | ||
| 90 | 7x |
normal_value <- setdiff(levels(df[[variables$anl]]), unlist(abnormal)) |
| 91 | ||
| 92 |
# Based on the understanding of clinical data, there should only be one level of normal which is "NORMAL" |
|
| 93 | 7x |
checkmate::assert_vector(normal_value, len = 1) |
| 94 | ||
| 95 |
# Default method will only have what is observed in the df, and records with all normal values will be excluded to |
|
| 96 |
# avoid error in layout building. |
|
| 97 | 7x |
if (method == "default") {
|
| 98 | 3x |
df_abnormal <- subset(df, df[[variables$anl]] %in% unlist(abnormal)) |
| 99 | 3x |
map <- unique(df_abnormal[c(variables$split_rows, variables$anl)]) |
| 100 | 3x |
map_normal <- unique(subset(map, select = variables$split_rows)) |
| 101 | 3x |
map_normal[[variables$anl]] <- normal_value |
| 102 | 3x |
map <- rbind(map, map_normal) |
| 103 | 4x |
} else if (method == "range") {
|
| 104 |
# range method follows the rule that at least one observation with ANRLO > 0 for low |
|
| 105 |
# direction and at least one observation with ANRHI is not missing for high direction. |
|
| 106 | 4x |
checkmate::assert_subset(c("range_low", "range_high"), names(variables))
|
| 107 | 4x |
checkmate::assert_subset(c("LOW", "HIGH"), toupper(names(abnormal)))
|
| 108 | ||
| 109 | 4x |
assert_df_with_variables(df, |
| 110 | 4x |
variables = list( |
| 111 | 4x |
range_low = variables$range_low, |
| 112 | 4x |
range_high = variables$range_high |
| 113 |
) |
|
| 114 |
) |
|
| 115 | ||
| 116 |
# Define low direction of map |
|
| 117 | 4x |
df_low <- subset(df, df[[variables$range_low]] > 0) |
| 118 | 4x |
map_low <- unique(df_low[variables$split_rows]) |
| 119 | 4x |
low_levels <- unname(unlist(abnormal[toupper(names(abnormal)) == "LOW"])) |
| 120 | 4x |
low_levels_df <- as.data.frame(low_levels) |
| 121 | 4x |
colnames(low_levels_df) <- variables$anl |
| 122 | 4x |
low_levels_df <- do.call("rbind", replicate(nrow(map_low), low_levels_df, simplify = FALSE))
|
| 123 | 4x |
rownames(map_low) <- NULL # Just to avoid strange row index in case upstream functions changed |
| 124 | 4x |
map_low <- map_low[rep(seq_len(nrow(map_low)), each = length(low_levels)), , drop = FALSE] |
| 125 | 4x |
map_low <- cbind(map_low, low_levels_df) |
| 126 | ||
| 127 |
# Define high direction of map |
|
| 128 | 4x |
df_high <- subset(df, df[[variables$range_high]] != na_str | !is.na(df[[variables$range_high]])) |
| 129 | 4x |
map_high <- unique(df_high[variables$split_rows]) |
| 130 | 4x |
high_levels <- unname(unlist(abnormal[toupper(names(abnormal)) == "HIGH"])) |
| 131 | 4x |
high_levels_df <- as.data.frame(high_levels) |
| 132 | 4x |
colnames(high_levels_df) <- variables$anl |
| 133 | 4x |
high_levels_df <- do.call("rbind", replicate(nrow(map_high), high_levels_df, simplify = FALSE))
|
| 134 | 4x |
rownames(map_high) <- NULL |
| 135 | 4x |
map_high <- map_high[rep(seq_len(nrow(map_high)), each = length(high_levels)), , drop = FALSE] |
| 136 | 4x |
map_high <- cbind(map_high, high_levels_df) |
| 137 | ||
| 138 |
# Define normal of map |
|
| 139 | 4x |
map_normal <- unique(rbind(map_low, map_high)[variables$split_rows]) |
| 140 | 4x |
map_normal[variables$anl] <- normal_value |
| 141 | ||
| 142 | 4x |
map <- rbind(map_low, map_high, map_normal) |
| 143 |
} |
|
| 144 | ||
| 145 |
# map should be all characters |
|
| 146 | 7x |
map <- data.frame(lapply(map, as.character), stringsAsFactors = FALSE) |
| 147 | ||
| 148 |
# sort the map final output by split_rows variables |
|
| 149 | 7x |
for (i in rev(seq_len(length(variables$split_rows)))) {
|
| 150 | 7x |
map <- map[order(map[[i]]), ] |
| 151 |
} |
|
| 152 | 7x |
map |
| 153 |
} |
| 1 |
#' Control function for descriptive statistics |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Sets a list of parameters for summaries of descriptive statistics. Typically used internally to specify |
|
| 6 |
#' details for [s_summary()]. This function family is mainly used by [analyze_vars()]. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams argument_convention |
|
| 9 |
#' @param quantiles (`numeric(2)`)\cr vector of length two to specify the quantiles to calculate. |
|
| 10 |
#' @param quantile_type (`numeric(1)`)\cr number between 1 and 9 selecting quantile algorithms to be used. |
|
| 11 |
#' Default is set to 2 as this matches the default quantile algorithm in SAS `proc univariate` set by `QNTLDEF=5`. |
|
| 12 |
#' This differs from R's default. See more about `type` in [stats::quantile()]. |
|
| 13 |
#' @param test_mean (`numeric(1)`)\cr number to test against the mean under the null hypothesis when calculating |
|
| 14 |
#' p-value. |
|
| 15 |
#' |
|
| 16 |
#' @return A list of components with the same names as the arguments. |
|
| 17 |
#' |
|
| 18 |
#' @export |
|
| 19 |
control_analyze_vars <- function(conf_level = 0.95, |
|
| 20 |
quantiles = c(0.25, 0.75), |
|
| 21 |
quantile_type = 2, |
|
| 22 |
test_mean = 0) {
|
|
| 23 | 1098x |
checkmate::assert_vector(quantiles, len = 2) |
| 24 | 1098x |
checkmate::assert_int(quantile_type, lower = 1, upper = 9) |
| 25 | 1098x |
checkmate::assert_numeric(test_mean) |
| 26 | 1098x |
lapply(quantiles, assert_proportion_value) |
| 27 | 1097x |
assert_proportion_value(conf_level) |
| 28 | 1096x |
list(conf_level = conf_level, quantiles = quantiles, quantile_type = quantile_type, test_mean = test_mean) |
| 29 |
} |
|
| 30 | ||
| 31 |
# Helper function to fix numeric or counts pval if necessary |
|
| 32 |
.correct_num_or_counts_pval <- function(type, .stats) {
|
|
| 33 | 332x |
if (type == "numeric") {
|
| 34 | 92x |
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
|
| 35 | 10x |
.stats[grepl("^pval", .stats)] <- "pval" # tmp fix xxx
|
| 36 |
} |
|
| 37 |
} else {
|
|
| 38 | 240x |
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
|
| 39 | 9x |
.stats[grepl("^pval", .stats)] <- "pval_counts" # tmp fix xxx
|
| 40 |
} |
|
| 41 |
} |
|
| 42 | 332x |
.stats |
| 43 |
} |
|
| 44 | ||
| 45 |
#' Analyze variables |
|
| 46 |
#' |
|
| 47 |
#' @description `r lifecycle::badge("stable")`
|
|
| 48 |
#' |
|
| 49 |
#' The analyze function [analyze_vars()] creates a layout element to summarize one or more variables, using the S3 |
|
| 50 |
#' generic function [s_summary()] to calculate a list of summary statistics. A list of all available statistics for |
|
| 51 |
#' numeric variables can be viewed by running `get_stats("analyze_vars_numeric")` and for non-numeric variables by
|
|
| 52 |
#' running `get_stats("analyze_vars_counts")`. Use the `.stats` parameter to specify the statistics to include in your
|
|
| 53 |
#' output summary table. Use `compare_with_ref_group = TRUE` to compare the variable with reference groups. |
|
| 54 |
#' |
|
| 55 |
#' @details |
|
| 56 |
#' **Automatic digit formatting:** The number of digits to display can be automatically determined from the analyzed |
|
| 57 |
#' variable(s) (`vars`) for certain statistics by setting the statistic format to `"auto"` in `.formats`. |
|
| 58 |
#' This utilizes the [format_auto()] formatting function. Note that only data for the current row & variable (for all |
|
| 59 |
#' columns) will be considered (`.df_row[[.var]]`, see [`rtables::additional_fun_params`]) and not the whole dataset. |
|
| 60 |
#' |
|
| 61 |
#' @inheritParams argument_convention |
|
| 62 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 63 |
#' |
|
| 64 |
#' Options for numeric variables are: ``r shQuote(get_stats("analyze_vars_numeric"), type = "sh")``
|
|
| 65 |
#' |
|
| 66 |
#' Options for non-numeric variables are: ``r shQuote(get_stats("analyze_vars_counts"), type = "sh")``
|
|
| 67 |
#' |
|
| 68 |
#' @name analyze_variables |
|
| 69 |
#' @order 1 |
|
| 70 |
NULL |
|
| 71 | ||
| 72 |
#' @describeIn analyze_variables S3 generic function to produces a variable summary. |
|
| 73 |
#' |
|
| 74 |
#' @return |
|
| 75 |
#' * `s_summary()` returns different statistics depending on the class of `x`. |
|
| 76 |
#' |
|
| 77 |
#' @export |
|
| 78 |
s_summary <- function(x, ...) {
|
|
| 79 | 1661x |
UseMethod("s_summary", x)
|
| 80 |
} |
|
| 81 | ||
| 82 |
#' @describeIn analyze_variables Method for `numeric` class. |
|
| 83 |
#' |
|
| 84 |
#' @param control (`list`)\cr parameters for descriptive statistics details, specified by using |
|
| 85 |
#' the helper function [control_analyze_vars()]. Some possible parameter options are: |
|
| 86 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for mean and median. |
|
| 87 |
#' * `quantiles` (`numeric(2)`)\cr vector of length two to specify the quantiles. |
|
| 88 |
#' * `quantile_type` (`numeric(1)`)\cr between 1 and 9 selecting quantile algorithms to be used. |
|
| 89 |
#' See more about `type` in [stats::quantile()]. |
|
| 90 |
#' * `test_mean` (`numeric(1)`)\cr value to test against the mean under the null hypothesis when calculating p-value. |
|
| 91 |
#' |
|
| 92 |
#' @return |
|
| 93 |
#' * If `x` is of class `numeric`, returns a `list` with the following named `numeric` items: |
|
| 94 |
#' * `n`: The [length()] of `x`. |
|
| 95 |
#' * `sum`: The [sum()] of `x`. |
|
| 96 |
#' * `mean`: The [mean()] of `x`. |
|
| 97 |
#' * `sd`: The [stats::sd()] of `x`. |
|
| 98 |
#' * `se`: The standard error of `x` mean, i.e.: (`sd(x) / sqrt(length(x))`). |
|
| 99 |
#' * `mean_sd`: The [mean()] and [stats::sd()] of `x`. |
|
| 100 |
#' * `mean_se`: The [mean()] of `x` and its standard error (see above). |
|
| 101 |
#' * `mean_ci`: The CI for the mean of `x` (from [stat_mean_ci()]). |
|
| 102 |
#' * `mean_sei`: The SE interval for the mean of `x`, i.e.: ([mean()] -/+ [stats::sd()] / [sqrt()]). |
|
| 103 |
#' * `mean_sdi`: The SD interval for the mean of `x`, i.e.: ([mean()] -/+ [stats::sd()]). |
|
| 104 |
#' * `mean_pval`: The two-sided p-value of the mean of `x` (from [stat_mean_pval()]). |
|
| 105 |
#' * `median`: The [stats::median()] of `x`. |
|
| 106 |
#' * `mad`: The median absolute deviation of `x`, i.e.: ([stats::median()] of `xc`, |
|
| 107 |
#' where `xc` = `x` - [stats::median()]). |
|
| 108 |
#' * `median_ci`: The CI for the median of `x` (from [stat_median_ci()]). |
|
| 109 |
#' * `quantiles`: Two sample quantiles of `x` (from [stats::quantile()]). |
|
| 110 |
#' * `iqr`: The [stats::IQR()] of `x`. |
|
| 111 |
#' * `range`: The [range_noinf()] of `x`. |
|
| 112 |
#' * `min`: The [max()] of `x`. |
|
| 113 |
#' * `max`: The [min()] of `x`. |
|
| 114 |
#' * `median_range`: The [median()] and [range_noinf()] of `x`. |
|
| 115 |
#' * `cv`: The coefficient of variation of `x`, i.e.: ([stats::sd()] / [mean()] * 100). |
|
| 116 |
#' * `geom_mean`: The geometric mean of `x`, i.e.: (`exp(mean(log(x)))`). |
|
| 117 |
#' * `geom_cv`: The geometric coefficient of variation of `x`, i.e.: (`sqrt(exp(sd(log(x)) ^ 2) - 1) * 100`). |
|
| 118 |
#' |
|
| 119 |
#' @note |
|
| 120 |
#' * If `x` is an empty vector, `NA` is returned. This is the expected feature so as to return `rcell` content in |
|
| 121 |
#' `rtables` when the intersection of a column and a row delimits an empty data selection. |
|
| 122 |
#' * When the `mean` function is applied to an empty vector, `NA` will be returned instead of `NaN`, the latter |
|
| 123 |
#' being standard behavior in R. |
|
| 124 |
#' |
|
| 125 |
#' @method s_summary numeric |
|
| 126 |
#' |
|
| 127 |
#' @examples |
|
| 128 |
#' # `s_summary.numeric` |
|
| 129 |
#' |
|
| 130 |
#' ## Basic usage: empty numeric returns NA-filled items. |
|
| 131 |
#' s_summary(numeric()) |
|
| 132 |
#' |
|
| 133 |
#' ## Management of NA values. |
|
| 134 |
#' x <- c(NA_real_, 1) |
|
| 135 |
#' s_summary(x, na_rm = TRUE) |
|
| 136 |
#' s_summary(x, na_rm = FALSE) |
|
| 137 |
#' |
|
| 138 |
#' x <- c(NA_real_, 1, 2) |
|
| 139 |
#' s_summary(x) |
|
| 140 |
#' |
|
| 141 |
#' ## Benefits in `rtables` contructions: |
|
| 142 |
#' dta_test <- data.frame( |
|
| 143 |
#' Group = rep(LETTERS[seq(3)], each = 2), |
|
| 144 |
#' sub_group = rep(letters[seq(2)], each = 3), |
|
| 145 |
#' x = seq(6) |
|
| 146 |
#' ) |
|
| 147 |
#' |
|
| 148 |
#' ## The summary obtained in with `rtables`: |
|
| 149 |
#' basic_table() %>% |
|
| 150 |
#' split_cols_by(var = "Group") %>% |
|
| 151 |
#' split_rows_by(var = "sub_group") %>% |
|
| 152 |
#' analyze(vars = "x", afun = s_summary) %>% |
|
| 153 |
#' build_table(df = dta_test) |
|
| 154 |
#' |
|
| 155 |
#' ## By comparison with `lapply`: |
|
| 156 |
#' X <- split(dta_test, f = with(dta_test, interaction(Group, sub_group))) |
|
| 157 |
#' lapply(X, function(x) s_summary(x$x)) |
|
| 158 |
#' |
|
| 159 |
#' @export |
|
| 160 |
s_summary.numeric <- function(x, control = control_analyze_vars(), ...) {
|
|
| 161 | 1143x |
checkmate::assert_numeric(x) |
| 162 | 1143x |
args_list <- list(...) |
| 163 | 1143x |
.N_row <- args_list[[".N_row"]] # nolint |
| 164 | 1143x |
.N_col <- args_list[[".N_col"]] # nolint |
| 165 | 1143x |
na_rm <- args_list[["na_rm"]] %||% TRUE |
| 166 | 1143x |
compare_with_ref_group <- args_list[["compare_with_ref_group"]] |
| 167 | ||
| 168 | 1143x |
if (na_rm) {
|
| 169 | 1141x |
x <- x[!is.na(x)] |
| 170 |
} |
|
| 171 | ||
| 172 | 1143x |
y <- list() |
| 173 | ||
| 174 | 1143x |
y$n <- c("n" = length(x))
|
| 175 | ||
| 176 | 1143x |
y$sum <- c("sum" = ifelse(length(x) == 0, NA_real_, sum(x, na.rm = FALSE)))
|
| 177 | ||
| 178 | 1143x |
y$mean <- c("mean" = ifelse(length(x) == 0, NA_real_, mean(x, na.rm = FALSE)))
|
| 179 | ||
| 180 | 1143x |
y$sd <- c("sd" = stats::sd(x, na.rm = FALSE))
|
| 181 | ||
| 182 | 1143x |
y$se <- c("se" = stats::sd(x, na.rm = FALSE) / sqrt(length(stats::na.omit(x))))
|
| 183 | ||
| 184 | 1143x |
y$mean_sd <- c(y$mean, "sd" = stats::sd(x, na.rm = FALSE)) |
| 185 | ||
| 186 | 1143x |
y$mean_se <- c(y$mean, y$se) |
| 187 | ||
| 188 | 1143x |
mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE) |
| 189 | 1143x |
y$mean_ci <- formatters::with_label(mean_ci, paste("Mean", f_conf_level(control$conf_level)))
|
| 190 | ||
| 191 | 1143x |
mean_sei <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE) / sqrt(y$n) |
| 192 | 1143x |
names(mean_sei) <- c("mean_sei_lwr", "mean_sei_upr")
|
| 193 | 1143x |
y$mean_sei <- formatters::with_label(mean_sei, "Mean -/+ 1xSE") |
| 194 | ||
| 195 | 1143x |
mean_sdi <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE) |
| 196 | 1143x |
names(mean_sdi) <- c("mean_sdi_lwr", "mean_sdi_upr")
|
| 197 | 1143x |
y$mean_sdi <- formatters::with_label(mean_sdi, "Mean -/+ 1xSD") |
| 198 | 1143x |
mean_ci_3d <- c(y$mean, y$mean_ci) |
| 199 | 1143x |
y$mean_ci_3d <- formatters::with_label(mean_ci_3d, paste0("Mean (", f_conf_level(control$conf_level), ")"))
|
| 200 | ||
| 201 | 1143x |
mean_pval <- stat_mean_pval(x, test_mean = control$test_mean, na.rm = FALSE, n_min = 2) |
| 202 | 1143x |
y$mean_pval <- formatters::with_label(mean_pval, paste("Mean", f_pval(control$test_mean)))
|
| 203 | ||
| 204 | 1143x |
y$median <- c("median" = stats::median(x, na.rm = FALSE))
|
| 205 | ||
| 206 | 1143x |
y$mad <- c("mad" = stats::median(x - y$median, na.rm = FALSE))
|
| 207 | ||
| 208 | 1143x |
median_ci <- stat_median_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE) |
| 209 | 1143x |
y$median_ci <- formatters::with_label(median_ci, paste("Median", f_conf_level(control$conf_level)))
|
| 210 | ||
| 211 | 1143x |
median_ci_3d <- c(y$median, median_ci) |
| 212 | 1143x |
y$median_ci_3d <- formatters::with_label(median_ci_3d, paste0("Median (", f_conf_level(control$conf_level), ")"))
|
| 213 | ||
| 214 | 1143x |
q <- control$quantiles |
| 215 | 1143x |
if (any(is.na(x))) {
|
| 216 | 2x |
qnts <- rep(NA_real_, length(q)) |
| 217 |
} else {
|
|
| 218 | 1141x |
qnts <- stats::quantile(x, probs = q, type = control$quantile_type, na.rm = FALSE) |
| 219 |
} |
|
| 220 | 1143x |
names(qnts) <- paste("quantile", q, sep = "_")
|
| 221 | 1143x |
y$quantiles <- formatters::with_label(qnts, paste0(paste(paste0(q * 100, "%"), collapse = " and "), "-ile")) |
| 222 | ||
| 223 | 1143x |
y$iqr <- c("iqr" = ifelse(
|
| 224 | 1143x |
any(is.na(x)), |
| 225 | 1143x |
NA_real_, |
| 226 | 1143x |
stats::IQR(x, na.rm = FALSE, type = control$quantile_type) |
| 227 |
)) |
|
| 228 | ||
| 229 | 1143x |
y$range <- stats::setNames(range_noinf(x, na.rm = FALSE), c("min", "max"))
|
| 230 | 1143x |
y$min <- y$range[1] |
| 231 | 1143x |
y$max <- y$range[2] |
| 232 | ||
| 233 | 1143x |
y$median_range <- formatters::with_label(c(y$median, y$range), "Median (Min - Max)") |
| 234 | ||
| 235 | 1143x |
y$cv <- c("cv" = unname(y$sd) / unname(y$mean) * 100)
|
| 236 | ||
| 237 |
# Geometric Mean - Convert negative values to NA for log calculation. |
|
| 238 | 1143x |
geom_verbose <- args_list[["geom_verbose"]] %||% FALSE # Additional info if requested |
| 239 | 1143x |
checkmate::assert_flag(geom_verbose) |
| 240 | 1143x |
x_no_negative_vals <- x |
| 241 | 1143x |
if (identical(x_no_negative_vals, numeric())) {
|
| 242 | 76x |
x_no_negative_vals <- NA |
| 243 |
} |
|
| 244 | 1143x |
x_no_negative_vals[x_no_negative_vals <= 0] <- NA |
| 245 | 1143x |
if (geom_verbose) {
|
| 246 | 2x |
if (any(x <= 0)) {
|
| 247 | 2x |
warning("Negative values were converted to NA for calculation of the geometric mean.")
|
| 248 |
} |
|
| 249 | 2x |
if (all(is.na(x_no_negative_vals))) {
|
| 250 | 1x |
warning("Since all values are negative or NA, the geometric mean is NA.")
|
| 251 |
} |
|
| 252 |
} |
|
| 253 | 1143x |
y$geom_mean <- c("geom_mean" = exp(mean(log(x_no_negative_vals), na.rm = FALSE)))
|
| 254 | 1143x |
y$geom_sd <- c("geom_sd" = geom_sd <- exp(sd(log(x_no_negative_vals), na.rm = FALSE)))
|
| 255 | 1143x |
y$geom_mean_sd <- c(y$geom_mean, y$geom_sd) |
| 256 | 1143x |
geom_mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE, geom_mean = TRUE) |
| 257 | 1143x |
y$geom_mean_ci <- formatters::with_label(geom_mean_ci, paste("Geometric Mean", f_conf_level(control$conf_level)))
|
| 258 | ||
| 259 | 1143x |
y$geom_cv <- c("geom_cv" = sqrt(exp(stats::sd(log(x_no_negative_vals), na.rm = FALSE) ^ 2) - 1) * 100) # styler: off
|
| 260 | ||
| 261 | 1143x |
geom_mean_ci_3d <- c(y$geom_mean, y$geom_mean_ci) |
| 262 | 1143x |
y$geom_mean_ci_3d <- formatters::with_label( |
| 263 | 1143x |
geom_mean_ci_3d, |
| 264 | 1143x |
paste0("Geometric Mean (", f_conf_level(control$conf_level), ")")
|
| 265 |
) |
|
| 266 | ||
| 267 |
# Compare with reference group |
|
| 268 | 1143x |
if (isTRUE(compare_with_ref_group)) {
|
| 269 | 13x |
.ref_group <- args_list[[".ref_group"]] |
| 270 | 13x |
.in_ref_col <- args_list[[".in_ref_col"]] |
| 271 | 13x |
checkmate::assert_numeric(.ref_group) |
| 272 | 13x |
checkmate::assert_flag(.in_ref_col) |
| 273 | ||
| 274 | 13x |
y$pval <- numeric() |
| 275 | 13x |
if (!.in_ref_col && n_available(x) > 1 && n_available(.ref_group) > 1) {
|
| 276 | 9x |
y$pval <- stats::t.test(x, .ref_group)$p.value |
| 277 |
} |
|
| 278 |
} |
|
| 279 | ||
| 280 | 1143x |
y |
| 281 |
} |
|
| 282 | ||
| 283 |
#' @describeIn analyze_variables Method for `factor` class. |
|
| 284 |
#' |
|
| 285 |
#' @return |
|
| 286 |
#' * If `x` is of class `factor` or converted from `character`, returns a `list` with named `numeric` items: |
|
| 287 |
#' * `n`: The [length()] of `x`. |
|
| 288 |
#' * `count`: A list with the number of cases for each level of the factor `x`. |
|
| 289 |
#' * `count_fraction`: Similar to `count` but also includes the proportion of cases for each level of the |
|
| 290 |
#' factor `x` relative to the denominator, or `NA` if the denominator is zero. |
|
| 291 |
#' |
|
| 292 |
#' @note |
|
| 293 |
#' * If `x` is an empty `factor`, a list is still returned for `counts` with one element |
|
| 294 |
#' per factor level. If there are no levels in `x`, the function fails. |
|
| 295 |
#' * If factor variables contain `NA`, these `NA` values are excluded by default. To include `NA` values |
|
| 296 |
#' set `na_rm = FALSE` and missing values will be displayed as an `NA` level. Alternatively, an explicit |
|
| 297 |
#' factor level can be defined for `NA` values during pre-processing via [df_explicit_na()] - the |
|
| 298 |
#' default `na_level` (`"<Missing>"`) will also be excluded when `na_rm` is set to `TRUE`. |
|
| 299 |
#' |
|
| 300 |
#' @method s_summary factor |
|
| 301 |
#' |
|
| 302 |
#' @examples |
|
| 303 |
#' # `s_summary.factor` |
|
| 304 |
#' |
|
| 305 |
#' ## Basic usage: |
|
| 306 |
#' s_summary(factor(c("a", "a", "b", "c", "a")))
|
|
| 307 |
#' |
|
| 308 |
#' # Empty factor returns zero-filled items. |
|
| 309 |
#' s_summary(factor(levels = c("a", "b", "c")))
|
|
| 310 |
#' |
|
| 311 |
#' ## Management of NA values. |
|
| 312 |
#' x <- factor(c(NA, "Female")) |
|
| 313 |
#' x <- explicit_na(x) |
|
| 314 |
#' s_summary(x, na_rm = TRUE) |
|
| 315 |
#' s_summary(x, na_rm = FALSE) |
|
| 316 |
#' |
|
| 317 |
#' ## Different denominators. |
|
| 318 |
#' x <- factor(c("a", "a", "b", "c", "a"))
|
|
| 319 |
#' s_summary(x, denom = "N_row", .N_row = 10L) |
|
| 320 |
#' s_summary(x, denom = "N_col", .N_col = 20L) |
|
| 321 |
#' |
|
| 322 |
#' @export |
|
| 323 |
s_summary.factor <- function(x, denom = c("n", "N_col", "N_row"), ...) {
|
|
| 324 | 304x |
assert_valid_factor(x) |
| 325 | 301x |
args_list <- list(...) |
| 326 | 301x |
.N_row <- args_list[[".N_row"]] # nolint |
| 327 | 301x |
.N_col <- args_list[[".N_col"]] # nolint |
| 328 | 301x |
na_rm <- args_list[["na_rm"]] %||% TRUE |
| 329 | 301x |
verbose <- args_list[["verbose"]] %||% TRUE |
| 330 | 301x |
compare_with_ref_group <- args_list[["compare_with_ref_group"]] |
| 331 | ||
| 332 | 301x |
if (na_rm) {
|
| 333 | 292x |
x <- x[!is.na(x)] %>% fct_discard("<Missing>")
|
| 334 |
} else {
|
|
| 335 | 9x |
x <- x %>% explicit_na(label = "NA") |
| 336 |
} |
|
| 337 | ||
| 338 | 301x |
y <- list() |
| 339 | ||
| 340 | 301x |
y$n <- list("n" = c("n" = length(x))) # all list of a list
|
| 341 | ||
| 342 | 301x |
y$count <- lapply(as.list(table(x, useNA = "ifany")), setNames, nm = "count") |
| 343 | ||
| 344 | 301x |
denom <- match.arg(denom) %>% |
| 345 | 301x |
switch( |
| 346 | 301x |
n = length(x), |
| 347 | 301x |
N_row = .N_row, |
| 348 | 301x |
N_col = .N_col |
| 349 |
) |
|
| 350 | ||
| 351 | 301x |
y$count_fraction <- lapply( |
| 352 | 301x |
y$count, |
| 353 | 301x |
function(x) {
|
| 354 | 2182x |
c(x, "p" = ifelse(denom > 0, x / denom, 0)) |
| 355 |
} |
|
| 356 |
) |
|
| 357 | ||
| 358 | 301x |
y$count_fraction_fixed_dp <- y$count_fraction |
| 359 | ||
| 360 | 301x |
y$fraction <- lapply( |
| 361 | 301x |
y$count, |
| 362 | 301x |
function(count) c("num" = unname(count), "denom" = denom)
|
| 363 |
) |
|
| 364 | ||
| 365 | 301x |
y$n_blq <- list("n_blq" = c("n_blq" = sum(grepl("BLQ|LTR|<[1-9]|<PCLLOQ", x))))
|
| 366 | ||
| 367 | ||
| 368 | 301x |
if (isTRUE(compare_with_ref_group)) {
|
| 369 | 16x |
.ref_group <- as_factor_keep_attributes(args_list[[".ref_group"]], verbose = verbose) |
| 370 | 16x |
.in_ref_col <- args_list[[".in_ref_col"]] |
| 371 | 16x |
checkmate::assert_flag(.in_ref_col) |
| 372 | 16x |
assert_valid_factor(x) |
| 373 | 16x |
assert_valid_factor(.ref_group) |
| 374 | ||
| 375 | 16x |
if (na_rm) {
|
| 376 | 14x |
x <- x[!is.na(x)] %>% fct_discard("<Missing>")
|
| 377 | 14x |
.ref_group <- .ref_group[!is.na(.ref_group)] %>% fct_discard("<Missing>")
|
| 378 |
} else {
|
|
| 379 | 2x |
x <- x %>% explicit_na(label = "NA") |
| 380 | 2x |
.ref_group <- .ref_group %>% explicit_na(label = "NA") |
| 381 |
} |
|
| 382 | ||
| 383 | 1x |
if ("NA" %in% levels(x)) levels(.ref_group) <- c(levels(.ref_group), "NA")
|
| 384 | 16x |
checkmate::assert_factor(x, levels = levels(.ref_group), min.levels = 2) |
| 385 | ||
| 386 | 16x |
y$pval_counts <- numeric() |
| 387 | 16x |
if (!.in_ref_col && length(x) > 0 && length(.ref_group) > 0) {
|
| 388 | 13x |
tab <- rbind(table(x), table(.ref_group)) |
| 389 | 13x |
res <- suppressWarnings(stats::chisq.test(tab)) |
| 390 | 13x |
y$pval_counts <- res$p.value |
| 391 |
} |
|
| 392 |
} |
|
| 393 | ||
| 394 | 301x |
y |
| 395 |
} |
|
| 396 | ||
| 397 |
#' @describeIn analyze_variables Method for `character` class. This makes an automatic |
|
| 398 |
#' conversion to factor (with a warning) and then forwards to the method for factors. |
|
| 399 |
#' |
|
| 400 |
#' @note |
|
| 401 |
#' * Automatic conversion of character to factor does not guarantee that the table |
|
| 402 |
#' can be generated correctly. In particular for sparse tables this very likely can fail. |
|
| 403 |
#' It is therefore better to always pre-process the dataset such that factors are manually |
|
| 404 |
#' created from character variables before passing the dataset to [rtables::build_table()]. |
|
| 405 |
#' |
|
| 406 |
#' @method s_summary character |
|
| 407 |
#' |
|
| 408 |
#' @examples |
|
| 409 |
#' # `s_summary.character` |
|
| 410 |
#' |
|
| 411 |
#' ## Basic usage: |
|
| 412 |
#' s_summary(c("a", "a", "b", "c", "a"), verbose = FALSE)
|
|
| 413 |
#' s_summary(c("a", "a", "b", "c", "a", ""), .var = "x", na_rm = FALSE, verbose = FALSE)
|
|
| 414 |
#' |
|
| 415 |
#' @export |
|
| 416 |
s_summary.character <- function(x, denom = c("n", "N_col", "N_row"), ...) {
|
|
| 417 | 12x |
args_list <- list(...) |
| 418 | 12x |
na_rm <- args_list[["na_rm"]] %||% TRUE |
| 419 | 12x |
verbose <- args_list[["verbose"]] %||% TRUE |
| 420 | ||
| 421 | 12x |
if (na_rm) {
|
| 422 | 11x |
y <- as_factor_keep_attributes(x, verbose = verbose) |
| 423 |
} else {
|
|
| 424 | 1x |
y <- as_factor_keep_attributes(x, verbose = verbose, na_level = "NA") |
| 425 |
} |
|
| 426 | ||
| 427 | 12x |
s_summary(x = y, denom = denom, ...) |
| 428 |
} |
|
| 429 | ||
| 430 |
#' @describeIn analyze_variables Method for `logical` class. |
|
| 431 |
#' |
|
| 432 |
#' @return |
|
| 433 |
#' * If `x` is of class `logical`, returns a `list` with named `numeric` items: |
|
| 434 |
#' * `n`: The [length()] of `x` (possibly after removing `NA`s). |
|
| 435 |
#' * `count`: Count of `TRUE` in `x`. |
|
| 436 |
#' * `count_fraction`: Count and proportion of `TRUE` in `x` relative to the denominator, or `NA` if the |
|
| 437 |
#' denominator is zero. Note that `NA`s in `x` are never counted or leading to `NA` here. |
|
| 438 |
#' |
|
| 439 |
#' @method s_summary logical |
|
| 440 |
#' |
|
| 441 |
#' @examples |
|
| 442 |
#' # `s_summary.logical` |
|
| 443 |
#' |
|
| 444 |
#' ## Basic usage: |
|
| 445 |
#' s_summary(c(TRUE, FALSE, TRUE, TRUE)) |
|
| 446 |
#' |
|
| 447 |
#' # Empty factor returns zero-filled items. |
|
| 448 |
#' s_summary(as.logical(c())) |
|
| 449 |
#' |
|
| 450 |
#' ## Management of NA values. |
|
| 451 |
#' x <- c(NA, TRUE, FALSE) |
|
| 452 |
#' s_summary(x, na_rm = TRUE) |
|
| 453 |
#' s_summary(x, na_rm = FALSE) |
|
| 454 |
#' |
|
| 455 |
#' ## Different denominators. |
|
| 456 |
#' x <- c(TRUE, FALSE, TRUE, TRUE) |
|
| 457 |
#' s_summary(x, denom = "N_row", .N_row = 10L) |
|
| 458 |
#' s_summary(x, denom = "N_col", .N_col = 20L) |
|
| 459 |
#' |
|
| 460 |
#' @export |
|
| 461 |
s_summary.logical <- function(x, denom = c("n", "N_col", "N_row"), ...) {
|
|
| 462 | 211x |
checkmate::assert_logical(x) |
| 463 | 211x |
args_list <- list(...) |
| 464 | 211x |
.N_row <- args_list[[".N_row"]] # nolint |
| 465 | 211x |
.N_col <- args_list[[".N_col"]] # nolint |
| 466 | 211x |
na_rm <- args_list[["na_rm"]] %||% TRUE |
| 467 | 211x |
compare_with_ref_group <- args_list[["compare_with_ref_group"]] |
| 468 | ||
| 469 | 211x |
if (na_rm) {
|
| 470 | 208x |
x <- x[!is.na(x)] |
| 471 |
} |
|
| 472 | ||
| 473 | 211x |
y <- list() |
| 474 | 211x |
y$n <- c("n" = length(x))
|
| 475 | 211x |
denom <- match.arg(denom) %>% |
| 476 | 211x |
switch( |
| 477 | 211x |
n = length(x), |
| 478 | 211x |
N_row = .N_row, |
| 479 | 211x |
N_col = .N_col |
| 480 |
) |
|
| 481 | 211x |
y$count <- c("count" = sum(x, na.rm = TRUE))
|
| 482 | 211x |
y$count_fraction <- c(y$count, "fraction" = ifelse(denom > 0, y$count / denom, 0)) |
| 483 | 211x |
y$count_fraction_fixed_dp <- y$count_fraction |
| 484 | 211x |
y$fraction <- c("num" = unname(y$count), "denom" = denom)
|
| 485 | 211x |
y$n_blq <- c("n_blq" = 0L)
|
| 486 | ||
| 487 | ||
| 488 | 211x |
if (isTRUE(compare_with_ref_group)) {
|
| 489 | 4x |
.ref_group <- args_list[[".ref_group"]] |
| 490 | 4x |
.in_ref_col <- args_list[[".in_ref_col"]] |
| 491 | 4x |
checkmate::assert_flag(.in_ref_col) |
| 492 | ||
| 493 | 4x |
if (na_rm) {
|
| 494 | 3x |
x <- stats::na.omit(x) |
| 495 | 3x |
.ref_group <- stats::na.omit(.ref_group) |
| 496 |
} else {
|
|
| 497 | 1x |
x[is.na(x)] <- FALSE |
| 498 | 1x |
.ref_group[is.na(.ref_group)] <- FALSE |
| 499 |
} |
|
| 500 | ||
| 501 | 4x |
y$pval_counts <- numeric() |
| 502 | 4x |
if (!.in_ref_col && length(x) > 0 && length(.ref_group) > 0) {
|
| 503 | 4x |
x <- factor(x, levels = c(TRUE, FALSE)) |
| 504 | 4x |
.ref_group <- factor(.ref_group, levels = c(TRUE, FALSE)) |
| 505 | 4x |
tbl <- rbind(table(x), table(.ref_group)) |
| 506 | 4x |
y$pval_counts <- suppressWarnings(prop_chisq(tbl)) |
| 507 |
} |
|
| 508 |
} |
|
| 509 | ||
| 510 | 211x |
y |
| 511 |
} |
|
| 512 | ||
| 513 |
#' @describeIn analyze_variables Formatted analysis function which is used as `afun` in `analyze_vars()` and |
|
| 514 |
#' `compare_vars()` and as `cfun` in `summarize_colvars()`. |
|
| 515 |
#' |
|
| 516 |
#' @param compare_with_ref_group (`flag`)\cr whether comparison statistics should be analyzed instead of summary |
|
| 517 |
#' statistics (`compare_with_ref_group = TRUE` adds `pval` statistic comparing |
|
| 518 |
#' against reference group). |
|
| 519 |
#' |
|
| 520 |
#' @return |
|
| 521 |
#' * `a_summary()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 522 |
#' |
|
| 523 |
#' @note |
|
| 524 |
#' * To use for comparison (with additional p-value statistic), parameter |
|
| 525 |
#' `compare_with_ref_group` must be set to `TRUE`. |
|
| 526 |
#' * Ensure that either all `NA` values are converted to an explicit `NA` level or all `NA` values are left as is. |
|
| 527 |
#' |
|
| 528 |
#' @examples |
|
| 529 |
#' a_summary(factor(c("a", "a", "b", "c", "a")), .N_row = 10, .N_col = 10)
|
|
| 530 |
#' a_summary( |
|
| 531 |
#' factor(c("a", "a", "b", "c", "a")),
|
|
| 532 |
#' .ref_group = factor(c("a", "a", "b", "c")), compare_with_ref_group = TRUE, .in_ref_col = TRUE
|
|
| 533 |
#' ) |
|
| 534 |
#' |
|
| 535 |
#' a_summary(c("A", "B", "A", "C"), .var = "x", .N_col = 10, .N_row = 10, verbose = FALSE)
|
|
| 536 |
#' a_summary( |
|
| 537 |
#' c("A", "B", "A", "C"),
|
|
| 538 |
#' .ref_group = c("B", "A", "C"), .var = "x", compare_with_ref_group = TRUE, verbose = FALSE,
|
|
| 539 |
#' .in_ref_col = FALSE |
|
| 540 |
#' ) |
|
| 541 |
#' |
|
| 542 |
#' a_summary(c(TRUE, FALSE, FALSE, TRUE, TRUE), .N_row = 10, .N_col = 10) |
|
| 543 |
#' a_summary( |
|
| 544 |
#' c(TRUE, FALSE, FALSE, TRUE, TRUE), |
|
| 545 |
#' .ref_group = c(TRUE, FALSE), .in_ref_col = TRUE, compare_with_ref_group = TRUE, |
|
| 546 |
#' .in_ref_col = FALSE |
|
| 547 |
#' ) |
|
| 548 |
#' |
|
| 549 |
#' a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla") |
|
| 550 |
#' a_summary(rnorm(10, 5, 1), |
|
| 551 |
#' .ref_group = rnorm(20, -5, 1), .var = "bla", compare_with_ref_group = TRUE, |
|
| 552 |
#' .in_ref_col = FALSE |
|
| 553 |
#' ) |
|
| 554 |
#' |
|
| 555 |
#' @export |
|
| 556 |
a_summary <- function(x, |
|
| 557 |
..., |
|
| 558 |
.stats = NULL, |
|
| 559 |
.stat_names = NULL, |
|
| 560 |
.formats = NULL, |
|
| 561 |
.labels = NULL, |
|
| 562 |
.indent_mods = NULL) {
|
|
| 563 | 332x |
dots_extra_args <- list(...) |
| 564 | ||
| 565 |
# Check if there are user-defined functions |
|
| 566 | 332x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 567 | 332x |
.stats <- default_and_custom_stats_list$all_stats # just the labels of stats |
| 568 | 332x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 569 | ||
| 570 |
# Correction of the pval indication if it is numeric or counts |
|
| 571 | 332x |
type <- ifelse(is.numeric(x), "numeric", "counts") # counts is "categorical" |
| 572 | 332x |
.stats <- .correct_num_or_counts_pval(type, .stats) |
| 573 | ||
| 574 |
# Adding automatically extra parameters to the statistic function (see ?rtables::additional_fun_params) |
|
| 575 | 332x |
extra_afun_params <- retrieve_extra_afun_params( |
| 576 | 332x |
names(dots_extra_args$.additional_fun_parameters) |
| 577 |
) |
|
| 578 | 332x |
dots_extra_args$.additional_fun_parameters <- NULL # After extraction we do not need them anymore |
| 579 | ||
| 580 |
# Check if compare_with_ref_group is TRUE but no ref col is set |
|
| 581 | 332x |
if (isTRUE(dots_extra_args$compare_with_ref_group) && |
| 582 | 332x |
all( |
| 583 | 332x |
length(dots_extra_args[[".ref_group"]]) == 0, # only used for testing |
| 584 | 332x |
length(extra_afun_params[[".ref_group"]]) == 0 |
| 585 |
) |
|
| 586 |
) {
|
|
| 587 | ! |
stop( |
| 588 | ! |
"For comparison (compare_with_ref_group = TRUE), the reference group must be specified.", |
| 589 | ! |
"\nSee ref_group in split_cols_by()." |
| 590 |
) |
|
| 591 |
} |
|
| 592 | ||
| 593 |
# Main statistical functions application |
|
| 594 | 332x |
x_stats <- .apply_stat_functions( |
| 595 | 332x |
default_stat_fnc = s_summary, |
| 596 | 332x |
custom_stat_fnc_list = custom_stat_functions, |
| 597 | 332x |
args_list = c( |
| 598 | 332x |
x = list(x), |
| 599 | 332x |
extra_afun_params, |
| 600 | 332x |
dots_extra_args |
| 601 |
) |
|
| 602 |
) |
|
| 603 | ||
| 604 |
# Fill in with stats defaults if needed |
|
| 605 | 332x |
met_grp <- paste0(c("analyze_vars", type), collapse = "_")
|
| 606 | 332x |
.stats <- get_stats( |
| 607 | 332x |
met_grp, |
| 608 | 332x |
stats_in = .stats, |
| 609 | 332x |
custom_stats_in = names(custom_stat_functions), |
| 610 | 332x |
add_pval = dots_extra_args$compare_with_ref_group %||% FALSE |
| 611 |
) |
|
| 612 | ||
| 613 | 332x |
x_stats <- x_stats[.stats] |
| 614 | ||
| 615 | 332x |
is_char <- is.character(x) || is.factor(x) |
| 616 | 332x |
if (is_char) {
|
| 617 | 236x |
levels_per_stats <- lapply(x_stats, names) |
| 618 |
} else {
|
|
| 619 | 96x |
levels_per_stats <- names(x_stats) %>% |
| 620 | 96x |
as.list() %>% |
| 621 | 96x |
setNames(names(x_stats)) |
| 622 |
} |
|
| 623 | ||
| 624 |
# Fill in formats/indents/labels with custom input and defaults |
|
| 625 | 332x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 626 | 332x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 627 | 332x |
lbls <- get_labels_from_stats(.stats, .labels, levels_per_stats) |
| 628 | ||
| 629 | 332x |
if (is_char) {
|
| 630 |
# Keep pval_counts stat if present from comparisons and empty |
|
| 631 | 236x |
if ("pval_counts" %in% names(x_stats) && length(x_stats[["pval_counts"]]) == 0) {
|
| 632 | 3x |
x_stats[["pval_counts"]] <- list(NULL) %>% setNames("pval_counts")
|
| 633 |
} |
|
| 634 | ||
| 635 |
# Unlist stats |
|
| 636 | 236x |
x_stats <- x_stats %>% |
| 637 | 236x |
.unlist_keep_nulls() %>% |
| 638 | 236x |
setNames(names(.formats)) |
| 639 |
} |
|
| 640 | ||
| 641 |
# Check for custom labels from control_analyze_vars |
|
| 642 | 332x |
.labels <- if ("control" %in% names(dots_extra_args)) {
|
| 643 | 2x |
labels_use_control(lbls, dots_extra_args[["control"]], .labels) |
| 644 |
} else {
|
|
| 645 | 330x |
lbls |
| 646 |
} |
|
| 647 | ||
| 648 |
# Auto format handling |
|
| 649 | 332x |
.formats <- apply_auto_formatting( |
| 650 | 332x |
.formats, |
| 651 | 332x |
x_stats, |
| 652 | 332x |
extra_afun_params$.df_row, |
| 653 | 332x |
extra_afun_params$.var |
| 654 |
) |
|
| 655 | ||
| 656 |
# Get and check statistical names from defaults |
|
| 657 | 332x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 658 | ||
| 659 | 332x |
in_rows( |
| 660 | 332x |
.list = x_stats, |
| 661 | 332x |
.formats = .formats, |
| 662 | 332x |
.names = names(.labels), |
| 663 | 332x |
.stat_names = .stat_names, |
| 664 | 332x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 665 | 332x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 666 |
) |
|
| 667 |
} |
|
| 668 | ||
| 669 |
#' @describeIn analyze_variables Layout-creating function which can take statistics function arguments |
|
| 670 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 671 |
#' |
|
| 672 |
#' @param ... additional arguments passed to `s_summary()`, including: |
|
| 673 |
#' * `denom`: (`string`) See parameter description below. |
|
| 674 |
#' * `.N_row`: (`numeric(1)`) Row-wise N (row group count) for the group of observations being analyzed (i.e. with no |
|
| 675 |
#' column-based subsetting). |
|
| 676 |
#' * `.N_col`: (`numeric(1)`) Column-wise N (column count) for the full column being tabulated within. |
|
| 677 |
#' * `verbose`: (`flag`) Whether additional warnings and messages should be printed. Mainly used to print out |
|
| 678 |
#' information about factor casting. Defaults to `TRUE`. Used for `character`/`factor` variables only. |
|
| 679 |
#' @param compare_with_ref_group (logical)\cr whether to compare the variable with a reference group. |
|
| 680 |
#' @param .indent_mods (named `integer`)\cr indent modifiers for the labels. Each element of the vector |
|
| 681 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation |
|
| 682 |
#' for that statistic's row label. |
|
| 683 |
#' |
|
| 684 |
#' @return |
|
| 685 |
#' * `analyze_vars()` returns a layout object suitable for passing to further layouting functions, |
|
| 686 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 687 |
#' the statistics from `s_summary()` to the table layout. |
|
| 688 |
#' |
|
| 689 |
#' @examples |
|
| 690 |
#' ## Fabricated dataset. |
|
| 691 |
#' dta_test <- data.frame( |
|
| 692 |
#' USUBJID = rep(1:6, each = 3), |
|
| 693 |
#' PARAMCD = rep("lab", 6 * 3),
|
|
| 694 |
#' AVISIT = rep(paste0("V", 1:3), 6),
|
|
| 695 |
#' ARM = rep(LETTERS[1:3], rep(6, 3)), |
|
| 696 |
#' AVAL = c(9:1, rep(NA, 9)) |
|
| 697 |
#' ) |
|
| 698 |
#' |
|
| 699 |
#' # `analyze_vars()` in `rtables` pipelines |
|
| 700 |
#' ## Default output within a `rtables` pipeline. |
|
| 701 |
#' l <- basic_table() %>% |
|
| 702 |
#' split_cols_by(var = "ARM") %>% |
|
| 703 |
#' split_rows_by(var = "AVISIT") %>% |
|
| 704 |
#' analyze_vars(vars = "AVAL") |
|
| 705 |
#' |
|
| 706 |
#' build_table(l, df = dta_test) |
|
| 707 |
#' |
|
| 708 |
#' ## Select and format statistics output. |
|
| 709 |
#' l <- basic_table() %>% |
|
| 710 |
#' split_cols_by(var = "ARM") %>% |
|
| 711 |
#' split_rows_by(var = "AVISIT") %>% |
|
| 712 |
#' analyze_vars( |
|
| 713 |
#' vars = "AVAL", |
|
| 714 |
#' .stats = c("n", "mean_sd", "quantiles"),
|
|
| 715 |
#' .formats = c("mean_sd" = "xx.x, xx.x"),
|
|
| 716 |
#' .labels = c(n = "n", mean_sd = "Mean, SD", quantiles = c("Q1 - Q3"))
|
|
| 717 |
#' ) |
|
| 718 |
#' |
|
| 719 |
#' build_table(l, df = dta_test) |
|
| 720 |
#' |
|
| 721 |
#' ## Use arguments interpreted by `s_summary`. |
|
| 722 |
#' l <- basic_table() %>% |
|
| 723 |
#' split_cols_by(var = "ARM") %>% |
|
| 724 |
#' split_rows_by(var = "AVISIT") %>% |
|
| 725 |
#' analyze_vars(vars = "AVAL", na_rm = FALSE) |
|
| 726 |
#' |
|
| 727 |
#' build_table(l, df = dta_test) |
|
| 728 |
#' |
|
| 729 |
#' ## Handle `NA` levels first when summarizing factors. |
|
| 730 |
#' dta_test$AVISIT <- NA_character_ |
|
| 731 |
#' dta_test <- df_explicit_na(dta_test) |
|
| 732 |
#' l <- basic_table() %>% |
|
| 733 |
#' split_cols_by(var = "ARM") %>% |
|
| 734 |
#' analyze_vars(vars = "AVISIT", na_rm = FALSE) |
|
| 735 |
#' |
|
| 736 |
#' build_table(l, df = dta_test) |
|
| 737 |
#' |
|
| 738 |
#' # auto format |
|
| 739 |
#' dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4))
|
|
| 740 |
#' basic_table() %>% |
|
| 741 |
#' analyze_vars( |
|
| 742 |
#' vars = "VAR", |
|
| 743 |
#' .stats = c("n", "mean", "mean_sd", "range"),
|
|
| 744 |
#' .formats = c("mean_sd" = "auto", "range" = "auto")
|
|
| 745 |
#' ) %>% |
|
| 746 |
#' build_table(dt) |
|
| 747 |
#' |
|
| 748 |
#' @export |
|
| 749 |
#' @order 2 |
|
| 750 |
analyze_vars <- function(lyt, |
|
| 751 |
vars, |
|
| 752 |
var_labels = vars, |
|
| 753 |
na_str = default_na_str(), |
|
| 754 |
nested = TRUE, |
|
| 755 |
show_labels = "default", |
|
| 756 |
table_names = vars, |
|
| 757 |
section_div = NA_character_, |
|
| 758 |
..., |
|
| 759 |
na_rm = TRUE, |
|
| 760 |
compare_with_ref_group = FALSE, |
|
| 761 |
.stats = c("n", "mean_sd", "median", "range", "count_fraction"),
|
|
| 762 |
.stat_names = NULL, |
|
| 763 |
.formats = NULL, |
|
| 764 |
.labels = NULL, |
|
| 765 |
.indent_mods = NULL) {
|
|
| 766 |
# Depending on main functions |
|
| 767 | 40x |
extra_args <- list( |
| 768 | 40x |
"na_rm" = na_rm, |
| 769 | 40x |
"compare_with_ref_group" = compare_with_ref_group, |
| 770 |
... |
|
| 771 |
) |
|
| 772 | ||
| 773 |
# Needed defaults |
|
| 774 | 40x |
if (!is.null(.stats)) extra_args[[".stats"]] <- .stats |
| 775 | 3x |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 776 | 9x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 777 | 4x |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 778 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 779 | ||
| 780 |
# Adding all additional information from layout to analysis functions (see ?rtables::additional_fun_params) |
|
| 781 | 40x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 782 | 40x |
formals(a_summary) <- c( |
| 783 | 40x |
formals(a_summary), |
| 784 | 40x |
extra_args[[".additional_fun_parameters"]] |
| 785 |
) |
|
| 786 | ||
| 787 |
# Main {rtables} structural call
|
|
| 788 | 40x |
analyze( |
| 789 | 40x |
lyt = lyt, |
| 790 | 40x |
vars = vars, |
| 791 | 40x |
var_labels = var_labels, |
| 792 | 40x |
afun = a_summary, |
| 793 | 40x |
na_str = na_str, |
| 794 | 40x |
inclNAs = !na_rm, |
| 795 | 40x |
nested = nested, |
| 796 | 40x |
extra_args = extra_args, |
| 797 | 40x |
show_labels = show_labels, |
| 798 | 40x |
table_names = table_names, |
| 799 | 40x |
section_div = section_div |
| 800 |
) |
|
| 801 |
} |
| 1 |
#' Formatting functions |
|
| 2 |
#' |
|
| 3 |
#' See below for the list of formatting functions created in `tern` to work with `rtables`. |
|
| 4 |
#' |
|
| 5 |
#' Other available formats can be listed via [`formatters::list_valid_format_labels()`]. Additional |
|
| 6 |
#' custom formats can be created via the [`formatters::sprintf_format()`] function. |
|
| 7 |
#' |
|
| 8 |
#' @family formatting functions |
|
| 9 |
#' @name formatting_functions |
|
| 10 |
NULL |
|
| 11 | ||
| 12 |
#' Format fraction and percentage |
|
| 13 |
#' |
|
| 14 |
#' @description `r lifecycle::badge("stable")`
|
|
| 15 |
#' |
|
| 16 |
#' Formats a fraction together with ratio in percent. |
|
| 17 |
#' |
|
| 18 |
#' @param x (named `integer`)\cr vector with elements `num` and `denom`. |
|
| 19 |
#' @param ... not used. Required for `rtables` interface. |
|
| 20 |
#' |
|
| 21 |
#' @return A string in the format `num / denom (ratio %)`. If `num` is 0, the format is `num / denom`. |
|
| 22 |
#' |
|
| 23 |
#' @examples |
|
| 24 |
#' format_fraction(x = c(num = 2L, denom = 3L)) |
|
| 25 |
#' format_fraction(x = c(num = 0L, denom = 3L)) |
|
| 26 |
#' |
|
| 27 |
#' @family formatting functions |
|
| 28 |
#' @export |
|
| 29 |
format_fraction <- function(x, ...) {
|
|
| 30 | 220x |
attr(x, "label") <- NULL |
| 31 | ||
| 32 | 220x |
checkmate::assert_vector(x) |
| 33 | 220x |
checkmate::assert_count(x["num"]) |
| 34 | 218x |
checkmate::assert_count(x["denom"]) |
| 35 | ||
| 36 | 218x |
result <- if (x["num"] == 0) {
|
| 37 | 10x |
paste0(x["num"], "/", x["denom"]) |
| 38 |
} else {
|
|
| 39 | 208x |
paste0( |
| 40 | 208x |
x["num"], "/", x["denom"], |
| 41 | 208x |
" (", round(x["num"] / x["denom"] * 100, 1), "%)"
|
| 42 |
) |
|
| 43 |
} |
|
| 44 | ||
| 45 | 218x |
return(result) |
| 46 |
} |
|
| 47 | ||
| 48 |
#' Format fraction and percentage with fixed single decimal place |
|
| 49 |
#' |
|
| 50 |
#' @description `r lifecycle::badge("stable")`
|
|
| 51 |
#' |
|
| 52 |
#' Formats a fraction together with ratio in percent with fixed single decimal place. |
|
| 53 |
#' Includes trailing zero in case of whole number percentages to always keep one decimal place. |
|
| 54 |
#' |
|
| 55 |
#' @inheritParams format_fraction |
|
| 56 |
#' |
|
| 57 |
#' @return A string in the format `num / denom (ratio %)`. If `num` is 0, the format is `num / denom`. |
|
| 58 |
#' |
|
| 59 |
#' @examples |
|
| 60 |
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 2L)) |
|
| 61 |
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 4L)) |
|
| 62 |
#' format_fraction_fixed_dp(x = c(num = 0L, denom = 3L)) |
|
| 63 |
#' |
|
| 64 |
#' @family formatting functions |
|
| 65 |
#' @export |
|
| 66 |
format_fraction_fixed_dp <- function(x, ...) {
|
|
| 67 | 3x |
attr(x, "label") <- NULL |
| 68 | 3x |
checkmate::assert_vector(x) |
| 69 | 3x |
checkmate::assert_count(x["num"]) |
| 70 | 3x |
checkmate::assert_count(x["denom"]) |
| 71 | ||
| 72 | 3x |
result <- if (x["num"] == 0) {
|
| 73 | 1x |
paste0(x["num"], "/", x["denom"]) |
| 74 |
} else {
|
|
| 75 | 2x |
paste0( |
| 76 | 2x |
x["num"], "/", x["denom"], |
| 77 | 2x |
" (", sprintf("%.1f", round(x["num"] / x["denom"] * 100, 1)), "%)"
|
| 78 |
) |
|
| 79 |
} |
|
| 80 | 3x |
return(result) |
| 81 |
} |
|
| 82 | ||
| 83 |
#' Format count and fraction |
|
| 84 |
#' |
|
| 85 |
#' @description `r lifecycle::badge("stable")`
|
|
| 86 |
#' |
|
| 87 |
#' Formats a count together with fraction with special consideration when count is `0`. |
|
| 88 |
#' |
|
| 89 |
#' @param x (`numeric(2)`)\cr vector of length 2 with count and fraction, respectively. |
|
| 90 |
#' @param ... not used. Required for `rtables` interface. |
|
| 91 |
#' |
|
| 92 |
#' @return A string in the format `count (fraction %)`. If `count` is 0, the format is `0`. |
|
| 93 |
#' |
|
| 94 |
#' @examples |
|
| 95 |
#' format_count_fraction(x = c(2, 0.6667)) |
|
| 96 |
#' format_count_fraction(x = c(0, 0)) |
|
| 97 |
#' |
|
| 98 |
#' @family formatting functions |
|
| 99 |
#' @export |
|
| 100 |
format_count_fraction <- function(x, ...) {
|
|
| 101 | 102x |
attr(x, "label") <- NULL |
| 102 | ||
| 103 | 102x |
if (any(is.na(x))) {
|
| 104 | 1x |
return("NA")
|
| 105 |
} |
|
| 106 | ||
| 107 | 101x |
checkmate::assert_vector(x) |
| 108 | 101x |
checkmate::assert_integerish(x[1]) |
| 109 | 101x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
| 110 | ||
| 111 | 101x |
result <- if (x[1] == 0) {
|
| 112 | 13x |
"0" |
| 113 |
} else {
|
|
| 114 | 88x |
paste0(x[1], " (", round(x[2] * 100, 1), "%)")
|
| 115 |
} |
|
| 116 | ||
| 117 | 101x |
return(result) |
| 118 |
} |
|
| 119 | ||
| 120 |
#' Format count and percentage with fixed single decimal place |
|
| 121 |
#' |
|
| 122 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 123 |
#' |
|
| 124 |
#' Formats a count together with fraction with special consideration when count is `0`. |
|
| 125 |
#' |
|
| 126 |
#' @inheritParams format_count_fraction |
|
| 127 |
#' |
|
| 128 |
#' @return A string in the format `count (fraction %)`. If `count` is 0, the format is `0`. |
|
| 129 |
#' |
|
| 130 |
#' @examples |
|
| 131 |
#' format_count_fraction_fixed_dp(x = c(2, 0.6667)) |
|
| 132 |
#' format_count_fraction_fixed_dp(x = c(2, 0.5)) |
|
| 133 |
#' format_count_fraction_fixed_dp(x = c(0, 0)) |
|
| 134 |
#' |
|
| 135 |
#' @family formatting functions |
|
| 136 |
#' @export |
|
| 137 |
format_count_fraction_fixed_dp <- function(x, ...) {
|
|
| 138 | 1408x |
attr(x, "label") <- NULL |
| 139 | ||
| 140 | 1408x |
if (any(is.na(x))) {
|
| 141 | ! |
return("NA")
|
| 142 |
} |
|
| 143 | ||
| 144 | 1408x |
checkmate::assert_vector(x) |
| 145 | 1408x |
checkmate::assert_integerish(x[1]) |
| 146 | 1408x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
| 147 | ||
| 148 | 1408x |
result <- if (x[1] == 0) {
|
| 149 | 195x |
"0" |
| 150 | 1408x |
} else if (.is_equal_float(x[2], 1)) {
|
| 151 | 549x |
sprintf("%d (100%%)", x[1])
|
| 152 |
} else {
|
|
| 153 | 664x |
sprintf("%d (%.1f%%)", x[1], x[2] * 100)
|
| 154 |
} |
|
| 155 | ||
| 156 | 1408x |
return(result) |
| 157 |
} |
|
| 158 | ||
| 159 |
#' Format count and fraction with special case for count < 10 |
|
| 160 |
#' |
|
| 161 |
#' @description `r lifecycle::badge("stable")`
|
|
| 162 |
#' |
|
| 163 |
#' Formats a count together with fraction with special consideration when count is less than 10. |
|
| 164 |
#' |
|
| 165 |
#' @inheritParams format_count_fraction |
|
| 166 |
#' |
|
| 167 |
#' @return A string in the format `count (fraction %)`. If `count` is less than 10, only `count` is printed. |
|
| 168 |
#' |
|
| 169 |
#' @examples |
|
| 170 |
#' format_count_fraction_lt10(x = c(275, 0.9673)) |
|
| 171 |
#' format_count_fraction_lt10(x = c(2, 0.6667)) |
|
| 172 |
#' format_count_fraction_lt10(x = c(9, 1)) |
|
| 173 |
#' |
|
| 174 |
#' @family formatting functions |
|
| 175 |
#' @export |
|
| 176 |
format_count_fraction_lt10 <- function(x, ...) {
|
|
| 177 | 7x |
attr(x, "label") <- NULL |
| 178 | ||
| 179 | 7x |
if (any(is.na(x))) {
|
| 180 | 1x |
return("NA")
|
| 181 |
} |
|
| 182 | ||
| 183 | 6x |
checkmate::assert_vector(x) |
| 184 | 6x |
checkmate::assert_integerish(x[1]) |
| 185 | 6x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
| 186 | ||
| 187 | 6x |
result <- if (x[1] < 10) {
|
| 188 | 3x |
paste0(x[1]) |
| 189 |
} else {
|
|
| 190 | 3x |
paste0(x[1], " (", round(x[2] * 100, 1), "%)")
|
| 191 |
} |
|
| 192 | ||
| 193 | 6x |
return(result) |
| 194 |
} |
|
| 195 | ||
| 196 |
#' Format XX as a formatting function |
|
| 197 |
#' |
|
| 198 |
#' Translate a string where x and dots are interpreted as number place |
|
| 199 |
#' holders, and others as formatting elements. |
|
| 200 |
#' |
|
| 201 |
#' @param str (`string`)\cr template. |
|
| 202 |
#' |
|
| 203 |
#' @return An `rtables` formatting function. |
|
| 204 |
#' |
|
| 205 |
#' @examples |
|
| 206 |
#' test <- list(c(1.658, 0.5761), c(1e1, 785.6)) |
|
| 207 |
#' |
|
| 208 |
#' z <- format_xx("xx (xx.x)")
|
|
| 209 |
#' sapply(test, z) |
|
| 210 |
#' |
|
| 211 |
#' z <- format_xx("xx.x - xx.x")
|
|
| 212 |
#' sapply(test, z) |
|
| 213 |
#' |
|
| 214 |
#' z <- format_xx("xx.x, incl. xx.x% NE")
|
|
| 215 |
#' sapply(test, z) |
|
| 216 |
#' |
|
| 217 |
#' @family formatting functions |
|
| 218 |
#' @export |
|
| 219 |
format_xx <- function(str) {
|
|
| 220 |
# Find position in the string. |
|
| 221 | 1x |
positions <- gregexpr(pattern = "x+\\.x+|x+", text = str, perl = TRUE) |
| 222 | 1x |
x_positions <- regmatches(x = str, m = positions)[[1]] |
| 223 | ||
| 224 |
# Roundings depends on the number of x behind [.]. |
|
| 225 | 1x |
roundings <- lapply( |
| 226 | 1x |
X = x_positions, |
| 227 | 1x |
function(x) {
|
| 228 | 2x |
y <- strsplit(split = "\\.", x = x)[[1]] |
| 229 | 2x |
rounding <- function(x) {
|
| 230 | 4x |
round(x, digits = ifelse(length(y) > 1, nchar(y[2]), 0)) |
| 231 |
} |
|
| 232 | 2x |
return(rounding) |
| 233 |
} |
|
| 234 |
) |
|
| 235 | ||
| 236 | 1x |
rtable_format <- function(x, output) {
|
| 237 | 2x |
values <- Map(y = x, fun = roundings, function(y, fun) fun(y)) |
| 238 | 2x |
regmatches(x = str, m = positions)[[1]] <- values |
| 239 | 2x |
return(str) |
| 240 |
} |
|
| 241 | ||
| 242 | 1x |
return(rtable_format) |
| 243 |
} |
|
| 244 | ||
| 245 |
#' Format numeric values by significant figures |
|
| 246 |
#' |
|
| 247 |
#' Format numeric values to print with a specified number of significant figures. |
|
| 248 |
#' |
|
| 249 |
#' @param sigfig (`integer(1)`)\cr number of significant figures to display. |
|
| 250 |
#' @param format (`string`)\cr the format label (string) to apply when printing the value. Decimal |
|
| 251 |
#' places in string are ignored in favor of formatting by significant figures. Formats options are: |
|
| 252 |
#' `"xx"`, `"xx / xx"`, `"(xx, xx)"`, `"xx - xx"`, and `"xx (xx)"`. |
|
| 253 |
#' @param num_fmt (`string`)\cr numeric format modifiers to apply to the value. Defaults to `"fg"` for |
|
| 254 |
#' standard significant figures formatting - fixed (non-scientific notation) format (`"f"`) |
|
| 255 |
#' and `sigfig` equal to number of significant figures instead of decimal places (`"g"`). See the |
|
| 256 |
#' [formatC()] `format` argument for more options. |
|
| 257 |
#' |
|
| 258 |
#' @return An `rtables` formatting function. |
|
| 259 |
#' |
|
| 260 |
#' @examples |
|
| 261 |
#' fmt_3sf <- format_sigfig(3) |
|
| 262 |
#' fmt_3sf(1.658) |
|
| 263 |
#' fmt_3sf(1e1) |
|
| 264 |
#' |
|
| 265 |
#' fmt_5sf <- format_sigfig(5) |
|
| 266 |
#' fmt_5sf(0.57) |
|
| 267 |
#' fmt_5sf(0.000025645) |
|
| 268 |
#' |
|
| 269 |
#' @family formatting functions |
|
| 270 |
#' @export |
|
| 271 |
format_sigfig <- function(sigfig, format = "xx", num_fmt = "fg") {
|
|
| 272 | 3x |
checkmate::assert_integerish(sigfig) |
| 273 | 3x |
format <- gsub("xx\\.|xx\\.x+", "xx", format)
|
| 274 | 3x |
checkmate::assert_choice(format, c("xx", "xx / xx", "(xx, xx)", "xx - xx", "xx (xx)"))
|
| 275 | 3x |
function(x, ...) {
|
| 276 | ! |
if (!is.numeric(x)) stop("`format_sigfig` cannot be used for non-numeric values. Please choose another format.")
|
| 277 | 12x |
num <- formatC(signif(x, digits = sigfig), digits = sigfig, format = num_fmt, flag = "#") |
| 278 | 12x |
num <- gsub("\\.$", "", num) # remove trailing "."
|
| 279 | ||
| 280 | 12x |
format_value(num, format) |
| 281 |
} |
|
| 282 |
} |
|
| 283 | ||
| 284 |
#' Format fraction with lower threshold |
|
| 285 |
#' |
|
| 286 |
#' @description `r lifecycle::badge("stable")`
|
|
| 287 |
#' |
|
| 288 |
#' Formats a fraction when the second element of the input `x` is the fraction. It applies |
|
| 289 |
#' a lower threshold, below which it is just stated that the fraction is smaller than that. |
|
| 290 |
#' |
|
| 291 |
#' @param threshold (`proportion`)\cr lower threshold. |
|
| 292 |
#' |
|
| 293 |
#' @return An `rtables` formatting function that takes numeric input `x` where the second |
|
| 294 |
#' element is the fraction that is formatted. If the fraction is above or equal to the threshold, |
|
| 295 |
#' then it is displayed in percentage. If it is positive but below the threshold, it returns, |
|
| 296 |
#' e.g. "<1" if the threshold is `0.01`. If it is zero, then just "0" is returned. |
|
| 297 |
#' |
|
| 298 |
#' @examples |
|
| 299 |
#' format_fun <- format_fraction_threshold(0.05) |
|
| 300 |
#' format_fun(x = c(20, 0.1)) |
|
| 301 |
#' format_fun(x = c(2, 0.01)) |
|
| 302 |
#' format_fun(x = c(0, 0)) |
|
| 303 |
#' |
|
| 304 |
#' @family formatting functions |
|
| 305 |
#' @export |
|
| 306 |
format_fraction_threshold <- function(threshold) {
|
|
| 307 | 1x |
assert_proportion_value(threshold) |
| 308 | 1x |
string_below_threshold <- paste0("<", round(threshold * 100))
|
| 309 | 1x |
function(x, ...) {
|
| 310 | 3x |
assert_proportion_value(x[2], include_boundaries = TRUE) |
| 311 | 3x |
ifelse( |
| 312 | 3x |
x[2] > 0.01, |
| 313 | 3x |
round(x[2] * 100), |
| 314 | 3x |
ifelse( |
| 315 | 3x |
x[2] == 0, |
| 316 | 3x |
"0", |
| 317 | 3x |
string_below_threshold |
| 318 |
) |
|
| 319 |
) |
|
| 320 |
} |
|
| 321 |
} |
|
| 322 | ||
| 323 |
#' Format extreme values |
|
| 324 |
#' |
|
| 325 |
#' @description `r lifecycle::badge("stable")`
|
|
| 326 |
#' |
|
| 327 |
#' `rtables` formatting functions that handle extreme values. |
|
| 328 |
#' |
|
| 329 |
#' @param digits (`integer(1)`)\cr number of decimal places to display. |
|
| 330 |
#' |
|
| 331 |
#' @details For each input, apply a format to the specified number of `digits`. If the value is |
|
| 332 |
#' below a threshold, it returns "<0.01" e.g. if the number of `digits` is 2. If the value is |
|
| 333 |
#' above a threshold, it returns ">999.99" e.g. if the number of `digits` is 2. |
|
| 334 |
#' If it is zero, then returns "0.00". |
|
| 335 |
#' |
|
| 336 |
#' @family formatting functions |
|
| 337 |
#' @name extreme_format |
|
| 338 |
NULL |
|
| 339 | ||
| 340 |
#' @describeIn extreme_format Internal helper function to calculate the threshold and create formatted strings |
|
| 341 |
#' used in Formatting Functions. Returns a list with elements `threshold` and `format_string`. |
|
| 342 |
#' |
|
| 343 |
#' @return |
|
| 344 |
#' * `h_get_format_threshold()` returns a `list` of 2 elements: `threshold`, with `low` and `high` thresholds, |
|
| 345 |
#' and `format_string`, with thresholds formatted as strings. |
|
| 346 |
#' |
|
| 347 |
#' @examples |
|
| 348 |
#' h_get_format_threshold(2L) |
|
| 349 |
#' |
|
| 350 |
#' @export |
|
| 351 |
h_get_format_threshold <- function(digits = 2L) {
|
|
| 352 | 2013x |
checkmate::assert_integerish(digits) |
| 353 | ||
| 354 | 2013x |
low_threshold <- 1 / (10 ^ digits) # styler: off |
| 355 | 2013x |
high_threshold <- 1000 - (1 / (10 ^ digits)) # styler: off |
| 356 | ||
| 357 | 2013x |
string_below_threshold <- paste0("<", low_threshold)
|
| 358 | 2013x |
string_above_threshold <- paste0(">", high_threshold)
|
| 359 | ||
| 360 | 2013x |
list( |
| 361 | 2013x |
"threshold" = c(low = low_threshold, high = high_threshold), |
| 362 | 2013x |
"format_string" = c(low = string_below_threshold, high = string_above_threshold) |
| 363 |
) |
|
| 364 |
} |
|
| 365 | ||
| 366 |
#' @describeIn extreme_format Internal helper function to apply a threshold format to a value. |
|
| 367 |
#' Creates a formatted string to be used in Formatting Functions. |
|
| 368 |
#' |
|
| 369 |
#' @param x (`numeric(1)`)\cr value to format. |
|
| 370 |
#' |
|
| 371 |
#' @return |
|
| 372 |
#' * `h_format_threshold()` returns the given value, or if the value is not within the digit threshold the relation |
|
| 373 |
#' of the given value to the digit threshold, as a formatted string. |
|
| 374 |
#' |
|
| 375 |
#' @examples |
|
| 376 |
#' h_format_threshold(0.001) |
|
| 377 |
#' h_format_threshold(1000) |
|
| 378 |
#' |
|
| 379 |
#' @export |
|
| 380 |
h_format_threshold <- function(x, digits = 2L) {
|
|
| 381 | 2015x |
if (is.na(x)) {
|
| 382 | 4x |
return(x) |
| 383 |
} |
|
| 384 | ||
| 385 | 2011x |
checkmate::assert_numeric(x, lower = 0) |
| 386 | ||
| 387 | 2011x |
l_fmt <- h_get_format_threshold(digits) |
| 388 | ||
| 389 | 2011x |
result <- if (x < l_fmt$threshold["low"] && 0 < x) {
|
| 390 | 44x |
l_fmt$format_string["low"] |
| 391 | 2011x |
} else if (x > l_fmt$threshold["high"]) {
|
| 392 | 99x |
l_fmt$format_string["high"] |
| 393 |
} else {
|
|
| 394 | 1868x |
sprintf(fmt = paste0("%.", digits, "f"), x)
|
| 395 |
} |
|
| 396 | ||
| 397 | 2011x |
unname(result) |
| 398 |
} |
|
| 399 | ||
| 400 |
#' Format a single extreme value |
|
| 401 |
#' |
|
| 402 |
#' @description `r lifecycle::badge("stable")`
|
|
| 403 |
#' |
|
| 404 |
#' Create a formatting function for a single extreme value. |
|
| 405 |
#' |
|
| 406 |
#' @inheritParams extreme_format |
|
| 407 |
#' |
|
| 408 |
#' @return An `rtables` formatting function that uses threshold `digits` to return a formatted extreme value. |
|
| 409 |
#' |
|
| 410 |
#' @examples |
|
| 411 |
#' format_fun <- format_extreme_values(2L) |
|
| 412 |
#' format_fun(x = 0.127) |
|
| 413 |
#' format_fun(x = Inf) |
|
| 414 |
#' format_fun(x = 0) |
|
| 415 |
#' format_fun(x = 0.009) |
|
| 416 |
#' |
|
| 417 |
#' @family formatting functions |
|
| 418 |
#' @export |
|
| 419 |
format_extreme_values <- function(digits = 2L) {
|
|
| 420 | 1x |
function(x, ...) {
|
| 421 | 5x |
checkmate::assert_scalar(x, na.ok = TRUE) |
| 422 | ||
| 423 | 5x |
h_format_threshold(x = x, digits = digits) |
| 424 |
} |
|
| 425 |
} |
|
| 426 | ||
| 427 |
#' Format extreme values part of a confidence interval |
|
| 428 |
#' |
|
| 429 |
#' @description `r lifecycle::badge("stable")`
|
|
| 430 |
#' |
|
| 431 |
#' Formatting Function for extreme values part of a confidence interval. Values |
|
| 432 |
#' are formatted as e.g. "(xx.xx, xx.xx)" if the number of `digits` is 2. |
|
| 433 |
#' |
|
| 434 |
#' @inheritParams extreme_format |
|
| 435 |
#' |
|
| 436 |
#' @return An `rtables` formatting function that uses threshold `digits` to return a formatted extreme |
|
| 437 |
#' values confidence interval. |
|
| 438 |
#' |
|
| 439 |
#' @examples |
|
| 440 |
#' format_fun <- format_extreme_values_ci(2L) |
|
| 441 |
#' format_fun(x = c(0.127, Inf)) |
|
| 442 |
#' format_fun(x = c(0, 0.009)) |
|
| 443 |
#' |
|
| 444 |
#' @family formatting functions |
|
| 445 |
#' @export |
|
| 446 |
format_extreme_values_ci <- function(digits = 2L) {
|
|
| 447 | 9x |
function(x, ...) {
|
| 448 | 54x |
checkmate::assert_vector(x, len = 2) |
| 449 | 54x |
l_result <- h_format_threshold(x = x[1], digits = digits) |
| 450 | 54x |
h_result <- h_format_threshold(x = x[2], digits = digits) |
| 451 | ||
| 452 | 54x |
paste0("(", l_result, ", ", h_result, ")")
|
| 453 |
} |
|
| 454 |
} |
|
| 455 | ||
| 456 |
#' Format automatically using data significant digits |
|
| 457 |
#' |
|
| 458 |
#' @description `r lifecycle::badge("stable")`
|
|
| 459 |
#' |
|
| 460 |
#' Formatting function for the majority of default methods used in [analyze_vars()]. |
|
| 461 |
#' For non-derived values, the significant digits of data is used (e.g. range), while derived |
|
| 462 |
#' values have one more digits (measure of location and dispersion like mean, standard deviation). |
|
| 463 |
#' This function can be called internally with "auto" like, for example, |
|
| 464 |
#' `.formats = c("mean" = "auto")`. See details to see how this works with the inner function.
|
|
| 465 |
#' |
|
| 466 |
#' @param dt_var (`numeric`)\cr variable data the statistics were calculated from. Used only to |
|
| 467 |
#' find significant digits. In [analyze_vars] this comes from `.df_row` (see |
|
| 468 |
#' [rtables::additional_fun_params]), and it is the row data after the above row splits. No |
|
| 469 |
#' column split is considered. |
|
| 470 |
#' @param x_stat (`string`)\cr string indicating the current statistical method used. |
|
| 471 |
#' |
|
| 472 |
#' @return A string that `rtables` prints in a table cell. |
|
| 473 |
#' |
|
| 474 |
#' @details |
|
| 475 |
#' The internal function is needed to work with `rtables` default structure for |
|
| 476 |
#' format functions, i.e. `function(x, ...)`, where is x are results from statistical evaluation. |
|
| 477 |
#' It can be more than one element (e.g. for `.stats = "mean_sd"`). |
|
| 478 |
#' |
|
| 479 |
#' @examples |
|
| 480 |
#' x_todo <- c(0.001, 0.2, 0.0011000, 3, 4) |
|
| 481 |
#' res <- c(mean(x_todo[1:3]), sd(x_todo[1:3])) |
|
| 482 |
#' |
|
| 483 |
#' # x is the result coming into the formatting function -> res!! |
|
| 484 |
#' format_auto(dt_var = x_todo, x_stat = "mean_sd")(x = res) |
|
| 485 |
#' format_auto(x_todo, "range")(x = range(x_todo)) |
|
| 486 |
#' no_sc_x <- c(0.0000001, 1) |
|
| 487 |
#' format_auto(no_sc_x, "range")(x = no_sc_x) |
|
| 488 |
#' |
|
| 489 |
#' @family formatting functions |
|
| 490 |
#' @export |
|
| 491 |
format_auto <- function(dt_var, x_stat) {
|
|
| 492 | 16x |
function(x = "", ...) {
|
| 493 | 56x |
checkmate::assert_numeric(x, min.len = 1) |
| 494 | 56x |
checkmate::assert_numeric(dt_var, min.len = 1) |
| 495 |
# Defaults - they may be a param in the future |
|
| 496 | 56x |
der_stats <- c( |
| 497 | 56x |
"mean", "sd", "se", "median", "geom_mean", "quantiles", "iqr", |
| 498 | 56x |
"mean_sd", "mean_se", "mean_se", "mean_ci", "mean_sei", "mean_sdi", |
| 499 | 56x |
"median_ci" |
| 500 |
) |
|
| 501 | 56x |
nonder_stats <- c("n", "range", "min", "max")
|
| 502 | ||
| 503 |
# Safenet for miss-modifications |
|
| 504 | 56x |
stopifnot(length(intersect(der_stats, nonder_stats)) == 0) # nolint |
| 505 | 56x |
checkmate::assert_choice(x_stat, c(der_stats, nonder_stats)) |
| 506 | ||
| 507 |
# Finds the max number of digits in data |
|
| 508 | 56x |
detect_dig <- vapply(dt_var, count_decimalplaces, FUN.VALUE = numeric(1)) %>% |
| 509 | 56x |
max() |
| 510 | ||
| 511 | 56x |
if (x_stat %in% der_stats) {
|
| 512 | 40x |
detect_dig <- detect_dig + 1 |
| 513 |
} |
|
| 514 | ||
| 515 |
# Render input |
|
| 516 | 56x |
str_vals <- formatC(x, digits = detect_dig, format = "f") |
| 517 | 56x |
def_fmt <- get_formats_from_stats(x_stat)[[x_stat]] |
| 518 | 56x |
str_fmt <- str_extract(def_fmt, invert = FALSE)[[1]] |
| 519 | 56x |
if (length(str_fmt) != length(str_vals)) {
|
| 520 | 2x |
stop( |
| 521 | 2x |
"Number of inserted values as result (", length(str_vals),
|
| 522 | 2x |
") is not the same as there should be in the default tern formats for ", |
| 523 | 2x |
x_stat, " (-> ", def_fmt, " needs ", length(str_fmt), " values). ", |
| 524 | 2x |
"See tern_default_formats to check all of them." |
| 525 |
) |
|
| 526 |
} |
|
| 527 | ||
| 528 |
# Squashing them together |
|
| 529 | 54x |
inv_str_fmt <- str_extract(def_fmt, invert = TRUE)[[1]] |
| 530 | 54x |
stopifnot(length(inv_str_fmt) == length(str_vals) + 1) # nolint |
| 531 | ||
| 532 | 54x |
out <- vector("character", length = length(inv_str_fmt) + length(str_vals))
|
| 533 | 54x |
is_even <- seq_along(out) %% 2 == 0 |
| 534 | 54x |
out[is_even] <- str_vals |
| 535 | 54x |
out[!is_even] <- inv_str_fmt |
| 536 | ||
| 537 | 54x |
return(paste0(out, collapse = "")) |
| 538 |
} |
|
| 539 |
} |
|
| 540 | ||
| 541 |
# Utility function that could be useful in general |
|
| 542 |
str_extract <- function(string, pattern = "xx|xx\\.|xx\\.x+", invert = FALSE) {
|
|
| 543 | 110x |
regmatches(string, gregexpr(pattern, string), invert = invert) |
| 544 |
} |
|
| 545 | ||
| 546 |
# Helper function |
|
| 547 |
count_decimalplaces <- function(dec) {
|
|
| 548 | 2038x |
if (is.na(dec)) {
|
| 549 | 6x |
return(0) |
| 550 | 2032x |
} else if (abs(dec - round(dec)) > .Machine$double.eps^0.5) { # For precision
|
| 551 | 1939x |
nchar(strsplit(format(dec, scientific = FALSE, trim = FALSE), ".", fixed = TRUE)[[1]][[2]]) |
| 552 |
} else {
|
|
| 553 | 93x |
return(0) |
| 554 |
} |
|
| 555 |
} |
|
| 556 | ||
| 557 |
#' Apply automatic formatting |
|
| 558 |
#' |
|
| 559 |
#' Checks if any of the listed formats in `.formats` are `"auto"`, and replaces `"auto"` with |
|
| 560 |
#' the correct implementation of `format_auto` for the given statistics, data, and variable. |
|
| 561 |
#' |
|
| 562 |
#' @inheritParams argument_convention |
|
| 563 |
#' @param x_stats (named `list`)\cr a named list of statistics where each element corresponds |
|
| 564 |
#' to an element in `.formats`, with matching names. |
|
| 565 |
#' |
|
| 566 |
#' @keywords internal |
|
| 567 |
apply_auto_formatting <- function(.formats, x_stats, .df_row, .var) {
|
|
| 568 | 1574x |
is_auto_fmt <- vapply(.formats, function(ii) is.character(ii) && ii == "auto", logical(1)) |
| 569 | 1574x |
if (any(is_auto_fmt)) {
|
| 570 | 8x |
auto_stats <- x_stats[is_auto_fmt] |
| 571 | 8x |
var_df <- .df_row[[.var]] # xxx this can be extended for the WHOLE data or single facets |
| 572 | 8x |
.formats[is_auto_fmt] <- lapply(names(auto_stats), format_auto, dt_var = var_df) |
| 573 |
} |
|
| 574 | 1574x |
.formats |
| 575 |
} |
| 1 |
#' Create a forest plot from an `rtable` |
|
| 2 |
#' |
|
| 3 |
#' Given a [rtables::rtable()] object with at least one column with a single value and one column with 2 |
|
| 4 |
#' values, converts table to a [ggplot2::ggplot()] object and generates an accompanying forest plot. The |
|
| 5 |
#' table and forest plot are printed side-by-side. |
|
| 6 |
#' |
|
| 7 |
#' @description `r lifecycle::badge("stable")`
|
|
| 8 |
#' |
|
| 9 |
#' @inheritParams rtable2gg |
|
| 10 |
#' @inheritParams argument_convention |
|
| 11 |
#' @param tbl (`VTableTree`)\cr `rtables` table with at least one column with a single value and one column with 2 |
|
| 12 |
#' values. |
|
| 13 |
#' @param col_x (`integer(1)` or `NULL`)\cr column index with estimator. By default tries to get this from |
|
| 14 |
#' `tbl` attribute `col_x`, otherwise needs to be manually specified. If `NULL`, points will be excluded |
|
| 15 |
#' from forest plot. |
|
| 16 |
#' @param col_ci (`integer(1)` or `NULL`)\cr column index with confidence intervals. By default tries to get this from |
|
| 17 |
#' `tbl` attribute `col_ci`, otherwise needs to be manually specified. If `NULL`, lines will be excluded |
|
| 18 |
#' from forest plot. |
|
| 19 |
#' @param vline (`numeric(1)` or `NULL`)\cr x coordinate for vertical line, if `NULL` then the line is omitted. |
|
| 20 |
#' @param forest_header (`character(2)`)\cr text displayed to the left and right of `vline`, respectively. |
|
| 21 |
#' If `vline = NULL` then `forest_header` is not printed. By default tries to get this from `tbl` attribute |
|
| 22 |
#' `forest_header`. If `NULL`, defaults will be extracted from the table if possible, and set to |
|
| 23 |
#' `"Comparison\nBetter"` and `"Treatment\nBetter"` if not. |
|
| 24 |
#' @param xlim (`numeric(2)`)\cr limits for x axis. |
|
| 25 |
#' @param logx (`flag`)\cr show the x-values on logarithm scale. |
|
| 26 |
#' @param x_at (`numeric`)\cr x-tick locations, if `NULL`, `x_at` is set to `vline` and both `xlim` values. |
|
| 27 |
#' @param width_row_names `r lifecycle::badge("deprecated")` Please use the `lbl_col_padding` argument instead.
|
|
| 28 |
#' @param width_columns (`numeric`)\cr a vector of column widths. Each element's position in |
|
| 29 |
#' `colwidths` corresponds to the column of `tbl` in the same position. If `NULL`, column widths are calculated |
|
| 30 |
#' according to maximum number of characters per column. |
|
| 31 |
#' @param width_forest `r lifecycle::badge("deprecated")` Please use the `rel_width_forest` argument instead.
|
|
| 32 |
#' @param rel_width_forest (`proportion`)\cr proportion of total width to allocate to the forest plot. Relative |
|
| 33 |
#' width of table is then `1 - rel_width_forest`. If `as_list = TRUE`, this parameter is ignored. |
|
| 34 |
#' @param font_size (`numeric(1)`)\cr font size. |
|
| 35 |
#' @param col_symbol_size (`numeric` or `NULL`)\cr column index from `tbl` containing data to be used |
|
| 36 |
#' to determine relative size for estimator plot symbol. Typically, the symbol size is proportional |
|
| 37 |
#' to the sample size used to calculate the estimator. If `NULL`, the same symbol size is used for all subgroups. |
|
| 38 |
#' By default tries to get this from `tbl` attribute `col_symbol_size`, otherwise needs to be manually specified. |
|
| 39 |
#' @param col (`character`)\cr color(s). |
|
| 40 |
#' @param ggtheme (`theme`)\cr a graphical theme as provided by `ggplot2` to control styling of the plot. |
|
| 41 |
#' @param as_list (`flag`)\cr whether the two `ggplot` objects should be returned as a list. If `TRUE`, a named list |
|
| 42 |
#' with two elements, `table` and `plot`, will be returned. If `FALSE` (default) the table and forest plot are |
|
| 43 |
#' printed side-by-side via [cowplot::plot_grid()]. |
|
| 44 |
#' @param gp `r lifecycle::badge("deprecated")` `g_forest` is now generated as a `ggplot` object. This argument
|
|
| 45 |
#' is no longer used. |
|
| 46 |
#' @param draw `r lifecycle::badge("deprecated")` `g_forest` is now generated as a `ggplot` object. This argument
|
|
| 47 |
#' is no longer used. |
|
| 48 |
#' @param newpage `r lifecycle::badge("deprecated")` `g_forest` is now generated as a `ggplot` object. This argument
|
|
| 49 |
#' is no longer used. |
|
| 50 |
#' |
|
| 51 |
#' @return `ggplot` forest plot and table. |
|
| 52 |
#' |
|
| 53 |
#' @examples |
|
| 54 |
#' library(dplyr) |
|
| 55 |
#' library(forcats) |
|
| 56 |
#' |
|
| 57 |
#' adrs <- tern_ex_adrs |
|
| 58 |
#' n_records <- 20 |
|
| 59 |
#' adrs_labels <- formatters::var_labels(adrs, fill = TRUE) |
|
| 60 |
#' adrs <- adrs %>% |
|
| 61 |
#' filter(PARAMCD == "BESRSPI") %>% |
|
| 62 |
#' filter(ARM %in% c("A: Drug X", "B: Placebo")) %>%
|
|
| 63 |
#' slice(seq_len(n_records)) %>% |
|
| 64 |
#' droplevels() %>% |
|
| 65 |
#' mutate( |
|
| 66 |
#' # Reorder levels of factor to make the placebo group the reference arm. |
|
| 67 |
#' ARM = fct_relevel(ARM, "B: Placebo"), |
|
| 68 |
#' rsp = AVALC == "CR" |
|
| 69 |
#' ) |
|
| 70 |
#' formatters::var_labels(adrs) <- c(adrs_labels, "Response") |
|
| 71 |
#' df <- extract_rsp_subgroups( |
|
| 72 |
#' variables = list(rsp = "rsp", arm = "ARM", subgroups = c("SEX", "STRATA2")),
|
|
| 73 |
#' data = adrs |
|
| 74 |
#' ) |
|
| 75 |
#' # Full commonly used response table. |
|
| 76 |
#' |
|
| 77 |
#' tbl <- basic_table() %>% |
|
| 78 |
#' tabulate_rsp_subgroups(df) |
|
| 79 |
#' g_forest(tbl) |
|
| 80 |
#' |
|
| 81 |
#' # Odds ratio only table. |
|
| 82 |
#' |
|
| 83 |
#' tbl_or <- basic_table() %>% |
|
| 84 |
#' tabulate_rsp_subgroups(df, vars = c("n_tot", "or", "ci"))
|
|
| 85 |
#' g_forest( |
|
| 86 |
#' tbl_or, |
|
| 87 |
#' forest_header = c("Comparison\nBetter", "Treatment\nBetter")
|
|
| 88 |
#' ) |
|
| 89 |
#' |
|
| 90 |
#' # Survival forest plot example. |
|
| 91 |
#' adtte <- tern_ex_adtte |
|
| 92 |
#' # Save variable labels before data processing steps. |
|
| 93 |
#' adtte_labels <- formatters::var_labels(adtte, fill = TRUE) |
|
| 94 |
#' adtte_f <- adtte %>% |
|
| 95 |
#' filter( |
|
| 96 |
#' PARAMCD == "OS", |
|
| 97 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
| 98 |
#' SEX %in% c("M", "F")
|
|
| 99 |
#' ) %>% |
|
| 100 |
#' mutate( |
|
| 101 |
#' # Reorder levels of ARM to display reference arm before treatment arm. |
|
| 102 |
#' ARM = droplevels(fct_relevel(ARM, "B: Placebo")), |
|
| 103 |
#' SEX = droplevels(SEX), |
|
| 104 |
#' AVALU = as.character(AVALU), |
|
| 105 |
#' is_event = CNSR == 0 |
|
| 106 |
#' ) |
|
| 107 |
#' labels <- list( |
|
| 108 |
#' "ARM" = adtte_labels["ARM"], |
|
| 109 |
#' "SEX" = adtte_labels["SEX"], |
|
| 110 |
#' "AVALU" = adtte_labels["AVALU"], |
|
| 111 |
#' "is_event" = "Event Flag" |
|
| 112 |
#' ) |
|
| 113 |
#' formatters::var_labels(adtte_f)[names(labels)] <- as.character(labels) |
|
| 114 |
#' df <- extract_survival_subgroups( |
|
| 115 |
#' variables = list( |
|
| 116 |
#' tte = "AVAL", |
|
| 117 |
#' is_event = "is_event", |
|
| 118 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
| 119 |
#' ), |
|
| 120 |
#' data = adtte_f |
|
| 121 |
#' ) |
|
| 122 |
#' table_hr <- basic_table() %>% |
|
| 123 |
#' tabulate_survival_subgroups(df, time_unit = adtte_f$AVALU[1]) |
|
| 124 |
#' g_forest(table_hr) |
|
| 125 |
#' |
|
| 126 |
#' # Works with any `rtable`. |
|
| 127 |
#' tbl <- rtable( |
|
| 128 |
#' header = c("E", "CI", "N"),
|
|
| 129 |
#' rrow("", 1, c(.8, 1.2), 200),
|
|
| 130 |
#' rrow("", 1.2, c(1.1, 1.4), 50)
|
|
| 131 |
#' ) |
|
| 132 |
#' g_forest( |
|
| 133 |
#' tbl = tbl, |
|
| 134 |
#' col_x = 1, |
|
| 135 |
#' col_ci = 2, |
|
| 136 |
#' xlim = c(0.5, 2), |
|
| 137 |
#' x_at = c(0.5, 1, 2), |
|
| 138 |
#' col_symbol_size = 3 |
|
| 139 |
#' ) |
|
| 140 |
#' |
|
| 141 |
#' tbl <- rtable( |
|
| 142 |
#' header = rheader( |
|
| 143 |
#' rrow("", rcell("A", colspan = 2)),
|
|
| 144 |
#' rrow("", "c1", "c2")
|
|
| 145 |
#' ), |
|
| 146 |
#' rrow("row 1", 1, c(.8, 1.2)),
|
|
| 147 |
#' rrow("row 2", 1.2, c(1.1, 1.4))
|
|
| 148 |
#' ) |
|
| 149 |
#' g_forest( |
|
| 150 |
#' tbl = tbl, |
|
| 151 |
#' col_x = 1, |
|
| 152 |
#' col_ci = 2, |
|
| 153 |
#' xlim = c(0.5, 2), |
|
| 154 |
#' x_at = c(0.5, 1, 2), |
|
| 155 |
#' vline = 1, |
|
| 156 |
#' forest_header = c("Hello", "World")
|
|
| 157 |
#' ) |
|
| 158 |
#' |
|
| 159 |
#' @export |
|
| 160 |
g_forest <- function(tbl, |
|
| 161 |
col_x = attr(tbl, "col_x"), |
|
| 162 |
col_ci = attr(tbl, "col_ci"), |
|
| 163 |
vline = 1, |
|
| 164 |
forest_header = attr(tbl, "forest_header"), |
|
| 165 |
xlim = c(0.1, 10), |
|
| 166 |
logx = TRUE, |
|
| 167 |
x_at = c(0.1, 1, 10), |
|
| 168 |
width_row_names = lifecycle::deprecated(), |
|
| 169 |
width_columns = NULL, |
|
| 170 |
width_forest = lifecycle::deprecated(), |
|
| 171 |
lbl_col_padding = 0, |
|
| 172 |
rel_width_forest = 0.25, |
|
| 173 |
font_size = 12, |
|
| 174 |
col_symbol_size = attr(tbl, "col_symbol_size"), |
|
| 175 |
col = getOption("ggplot2.discrete.colour")[1],
|
|
| 176 |
ggtheme = NULL, |
|
| 177 |
as_list = FALSE, |
|
| 178 |
gp = lifecycle::deprecated(), |
|
| 179 |
draw = lifecycle::deprecated(), |
|
| 180 |
newpage = lifecycle::deprecated()) {
|
|
| 181 |
# Deprecated argument warnings |
|
| 182 | 4x |
if (lifecycle::is_present(width_row_names)) {
|
| 183 | 1x |
lifecycle::deprecate_warn( |
| 184 | 1x |
"0.9.4", "g_forest(width_row_names)", "g_forest(lbl_col_padding)", |
| 185 | 1x |
details = "The width of the row label column can be adjusted via the `lbl_col_padding` parameter." |
| 186 |
) |
|
| 187 |
} |
|
| 188 | 4x |
if (lifecycle::is_present(width_forest)) {
|
| 189 | 1x |
lifecycle::deprecate_warn( |
| 190 | 1x |
"0.9.4", "g_forest(width_forest)", "g_forest(rel_width_forest)", |
| 191 | 1x |
details = "Relative width of the forest plot (as a proportion) can be set via the `rel_width_forest` parameter." |
| 192 |
) |
|
| 193 |
} |
|
| 194 | 4x |
if (lifecycle::is_present(gp)) {
|
| 195 | 1x |
lifecycle::deprecate_warn( |
| 196 | 1x |
"0.9.4", "g_forest(gp)", "g_forest(ggtheme)", |
| 197 | 1x |
details = paste( |
| 198 | 1x |
"`g_forest` is now generated as a `ggplot` object.", |
| 199 | 1x |
"Additional display settings should be supplied via the `ggtheme` parameter." |
| 200 |
) |
|
| 201 |
) |
|
| 202 |
} |
|
| 203 | 4x |
if (lifecycle::is_present(draw)) {
|
| 204 | 1x |
lifecycle::deprecate_warn( |
| 205 | 1x |
"0.9.4", "g_forest(draw)", |
| 206 | 1x |
details = "`g_forest` now generates `ggplot` objects. This parameter has no effect." |
| 207 |
) |
|
| 208 |
} |
|
| 209 | 4x |
if (lifecycle::is_present(newpage)) {
|
| 210 | 1x |
lifecycle::deprecate_warn( |
| 211 | 1x |
"0.9.4", "g_forest(newpage)", |
| 212 | 1x |
details = "`g_forest` now generates `ggplot` objects. This parameter has no effect." |
| 213 |
) |
|
| 214 |
} |
|
| 215 | ||
| 216 | 4x |
checkmate::assert_class(tbl, "VTableTree") |
| 217 | 4x |
checkmate::assert_number(col_x, lower = 0, upper = ncol(tbl), null.ok = TRUE) |
| 218 | 4x |
checkmate::assert_number(col_ci, lower = 0, upper = ncol(tbl), null.ok = TRUE) |
| 219 | 4x |
checkmate::assert_number(col_symbol_size, lower = 0, upper = ncol(tbl), null.ok = TRUE) |
| 220 | 4x |
checkmate::assert_number(font_size, lower = 0) |
| 221 | 4x |
checkmate::assert_character(col, null.ok = TRUE) |
| 222 | 4x |
checkmate::assert_true(is.null(col) | length(col) == 1 | length(col) == nrow(tbl)) |
| 223 | ||
| 224 |
# Extract info from table |
|
| 225 | 4x |
mat <- matrix_form(tbl, indent_rownames = TRUE) |
| 226 | 4x |
mat_strings <- formatters::mf_strings(mat) |
| 227 | 4x |
nlines_hdr <- formatters::mf_nlheader(mat) |
| 228 | 4x |
nrows_body <- nrow(mat_strings) - nlines_hdr |
| 229 | 4x |
tbl_stats <- mat_strings[nlines_hdr, -1] |
| 230 | ||
| 231 |
# Generate and modify table as ggplot object |
|
| 232 | 4x |
gg_table <- rtable2gg(tbl, fontsize = font_size, colwidths = width_columns, lbl_col_padding = lbl_col_padding) + |
| 233 | 4x |
theme(plot.margin = margin(0, 0, 0, 0.025, "npc")) |
| 234 | 4x |
gg_table$scales$scales[[1]]$expand <- c(0.01, 0.01) |
| 235 | 4x |
gg_table$scales$scales[[2]]$limits[2] <- nrow(mat_strings) + 1 |
| 236 | 4x |
if (nlines_hdr == 2) {
|
| 237 | 4x |
gg_table$scales$scales[[2]]$expand <- c(0, 0) |
| 238 | 4x |
arms <- unique(mat_strings[1, ][nzchar(trimws(mat_strings[1, ]))]) |
| 239 |
} else {
|
|
| 240 | ! |
arms <- NULL |
| 241 |
} |
|
| 242 | ||
| 243 | 4x |
tbl_df <- as_result_df(tbl) |
| 244 | 4x |
dat_cols <- seq(which(names(tbl_df) == "node_class") + 1, ncol(tbl_df)) |
| 245 | 4x |
tbl_df <- tbl_df[, c(which(names(tbl_df) == "row_num"), dat_cols)] |
| 246 | 4x |
names(tbl_df) <- c("row_num", tbl_stats)
|
| 247 | ||
| 248 |
# Check table data columns |
|
| 249 | 4x |
if (!is.null(col_ci)) {
|
| 250 | 4x |
ci_col <- col_ci + 1 |
| 251 |
} else {
|
|
| 252 | ! |
tbl_df[["empty_ci"]] <- rep(list(c(NA_real_, NA_real_)), nrow(tbl_df)) |
| 253 | ! |
ci_col <- which(names(tbl_df) == "empty_ci") |
| 254 |
} |
|
| 255 | ! |
if (length(tbl_df[, ci_col][[1]]) != 2) stop("CI column must have two elements (lower and upper limits).")
|
| 256 | ||
| 257 | 4x |
if (!is.null(col_x)) {
|
| 258 | 4x |
x_col <- col_x + 1 |
| 259 |
} else {
|
|
| 260 | ! |
tbl_df[["empty_x"]] <- NA_real_ |
| 261 | ! |
x_col <- which(names(tbl_df) == "empty_x") |
| 262 |
} |
|
| 263 | 4x |
if (!is.null(col_symbol_size)) {
|
| 264 | 3x |
sym_size <- unlist(tbl_df[, col_symbol_size + 1]) |
| 265 |
} else {
|
|
| 266 | 1x |
sym_size <- rep(1, nrow(tbl_df)) |
| 267 |
} |
|
| 268 | ||
| 269 | 4x |
tbl_df[, c("ci_lwr", "ci_upr")] <- t(sapply(tbl_df[, ci_col], unlist))
|
| 270 | 4x |
x <- unlist(tbl_df[, x_col]) |
| 271 | 4x |
lwr <- unlist(tbl_df[["ci_lwr"]]) |
| 272 | 4x |
upr <- unlist(tbl_df[["ci_upr"]]) |
| 273 | 4x |
row_num <- nrow(mat_strings) - tbl_df[["row_num"]] - as.numeric(nlines_hdr == 2) |
| 274 | ||
| 275 | ! |
if (is.null(col)) col <- "#343cff" |
| 276 | 4x |
if (length(col) == 1) col <- rep(col, nrow(tbl_df)) |
| 277 | ! |
if (is.null(x_at)) x_at <- union(xlim, vline) |
| 278 | 4x |
x_labels <- x_at |
| 279 | ||
| 280 |
# Apply log transformation |
|
| 281 | 4x |
if (logx) {
|
| 282 | 4x |
x_t <- log(x) |
| 283 | 4x |
lwr_t <- log(lwr) |
| 284 | 4x |
upr_t <- log(upr) |
| 285 | 4x |
xlim_t <- log(xlim) |
| 286 |
} else {
|
|
| 287 | ! |
x_t <- x |
| 288 | ! |
lwr_t <- lwr |
| 289 | ! |
upr_t <- upr |
| 290 | ! |
xlim_t <- xlim |
| 291 |
} |
|
| 292 | ||
| 293 |
# Set up plot area |
|
| 294 | 4x |
gg_plt <- ggplot(data = tbl_df) + |
| 295 | 4x |
theme( |
| 296 | 4x |
panel.background = element_rect(fill = "transparent", color = NA_character_), |
| 297 | 4x |
plot.background = element_rect(fill = "transparent", color = NA_character_), |
| 298 | 4x |
panel.grid.major = element_blank(), |
| 299 | 4x |
panel.grid.minor = element_blank(), |
| 300 | 4x |
axis.title.x = element_blank(), |
| 301 | 4x |
axis.title.y = element_blank(), |
| 302 | 4x |
axis.line.x = element_line(), |
| 303 | 4x |
axis.text = element_text(size = font_size), |
| 304 | 4x |
legend.position = "none", |
| 305 | 4x |
plot.margin = margin(0, 0.1, 0.05, 0, "npc") |
| 306 |
) + |
|
| 307 | 4x |
scale_x_continuous( |
| 308 | 4x |
transform = ifelse(logx, "log", "identity"), |
| 309 | 4x |
limits = xlim, |
| 310 | 4x |
breaks = x_at, |
| 311 | 4x |
labels = x_labels, |
| 312 | 4x |
expand = c(0.01, 0) |
| 313 |
) + |
|
| 314 | 4x |
scale_y_continuous( |
| 315 | 4x |
limits = c(0, nrow(mat_strings) + 1), |
| 316 | 4x |
breaks = NULL, |
| 317 | 4x |
expand = c(0, 0) |
| 318 |
) + |
|
| 319 | 4x |
coord_cartesian(clip = "off") |
| 320 | ||
| 321 | 4x |
if (is.null(ggtheme)) {
|
| 322 | 4x |
gg_plt <- gg_plt + annotate( |
| 323 | 4x |
"rect", |
| 324 | 4x |
xmin = xlim[1], |
| 325 | 4x |
xmax = xlim[2], |
| 326 | 4x |
ymin = 0, |
| 327 | 4x |
ymax = nrows_body + 0.5, |
| 328 | 4x |
fill = "grey92" |
| 329 |
) |
|
| 330 |
} |
|
| 331 | ||
| 332 | 4x |
if (!is.null(vline)) {
|
| 333 |
# Set default forest header |
|
| 334 | 4x |
if (is.null(forest_header)) {
|
| 335 | ! |
forest_header <- c( |
| 336 | ! |
paste(if (length(arms) == 2) arms[1] else "Comparison", "Better", sep = "\n"), |
| 337 | ! |
paste(if (length(arms) == 2) arms[2] else "Treatment", "Better", sep = "\n") |
| 338 |
) |
|
| 339 |
} |
|
| 340 | ||
| 341 |
# Add vline and forest header labels |
|
| 342 | 4x |
mid_pts <- if (logx) {
|
| 343 | 4x |
c(exp(mean(log(c(xlim[1], vline)))), exp(mean(log(c(vline, xlim[2]))))) |
| 344 |
} else {
|
|
| 345 | ! |
c(mean(c(xlim[1], vline)), mean(c(vline, xlim[2]))) |
| 346 |
} |
|
| 347 | 4x |
gg_plt <- gg_plt + |
| 348 | 4x |
annotate( |
| 349 | 4x |
"segment", |
| 350 | 4x |
x = vline, xend = vline, y = 0, yend = nrows_body + 0.5 |
| 351 |
) + |
|
| 352 | 4x |
annotate( |
| 353 | 4x |
"text", |
| 354 | 4x |
x = mid_pts[1], y = nrows_body + 1.25, |
| 355 | 4x |
label = forest_header[1], |
| 356 | 4x |
size = font_size / .pt, |
| 357 | 4x |
lineheight = 0.9 |
| 358 |
) + |
|
| 359 | 4x |
annotate( |
| 360 | 4x |
"text", |
| 361 | 4x |
x = mid_pts[2], y = nrows_body + 1.25, |
| 362 | 4x |
label = forest_header[2], |
| 363 | 4x |
size = font_size / .pt, |
| 364 | 4x |
lineheight = 0.9 |
| 365 |
) |
|
| 366 |
} |
|
| 367 | ||
| 368 |
# Add points to plot |
|
| 369 | 4x |
if (any(!is.na(x_t))) {
|
| 370 | 4x |
x_t[x < xlim[1] | x > xlim[2]] <- NA |
| 371 | 4x |
gg_plt <- gg_plt + geom_point( |
| 372 | 4x |
x = x_t, |
| 373 | 4x |
y = row_num, |
| 374 | 4x |
color = col, |
| 375 | 4x |
aes(size = sym_size), |
| 376 | 4x |
na.rm = TRUE |
| 377 |
) |
|
| 378 |
} |
|
| 379 | ||
| 380 | 4x |
for (i in seq_len(nrow(tbl_df))) {
|
| 381 |
# Determine which arrow(s) to add to CI lines |
|
| 382 | 17x |
which_arrow <- c(lwr_t[i] < xlim_t[1], upr_t[i] > xlim_t[2]) |
| 383 | 17x |
which_arrow <- dplyr::case_when( |
| 384 | 17x |
all(which_arrow) ~ "both", |
| 385 | 17x |
which_arrow[1] ~ "first", |
| 386 | 17x |
which_arrow[2] ~ "last", |
| 387 | 17x |
TRUE ~ NA_character_ |
| 388 |
) |
|
| 389 | ||
| 390 |
# Add CI lines |
|
| 391 | 17x |
gg_plt <- gg_plt + |
| 392 | 17x |
if (!is.na(which_arrow)) {
|
| 393 | 15x |
annotate( |
| 394 | 15x |
"segment", |
| 395 | 15x |
x = if (!which_arrow %in% c("first", "both")) lwr[i] else xlim[1],
|
| 396 | 15x |
xend = if (!which_arrow %in% c("last", "both")) upr[i] else xlim[2],
|
| 397 | 15x |
y = row_num[i], yend = row_num[i], |
| 398 | 15x |
color = if (length(col) == 1) col else col[i], |
| 399 | 15x |
arrow = arrow(length = unit(0.05, "npc"), ends = which_arrow), |
| 400 | 15x |
na.rm = TRUE |
| 401 |
) |
|
| 402 |
} else {
|
|
| 403 | 2x |
annotate( |
| 404 | 2x |
"segment", |
| 405 | 2x |
x = lwr[i], xend = upr[i], |
| 406 | 2x |
y = row_num[i], yend = row_num[i], |
| 407 | 2x |
color = if (length(col) == 1) col else col[i], |
| 408 | 2x |
na.rm = TRUE |
| 409 |
) |
|
| 410 |
} |
|
| 411 |
} |
|
| 412 | ||
| 413 |
# Apply custom ggtheme to plot |
|
| 414 | ! |
if (!is.null(ggtheme)) gg_plt <- gg_plt + ggtheme |
| 415 | ||
| 416 | 4x |
if (as_list) {
|
| 417 | 1x |
list( |
| 418 | 1x |
table = gg_table, |
| 419 | 1x |
plot = gg_plt |
| 420 |
) |
|
| 421 |
} else {
|
|
| 422 | 3x |
cowplot::plot_grid( |
| 423 | 3x |
gg_table, |
| 424 | 3x |
gg_plt, |
| 425 | 3x |
align = "h", |
| 426 | 3x |
axis = "tblr", |
| 427 | 3x |
rel_widths = c(1 - rel_width_forest, rel_width_forest) |
| 428 |
) |
|
| 429 |
} |
|
| 430 |
} |
|
| 431 | ||
| 432 |
#' Forest plot grob |
|
| 433 |
#' |
|
| 434 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 435 |
#' |
|
| 436 |
#' @inheritParams g_forest |
|
| 437 |
#' @param tbl (`VTableTree`)\cr `rtables` table object. |
|
| 438 |
#' @param x (`numeric`)\cr coordinate of point. |
|
| 439 |
#' @param lower,upper (`numeric`)\cr lower/upper bound of the confidence interval. |
|
| 440 |
#' @param symbol_size (`numeric`)\cr vector with relative size for plot symbol. |
|
| 441 |
#' If `NULL`, the same symbol size is used. |
|
| 442 |
#' |
|
| 443 |
#' @details |
|
| 444 |
#' The heights get automatically determined. |
|
| 445 |
#' |
|
| 446 |
#' @examples |
|
| 447 |
#' tbl <- rtable( |
|
| 448 |
#' header = rheader( |
|
| 449 |
#' rrow("", "E", rcell("CI", colspan = 2), "N"),
|
|
| 450 |
#' rrow("", "A", "B", "C", "D")
|
|
| 451 |
#' ), |
|
| 452 |
#' rrow("row 1", 1, 0.8, 1.1, 16),
|
|
| 453 |
#' rrow("row 2", 1.4, 0.8, 1.6, 25),
|
|
| 454 |
#' rrow("row 3", 1.2, 0.8, 1.6, 36)
|
|
| 455 |
#' ) |
|
| 456 |
#' |
|
| 457 |
#' x <- c(1, 1.4, 1.2) |
|
| 458 |
#' lower <- c(0.8, 0.8, 0.8) |
|
| 459 |
#' upper <- c(1.1, 1.6, 1.6) |
|
| 460 |
#' # numeric vector with multiplication factor to scale each circle radius |
|
| 461 |
#' # default radius is 1/3.5 lines |
|
| 462 |
#' symbol_scale <- c(1, 1.25, 1.5) |
|
| 463 |
#' |
|
| 464 |
#' # Internal function - forest_grob |
|
| 465 |
#' \donttest{
|
|
| 466 |
#' p <- forest_grob(tbl, x, lower, upper, |
|
| 467 |
#' vline = 1, forest_header = c("A", "B"),
|
|
| 468 |
#' x_at = c(.1, 1, 10), xlim = c(0.1, 10), logx = TRUE, symbol_size = symbol_scale, |
|
| 469 |
#' vp = grid::plotViewport(margins = c(1, 1, 1, 1)) |
|
| 470 |
#' ) |
|
| 471 |
#' |
|
| 472 |
#' draw_grob(p) |
|
| 473 |
#' } |
|
| 474 |
#' |
|
| 475 |
#' @noRd |
|
| 476 |
#' @keywords internal |
|
| 477 |
forest_grob <- function(tbl, |
|
| 478 |
x, |
|
| 479 |
lower, |
|
| 480 |
upper, |
|
| 481 |
vline, |
|
| 482 |
forest_header, |
|
| 483 |
xlim = NULL, |
|
| 484 |
logx = FALSE, |
|
| 485 |
x_at = NULL, |
|
| 486 |
width_row_names = NULL, |
|
| 487 |
width_columns = NULL, |
|
| 488 |
width_forest = grid::unit(1, "null"), |
|
| 489 |
symbol_size = NULL, |
|
| 490 |
col = "blue", |
|
| 491 |
name = NULL, |
|
| 492 |
gp = NULL, |
|
| 493 |
vp = NULL) {
|
|
| 494 | 1x |
lifecycle::deprecate_warn( |
| 495 | 1x |
"0.9.4", "forest_grob()", |
| 496 | 1x |
details = "`g_forest` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 497 |
) |
|
| 498 | ||
| 499 | 1x |
nr <- nrow(tbl) |
| 500 | 1x |
if (is.null(vline)) {
|
| 501 | ! |
checkmate::assert_true(is.null(forest_header)) |
| 502 |
} else {
|
|
| 503 | 1x |
checkmate::assert_number(vline) |
| 504 | 1x |
checkmate::assert_character(forest_header, len = 2, null.ok = TRUE) |
| 505 |
} |
|
| 506 | ||
| 507 | 1x |
checkmate::assert_numeric(x, len = nr) |
| 508 | 1x |
checkmate::assert_numeric(lower, len = nr) |
| 509 | 1x |
checkmate::assert_numeric(upper, len = nr) |
| 510 | 1x |
checkmate::assert_numeric(symbol_size, len = nr, null.ok = TRUE) |
| 511 | 1x |
checkmate::assert_character(col) |
| 512 | ||
| 513 | 1x |
if (is.null(symbol_size)) {
|
| 514 | ! |
symbol_size <- rep(1, nr) |
| 515 |
} |
|
| 516 | ||
| 517 | 1x |
if (is.null(xlim)) {
|
| 518 | ! |
r <- range(c(x, lower, upper), na.rm = TRUE) |
| 519 | ! |
xlim <- r + c(-0.05, 0.05) * diff(r) |
| 520 |
} |
|
| 521 | ||
| 522 | 1x |
if (logx) {
|
| 523 | 1x |
if (is.null(x_at)) {
|
| 524 | ! |
x_at <- pretty(log(stats::na.omit(c(x, lower, upper)))) |
| 525 | ! |
x_labels <- exp(x_at) |
| 526 |
} else {
|
|
| 527 | 1x |
x_labels <- x_at |
| 528 | 1x |
x_at <- log(x_at) |
| 529 |
} |
|
| 530 | 1x |
xlim <- log(xlim) |
| 531 | 1x |
x <- log(x) |
| 532 | 1x |
lower <- log(lower) |
| 533 | 1x |
upper <- log(upper) |
| 534 | 1x |
if (!is.null(vline)) {
|
| 535 | 1x |
vline <- log(vline) |
| 536 |
} |
|
| 537 |
} else {
|
|
| 538 | ! |
x_labels <- TRUE |
| 539 |
} |
|
| 540 | ||
| 541 | 1x |
data_forest_vp <- grid::dataViewport(xlim, c(0, 1)) |
| 542 | ||
| 543 |
# Get table content as matrix form. |
|
| 544 | 1x |
mf <- matrix_form(tbl) |
| 545 | ||
| 546 |
# Use `rtables` indent_string eventually. |
|
| 547 | 1x |
mf$strings[, 1] <- paste0( |
| 548 | 1x |
strrep(" ", c(rep(0, attr(mf, "nrow_header")), mf$row_info$indent)),
|
| 549 | 1x |
mf$strings[, 1] |
| 550 |
) |
|
| 551 | ||
| 552 | 1x |
n_header <- attr(mf, "nrow_header") |
| 553 | ||
| 554 | ! |
if (any(mf$display[, 1] == FALSE)) stop("row names need to be always displayed")
|
| 555 | ||
| 556 |
# Pre-process the data to be used in lapply and cell_in_rows. |
|
| 557 | 1x |
to_args_for_cell_in_rows_fun <- function(part = c("body", "header"),
|
| 558 | 1x |
underline_colspan = FALSE) {
|
| 559 | 2x |
part <- match.arg(part) |
| 560 | 2x |
if (part == "body") {
|
| 561 | 1x |
mat_row_indices <- seq_len(nrow(tbl)) + n_header |
| 562 | 1x |
row_ind_offset <- -n_header |
| 563 |
} else {
|
|
| 564 | 1x |
mat_row_indices <- seq_len(n_header) |
| 565 | 1x |
row_ind_offset <- 0 |
| 566 |
} |
|
| 567 | ||
| 568 | 2x |
lapply(mat_row_indices, function(i) {
|
| 569 | 5x |
disp <- mf$display[i, -1] |
| 570 | 5x |
list( |
| 571 | 5x |
row_name = mf$strings[i, 1], |
| 572 | 5x |
cells = mf$strings[i, -1][disp], |
| 573 | 5x |
cell_spans = mf$spans[i, -1][disp], |
| 574 | 5x |
row_index = i + row_ind_offset, |
| 575 | 5x |
underline_colspan = underline_colspan |
| 576 |
) |
|
| 577 |
}) |
|
| 578 |
} |
|
| 579 | ||
| 580 | 1x |
args_header <- to_args_for_cell_in_rows_fun("header", underline_colspan = TRUE)
|
| 581 | 1x |
args_body <- to_args_for_cell_in_rows_fun("body", underline_colspan = FALSE)
|
| 582 | ||
| 583 | 1x |
grid::gTree( |
| 584 | 1x |
name = name, |
| 585 | 1x |
children = grid::gList( |
| 586 | 1x |
grid::gTree( |
| 587 | 1x |
children = do.call(grid::gList, lapply(args_header, do.call, what = cell_in_rows)), |
| 588 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_header")
|
| 589 |
), |
|
| 590 | 1x |
grid::gTree( |
| 591 | 1x |
children = do.call(grid::gList, lapply(args_body, do.call, what = cell_in_rows)), |
| 592 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_body")
|
| 593 |
), |
|
| 594 | 1x |
grid::linesGrob( |
| 595 | 1x |
grid::unit(c(0, 1), "npc"), |
| 596 | 1x |
y = grid::unit(c(.5, .5), "npc"), |
| 597 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_spacer")
|
| 598 |
), |
|
| 599 |
# forest part |
|
| 600 | 1x |
if (is.null(vline)) {
|
| 601 | ! |
NULL |
| 602 |
} else {
|
|
| 603 | 1x |
grid::gTree( |
| 604 | 1x |
children = grid::gList( |
| 605 | 1x |
grid::gTree( |
| 606 | 1x |
children = grid::gList( |
| 607 | 1x |
grid::textGrob( |
| 608 | 1x |
forest_header[1], |
| 609 | 1x |
x = grid::unit(vline, "native") - grid::unit(1, "lines"), |
| 610 | 1x |
just = c("right", "center")
|
| 611 |
), |
|
| 612 | 1x |
grid::textGrob( |
| 613 | 1x |
forest_header[2], |
| 614 | 1x |
x = grid::unit(vline, "native") + grid::unit(1, "lines"), |
| 615 | 1x |
just = c("left", "center")
|
| 616 |
) |
|
| 617 |
), |
|
| 618 | 1x |
vp = grid::vpStack(grid::viewport(layout.pos.col = ncol(tbl) + 2), data_forest_vp) |
| 619 |
) |
|
| 620 |
), |
|
| 621 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_header")
|
| 622 |
) |
|
| 623 |
}, |
|
| 624 | 1x |
grid::gTree( |
| 625 | 1x |
children = grid::gList( |
| 626 | 1x |
grid::gTree( |
| 627 | 1x |
children = grid::gList( |
| 628 | 1x |
grid::rectGrob(gp = grid::gpar(col = "gray90", fill = "gray90")), |
| 629 | 1x |
if (is.null(vline)) {
|
| 630 | ! |
NULL |
| 631 |
} else {
|
|
| 632 | 1x |
grid::linesGrob( |
| 633 | 1x |
x = grid::unit(rep(vline, 2), "native"), |
| 634 | 1x |
y = grid::unit(c(0, 1), "npc"), |
| 635 | 1x |
gp = grid::gpar(lwd = 2), |
| 636 | 1x |
vp = data_forest_vp |
| 637 |
) |
|
| 638 |
}, |
|
| 639 | 1x |
grid::xaxisGrob(at = x_at, label = x_labels, vp = data_forest_vp) |
| 640 |
), |
|
| 641 | 1x |
vp = grid::viewport(layout.pos.col = ncol(tbl) + 2) |
| 642 |
) |
|
| 643 |
), |
|
| 644 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_body")
|
| 645 |
), |
|
| 646 | 1x |
grid::gTree( |
| 647 | 1x |
children = do.call( |
| 648 | 1x |
grid::gList, |
| 649 | 1x |
Map( |
| 650 | 1x |
function(xi, li, ui, row_index, size_i, col) {
|
| 651 | 3x |
forest_dot_line( |
| 652 | 3x |
xi, |
| 653 | 3x |
li, |
| 654 | 3x |
ui, |
| 655 | 3x |
row_index, |
| 656 | 3x |
xlim, |
| 657 | 3x |
symbol_size = size_i, |
| 658 | 3x |
col = col, |
| 659 | 3x |
datavp = data_forest_vp |
| 660 |
) |
|
| 661 |
}, |
|
| 662 | 1x |
x, |
| 663 | 1x |
lower, |
| 664 | 1x |
upper, |
| 665 | 1x |
seq_along(x), |
| 666 | 1x |
symbol_size, |
| 667 | 1x |
col, |
| 668 | 1x |
USE.NAMES = FALSE |
| 669 |
) |
|
| 670 |
), |
|
| 671 | 1x |
vp = grid::vpPath("vp_table_layout", "vp_body")
|
| 672 |
) |
|
| 673 |
), |
|
| 674 | 1x |
childrenvp = forest_viewport(tbl, width_row_names, width_columns, width_forest), |
| 675 | 1x |
vp = vp, |
| 676 | 1x |
gp = gp |
| 677 |
) |
|
| 678 |
} |
|
| 679 | ||
| 680 |
cell_in_rows <- function(row_name, |
|
| 681 |
cells, |
|
| 682 |
cell_spans, |
|
| 683 |
row_index, |
|
| 684 |
underline_colspan = FALSE) {
|
|
| 685 | 5x |
checkmate::assert_string(row_name) |
| 686 | 5x |
checkmate::assert_character(cells, min.len = 1, any.missing = FALSE) |
| 687 | 5x |
checkmate::assert_numeric(cell_spans, len = length(cells), any.missing = FALSE) |
| 688 | 5x |
checkmate::assert_number(row_index) |
| 689 | 5x |
checkmate::assert_flag(underline_colspan) |
| 690 | ||
| 691 | 5x |
vp_name_rn <- paste0("rowname-", row_index)
|
| 692 | 5x |
g_rowname <- if (!is.null(row_name) && row_name != "") {
|
| 693 | 3x |
grid::textGrob( |
| 694 | 3x |
name = vp_name_rn, |
| 695 | 3x |
label = row_name, |
| 696 | 3x |
x = grid::unit(0, "npc"), |
| 697 | 3x |
just = c("left", "center"),
|
| 698 | 3x |
vp = grid::vpPath(paste0("rowname-", row_index))
|
| 699 |
) |
|
| 700 |
} else {
|
|
| 701 | 2x |
NULL |
| 702 |
} |
|
| 703 | ||
| 704 | 5x |
gl_cols <- if (!(length(cells) > 0)) {
|
| 705 | ! |
list(NULL) |
| 706 |
} else {
|
|
| 707 | 5x |
j <- 1 # column index of cell |
| 708 | ||
| 709 | 5x |
lapply(seq_along(cells), function(k) {
|
| 710 | 19x |
cell_ascii <- cells[[k]] |
| 711 | 19x |
cs <- cell_spans[[k]] |
| 712 | ||
| 713 | 19x |
if (is.na(cell_ascii) || is.null(cell_ascii)) {
|
| 714 | ! |
cell_ascii <- "NA" |
| 715 |
} |
|
| 716 | ||
| 717 | 19x |
cell_name <- paste0("g-cell-", row_index, "-", j)
|
| 718 | ||
| 719 | 19x |
cell_grobs <- if (identical(cell_ascii, "")) {
|
| 720 | ! |
NULL |
| 721 |
} else {
|
|
| 722 | 19x |
if (cs == 1) {
|
| 723 | 18x |
grid::textGrob( |
| 724 | 18x |
label = cell_ascii, |
| 725 | 18x |
name = cell_name, |
| 726 | 18x |
vp = grid::vpPath(paste0("cell-", row_index, "-", j))
|
| 727 |
) |
|
| 728 |
} else {
|
|
| 729 |
# +1 because of rowname |
|
| 730 | 1x |
vp_joined_cols <- grid::viewport(layout.pos.row = row_index, layout.pos.col = seq(j + 1, j + cs)) |
| 731 | ||
| 732 | 1x |
lab <- grid::textGrob( |
| 733 | 1x |
label = cell_ascii, |
| 734 | 1x |
name = cell_name, |
| 735 | 1x |
vp = vp_joined_cols |
| 736 |
) |
|
| 737 | ||
| 738 | 1x |
if (!underline_colspan || grepl("^[[:space:]]*$", cell_ascii)) {
|
| 739 | ! |
lab |
| 740 |
} else {
|
|
| 741 | 1x |
grid::gList( |
| 742 | 1x |
lab, |
| 743 | 1x |
grid::linesGrob( |
| 744 | 1x |
x = grid::unit.c(grid::unit(.2, "lines"), grid::unit(1, "npc") - grid::unit(.2, "lines")), |
| 745 | 1x |
y = grid::unit(c(0, 0), "npc"), |
| 746 | 1x |
vp = vp_joined_cols |
| 747 |
) |
|
| 748 |
) |
|
| 749 |
} |
|
| 750 |
} |
|
| 751 |
} |
|
| 752 | 19x |
j <<- j + cs |
| 753 | ||
| 754 | 19x |
cell_grobs |
| 755 |
}) |
|
| 756 |
} |
|
| 757 | ||
| 758 | 5x |
grid::gList( |
| 759 | 5x |
g_rowname, |
| 760 | 5x |
do.call(grid::gList, gl_cols) |
| 761 |
) |
|
| 762 |
} |
|
| 763 | ||
| 764 |
#' Graphic object: forest dot line |
|
| 765 |
#' |
|
| 766 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 767 |
#' |
|
| 768 |
#' Calculate the `grob` corresponding to the dot line within the forest plot. |
|
| 769 |
#' |
|
| 770 |
#' @noRd |
|
| 771 |
#' @keywords internal |
|
| 772 |
forest_dot_line <- function(x, |
|
| 773 |
lower, |
|
| 774 |
upper, |
|
| 775 |
row_index, |
|
| 776 |
xlim, |
|
| 777 |
symbol_size = 1, |
|
| 778 |
col = "blue", |
|
| 779 |
datavp) {
|
|
| 780 | 3x |
lifecycle::deprecate_warn( |
| 781 | 3x |
"0.9.4", "forest_dot_line()", |
| 782 | 3x |
details = "`g_forest` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 783 |
) |
|
| 784 | ||
| 785 | 3x |
ci <- c(lower, upper) |
| 786 | 3x |
if (any(!is.na(c(x, ci)))) {
|
| 787 |
# line |
|
| 788 | 3x |
y <- grid::unit(c(0.5, 0.5), "npc") |
| 789 | ||
| 790 | 3x |
g_line <- if (all(!is.na(ci)) && ci[2] > xlim[1] && ci[1] < xlim[2]) {
|
| 791 |
# - |
|
| 792 | 3x |
if (ci[1] >= xlim[1] && ci[2] <= xlim[2]) {
|
| 793 | 3x |
grid::linesGrob(x = grid::unit(c(ci[1], ci[2]), "native"), y = y) |
| 794 | ! |
} else if (ci[1] < xlim[1] && ci[2] > xlim[2]) {
|
| 795 |
# <-> |
|
| 796 | ! |
grid::linesGrob( |
| 797 | ! |
x = grid::unit(xlim, "native"), |
| 798 | ! |
y = y, |
| 799 | ! |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "both") |
| 800 |
) |
|
| 801 | ! |
} else if (ci[1] < xlim[1] && ci[2] <= xlim[2]) {
|
| 802 |
# <- |
|
| 803 | ! |
grid::linesGrob( |
| 804 | ! |
x = grid::unit(c(xlim[1], ci[2]), "native"), |
| 805 | ! |
y = y, |
| 806 | ! |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "first") |
| 807 |
) |
|
| 808 | ! |
} else if (ci[1] >= xlim[1] && ci[2] > xlim[2]) {
|
| 809 |
# -> |
|
| 810 | ! |
grid::linesGrob( |
| 811 | ! |
x = grid::unit(c(ci[1], xlim[2]), "native"), |
| 812 | ! |
y = y, |
| 813 | ! |
arrow = grid::arrow(angle = 30, length = grid::unit(0.5, "lines"), ends = "last") |
| 814 |
) |
|
| 815 |
} |
|
| 816 |
} else {
|
|
| 817 | ! |
NULL |
| 818 |
} |
|
| 819 | ||
| 820 | 3x |
g_circle <- if (!is.na(x) && x >= xlim[1] && x <= xlim[2]) {
|
| 821 | 3x |
grid::circleGrob( |
| 822 | 3x |
x = grid::unit(x, "native"), |
| 823 | 3x |
y = y, |
| 824 | 3x |
r = grid::unit(1 / 3.5 * symbol_size, "lines"), |
| 825 | 3x |
name = "point" |
| 826 |
) |
|
| 827 |
} else {
|
|
| 828 | ! |
NULL |
| 829 |
} |
|
| 830 | ||
| 831 | 3x |
grid::gTree( |
| 832 | 3x |
children = grid::gList( |
| 833 | 3x |
grid::gTree( |
| 834 | 3x |
children = grid::gList( |
| 835 | 3x |
grid::gList( |
| 836 | 3x |
g_line, |
| 837 | 3x |
g_circle |
| 838 |
) |
|
| 839 |
), |
|
| 840 | 3x |
vp = datavp, |
| 841 | 3x |
gp = grid::gpar(col = col, fill = col) |
| 842 |
) |
|
| 843 |
), |
|
| 844 | 3x |
vp = grid::vpPath(paste0("forest-", row_index))
|
| 845 |
) |
|
| 846 |
} else {
|
|
| 847 | ! |
NULL |
| 848 |
} |
|
| 849 |
} |
|
| 850 | ||
| 851 |
#' Create a viewport tree for the forest plot |
|
| 852 |
#' |
|
| 853 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 854 |
#' |
|
| 855 |
#' @param tbl (`VTableTree`)\cr `rtables` table object. |
|
| 856 |
#' @param width_row_names (`grid::unit`)\cr width of row names. |
|
| 857 |
#' @param width_columns (`grid::unit`)\cr width of column spans. |
|
| 858 |
#' @param width_forest (`grid::unit`)\cr width of the forest plot. |
|
| 859 |
#' @param gap_column (`grid::unit`)\cr gap width between the columns. |
|
| 860 |
#' @param gap_header (`grid::unit`)\cr gap width between the header. |
|
| 861 |
#' @param mat_form (`MatrixPrintForm`)\cr matrix print form of the table. |
|
| 862 |
#' |
|
| 863 |
#' @return A viewport tree. |
|
| 864 |
#' |
|
| 865 |
#' @examples |
|
| 866 |
#' library(grid) |
|
| 867 |
#' |
|
| 868 |
#' tbl <- rtable( |
|
| 869 |
#' header = rheader( |
|
| 870 |
#' rrow("", "E", rcell("CI", colspan = 2)),
|
|
| 871 |
#' rrow("", "A", "B", "C")
|
|
| 872 |
#' ), |
|
| 873 |
#' rrow("row 1", 1, 0.8, 1.1),
|
|
| 874 |
#' rrow("row 2", 1.4, 0.8, 1.6),
|
|
| 875 |
#' rrow("row 3", 1.2, 0.8, 1.2)
|
|
| 876 |
#' ) |
|
| 877 |
#' |
|
| 878 |
#' \donttest{
|
|
| 879 |
#' v <- forest_viewport(tbl) |
|
| 880 |
#' |
|
| 881 |
#' grid::grid.newpage() |
|
| 882 |
#' showViewport(v) |
|
| 883 |
#' } |
|
| 884 |
#' |
|
| 885 |
#' @export |
|
| 886 |
forest_viewport <- function(tbl, |
|
| 887 |
width_row_names = NULL, |
|
| 888 |
width_columns = NULL, |
|
| 889 |
width_forest = grid::unit(1, "null"), |
|
| 890 |
gap_column = grid::unit(1, "lines"), |
|
| 891 |
gap_header = grid::unit(1, "lines"), |
|
| 892 |
mat_form = NULL) {
|
|
| 893 | 2x |
lifecycle::deprecate_warn( |
| 894 | 2x |
"0.9.4", |
| 895 | 2x |
"forest_viewport()", |
| 896 | 2x |
details = "`g_forest` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 897 |
) |
|
| 898 | ||
| 899 | 2x |
checkmate::assert_class(tbl, "VTableTree") |
| 900 | 2x |
checkmate::assert_true(grid::is.unit(width_forest)) |
| 901 | 2x |
if (!is.null(width_row_names)) {
|
| 902 | ! |
checkmate::assert_true(grid::is.unit(width_row_names)) |
| 903 |
} |
|
| 904 | 2x |
if (!is.null(width_columns)) {
|
| 905 | ! |
checkmate::assert_true(grid::is.unit(width_columns)) |
| 906 |
} |
|
| 907 | ||
| 908 | 2x |
if (is.null(mat_form)) mat_form <- matrix_form(tbl) |
| 909 | ||
| 910 | 2x |
mat_form$strings[!mat_form$display] <- "" |
| 911 | ||
| 912 | 2x |
nr <- nrow(tbl) |
| 913 | 2x |
nc <- ncol(tbl) |
| 914 | 2x |
nr_h <- attr(mat_form, "nrow_header") |
| 915 | ||
| 916 | 2x |
if (is.null(width_row_names) || is.null(width_columns)) {
|
| 917 | 2x |
tbl_widths <- formatters::propose_column_widths(mat_form) |
| 918 | 2x |
strs_with_width <- strrep("x", tbl_widths) # that works for mono spaced fonts
|
| 919 | 2x |
if (is.null(width_row_names)) width_row_names <- grid::stringWidth(strs_with_width[1]) |
| 920 | 2x |
if (is.null(width_columns)) width_columns <- grid::stringWidth(strs_with_width[-1]) |
| 921 |
} |
|
| 922 | ||
| 923 |
# Widths for row name, cols, forest. |
|
| 924 | 2x |
widths <- grid::unit.c( |
| 925 | 2x |
width_row_names + gap_column, |
| 926 | 2x |
width_columns + gap_column, |
| 927 | 2x |
width_forest |
| 928 |
) |
|
| 929 | ||
| 930 | 2x |
n_lines_per_row <- apply( |
| 931 | 2x |
X = mat_form$strings, |
| 932 | 2x |
MARGIN = 1, |
| 933 | 2x |
FUN = function(row) {
|
| 934 | 10x |
tmp <- vapply( |
| 935 | 10x |
gregexpr("\n", row, fixed = TRUE),
|
| 936 | 10x |
attr, numeric(1), |
| 937 | 10x |
"match.length" |
| 938 | 10x |
) + 1 |
| 939 | 10x |
max(c(tmp, 1)) |
| 940 |
} |
|
| 941 |
) |
|
| 942 | ||
| 943 | 2x |
i_header <- seq_len(nr_h) |
| 944 | ||
| 945 | 2x |
height_body_rows <- grid::unit(n_lines_per_row[-i_header] * 1.2, "lines") |
| 946 | 2x |
height_header_rows <- grid::unit(n_lines_per_row[i_header] * 1.2, "lines") |
| 947 | ||
| 948 | 2x |
height_body <- grid::unit(sum(n_lines_per_row[-i_header]) * 1.2, "lines") |
| 949 | 2x |
height_header <- grid::unit(sum(n_lines_per_row[i_header]) * 1.2, "lines") |
| 950 | ||
| 951 | 2x |
nc_g <- nc + 2 # number of columns incl. row names and forest |
| 952 | ||
| 953 | 2x |
vp_tbl <- grid::vpTree( |
| 954 | 2x |
parent = grid::viewport( |
| 955 | 2x |
name = "vp_table_layout", |
| 956 | 2x |
layout = grid::grid.layout( |
| 957 | 2x |
nrow = 3, ncol = 1, |
| 958 | 2x |
heights = grid::unit.c(height_header, gap_header, height_body) |
| 959 |
) |
|
| 960 |
), |
|
| 961 | 2x |
children = grid::vpList( |
| 962 | 2x |
vp_forest_table_part(nr_h, nc_g, 1, 1, widths, height_header_rows, "vp_header"), |
| 963 | 2x |
vp_forest_table_part(nr, nc_g, 3, 1, widths, height_body_rows, "vp_body"), |
| 964 | 2x |
grid::viewport(name = "vp_spacer", layout.pos.row = 2, layout.pos.col = 1) |
| 965 |
) |
|
| 966 |
) |
|
| 967 | 2x |
vp_tbl |
| 968 |
} |
|
| 969 | ||
| 970 |
#' Viewport forest plot: table part |
|
| 971 |
#' |
|
| 972 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 973 |
#' |
|
| 974 |
#' Prepares a viewport for the table included in the forest plot. |
|
| 975 |
#' |
|
| 976 |
#' @noRd |
|
| 977 |
#' @keywords internal |
|
| 978 |
vp_forest_table_part <- function(nrow, |
|
| 979 |
ncol, |
|
| 980 |
l_row, |
|
| 981 |
l_col, |
|
| 982 |
widths, |
|
| 983 |
heights, |
|
| 984 |
name) {
|
|
| 985 | 4x |
lifecycle::deprecate_warn( |
| 986 | 4x |
"0.9.4", "vp_forest_table_part()", |
| 987 | 4x |
details = "`g_forest` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 988 |
) |
|
| 989 | ||
| 990 | 4x |
grid::vpTree( |
| 991 | 4x |
grid::viewport( |
| 992 | 4x |
name = name, |
| 993 | 4x |
layout.pos.row = l_row, |
| 994 | 4x |
layout.pos.col = l_col, |
| 995 | 4x |
layout = grid::grid.layout(nrow = nrow, ncol = ncol, widths = widths, heights = heights) |
| 996 |
), |
|
| 997 | 4x |
children = grid::vpList( |
| 998 | 4x |
do.call( |
| 999 | 4x |
grid::vpList, |
| 1000 | 4x |
lapply( |
| 1001 | 4x |
seq_len(nrow), function(i) {
|
| 1002 | 10x |
grid::viewport(layout.pos.row = i, layout.pos.col = 1, name = paste0("rowname-", i))
|
| 1003 |
} |
|
| 1004 |
) |
|
| 1005 |
), |
|
| 1006 | 4x |
do.call( |
| 1007 | 4x |
grid::vpList, |
| 1008 | 4x |
apply( |
| 1009 | 4x |
expand.grid(seq_len(nrow), seq_len(ncol - 2)), |
| 1010 | 4x |
1, |
| 1011 | 4x |
function(x) {
|
| 1012 | 35x |
i <- x[1] |
| 1013 | 35x |
j <- x[2] |
| 1014 | 35x |
grid::viewport(layout.pos.row = i, layout.pos.col = j + 1, name = paste0("cell-", i, "-", j))
|
| 1015 |
} |
|
| 1016 |
) |
|
| 1017 |
), |
|
| 1018 | 4x |
do.call( |
| 1019 | 4x |
grid::vpList, |
| 1020 | 4x |
lapply( |
| 1021 | 4x |
seq_len(nrow), |
| 1022 | 4x |
function(i) {
|
| 1023 | 10x |
grid::viewport(layout.pos.row = i, layout.pos.col = ncol, name = paste0("forest-", i))
|
| 1024 |
} |
|
| 1025 |
) |
|
| 1026 |
) |
|
| 1027 |
) |
|
| 1028 |
) |
|
| 1029 |
} |
|
| 1030 | ||
| 1031 |
#' Forest rendering |
|
| 1032 |
#' |
|
| 1033 |
#' @description `r lifecycle::badge("deprecated")`
|
|
| 1034 |
#' |
|
| 1035 |
#' Renders the forest grob. |
|
| 1036 |
#' |
|
| 1037 |
#' @noRd |
|
| 1038 |
#' @keywords internal |
|
| 1039 |
grid.forest <- function(...) { # nolint
|
|
| 1040 | ! |
lifecycle::deprecate_warn( |
| 1041 | ! |
"0.9.4", "grid.forest()", |
| 1042 | ! |
details = "`g_forest` now generates `ggplot` objects. This function is no longer used within `tern`." |
| 1043 |
) |
|
| 1044 | ||
| 1045 | ! |
grid::grid.draw(forest_grob(...)) |
| 1046 |
} |
| 1 |
#' Additional assertions to use with `checkmate` |
|
| 2 |
#' |
|
| 3 |
#' Additional assertion functions which can be used together with the `checkmate` package. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams checkmate::assert_factor |
|
| 6 |
#' @param x (`any`)\cr object to test. |
|
| 7 |
#' @param df (`data.frame`)\cr data set to test. |
|
| 8 |
#' @param variables (named `list` of `character`)\cr list of variables to test. |
|
| 9 |
#' @param include_boundaries (`flag`)\cr whether to include boundaries when testing |
|
| 10 |
#' for proportions. |
|
| 11 |
#' @param na_level (`string`)\cr the string you have been using to represent NA or |
|
| 12 |
#' missing data. For `NA` values please consider using directly [is.na()] or |
|
| 13 |
#' similar approaches. |
|
| 14 |
#' |
|
| 15 |
#' @return Nothing if assertion passes, otherwise prints the error message. |
|
| 16 |
#' |
|
| 17 |
#' @name assertions |
|
| 18 |
NULL |
|
| 19 | ||
| 20 |
check_list_of_variables <- function(x) {
|
|
| 21 |
# drop NULL elements in list |
|
| 22 | 2999x |
x <- Filter(Negate(is.null), x) |
| 23 | ||
| 24 | 2999x |
res <- checkmate::check_list(x, |
| 25 | 2999x |
names = "named", |
| 26 | 2999x |
min.len = 1, |
| 27 | 2999x |
any.missing = FALSE, |
| 28 | 2999x |
types = "character" |
| 29 |
) |
|
| 30 |
# no empty strings allowed |
|
| 31 | 2999x |
if (isTRUE(res)) {
|
| 32 | 2994x |
res <- checkmate::check_character(unlist(x), min.chars = 1) |
| 33 |
} |
|
| 34 | 2999x |
return(res) |
| 35 |
} |
|
| 36 |
#' @describeIn assertions Checks whether `x` is a valid list of variable names. |
|
| 37 |
#' `NULL` elements of the list `x` are dropped with `Filter(Negate(is.null), x)`. |
|
| 38 |
#' |
|
| 39 |
#' @keywords internal |
|
| 40 |
assert_list_of_variables <- checkmate::makeAssertionFunction(check_list_of_variables) |
|
| 41 | ||
| 42 |
check_df_with_variables <- function(df, variables, na_level = NULL) {
|
|
| 43 | 2682x |
checkmate::assert_data_frame(df) |
| 44 | 2680x |
assert_list_of_variables(variables) |
| 45 | ||
| 46 |
# flag for equal variables and column names |
|
| 47 | 2678x |
err_flag <- all(unlist(variables) %in% colnames(df)) |
| 48 | 2678x |
checkmate::assert_flag(err_flag) |
| 49 | ||
| 50 | 2678x |
if (isFALSE(err_flag)) {
|
| 51 | 5x |
vars <- setdiff(unlist(variables), colnames(df)) |
| 52 | 5x |
return(paste( |
| 53 | 5x |
deparse(substitute(df)), |
| 54 | 5x |
"does not contain all specified variables as column names. Missing from data frame:", |
| 55 | 5x |
paste(vars, collapse = ", ") |
| 56 |
)) |
|
| 57 |
} |
|
| 58 |
# checking if na_level is present and in which column |
|
| 59 | 2673x |
if (!is.null(na_level)) {
|
| 60 | 9x |
checkmate::assert_string(na_level) |
| 61 | 9x |
res <- unlist(lapply(as.list(df)[unlist(variables)], function(x) any(x == na_level))) |
| 62 | 9x |
if (any(res)) {
|
| 63 | 1x |
return(paste0( |
| 64 | 1x |
deparse(substitute(df)), " contains explicit na_level (", na_level,
|
| 65 | 1x |
") in the following columns: ", paste0(unlist(variables)[res], |
| 66 | 1x |
collapse = ", " |
| 67 |
) |
|
| 68 |
)) |
|
| 69 |
} |
|
| 70 |
} |
|
| 71 | 2672x |
return(TRUE) |
| 72 |
} |
|
| 73 |
#' @describeIn assertions Check whether `df` is a data frame with the analysis `variables`. |
|
| 74 |
#' Please notice how this produces an error when not all variables are present in the |
|
| 75 |
#' data.frame while the opposite is not required. |
|
| 76 |
#' |
|
| 77 |
#' @keywords internal |
|
| 78 |
assert_df_with_variables <- checkmate::makeAssertionFunction(check_df_with_variables) |
|
| 79 | ||
| 80 |
check_valid_factor <- function(x, |
|
| 81 |
min.levels = 1, # nolint |
|
| 82 |
max.levels = NULL, # nolint |
|
| 83 |
null.ok = TRUE, # nolint |
|
| 84 |
any.missing = TRUE, # nolint |
|
| 85 |
n.levels = NULL, # nolint |
|
| 86 |
len = NULL) {
|
|
| 87 |
# checks on levels insertion |
|
| 88 | 1113x |
checkmate::assert_int(min.levels, lower = 1) |
| 89 | ||
| 90 |
# main factor check |
|
| 91 | 1113x |
res <- checkmate::check_factor(x, |
| 92 | 1113x |
min.levels = min.levels, |
| 93 | 1113x |
null.ok = null.ok, |
| 94 | 1113x |
max.levels = max.levels, |
| 95 | 1113x |
any.missing = any.missing, |
| 96 | 1113x |
n.levels = n.levels |
| 97 |
) |
|
| 98 | ||
| 99 |
# no empty strings allowed |
|
| 100 | 1113x |
if (isTRUE(res)) {
|
| 101 | 1099x |
res <- checkmate::check_character(levels(x), min.chars = 1) |
| 102 |
} |
|
| 103 | ||
| 104 | 1113x |
return(res) |
| 105 |
} |
|
| 106 |
#' @describeIn assertions Check whether `x` is a valid factor (i.e. has levels and no empty |
|
| 107 |
#' string levels). Note that `NULL` and `NA` elements are allowed. |
|
| 108 |
#' |
|
| 109 |
#' @keywords internal |
|
| 110 |
assert_valid_factor <- checkmate::makeAssertionFunction(check_valid_factor) |
|
| 111 | ||
| 112 |
check_df_with_factors <- function(df, |
|
| 113 |
variables, |
|
| 114 |
min.levels = 1, # nolint |
|
| 115 |
max.levels = NULL, # nolint |
|
| 116 |
any.missing = TRUE, # nolint |
|
| 117 |
na_level = NULL) {
|
|
| 118 | 254x |
res <- check_df_with_variables(df, variables, na_level) |
| 119 |
# checking if all the columns specified by variables are valid factors |
|
| 120 | 253x |
if (isTRUE(res)) {
|
| 121 |
# searching the data.frame with selected columns (variables) as a list |
|
| 122 | 251x |
res <- lapply( |
| 123 | 251x |
X = as.list(df)[unlist(variables)], |
| 124 | 251x |
FUN = check_valid_factor, |
| 125 | 251x |
min.levels = min.levels, |
| 126 | 251x |
max.levels = max.levels, |
| 127 | 251x |
any.missing = any.missing |
| 128 |
) |
|
| 129 | 251x |
res_lo <- unlist(vapply(res, Negate(isTRUE), logical(1))) |
| 130 | 251x |
if (any(res_lo)) {
|
| 131 | 6x |
return(paste0( |
| 132 | 6x |
deparse(substitute(df)), " does not contain only factor variables among:", |
| 133 | 6x |
"\n* Column `", paste0(unlist(variables)[res_lo], |
| 134 | 6x |
"` of the data.frame -> ", res[res_lo], |
| 135 | 6x |
collapse = "\n* " |
| 136 |
) |
|
| 137 |
)) |
|
| 138 |
} else {
|
|
| 139 | 245x |
res <- TRUE |
| 140 |
} |
|
| 141 |
} |
|
| 142 | 247x |
return(res) |
| 143 |
} |
|
| 144 | ||
| 145 |
#' @describeIn assertions Check whether `df` is a data frame where the analysis `variables` |
|
| 146 |
#' are all factors. Note that the creation of `NA` by direct call of `factor()` will |
|
| 147 |
#' trim `NA` levels out of the vector list itself. |
|
| 148 |
#' |
|
| 149 |
#' @keywords internal |
|
| 150 |
assert_df_with_factors <- checkmate::makeAssertionFunction(check_df_with_factors) |
|
| 151 | ||
| 152 |
#' @describeIn assertions Check whether `x` is a proportion: number between 0 and 1. |
|
| 153 |
#' |
|
| 154 |
#' @keywords internal |
|
| 155 |
assert_proportion_value <- function(x, include_boundaries = FALSE) {
|
|
| 156 | 18909x |
checkmate::assert_number(x, lower = 0, upper = 1) |
| 157 | 18897x |
checkmate::assert_flag(include_boundaries) |
| 158 | 18897x |
if (isFALSE(include_boundaries)) {
|
| 159 | 12969x |
checkmate::assert_true(x > 0) |
| 160 | 12967x |
checkmate::assert_true(x < 1) |
| 161 |
} |
|
| 162 |
} |
| 1 |
#' Survival time point analysis |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [surv_timepoint()] creates a layout element to analyze patient survival rates and difference |
|
| 6 |
#' of survival rates between groups at a given time point. The primary analysis variable `vars` is the time variable. |
|
| 7 |
#' Other required inputs are `time_point`, the numeric time point of interest, and `is_event`, a variable that |
|
| 8 |
#' indicates whether or not an event has occurred. The `method` argument is used to specify whether you want to analyze |
|
| 9 |
#' survival estimations (`"surv"`), difference in survival with the control (`"surv_diff"`), or both of these |
|
| 10 |
#' (`"both"`). |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams argument_convention |
|
| 13 |
#' @inheritParams s_surv_time |
|
| 14 |
#' @param time_point (`numeric(1)`)\cr survival time point of interest. |
|
| 15 |
#' @param control (`list`)\cr parameters for comparison details, specified by using the helper function |
|
| 16 |
#' [control_surv_timepoint()]. Some possible parameter options are: |
|
| 17 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for survival rate. |
|
| 18 |
#' * `conf_type` (`string`)\cr confidence interval type. Options are "plain" (default), "log", "log-log", |
|
| 19 |
#' see more in [survival::survfit()]. Note option "none" is no longer supported. |
|
| 20 |
#' @param method (`string`)\cr `"surv"` (survival estimations), `"surv_diff"` (difference in survival with the |
|
| 21 |
#' control), or `"both"`. |
|
| 22 |
#' @param table_names_suffix (`string`)\cr optional suffix for the `table_names` used for the `rtables` to |
|
| 23 |
#' avoid warnings from duplicate table names. |
|
| 24 |
#' @param .indent_mods (named `integer`)\cr indent modifiers for the labels. Each element of the vector |
|
| 25 |
#' should be a name-value pair with name corresponding to a statistic specified in `.stats` and value the indentation |
|
| 26 |
#' for that statistic's row label. |
|
| 27 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 28 |
#' |
|
| 29 |
#' Options are: ``r shQuote(get_stats("surv_timepoint"), type = "sh")``
|
|
| 30 |
#' |
|
| 31 |
#' @name survival_timepoint |
|
| 32 |
#' @order 1 |
|
| 33 |
NULL |
|
| 34 | ||
| 35 |
#' @describeIn survival_timepoint Statistics function which analyzes survival rate. |
|
| 36 |
#' |
|
| 37 |
#' @return |
|
| 38 |
#' * `s_surv_timepoint()` returns the statistics: |
|
| 39 |
#' * `pt_at_risk`: Patients remaining at risk. |
|
| 40 |
#' * `event_free_rate`: Event-free rate (%). |
|
| 41 |
#' * `rate_se`: Standard error of event free rate. |
|
| 42 |
#' * `rate_ci`: Confidence interval for event free rate. |
|
| 43 |
#' * `event_free_rate_3d`: Event-free rate (%) with Confidence interval. |
|
| 44 |
#' |
|
| 45 |
#' @keywords internal |
|
| 46 |
s_surv_timepoint <- function(df, |
|
| 47 |
.var, |
|
| 48 |
time_point, |
|
| 49 |
is_event, |
|
| 50 |
control = control_surv_timepoint(), |
|
| 51 |
...) {
|
|
| 52 | 35x |
checkmate::assert_string(.var) |
| 53 | 35x |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
| 54 | 35x |
checkmate::assert_numeric(df[[.var]], min.len = 1, any.missing = FALSE) |
| 55 | 35x |
checkmate::assert_number(time_point) |
| 56 | 35x |
checkmate::assert_logical(df[[is_event]], min.len = 1, any.missing = FALSE) |
| 57 | ||
| 58 | 35x |
conf_type <- control$conf_type |
| 59 | 35x |
conf_level <- control$conf_level |
| 60 | ||
| 61 | 35x |
formula <- stats::as.formula(paste0("survival::Surv(", .var, ", ", is_event, ") ~ 1"))
|
| 62 | 35x |
srv_fit <- survival::survfit( |
| 63 | 35x |
formula = formula, |
| 64 | 35x |
data = df, |
| 65 | 35x |
conf.int = conf_level, |
| 66 | 35x |
conf.type = conf_type |
| 67 |
) |
|
| 68 | 35x |
s_srv_fit <- summary(srv_fit, times = time_point, extend = TRUE) |
| 69 | 35x |
df_srv_fit <- as.data.frame(s_srv_fit[c("time", "n.risk", "surv", "lower", "upper", "std.err")])
|
| 70 | 35x |
if (df_srv_fit[["n.risk"]] == 0) {
|
| 71 | 1x |
pt_at_risk <- event_free_rate <- rate_se <- NA_real_ |
| 72 | 1x |
rate_ci <- c(NA_real_, NA_real_) |
| 73 |
} else {
|
|
| 74 | 34x |
pt_at_risk <- df_srv_fit$n.risk |
| 75 | 34x |
event_free_rate <- df_srv_fit$surv |
| 76 | 34x |
rate_se <- df_srv_fit$std.err |
| 77 | 34x |
rate_ci <- c(df_srv_fit$lower, df_srv_fit$upper) |
| 78 |
} |
|
| 79 | 35x |
event_free_rate_3d <- c(event_free_rate, rate_ci) |
| 80 | 35x |
list( |
| 81 | 35x |
pt_at_risk = formatters::with_label(pt_at_risk, "Patients remaining at risk"), |
| 82 | 35x |
event_free_rate = formatters::with_label(event_free_rate * 100, "Event Free Rate (%)"), |
| 83 | 35x |
rate_se = formatters::with_label(rate_se * 100, "Standard Error of Event Free Rate"), |
| 84 | 35x |
rate_ci = formatters::with_label(rate_ci * 100, f_conf_level(conf_level)), |
| 85 | 35x |
event_free_rate_3d = formatters::with_label( |
| 86 | 35x |
event_free_rate_3d * 100, paste0("Event Free Rate (", f_conf_level(conf_level), ")")
|
| 87 |
) |
|
| 88 |
) |
|
| 89 |
} |
|
| 90 | ||
| 91 |
#' @describeIn survival_timepoint Statistics function which analyzes difference between two survival rates. |
|
| 92 |
#' |
|
| 93 |
#' @return |
|
| 94 |
#' * `s_surv_timepoint_diff()` returns the statistics: |
|
| 95 |
#' * `rate_diff`: Event-free rate difference between two groups. |
|
| 96 |
#' * `rate_diff_ci`: Confidence interval for the difference. |
|
| 97 |
#' * `rate_diff_ci_3d`: Event-free rate difference and confidence interval between two groups. |
|
| 98 |
#' * `ztest_pval`: p-value to test the difference is 0. |
|
| 99 |
#' |
|
| 100 |
#' @keywords internal |
|
| 101 |
s_surv_timepoint_diff <- function(df, |
|
| 102 |
.var, |
|
| 103 |
.ref_group, |
|
| 104 |
.in_ref_col, |
|
| 105 |
time_point, |
|
| 106 |
control = control_surv_timepoint(), |
|
| 107 |
...) {
|
|
| 108 | 14x |
if (.in_ref_col) {
|
| 109 | 4x |
return( |
| 110 | 4x |
list( |
| 111 | 4x |
rate_diff = formatters::with_label(numeric(), "Difference in Event Free Rate"), |
| 112 | 4x |
rate_diff_ci = formatters::with_label(numeric(), f_conf_level(control$conf_level)), |
| 113 | 4x |
rate_diff_ci_3d = formatters::with_label( |
| 114 | 4x |
numeric(), paste0("Difference in Event Free Rate", f_conf_level(control$conf_level))
|
| 115 |
), |
|
| 116 | 4x |
ztest_pval = formatters::with_label(numeric(), "p-value (Z-test)") |
| 117 |
) |
|
| 118 |
) |
|
| 119 |
} |
|
| 120 | 10x |
data <- rbind(.ref_group, df) |
| 121 | 10x |
group <- factor(rep(c("ref", "x"), c(nrow(.ref_group), nrow(df))), levels = c("ref", "x"))
|
| 122 | 10x |
res_per_group <- lapply(split(data, group), function(x) {
|
| 123 | 20x |
s_surv_timepoint(df = x, .var = .var, time_point = time_point, control = control, ...) |
| 124 |
}) |
|
| 125 | ||
| 126 | 10x |
res_x <- res_per_group[[2]] |
| 127 | 10x |
res_ref <- res_per_group[[1]] |
| 128 | 10x |
rate_diff <- res_x$event_free_rate - res_ref$event_free_rate |
| 129 | 10x |
se_diff <- sqrt(res_x$rate_se^2 + res_ref$rate_se^2) |
| 130 | ||
| 131 | 10x |
qs <- c(-1, 1) * stats::qnorm(1 - (1 - control$conf_level) / 2) |
| 132 | 10x |
rate_diff_ci <- rate_diff + qs * se_diff |
| 133 | 10x |
rate_diff_ci_3d <- c(rate_diff, rate_diff_ci) |
| 134 | 10x |
ztest_pval <- if (is.na(rate_diff)) {
|
| 135 | 10x |
NA |
| 136 |
} else {
|
|
| 137 | 10x |
2 * (1 - stats::pnorm(abs(rate_diff) / se_diff)) |
| 138 |
} |
|
| 139 | 10x |
list( |
| 140 | 10x |
rate_diff = formatters::with_label(rate_diff, "Difference in Event Free Rate"), |
| 141 | 10x |
rate_diff_ci = formatters::with_label(rate_diff_ci, f_conf_level(control$conf_level)), |
| 142 | 10x |
rate_diff_ci_3d = formatters::with_label( |
| 143 | 10x |
rate_diff_ci_3d, paste0("Difference in Event Free Rate", f_conf_level(control$conf_level))
|
| 144 |
), |
|
| 145 | 10x |
ztest_pval = formatters::with_label(ztest_pval, "p-value (Z-test)") |
| 146 |
) |
|
| 147 |
} |
|
| 148 | ||
| 149 |
#' @describeIn survival_timepoint Formatted analysis function which is used as `afun` in `surv_timepoint()`. |
|
| 150 |
#' |
|
| 151 |
#' @return |
|
| 152 |
#' * `a_surv_timepoint()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 153 |
#' |
|
| 154 |
#' @keywords internal |
|
| 155 |
a_surv_timepoint <- function(df, |
|
| 156 |
..., |
|
| 157 |
.stats = NULL, |
|
| 158 |
.stat_names = NULL, |
|
| 159 |
.formats = NULL, |
|
| 160 |
.labels = NULL, |
|
| 161 |
.indent_mods = NULL) {
|
|
| 162 |
# Check for additional parameters to the statistics function |
|
| 163 | 24x |
dots_extra_args <- list(...) |
| 164 | 24x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 165 | 24x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 166 | 24x |
method <- dots_extra_args$method |
| 167 | ||
| 168 |
# Check for user-defined functions |
|
| 169 | 24x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 170 | 24x |
.stats <- default_and_custom_stats_list$all_stats |
| 171 | 24x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 172 | ||
| 173 |
# Apply statistics function |
|
| 174 | 24x |
x_stats <- .apply_stat_functions( |
| 175 | 24x |
default_stat_fnc = if (method == "surv") s_surv_timepoint else s_surv_timepoint_diff, |
| 176 | 24x |
custom_stat_fnc_list = custom_stat_functions, |
| 177 | 24x |
args_list = c( |
| 178 | 24x |
df = list(df), |
| 179 | 24x |
extra_afun_params, |
| 180 | 24x |
dots_extra_args |
| 181 |
) |
|
| 182 |
) |
|
| 183 | ||
| 184 |
# Fill in formatting defaults |
|
| 185 | 24x |
.stats <- get_stats(if (method == "surv") "surv_timepoint" else "surv_timepoint_diff", |
| 186 | 24x |
stats_in = .stats, |
| 187 | 24x |
custom_stats_in = names(custom_stat_functions) |
| 188 |
) |
|
| 189 | 24x |
x_stats <- x_stats[.stats] |
| 190 | 24x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 191 | 24x |
.labels <- get_labels_from_stats( |
| 192 | 24x |
.stats, .labels, |
| 193 | 24x |
tern_defaults = c(lapply(x_stats, attr, "label"), tern_default_labels) |
| 194 |
) |
|
| 195 | 24x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 196 | ||
| 197 |
# Auto format handling |
|
| 198 | 24x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 199 | ||
| 200 |
# Get and check statistical names |
|
| 201 | 24x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 202 | ||
| 203 | 24x |
in_rows( |
| 204 | 24x |
.list = x_stats, |
| 205 | 24x |
.formats = .formats, |
| 206 | 24x |
.names = .labels %>% .unlist_keep_nulls(), |
| 207 | 24x |
.stat_names = .stat_names, |
| 208 | 24x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 209 | 24x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 210 |
) |
|
| 211 |
} |
|
| 212 | ||
| 213 |
#' @describeIn survival_timepoint Layout-creating function which can take statistics function arguments |
|
| 214 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 215 |
#' |
|
| 216 |
#' @return |
|
| 217 |
#' * `surv_timepoint()` returns a layout object suitable for passing to further layouting functions, |
|
| 218 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 219 |
#' the statistics from `s_surv_timepoint()` and/or `s_surv_timepoint_diff()` to the table layout depending on |
|
| 220 |
#' the value of `method`. |
|
| 221 |
#' |
|
| 222 |
#' @examples |
|
| 223 |
#' library(dplyr) |
|
| 224 |
#' |
|
| 225 |
#' adtte_f <- tern_ex_adtte %>% |
|
| 226 |
#' filter(PARAMCD == "OS") %>% |
|
| 227 |
#' mutate( |
|
| 228 |
#' AVAL = day2month(AVAL), |
|
| 229 |
#' is_event = CNSR == 0 |
|
| 230 |
#' ) |
|
| 231 |
#' |
|
| 232 |
#' # Survival at given time points. |
|
| 233 |
#' basic_table() %>% |
|
| 234 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>% |
|
| 235 |
#' add_colcounts() %>% |
|
| 236 |
#' surv_timepoint( |
|
| 237 |
#' vars = "AVAL", |
|
| 238 |
#' var_labels = "Months", |
|
| 239 |
#' is_event = "is_event", |
|
| 240 |
#' time_point = 7 |
|
| 241 |
#' ) %>% |
|
| 242 |
#' build_table(df = adtte_f) |
|
| 243 |
#' |
|
| 244 |
#' # Difference in survival at given time points. |
|
| 245 |
#' basic_table() %>% |
|
| 246 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>% |
|
| 247 |
#' add_colcounts() %>% |
|
| 248 |
#' surv_timepoint( |
|
| 249 |
#' vars = "AVAL", |
|
| 250 |
#' var_labels = "Months", |
|
| 251 |
#' is_event = "is_event", |
|
| 252 |
#' time_point = 9, |
|
| 253 |
#' method = "surv_diff", |
|
| 254 |
#' .indent_mods = c("rate_diff" = 0L, "rate_diff_ci" = 2L, "ztest_pval" = 2L)
|
|
| 255 |
#' ) %>% |
|
| 256 |
#' build_table(df = adtte_f) |
|
| 257 |
#' |
|
| 258 |
#' # Survival and difference in survival at given time points. |
|
| 259 |
#' basic_table() %>% |
|
| 260 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>% |
|
| 261 |
#' add_colcounts() %>% |
|
| 262 |
#' surv_timepoint( |
|
| 263 |
#' vars = "AVAL", |
|
| 264 |
#' var_labels = "Months", |
|
| 265 |
#' is_event = "is_event", |
|
| 266 |
#' time_point = 9, |
|
| 267 |
#' method = "both" |
|
| 268 |
#' ) %>% |
|
| 269 |
#' build_table(df = adtte_f) |
|
| 270 |
#' |
|
| 271 |
#' @export |
|
| 272 |
#' @order 2 |
|
| 273 |
surv_timepoint <- function(lyt, |
|
| 274 |
vars, |
|
| 275 |
time_point, |
|
| 276 |
is_event, |
|
| 277 |
control = control_surv_timepoint(), |
|
| 278 |
method = c("surv", "surv_diff", "both"),
|
|
| 279 |
na_str = default_na_str(), |
|
| 280 |
nested = TRUE, |
|
| 281 |
..., |
|
| 282 |
table_names_suffix = "", |
|
| 283 |
var_labels = "Time", |
|
| 284 |
show_labels = "visible", |
|
| 285 |
.stats = c( |
|
| 286 |
"pt_at_risk", "event_free_rate", "rate_ci", |
|
| 287 |
"rate_diff", "rate_diff_ci", "ztest_pval" |
|
| 288 |
), |
|
| 289 |
.stat_names = NULL, |
|
| 290 |
.formats = list(rate_ci = "(xx.xx, xx.xx)"), |
|
| 291 |
.labels = NULL, |
|
| 292 |
.indent_mods = if (method == "both") {
|
|
| 293 | 2x |
c(rate_diff = 1L, rate_diff_ci = 2L, ztest_pval = 2L) |
| 294 |
} else {
|
|
| 295 | 4x |
c(rate_diff_ci = 1L, ztest_pval = 1L) |
| 296 |
}) {
|
|
| 297 | 6x |
method <- match.arg(method) |
| 298 | 6x |
checkmate::assert_string(table_names_suffix) |
| 299 | ||
| 300 |
# Process standard extra arguments |
|
| 301 | 6x |
extra_args <- list(".stats" = .stats)
|
| 302 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 303 | 6x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 304 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 305 | 6x |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 306 | ||
| 307 |
# Process additional arguments to the statistic function |
|
| 308 | 6x |
extra_args <- c( |
| 309 | 6x |
extra_args, |
| 310 | 6x |
time_point = list(time_point), is_event = is_event, control = list(control), |
| 311 |
... |
|
| 312 |
) |
|
| 313 | ||
| 314 |
# Append additional info from layout to the analysis function |
|
| 315 | 6x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 316 | 6x |
formals(a_surv_timepoint) <- c(formals(a_surv_timepoint), extra_args[[".additional_fun_parameters"]]) |
| 317 | ||
| 318 | 6x |
for (i in seq_along(time_point)) {
|
| 319 | 6x |
extra_args[["time_point"]] <- time_point[i] |
| 320 | ||
| 321 | 6x |
if (method %in% c("surv", "both")) {
|
| 322 | 4x |
extra_args_i <- extra_args |
| 323 | 4x |
extra_args_i[["method"]] <- "surv" |
| 324 | ||
| 325 | 4x |
lyt <- analyze( |
| 326 | 4x |
lyt = lyt, |
| 327 | 4x |
vars = vars, |
| 328 | 4x |
afun = a_surv_timepoint, |
| 329 | 4x |
na_str = na_str, |
| 330 | 4x |
nested = nested, |
| 331 | 4x |
extra_args = extra_args_i, |
| 332 | 4x |
var_labels = paste(time_point[i], var_labels), |
| 333 | 4x |
show_labels = show_labels, |
| 334 | 4x |
table_names = paste0("surv_", time_point[i], table_names_suffix)
|
| 335 |
) |
|
| 336 |
} |
|
| 337 | ||
| 338 | 6x |
if (method %in% c("surv_diff", "both")) {
|
| 339 | 4x |
extra_args_i <- extra_args |
| 340 | 4x |
extra_args_i[["method"]] <- "surv_diff" |
| 341 | ||
| 342 | 4x |
lyt <- analyze( |
| 343 | 4x |
lyt = lyt, |
| 344 | 4x |
vars = vars, |
| 345 | 4x |
afun = a_surv_timepoint, |
| 346 | 4x |
na_str = na_str, |
| 347 | 4x |
nested = nested, |
| 348 | 4x |
extra_args = extra_args_i, |
| 349 | 4x |
var_labels = paste(time_point[i], var_labels), |
| 350 | 4x |
show_labels = ifelse(method == "both", "hidden", show_labels), |
| 351 | 4x |
table_names = paste0("surv_diff_", time_point[i], table_names_suffix)
|
| 352 |
) |
|
| 353 |
} |
|
| 354 |
} |
|
| 355 | ||
| 356 | 6x |
lyt |
| 357 |
} |
| 1 |
#' Create a STEP graph |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Based on the STEP results, creates a `ggplot` graph showing the estimated HR or OR |
|
| 6 |
#' along the continuous biomarker value subgroups. |
|
| 7 |
#' |
|
| 8 |
#' @param df (`tibble`)\cr result of [tidy.step()]. |
|
| 9 |
#' @param use_percentile (`flag`)\cr whether to use percentiles for the x axis or actual |
|
| 10 |
#' biomarker values. |
|
| 11 |
#' @param est (named `list`)\cr `col` and `lty` settings for estimate line. |
|
| 12 |
#' @param ci_ribbon (named `list` or `NULL`)\cr `fill` and `alpha` settings for the confidence interval |
|
| 13 |
#' ribbon area, or `NULL` to not plot a CI ribbon. |
|
| 14 |
#' @param col (`character`)\cr color(s). |
|
| 15 |
#' |
|
| 16 |
#' @return A `ggplot` STEP graph. |
|
| 17 |
#' |
|
| 18 |
#' @seealso Custom tidy method [tidy.step()]. |
|
| 19 |
#' |
|
| 20 |
#' @examples |
|
| 21 |
#' library(survival) |
|
| 22 |
#' lung$sex <- factor(lung$sex) |
|
| 23 |
#' |
|
| 24 |
#' # Survival example. |
|
| 25 |
#' vars <- list( |
|
| 26 |
#' time = "time", |
|
| 27 |
#' event = "status", |
|
| 28 |
#' arm = "sex", |
|
| 29 |
#' biomarker = "age" |
|
| 30 |
#' ) |
|
| 31 |
#' |
|
| 32 |
#' step_matrix <- fit_survival_step( |
|
| 33 |
#' variables = vars, |
|
| 34 |
#' data = lung, |
|
| 35 |
#' control = c(control_coxph(), control_step(num_points = 10, degree = 2)) |
|
| 36 |
#' ) |
|
| 37 |
#' step_data <- broom::tidy(step_matrix) |
|
| 38 |
#' |
|
| 39 |
#' # Default plot. |
|
| 40 |
#' g_step(step_data) |
|
| 41 |
#' |
|
| 42 |
#' # Add the reference 1 horizontal line. |
|
| 43 |
#' library(ggplot2) |
|
| 44 |
#' g_step(step_data) + |
|
| 45 |
#' ggplot2::geom_hline(ggplot2::aes(yintercept = 1), linetype = 2) |
|
| 46 |
#' |
|
| 47 |
#' # Use actual values instead of percentiles, different color for estimate and no CI, |
|
| 48 |
#' # use log scale for y axis. |
|
| 49 |
#' g_step( |
|
| 50 |
#' step_data, |
|
| 51 |
#' use_percentile = FALSE, |
|
| 52 |
#' est = list(col = "blue", lty = 1), |
|
| 53 |
#' ci_ribbon = NULL |
|
| 54 |
#' ) + scale_y_log10() |
|
| 55 |
#' |
|
| 56 |
#' # Adding another curve based on additional column. |
|
| 57 |
#' step_data$extra <- exp(step_data$`Percentile Center`) |
|
| 58 |
#' g_step(step_data) + |
|
| 59 |
#' ggplot2::geom_line(ggplot2::aes(y = extra), linetype = 2, color = "green") |
|
| 60 |
#' |
|
| 61 |
#' # Response example. |
|
| 62 |
#' vars <- list( |
|
| 63 |
#' response = "status", |
|
| 64 |
#' arm = "sex", |
|
| 65 |
#' biomarker = "age" |
|
| 66 |
#' ) |
|
| 67 |
#' |
|
| 68 |
#' step_matrix <- fit_rsp_step( |
|
| 69 |
#' variables = vars, |
|
| 70 |
#' data = lung, |
|
| 71 |
#' control = c( |
|
| 72 |
#' control_logistic(response_definition = "I(response == 2)"), |
|
| 73 |
#' control_step() |
|
| 74 |
#' ) |
|
| 75 |
#' ) |
|
| 76 |
#' step_data <- broom::tidy(step_matrix) |
|
| 77 |
#' g_step(step_data) |
|
| 78 |
#' |
|
| 79 |
#' @export |
|
| 80 |
g_step <- function(df, |
|
| 81 |
use_percentile = "Percentile Center" %in% names(df), |
|
| 82 |
est = list(col = "blue", lty = 1), |
|
| 83 |
ci_ribbon = list(fill = getOption("ggplot2.discrete.colour")[1], alpha = 0.5),
|
|
| 84 |
col = getOption("ggplot2.discrete.colour")) {
|
|
| 85 | 2x |
checkmate::assert_tibble(df) |
| 86 | 2x |
checkmate::assert_flag(use_percentile) |
| 87 | 2x |
checkmate::assert_character(col, null.ok = TRUE) |
| 88 | 2x |
checkmate::assert_list(est, names = "named") |
| 89 | 2x |
checkmate::assert_list(ci_ribbon, names = "named", null.ok = TRUE) |
| 90 | ||
| 91 | 2x |
x_var <- ifelse(use_percentile, "Percentile Center", "Interval Center") |
| 92 | 2x |
df$x <- df[[x_var]] |
| 93 | 2x |
attrs <- attributes(df) |
| 94 | 2x |
df$y <- df[[attrs$estimate]] |
| 95 | ||
| 96 |
# Set legend names. To be modified also at call level |
|
| 97 | 2x |
legend_names <- c("Estimate", "CI 95%")
|
| 98 | ||
| 99 | 2x |
p <- ggplot2::ggplot(df, ggplot2::aes(x = .data[["x"]], y = .data[["y"]])) |
| 100 | ||
| 101 | 2x |
if (!is.null(col)) {
|
| 102 | 2x |
p <- p + |
| 103 | 2x |
ggplot2::scale_color_manual(values = col) |
| 104 |
} |
|
| 105 | ||
| 106 | 2x |
if (!is.null(ci_ribbon)) {
|
| 107 | 1x |
if (is.null(ci_ribbon$fill)) {
|
| 108 | ! |
ci_ribbon$fill <- "lightblue" |
| 109 |
} |
|
| 110 | 1x |
p <- p + ggplot2::geom_ribbon( |
| 111 | 1x |
ggplot2::aes( |
| 112 | 1x |
ymin = .data[["ci_lower"]], ymax = .data[["ci_upper"]], |
| 113 | 1x |
fill = legend_names[2] |
| 114 |
), |
|
| 115 | 1x |
alpha = ci_ribbon$alpha |
| 116 |
) + |
|
| 117 | 1x |
scale_fill_manual( |
| 118 | 1x |
name = "", values = c("CI 95%" = ci_ribbon$fill)
|
| 119 |
) |
|
| 120 |
} |
|
| 121 | 2x |
suppressMessages(p <- p + |
| 122 | 2x |
ggplot2::geom_line( |
| 123 | 2x |
ggplot2::aes(y = .data[["y"]], color = legend_names[1]), |
| 124 | 2x |
linetype = est$lty |
| 125 |
) + |
|
| 126 | 2x |
scale_colour_manual( |
| 127 | 2x |
name = "", values = c("Estimate" = "blue")
|
| 128 |
)) |
|
| 129 | ||
| 130 | 2x |
p <- p + ggplot2::labs(x = attrs$biomarker, y = attrs$estimate) |
| 131 | 2x |
if (use_percentile) {
|
| 132 | 1x |
p <- p + ggplot2::scale_x_continuous(labels = scales::percent) |
| 133 |
} |
|
| 134 | 2x |
p |
| 135 |
} |
|
| 136 | ||
| 137 |
#' Custom tidy method for STEP results |
|
| 138 |
#' |
|
| 139 |
#' @description `r lifecycle::badge("stable")`
|
|
| 140 |
#' |
|
| 141 |
#' Tidy the STEP results into a `tibble` format ready for plotting. |
|
| 142 |
#' |
|
| 143 |
#' @param x (`matrix`)\cr results from [fit_survival_step()]. |
|
| 144 |
#' @param ... not used. |
|
| 145 |
#' |
|
| 146 |
#' @return A `tibble` with one row per STEP subgroup. The estimates and CIs are on the HR or OR scale, |
|
| 147 |
#' respectively. Additional attributes carry metadata also used for plotting. |
|
| 148 |
#' |
|
| 149 |
#' @seealso [g_step()] which consumes the result from this function. |
|
| 150 |
#' |
|
| 151 |
#' @method tidy step |
|
| 152 |
#' |
|
| 153 |
#' @examples |
|
| 154 |
#' library(survival) |
|
| 155 |
#' lung$sex <- factor(lung$sex) |
|
| 156 |
#' vars <- list( |
|
| 157 |
#' time = "time", |
|
| 158 |
#' event = "status", |
|
| 159 |
#' arm = "sex", |
|
| 160 |
#' biomarker = "age" |
|
| 161 |
#' ) |
|
| 162 |
#' step_matrix <- fit_survival_step( |
|
| 163 |
#' variables = vars, |
|
| 164 |
#' data = lung, |
|
| 165 |
#' control = c(control_coxph(), control_step(num_points = 10, degree = 2)) |
|
| 166 |
#' ) |
|
| 167 |
#' broom::tidy(step_matrix) |
|
| 168 |
#' |
|
| 169 |
#' @export |
|
| 170 |
tidy.step <- function(x, ...) { # nolint
|
|
| 171 | 7x |
checkmate::assert_class(x, "step") |
| 172 | 7x |
dat <- as.data.frame(x) |
| 173 | 7x |
nams <- names(dat) |
| 174 | 7x |
is_surv <- "loghr" %in% names(dat) |
| 175 | 7x |
est_var <- ifelse(is_surv, "loghr", "logor") |
| 176 | 7x |
new_est_var <- ifelse(is_surv, "Hazard Ratio", "Odds Ratio") |
| 177 | 7x |
new_y_vars <- c(new_est_var, c("ci_lower", "ci_upper"))
|
| 178 | 7x |
names(dat)[match(est_var, nams)] <- new_est_var |
| 179 | 7x |
dat[, new_y_vars] <- exp(dat[, new_y_vars]) |
| 180 | 7x |
any_is_na <- any(is.na(dat[, new_y_vars])) |
| 181 | 7x |
any_is_very_large <- any(abs(dat[, new_y_vars]) > 1e10, na.rm = TRUE) |
| 182 | 7x |
if (any_is_na) {
|
| 183 | 2x |
warning(paste( |
| 184 | 2x |
"Missing values in the point estimate or CI columns,", |
| 185 | 2x |
"this will lead to holes in the `g_step()` plot" |
| 186 |
)) |
|
| 187 |
} |
|
| 188 | 7x |
if (any_is_very_large) {
|
| 189 | 2x |
warning(paste( |
| 190 | 2x |
"Very large absolute values in the point estimate or CI columns,", |
| 191 | 2x |
"consider adding `scale_y_log10()` to the `g_step()` result for plotting" |
| 192 |
)) |
|
| 193 |
} |
|
| 194 | 7x |
if (any_is_na || any_is_very_large) {
|
| 195 | 4x |
warning("Consider using larger `bandwidth`, less `num_points` in `control_step()` settings for fitting")
|
| 196 |
} |
|
| 197 | 7x |
structure( |
| 198 | 7x |
tibble::as_tibble(dat), |
| 199 | 7x |
estimate = new_est_var, |
| 200 | 7x |
biomarker = attr(x, "variables")$biomarker, |
| 201 | 7x |
ci = f_conf_level(attr(x, "control")$conf_level) |
| 202 |
) |
|
| 203 |
} |
| 1 |
#' Proportion estimation |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [estimate_proportion()] creates a layout element to estimate the proportion of responders |
|
| 6 |
#' within a studied population. The primary analysis variable, `vars`, indicates whether a response has occurred for |
|
| 7 |
#' each record. See the `method` parameter for options of methods to use when constructing the confidence interval of |
|
| 8 |
#' the proportion. Additionally, a stratification variable can be supplied via the `strata` element of the `variables` |
|
| 9 |
#' argument. |
|
| 10 |
#' |
|
| 11 |
#' @inheritParams prop_strat_wilson |
|
| 12 |
#' @inheritParams argument_convention |
|
| 13 |
#' @param method (`string`)\cr the method used to construct the confidence interval |
|
| 14 |
#' for proportion of successful outcomes; one of `waldcc`, `wald`, `clopper-pearson`, |
|
| 15 |
#' `wilson`, `wilsonc`, `strat_wilson`, `strat_wilsonc`, `agresti-coull` or `jeffreys`. |
|
| 16 |
#' @param long (`flag`)\cr whether a long description is required. |
|
| 17 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 18 |
#' |
|
| 19 |
#' Options are: ``r shQuote(get_stats("estimate_proportion"), type = "sh")``
|
|
| 20 |
#' |
|
| 21 |
#' @seealso [h_proportions] |
|
| 22 |
#' |
|
| 23 |
#' @name estimate_proportion |
|
| 24 |
#' @order 1 |
|
| 25 |
NULL |
|
| 26 | ||
| 27 |
#' @describeIn estimate_proportion Statistics function estimating a |
|
| 28 |
#' proportion along with its confidence interval. |
|
| 29 |
#' |
|
| 30 |
#' @param df (`logical` or `data.frame`)\cr if only a logical vector is used, |
|
| 31 |
#' it indicates whether each subject is a responder or not. `TRUE` represents |
|
| 32 |
#' a successful outcome. If a `data.frame` is provided, also the `strata` variable |
|
| 33 |
#' names must be provided in `variables` as a list element with the strata strings. |
|
| 34 |
#' In the case of `data.frame`, the logical vector of responses must be indicated as a |
|
| 35 |
#' variable name in `.var`. |
|
| 36 |
#' |
|
| 37 |
#' @return |
|
| 38 |
#' * `s_proportion()` returns statistics `n_prop` (`n` and proportion) and `prop_ci` (proportion CI) for a |
|
| 39 |
#' given variable. |
|
| 40 |
#' |
|
| 41 |
#' @examples |
|
| 42 |
#' # Case with only logical vector. |
|
| 43 |
#' rsp_v <- c(1, 0, 1, 0, 1, 1, 0, 0) |
|
| 44 |
#' s_proportion(rsp_v) |
|
| 45 |
#' |
|
| 46 |
#' # Example for Stratified Wilson CI |
|
| 47 |
#' nex <- 100 # Number of example rows |
|
| 48 |
#' dta <- data.frame( |
|
| 49 |
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE), |
|
| 50 |
#' "grp" = sample(c("A", "B"), nex, TRUE),
|
|
| 51 |
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
|
|
| 52 |
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
|
|
| 53 |
#' stringsAsFactors = TRUE |
|
| 54 |
#' ) |
|
| 55 |
#' |
|
| 56 |
#' s_proportion( |
|
| 57 |
#' df = dta, |
|
| 58 |
#' .var = "rsp", |
|
| 59 |
#' variables = list(strata = c("f1", "f2")),
|
|
| 60 |
#' conf_level = 0.90, |
|
| 61 |
#' method = "strat_wilson" |
|
| 62 |
#' ) |
|
| 63 |
#' |
|
| 64 |
#' @export |
|
| 65 |
s_proportion <- function(df, |
|
| 66 |
.var, |
|
| 67 |
conf_level = 0.95, |
|
| 68 |
method = c( |
|
| 69 |
"waldcc", "wald", "clopper-pearson", |
|
| 70 |
"wilson", "wilsonc", "strat_wilson", "strat_wilsonc", |
|
| 71 |
"agresti-coull", "jeffreys" |
|
| 72 |
), |
|
| 73 |
weights = NULL, |
|
| 74 |
max_iterations = 50, |
|
| 75 |
variables = list(strata = NULL), |
|
| 76 |
long = FALSE, |
|
| 77 |
denom = c("n", "N_col", "N_row"),
|
|
| 78 |
...) {
|
|
| 79 | 182x |
method <- match.arg(method) |
| 80 | 182x |
checkmate::assert_flag(long) |
| 81 | 182x |
assert_proportion_value(conf_level) |
| 82 | 182x |
args_list <- list(...) |
| 83 | 182x |
.N_row <- args_list[[".N_row"]] # nolint |
| 84 | 182x |
.N_col <- args_list[[".N_col"]] # nolint |
| 85 | ||
| 86 | 182x |
if (!is.null(variables$strata)) {
|
| 87 |
# Checks for strata |
|
| 88 | ! |
if (missing(df)) stop("When doing stratified analysis a data.frame with specific columns is needed.")
|
| 89 | 9x |
strata_colnames <- variables$strata |
| 90 | 9x |
checkmate::assert_character(strata_colnames, null.ok = FALSE) |
| 91 | 9x |
strata_vars <- stats::setNames(as.list(strata_colnames), strata_colnames) |
| 92 | 9x |
assert_df_with_variables(df, strata_vars) |
| 93 | ||
| 94 | 9x |
strata <- interaction(df[strata_colnames]) |
| 95 | 9x |
strata <- as.factor(strata) |
| 96 | ||
| 97 |
# Pushing down checks to prop_strat_wilson |
|
| 98 | 173x |
} else if (checkmate::test_subset(method, c("strat_wilson", "strat_wilsonc"))) {
|
| 99 | ! |
stop("To use stratified methods you need to specify the strata variables.")
|
| 100 |
} |
|
| 101 | ||
| 102 |
# Finding the Responders |
|
| 103 | 182x |
if (checkmate::test_atomic_vector(df)) {
|
| 104 | 167x |
rsp <- as.logical(df) |
| 105 |
} else {
|
|
| 106 | 15x |
rsp <- as.logical(df[[.var]]) |
| 107 |
} |
|
| 108 | ||
| 109 |
# Stop for stratified analysis |
|
| 110 | 182x |
if (method %in% c("strat_wilson", "strat_wilsonc") && denom[1] != "n") {
|
| 111 | 1x |
stop( |
| 112 | 1x |
"Stratified methods only support 'n' as the denominator (denom). ", |
| 113 | 1x |
"Consider adding negative responders directly to the dataset." |
| 114 |
) |
|
| 115 |
} |
|
| 116 | ||
| 117 | 181x |
denom <- match.arg(denom) %>% |
| 118 | 181x |
switch( |
| 119 | 181x |
n = length(rsp), |
| 120 | 181x |
N_row = .N_row, |
| 121 | 181x |
N_col = .N_col |
| 122 |
) |
|
| 123 | 181x |
n_rsp <- sum(rsp) |
| 124 | 181x |
p_hat <- ifelse(denom > 0, n_rsp / denom, 0) |
| 125 | ||
| 126 | 181x |
prop_ci <- switch(method, |
| 127 | 181x |
"clopper-pearson" = prop_clopper_pearson(rsp, n = denom, conf_level), |
| 128 | 181x |
"wilson" = prop_wilson(rsp, n = denom, conf_level), |
| 129 | 181x |
"wilsonc" = prop_wilson(rsp, n = denom, conf_level, correct = TRUE), |
| 130 | 181x |
"strat_wilson" = prop_strat_wilson(rsp, strata, weights, conf_level, max_iterations, correct = FALSE)$conf_int, |
| 131 | 181x |
"strat_wilsonc" = prop_strat_wilson(rsp, strata, weights, conf_level, max_iterations, correct = TRUE)$conf_int, |
| 132 | 181x |
"wald" = prop_wald(rsp, n = denom, conf_level), |
| 133 | 181x |
"waldcc" = prop_wald(rsp, n = denom, conf_level, correct = TRUE), |
| 134 | 181x |
"agresti-coull" = prop_agresti_coull(rsp, n = denom, conf_level), |
| 135 | 181x |
"jeffreys" = prop_jeffreys(rsp, n = denom, conf_level) |
| 136 |
) |
|
| 137 | ||
| 138 | 181x |
list( |
| 139 | 181x |
"n_prop" = formatters::with_label(c(n_rsp, p_hat), "Responders"), |
| 140 | 181x |
"prop_ci" = formatters::with_label(x = 100 * prop_ci, label = d_proportion(conf_level, method, long = long)) |
| 141 |
) |
|
| 142 |
} |
|
| 143 | ||
| 144 |
#' @describeIn estimate_proportion Formatted analysis function which is used as `afun` |
|
| 145 |
#' in `estimate_proportion()`. |
|
| 146 |
#' |
|
| 147 |
#' @return |
|
| 148 |
#' * `a_proportion()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 149 |
#' |
|
| 150 |
#' @export |
|
| 151 |
a_proportion <- function(df, |
|
| 152 |
..., |
|
| 153 |
.stats = NULL, |
|
| 154 |
.stat_names = NULL, |
|
| 155 |
.formats = NULL, |
|
| 156 |
.labels = NULL, |
|
| 157 |
.indent_mods = NULL) {
|
|
| 158 |
# Check for additional parameters to the statistics function |
|
| 159 | 15x |
dots_extra_args <- list(...) |
| 160 | 15x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 161 | 15x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 162 | ||
| 163 |
# Check for user-defined functions |
|
| 164 | 15x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 165 | 15x |
.stats <- default_and_custom_stats_list$all_stats |
| 166 | 15x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 167 | ||
| 168 |
# Apply statistics function |
|
| 169 | 15x |
x_stats <- .apply_stat_functions( |
| 170 | 15x |
default_stat_fnc = s_proportion, |
| 171 | 15x |
custom_stat_fnc_list = custom_stat_functions, |
| 172 | 15x |
args_list = c( |
| 173 | 15x |
df = list(df), |
| 174 | 15x |
extra_afun_params, |
| 175 | 15x |
dots_extra_args |
| 176 |
) |
|
| 177 |
) |
|
| 178 | ||
| 179 |
# Fill in formatting defaults |
|
| 180 | 14x |
.stats <- get_stats("estimate_proportion",
|
| 181 | 14x |
stats_in = .stats, |
| 182 | 14x |
custom_stats_in = names(custom_stat_functions) |
| 183 |
) |
|
| 184 | 14x |
x_stats <- x_stats[.stats] |
| 185 | 14x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 186 | 14x |
.labels <- get_labels_from_stats( |
| 187 | 14x |
.stats, .labels, |
| 188 | 14x |
tern_defaults = c(lapply(x_stats, attr, "label"), tern_default_labels) |
| 189 |
) |
|
| 190 | 14x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 191 | ||
| 192 |
# Auto format handling |
|
| 193 | 14x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 194 | ||
| 195 |
# Get and check statistical names |
|
| 196 | 14x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 197 | ||
| 198 | 14x |
in_rows( |
| 199 | 14x |
.list = x_stats, |
| 200 | 14x |
.formats = .formats, |
| 201 | 14x |
.names = .labels %>% .unlist_keep_nulls(), |
| 202 | 14x |
.stat_names = .stat_names, |
| 203 | 14x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 204 | 14x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 205 |
) |
|
| 206 |
} |
|
| 207 | ||
| 208 |
#' @describeIn estimate_proportion Layout-creating function which can take statistics function arguments |
|
| 209 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 210 |
#' |
|
| 211 |
#' @return |
|
| 212 |
#' * `estimate_proportion()` returns a layout object suitable for passing to further layouting functions, |
|
| 213 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 214 |
#' the statistics from `s_proportion()` to the table layout. |
|
| 215 |
#' |
|
| 216 |
#' @examples |
|
| 217 |
#' dta_test <- data.frame( |
|
| 218 |
#' USUBJID = paste0("S", 1:12),
|
|
| 219 |
#' ARM = rep(LETTERS[1:3], each = 4), |
|
| 220 |
#' AVAL = rep(LETTERS[1:3], each = 4) |
|
| 221 |
#' ) %>% |
|
| 222 |
#' dplyr::mutate(is_rsp = AVAL == "A") |
|
| 223 |
#' |
|
| 224 |
#' basic_table() %>% |
|
| 225 |
#' split_cols_by("ARM") %>%
|
|
| 226 |
#' estimate_proportion(vars = "is_rsp") %>% |
|
| 227 |
#' build_table(df = dta_test) |
|
| 228 |
#' |
|
| 229 |
#' @export |
|
| 230 |
#' @order 2 |
|
| 231 |
estimate_proportion <- function(lyt, |
|
| 232 |
vars, |
|
| 233 |
conf_level = 0.95, |
|
| 234 |
method = c( |
|
| 235 |
"waldcc", "wald", "clopper-pearson", |
|
| 236 |
"wilson", "wilsonc", "strat_wilson", "strat_wilsonc", |
|
| 237 |
"agresti-coull", "jeffreys" |
|
| 238 |
), |
|
| 239 |
weights = NULL, |
|
| 240 |
max_iterations = 50, |
|
| 241 |
variables = list(strata = NULL), |
|
| 242 |
long = FALSE, |
|
| 243 |
na_str = default_na_str(), |
|
| 244 |
nested = TRUE, |
|
| 245 |
..., |
|
| 246 |
show_labels = "hidden", |
|
| 247 |
table_names = vars, |
|
| 248 |
.stats = c("n_prop", "prop_ci"),
|
|
| 249 |
.stat_names = NULL, |
|
| 250 |
.formats = NULL, |
|
| 251 |
.labels = NULL, |
|
| 252 |
.indent_mods = NULL) {
|
|
| 253 |
# Process standard extra arguments |
|
| 254 | 6x |
extra_args <- list(".stats" = .stats)
|
| 255 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 256 | 3x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 257 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 258 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 259 | ||
| 260 |
# Process additional arguments to the statistic function |
|
| 261 | 6x |
extra_args <- c( |
| 262 | 6x |
extra_args, |
| 263 | 6x |
conf_level = list(conf_level), method = list(method), weights = list(weights), |
| 264 | 6x |
max_iterations = list(max_iterations), variables = list(variables), long = list(long), |
| 265 |
... |
|
| 266 |
) |
|
| 267 | ||
| 268 |
# Append additional info from layout to the analysis function |
|
| 269 | 6x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 270 | 6x |
formals(a_proportion) <- c(formals(a_proportion), extra_args[[".additional_fun_parameters"]]) |
| 271 | ||
| 272 | 6x |
analyze( |
| 273 | 6x |
lyt = lyt, |
| 274 | 6x |
vars = vars, |
| 275 | 6x |
afun = a_proportion, |
| 276 | 6x |
na_str = na_str, |
| 277 | 6x |
nested = nested, |
| 278 | 6x |
extra_args = extra_args, |
| 279 | 6x |
show_labels = show_labels, |
| 280 | 6x |
table_names = table_names |
| 281 |
) |
|
| 282 |
} |
|
| 283 | ||
| 284 |
#' Helper functions for calculating proportion confidence intervals |
|
| 285 |
#' |
|
| 286 |
#' @description `r lifecycle::badge("stable")`
|
|
| 287 |
#' |
|
| 288 |
#' Functions to calculate different proportion confidence intervals for use in [estimate_proportion()]. |
|
| 289 |
#' |
|
| 290 |
#' @inheritParams argument_convention |
|
| 291 |
#' @inheritParams estimate_proportion |
|
| 292 |
#' |
|
| 293 |
#' @return Confidence interval of a proportion. |
|
| 294 |
#' |
|
| 295 |
#' @seealso [estimate_proportion], descriptive function [d_proportion()], |
|
| 296 |
#' and helper functions [strata_normal_quantile()] and [update_weights_strat_wilson()]. |
|
| 297 |
#' |
|
| 298 |
#' @name h_proportions |
|
| 299 |
NULL |
|
| 300 | ||
| 301 |
#' @describeIn h_proportions Calculates the Wilson interval by calling [stats::prop.test()]. |
|
| 302 |
#' Also referred to as Wilson score interval. |
|
| 303 |
#' |
|
| 304 |
#' @examples |
|
| 305 |
#' rsp <- c( |
|
| 306 |
#' TRUE, TRUE, TRUE, TRUE, TRUE, |
|
| 307 |
#' FALSE, FALSE, FALSE, FALSE, FALSE |
|
| 308 |
#' ) |
|
| 309 |
#' prop_wilson(rsp, conf_level = 0.9) |
|
| 310 |
#' |
|
| 311 |
#' @export |
|
| 312 |
prop_wilson <- function(rsp, n = length(rsp), conf_level, correct = FALSE) {
|
|
| 313 | 5x |
y <- stats::prop.test( |
| 314 | 5x |
sum(rsp), |
| 315 | 5x |
n, |
| 316 | 5x |
correct = correct, |
| 317 | 5x |
conf.level = conf_level |
| 318 |
) |
|
| 319 | ||
| 320 | 5x |
as.numeric(y$conf.int) |
| 321 |
} |
|
| 322 | ||
| 323 |
#' @describeIn h_proportions Calculates the stratified Wilson confidence |
|
| 324 |
#' interval for unequal proportions as described in \insertCite{Yan2010-jt;textual}{tern}
|
|
| 325 |
#' |
|
| 326 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`. |
|
| 327 |
#' @param weights (`numeric` or `NULL`)\cr weights for each level of the strata. If `NULL`, they are |
|
| 328 |
#' estimated using the iterative algorithm proposed in \insertCite{Yan2010-jt;textual}{tern} that
|
|
| 329 |
#' minimizes the weighted squared length of the confidence interval. |
|
| 330 |
#' @param max_iterations (`count`)\cr maximum number of iterations for the iterative procedure used |
|
| 331 |
#' to find estimates of optimal weights. |
|
| 332 |
#' @param correct (`flag`)\cr whether to include the continuity correction. For further information, see for example |
|
| 333 |
#' for [stats::prop.test()]. |
|
| 334 |
#' |
|
| 335 |
#' @references |
|
| 336 |
#' \insertRef{Yan2010-jt}{tern}
|
|
| 337 |
#' |
|
| 338 |
#' @examples |
|
| 339 |
#' # Stratified Wilson confidence interval with unequal probabilities |
|
| 340 |
#' |
|
| 341 |
#' set.seed(1) |
|
| 342 |
#' rsp <- sample(c(TRUE, FALSE), 100, TRUE) |
|
| 343 |
#' strata_data <- data.frame( |
|
| 344 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
| 345 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
| 346 |
#' stringsAsFactors = TRUE |
|
| 347 |
#' ) |
|
| 348 |
#' strata <- interaction(strata_data) |
|
| 349 |
#' n_strata <- ncol(table(rsp, strata)) # Number of strata |
|
| 350 |
#' |
|
| 351 |
#' prop_strat_wilson( |
|
| 352 |
#' rsp = rsp, strata = strata, |
|
| 353 |
#' conf_level = 0.90 |
|
| 354 |
#' ) |
|
| 355 |
#' |
|
| 356 |
#' # Not automatic setting of weights |
|
| 357 |
#' prop_strat_wilson( |
|
| 358 |
#' rsp = rsp, strata = strata, |
|
| 359 |
#' weights = rep(1 / n_strata, n_strata), |
|
| 360 |
#' conf_level = 0.90 |
|
| 361 |
#' ) |
|
| 362 |
#' |
|
| 363 |
#' @export |
|
| 364 |
prop_strat_wilson <- function(rsp, |
|
| 365 |
strata, |
|
| 366 |
weights = NULL, |
|
| 367 |
conf_level = 0.95, |
|
| 368 |
max_iterations = NULL, |
|
| 369 |
correct = FALSE) {
|
|
| 370 | 20x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
| 371 | 20x |
checkmate::assert_factor(strata, len = length(rsp)) |
| 372 | 20x |
assert_proportion_value(conf_level) |
| 373 | ||
| 374 | 20x |
tbl <- table(rsp, strata) |
| 375 | 20x |
n_strata <- length(unique(strata)) |
| 376 | ||
| 377 |
# Checking the weights and maximum number of iterations. |
|
| 378 | 20x |
do_iter <- FALSE |
| 379 | 20x |
if (is.null(weights)) {
|
| 380 | 6x |
weights <- rep(1 / n_strata, n_strata) # Initialization for iterative procedure |
| 381 | 6x |
do_iter <- TRUE |
| 382 | ||
| 383 |
# Iteration parameters |
|
| 384 | 2x |
if (is.null(max_iterations)) max_iterations <- 10 |
| 385 | 6x |
checkmate::assert_int(max_iterations, na.ok = FALSE, null.ok = FALSE, lower = 1) |
| 386 |
} |
|
| 387 | 20x |
checkmate::assert_numeric(weights, lower = 0, upper = 1, any.missing = FALSE, len = n_strata) |
| 388 | 20x |
sum_weights <- checkmate::assert_int(sum(weights)) |
| 389 | ! |
if (as.integer(sum_weights + 0.5) != 1L) stop("Sum of weights must be 1L.")
|
| 390 | ||
| 391 | 20x |
xs <- tbl["TRUE", ] |
| 392 | 20x |
ns <- colSums(tbl) |
| 393 | 20x |
use_stratum <- (ns > 0) |
| 394 | 20x |
ns <- ns[use_stratum] |
| 395 | 20x |
xs <- xs[use_stratum] |
| 396 | 20x |
ests <- xs / ns |
| 397 | 20x |
vars <- ests * (1 - ests) / ns |
| 398 | ||
| 399 | 20x |
strata_qnorm <- strata_normal_quantile(vars, weights, conf_level) |
| 400 | ||
| 401 |
# Iterative setting of weights if they were not set externally |
|
| 402 | 20x |
weights_new <- if (do_iter) {
|
| 403 | 6x |
update_weights_strat_wilson(vars, strata_qnorm, weights, ns, max_iterations, conf_level)$weights |
| 404 |
} else {
|
|
| 405 | 14x |
weights |
| 406 |
} |
|
| 407 | ||
| 408 | 20x |
strata_conf_level <- 2 * stats::pnorm(strata_qnorm) - 1 |
| 409 | ||
| 410 | 20x |
ci_by_strata <- Map( |
| 411 | 20x |
function(x, n) {
|
| 412 |
# Classic Wilson's confidence interval |
|
| 413 | 139x |
suppressWarnings(stats::prop.test(x, n, correct = correct, conf.level = strata_conf_level)$conf.int) |
| 414 |
}, |
|
| 415 | 20x |
x = xs, |
| 416 | 20x |
n = ns |
| 417 |
) |
|
| 418 | 20x |
lower_by_strata <- sapply(ci_by_strata, "[", 1L) |
| 419 | 20x |
upper_by_strata <- sapply(ci_by_strata, "[", 2L) |
| 420 | ||
| 421 | 20x |
lower <- sum(weights_new * lower_by_strata) |
| 422 | 20x |
upper <- sum(weights_new * upper_by_strata) |
| 423 | ||
| 424 |
# Return values |
|
| 425 | 20x |
if (do_iter) {
|
| 426 | 6x |
list( |
| 427 | 6x |
conf_int = c( |
| 428 | 6x |
lower = lower, |
| 429 | 6x |
upper = upper |
| 430 |
), |
|
| 431 | 6x |
weights = weights_new |
| 432 |
) |
|
| 433 |
} else {
|
|
| 434 | 14x |
list( |
| 435 | 14x |
conf_int = c( |
| 436 | 14x |
lower = lower, |
| 437 | 14x |
upper = upper |
| 438 |
) |
|
| 439 |
) |
|
| 440 |
} |
|
| 441 |
} |
|
| 442 | ||
| 443 |
#' @describeIn h_proportions Calculates the Clopper-Pearson interval by calling [stats::binom.test()]. |
|
| 444 |
#' Also referred to as the `exact` method. |
|
| 445 |
#' |
|
| 446 |
#' @param n (`count`)\cr number of participants (if `denom = "N_col"`) or the number of responders |
|
| 447 |
#' (if `denom = "n"`, the default). |
|
| 448 |
#' |
|
| 449 |
#' @examples |
|
| 450 |
#' prop_clopper_pearson(rsp, conf_level = .95) |
|
| 451 |
#' |
|
| 452 |
#' @export |
|
| 453 |
prop_clopper_pearson <- function(rsp, n = length(rsp), conf_level) {
|
|
| 454 | 1x |
y <- stats::binom.test( |
| 455 | 1x |
x = sum(rsp), |
| 456 | 1x |
n = n, |
| 457 | 1x |
conf.level = conf_level |
| 458 |
) |
|
| 459 | 1x |
as.numeric(y$conf.int) |
| 460 |
} |
|
| 461 | ||
| 462 |
#' @describeIn h_proportions Calculates the Wald interval by following the usual textbook definition |
|
| 463 |
#' for a single proportion confidence interval using the normal approximation. |
|
| 464 |
#' |
|
| 465 |
#' @param correct (`flag`)\cr whether to apply continuity correction. |
|
| 466 |
#' |
|
| 467 |
#' @examples |
|
| 468 |
#' prop_wald(rsp, conf_level = 0.95) |
|
| 469 |
#' prop_wald(rsp, conf_level = 0.95, correct = TRUE) |
|
| 470 |
#' |
|
| 471 |
#' @export |
|
| 472 |
prop_wald <- function(rsp, n = length(rsp), conf_level, correct = FALSE) {
|
|
| 473 | 165x |
p_hat <- ifelse(n > 0, sum(rsp) / n, 0) |
| 474 | 165x |
z <- stats::qnorm((1 + conf_level) / 2) |
| 475 | 165x |
q_hat <- 1 - p_hat |
| 476 | 165x |
correct <- if (correct) 1 / (2 * n) else 0 |
| 477 | ||
| 478 | 165x |
err <- z * sqrt(p_hat * q_hat) / sqrt(n) + correct |
| 479 | 165x |
l_ci <- max(0, p_hat - err) |
| 480 | 165x |
u_ci <- min(1, p_hat + err) |
| 481 | ||
| 482 | 165x |
c(l_ci, u_ci) |
| 483 |
} |
|
| 484 | ||
| 485 |
#' @describeIn h_proportions Calculates the Agresti-Coull interval. Constructed (for 95% CI) by adding two successes |
|
| 486 |
#' and two failures to the data and then using the Wald formula to construct a CI. |
|
| 487 |
#' |
|
| 488 |
#' @examples |
|
| 489 |
#' prop_agresti_coull(rsp, conf_level = 0.95) |
|
| 490 |
#' |
|
| 491 |
#' @export |
|
| 492 |
prop_agresti_coull <- function(rsp, n = length(rsp), conf_level) {
|
|
| 493 | 3x |
x_sum <- sum(rsp) |
| 494 | 3x |
z <- stats::qnorm((1 + conf_level) / 2) |
| 495 | ||
| 496 |
# Add here both z^2 / 2 successes and failures. |
|
| 497 | 3x |
x_sum_tilde <- x_sum + z^2 / 2 |
| 498 | 3x |
n_tilde <- n + z^2 |
| 499 | ||
| 500 |
# Then proceed as with the Wald interval. |
|
| 501 | 3x |
p_tilde <- x_sum_tilde / n_tilde |
| 502 | 3x |
q_tilde <- 1 - p_tilde |
| 503 | 3x |
err <- z * sqrt(p_tilde * q_tilde) / sqrt(n_tilde) |
| 504 | 3x |
l_ci <- max(0, p_tilde - err) |
| 505 | 3x |
u_ci <- min(1, p_tilde + err) |
| 506 | ||
| 507 | 3x |
c(l_ci, u_ci) |
| 508 |
} |
|
| 509 | ||
| 510 |
#' @describeIn h_proportions Calculates the Jeffreys interval, an equal-tailed interval based on the |
|
| 511 |
#' non-informative Jeffreys prior for a binomial proportion. |
|
| 512 |
#' |
|
| 513 |
#' @examples |
|
| 514 |
#' prop_jeffreys(rsp, conf_level = 0.95) |
|
| 515 |
#' |
|
| 516 |
#' @export |
|
| 517 |
prop_jeffreys <- function(rsp, n = length(rsp), conf_level) {
|
|
| 518 | 5x |
x_sum <- sum(rsp) |
| 519 | ||
| 520 | 5x |
alpha <- 1 - conf_level |
| 521 | 5x |
l_ci <- ifelse( |
| 522 | 5x |
x_sum == 0, |
| 523 | 5x |
0, |
| 524 | 5x |
stats::qbeta(alpha / 2, x_sum + 0.5, n - x_sum + 0.5) |
| 525 |
) |
|
| 526 | ||
| 527 | 5x |
u_ci <- ifelse( |
| 528 | 5x |
x_sum == n, |
| 529 | 5x |
1, |
| 530 | 5x |
stats::qbeta(1 - alpha / 2, x_sum + 0.5, n - x_sum + 0.5) |
| 531 |
) |
|
| 532 | ||
| 533 | 5x |
c(l_ci, u_ci) |
| 534 |
} |
|
| 535 | ||
| 536 |
#' Description of the proportion summary |
|
| 537 |
#' |
|
| 538 |
#' @description `r lifecycle::badge("stable")`
|
|
| 539 |
#' |
|
| 540 |
#' This is a helper function that describes the analysis in [s_proportion()]. |
|
| 541 |
#' |
|
| 542 |
#' @inheritParams s_proportion |
|
| 543 |
#' @param long (`flag`)\cr whether a long or a short (default) description is required. |
|
| 544 |
#' |
|
| 545 |
#' @return String describing the analysis. |
|
| 546 |
#' |
|
| 547 |
#' @export |
|
| 548 |
d_proportion <- function(conf_level, |
|
| 549 |
method, |
|
| 550 |
long = FALSE) {
|
|
| 551 | 181x |
label <- paste0(conf_level * 100, "% CI") |
| 552 | ||
| 553 | ! |
if (long) label <- paste(label, "for Response Rates") |
| 554 | ||
| 555 | 181x |
method_part <- switch(method, |
| 556 | 181x |
"clopper-pearson" = "Clopper-Pearson", |
| 557 | 181x |
"waldcc" = "Wald, with correction", |
| 558 | 181x |
"wald" = "Wald, without correction", |
| 559 | 181x |
"wilson" = "Wilson, without correction", |
| 560 | 181x |
"strat_wilson" = "Stratified Wilson, without correction", |
| 561 | 181x |
"wilsonc" = "Wilson, with correction", |
| 562 | 181x |
"strat_wilsonc" = "Stratified Wilson, with correction", |
| 563 | 181x |
"agresti-coull" = "Agresti-Coull", |
| 564 | 181x |
"jeffreys" = "Jeffreys", |
| 565 | 181x |
stop(paste(method, "does not have a description")) |
| 566 |
) |
|
| 567 | ||
| 568 | 181x |
paste0(label, " (", method_part, ")")
|
| 569 |
} |
|
| 570 | ||
| 571 |
#' Helper function for the estimation of stratified quantiles |
|
| 572 |
#' |
|
| 573 |
#' @description `r lifecycle::badge("stable")`
|
|
| 574 |
#' |
|
| 575 |
#' This function wraps the estimation of stratified percentiles when we assume |
|
| 576 |
#' the approximation for large numbers. This is necessary only in the case |
|
| 577 |
#' proportions for each strata are unequal. |
|
| 578 |
#' |
|
| 579 |
#' @inheritParams argument_convention |
|
| 580 |
#' @inheritParams prop_strat_wilson |
|
| 581 |
#' |
|
| 582 |
#' @return Stratified quantile. |
|
| 583 |
#' |
|
| 584 |
#' @seealso [prop_strat_wilson()] |
|
| 585 |
#' |
|
| 586 |
#' @examples |
|
| 587 |
#' strata_data <- table(data.frame( |
|
| 588 |
#' "f1" = sample(c(TRUE, FALSE), 100, TRUE), |
|
| 589 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
| 590 |
#' stringsAsFactors = TRUE |
|
| 591 |
#' )) |
|
| 592 |
#' ns <- colSums(strata_data) |
|
| 593 |
#' ests <- strata_data["TRUE", ] / ns |
|
| 594 |
#' vars <- ests * (1 - ests) / ns |
|
| 595 |
#' weights <- rep(1 / length(ns), length(ns)) |
|
| 596 |
#' |
|
| 597 |
#' strata_normal_quantile(vars, weights, 0.95) |
|
| 598 |
#' |
|
| 599 |
#' @export |
|
| 600 |
strata_normal_quantile <- function(vars, weights, conf_level) {
|
|
| 601 | 43x |
summands <- weights^2 * vars |
| 602 |
# Stratified quantile |
|
| 603 | 43x |
sqrt(sum(summands)) / sum(sqrt(summands)) * stats::qnorm((1 + conf_level) / 2) |
| 604 |
} |
|
| 605 | ||
| 606 |
#' Helper function for the estimation of weights for `prop_strat_wilson()` |
|
| 607 |
#' |
|
| 608 |
#' @description `r lifecycle::badge("stable")`
|
|
| 609 |
#' |
|
| 610 |
#' This function wraps the iteration procedure that allows you to estimate |
|
| 611 |
#' the weights for each proportional strata. This assumes to minimize the |
|
| 612 |
#' weighted squared length of the confidence interval. |
|
| 613 |
#' |
|
| 614 |
#' @inheritParams prop_strat_wilson |
|
| 615 |
#' @param vars (`numeric`)\cr normalized proportions for each strata. |
|
| 616 |
#' @param strata_qnorm (`numeric(1)`)\cr initial estimation with identical weights of the quantiles. |
|
| 617 |
#' @param initial_weights (`numeric`)\cr initial weights used to calculate `strata_qnorm`. This can |
|
| 618 |
#' be optimized in the future if we need to estimate better initial weights. |
|
| 619 |
#' @param n_per_strata (`numeric`)\cr number of elements in each strata. |
|
| 620 |
#' @param max_iterations (`integer(1)`)\cr maximum number of iterations to be tried. Convergence is always checked. |
|
| 621 |
#' @param tol (`numeric(1)`)\cr tolerance threshold for convergence. |
|
| 622 |
#' |
|
| 623 |
#' @return A `list` of 3 elements: `n_it`, `weights`, and `diff_v`. |
|
| 624 |
#' |
|
| 625 |
#' @seealso For references and details see [prop_strat_wilson()]. |
|
| 626 |
#' |
|
| 627 |
#' @examples |
|
| 628 |
#' vs <- c(0.011, 0.013, 0.012, 0.014, 0.017, 0.018) |
|
| 629 |
#' sq <- 0.674 |
|
| 630 |
#' ws <- rep(1 / length(vs), length(vs)) |
|
| 631 |
#' ns <- c(22, 18, 17, 17, 14, 12) |
|
| 632 |
#' |
|
| 633 |
#' update_weights_strat_wilson(vs, sq, ws, ns, 100, 0.95, 0.001) |
|
| 634 |
#' |
|
| 635 |
#' @export |
|
| 636 |
update_weights_strat_wilson <- function(vars, |
|
| 637 |
strata_qnorm, |
|
| 638 |
initial_weights, |
|
| 639 |
n_per_strata, |
|
| 640 |
max_iterations = 50, |
|
| 641 |
conf_level = 0.95, |
|
| 642 |
tol = 0.001) {
|
|
| 643 | 9x |
it <- 0 |
| 644 | 9x |
diff_v <- NULL |
| 645 | ||
| 646 | 9x |
while (it < max_iterations) {
|
| 647 | 21x |
it <- it + 1 |
| 648 | 21x |
weights_new_t <- (1 + strata_qnorm^2 / n_per_strata)^2 |
| 649 | 21x |
weights_new_b <- (vars + strata_qnorm^2 / (4 * n_per_strata^2)) |
| 650 | 21x |
weights_new <- weights_new_t / weights_new_b |
| 651 | 21x |
weights_new <- weights_new / sum(weights_new) |
| 652 | 21x |
strata_qnorm <- strata_normal_quantile(vars, weights_new, conf_level) |
| 653 | 21x |
diff_v <- c(diff_v, sum(abs(weights_new - initial_weights))) |
| 654 | 8x |
if (diff_v[length(diff_v)] < tol) break |
| 655 | 13x |
initial_weights <- weights_new |
| 656 |
} |
|
| 657 | ||
| 658 | 9x |
if (it == max_iterations) {
|
| 659 | 1x |
warning("The heuristic to find weights did not converge with max_iterations = ", max_iterations)
|
| 660 |
} |
|
| 661 | ||
| 662 | 9x |
list( |
| 663 | 9x |
"n_it" = it, |
| 664 | 9x |
"weights" = weights_new, |
| 665 | 9x |
"diff_v" = diff_v |
| 666 |
) |
|
| 667 |
} |
| 1 |
#' Bland-Altman analysis |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 4 |
#' |
|
| 5 |
#' Statistics function that uses the Bland-Altman method to assess the agreement between two numerical vectors |
|
| 6 |
#' and calculates a variety of statistics. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams argument_convention |
|
| 9 |
#' @param y (`numeric`)\cr vector of numbers we want to analyze, to be compared with `x`. |
|
| 10 |
#' |
|
| 11 |
#' @return |
|
| 12 |
#' A named list of the following elements: |
|
| 13 |
#' * `df` |
|
| 14 |
#' * `difference_mean` |
|
| 15 |
#' * `ci_mean` |
|
| 16 |
#' * `difference_sd` |
|
| 17 |
#' * `difference_se` |
|
| 18 |
#' * `upper_agreement_limit` |
|
| 19 |
#' * `lower_agreement_limit` |
|
| 20 |
#' * `agreement_limit_se` |
|
| 21 |
#' * `upper_agreement_limit_ci` |
|
| 22 |
#' * `lower_agreement_limit_ci` |
|
| 23 |
#' * `t_value` |
|
| 24 |
#' * `n` |
|
| 25 |
#' |
|
| 26 |
#' @examples |
|
| 27 |
#' x <- seq(1, 60, 5) |
|
| 28 |
#' y <- seq(5, 50, 4) |
|
| 29 |
#' |
|
| 30 |
#' s_bland_altman(x, y, conf_level = 0.9) |
|
| 31 |
#' |
|
| 32 |
#' @export |
|
| 33 |
s_bland_altman <- function(x, y, conf_level = 0.95) {
|
|
| 34 | 7x |
checkmate::assert_numeric(x, min.len = 1, any.missing = TRUE) |
| 35 | 6x |
checkmate::assert_numeric(y, len = length(x), any.missing = TRUE) |
| 36 | 5x |
checkmate::assert_numeric(conf_level, lower = 0, upper = 1, any.missing = TRUE) |
| 37 | ||
| 38 | 4x |
alpha <- 1 - conf_level |
| 39 | ||
| 40 | 4x |
ind <- complete.cases(x, y) # use only pairwise complete observations, and check if x and y have the same length |
| 41 | 4x |
x <- x[ind] |
| 42 | 4x |
y <- y[ind] |
| 43 | 4x |
n <- sum(ind) # number of 'observations' |
| 44 | ||
| 45 | 4x |
if (n == 0) {
|
| 46 | ! |
stop("there is no valid paired data")
|
| 47 |
} |
|
| 48 | ||
| 49 | 4x |
difference <- x - y # vector of differences |
| 50 | 4x |
average <- (x + y) / 2 # vector of means |
| 51 | 4x |
difference_mean <- mean(difference) # mean difference |
| 52 | 4x |
difference_sd <- sd(difference) # SD of differences |
| 53 | 4x |
al <- qnorm(1 - alpha / 2) * difference_sd |
| 54 | 4x |
upper_agreement_limit <- difference_mean + al # agreement limits |
| 55 | 4x |
lower_agreement_limit <- difference_mean - al |
| 56 | ||
| 57 | 4x |
difference_se <- difference_sd / sqrt(n) # standard error of the mean |
| 58 | 4x |
al_se <- difference_sd * sqrt(3) / sqrt(n) # standard error of the agreement limit |
| 59 | 4x |
tvalue <- qt(1 - alpha / 2, n - 1) # t value for 95% CI calculation |
| 60 | 4x |
difference_mean_ci <- difference_se * tvalue |
| 61 | 4x |
al_ci <- al_se * tvalue |
| 62 | 4x |
upper_agreement_limit_ci <- c(upper_agreement_limit - al_ci, upper_agreement_limit + al_ci) |
| 63 | 4x |
lower_agreement_limit_ci <- c(lower_agreement_limit - al_ci, lower_agreement_limit + al_ci) |
| 64 | ||
| 65 | 4x |
list( |
| 66 | 4x |
df = data.frame(average, difference), |
| 67 | 4x |
difference_mean = difference_mean, |
| 68 | 4x |
ci_mean = difference_mean + c(-1, 1) * difference_mean_ci, |
| 69 | 4x |
difference_sd = difference_sd, |
| 70 | 4x |
difference_se = difference_se, |
| 71 | 4x |
upper_agreement_limit = upper_agreement_limit, |
| 72 | 4x |
lower_agreement_limit = lower_agreement_limit, |
| 73 | 4x |
agreement_limit_se = al_se, |
| 74 | 4x |
upper_agreement_limit_ci = upper_agreement_limit_ci, |
| 75 | 4x |
lower_agreement_limit_ci = lower_agreement_limit_ci, |
| 76 | 4x |
t_value = tvalue, |
| 77 | 4x |
n = n |
| 78 |
) |
|
| 79 |
} |
|
| 80 | ||
| 81 |
#' Bland-Altman plot |
|
| 82 |
#' |
|
| 83 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 84 |
#' |
|
| 85 |
#' Graphing function that produces a Bland-Altman plot. |
|
| 86 |
#' |
|
| 87 |
#' @inheritParams s_bland_altman |
|
| 88 |
#' |
|
| 89 |
#' @return A `ggplot` Bland-Altman plot. |
|
| 90 |
#' |
|
| 91 |
#' @examples |
|
| 92 |
#' x <- seq(1, 60, 5) |
|
| 93 |
#' y <- seq(5, 50, 4) |
|
| 94 |
#' |
|
| 95 |
#' g_bland_altman(x = x, y = y, conf_level = 0.9) |
|
| 96 |
#' |
|
| 97 |
#' @export |
|
| 98 |
#' @aliases bland_altman |
|
| 99 |
g_bland_altman <- function(x, y, conf_level = 0.95) {
|
|
| 100 | 1x |
result_tem <- s_bland_altman(x, y, conf_level = conf_level) |
| 101 | 1x |
xpos <- max(result_tem$df$average) * 0.9 + min(result_tem$df$average) * 0.1 |
| 102 | 1x |
yrange <- diff(range(result_tem$df$difference)) |
| 103 | ||
| 104 | 1x |
p <- ggplot(result_tem$df) + |
| 105 | 1x |
geom_point(aes(x = average, y = difference), color = "blue") + |
| 106 | 1x |
geom_hline(yintercept = result_tem$difference_mean, color = "blue", linetype = 1) + |
| 107 | 1x |
geom_hline(yintercept = 0, color = "blue", linetype = 2) + |
| 108 | 1x |
geom_hline(yintercept = result_tem$lower_agreement_limit, color = "red", linetype = 2) + |
| 109 | 1x |
geom_hline(yintercept = result_tem$upper_agreement_limit, color = "red", linetype = 2) + |
| 110 | 1x |
annotate( |
| 111 | 1x |
"text", |
| 112 | 1x |
x = xpos, |
| 113 | 1x |
y = result_tem$lower_agreement_limit + 0.03 * yrange, |
| 114 | 1x |
label = "lower limits of agreement", |
| 115 | 1x |
color = "red" |
| 116 |
) + |
|
| 117 | 1x |
annotate( |
| 118 | 1x |
"text", |
| 119 | 1x |
x = xpos, |
| 120 | 1x |
y = result_tem$upper_agreement_limit + 0.03 * yrange, |
| 121 | 1x |
label = "upper limits of agreement", |
| 122 | 1x |
color = "red" |
| 123 |
) + |
|
| 124 | 1x |
annotate( |
| 125 | 1x |
"text", |
| 126 | 1x |
x = xpos, |
| 127 | 1x |
y = result_tem$difference_mean + 0.03 * yrange, |
| 128 | 1x |
label = "mean of difference between two measures", |
| 129 | 1x |
color = "blue" |
| 130 |
) + |
|
| 131 | 1x |
annotate( |
| 132 | 1x |
"text", |
| 133 | 1x |
x = xpos, |
| 134 | 1x |
y = result_tem$lower_agreement_limit - 0.03 * yrange, |
| 135 | 1x |
label = sprintf("%.2f", result_tem$lower_agreement_limit),
|
| 136 | 1x |
color = "red" |
| 137 |
) + |
|
| 138 | 1x |
annotate( |
| 139 | 1x |
"text", |
| 140 | 1x |
x = xpos, |
| 141 | 1x |
y = result_tem$upper_agreement_limit - 0.03 * yrange, |
| 142 | 1x |
label = sprintf("%.2f", result_tem$upper_agreement_limit),
|
| 143 | 1x |
color = "red" |
| 144 |
) + |
|
| 145 | 1x |
annotate( |
| 146 | 1x |
"text", |
| 147 | 1x |
x = xpos, |
| 148 | 1x |
y = result_tem$difference_mean - 0.03 * yrange, |
| 149 | 1x |
label = sprintf("%.2f", result_tem$difference_meanm),
|
| 150 | 1x |
color = "blue" |
| 151 |
) + |
|
| 152 | 1x |
xlab("Average of two measures") +
|
| 153 | 1x |
ylab("Difference between two measures")
|
| 154 | ||
| 155 | 1x |
return(p) |
| 156 |
} |
| 1 |
#' Proportion difference estimation |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analysis function [estimate_proportion_diff()] creates a layout element to estimate the difference in proportion |
|
| 6 |
#' of responders within a studied population. The primary analysis variable, `vars`, is a logical variable indicating |
|
| 7 |
#' whether a response has occurred for each record. See the `method` parameter for options of methods to use when |
|
| 8 |
#' constructing the confidence interval of the proportion difference. A stratification variable can be supplied via the |
|
| 9 |
#' `strata` element of the `variables` argument. |
|
| 10 |
#' |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams prop_diff_strat_nc |
|
| 13 |
#' @inheritParams argument_convention |
|
| 14 |
#' @param method (`string`)\cr the method used for the confidence interval estimation. |
|
| 15 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 16 |
#' |
|
| 17 |
#' Options are: ``r shQuote(get_stats("estimate_proportion_diff"), type = "sh")``
|
|
| 18 |
#' |
|
| 19 |
#' @seealso [d_proportion_diff()] |
|
| 20 |
#' |
|
| 21 |
#' @name prop_diff |
|
| 22 |
#' @order 1 |
|
| 23 |
NULL |
|
| 24 | ||
| 25 |
#' @describeIn prop_diff Statistics function estimating the difference |
|
| 26 |
#' in terms of responder proportion. |
|
| 27 |
#' |
|
| 28 |
#' @return |
|
| 29 |
#' * `s_proportion_diff()` returns a named list of elements `diff` and `diff_ci`. |
|
| 30 |
#' |
|
| 31 |
#' @note When performing an unstratified analysis, methods `"cmh"`, `"strat_newcombe"`, and `"strat_newcombecc"` are |
|
| 32 |
#' not permitted. |
|
| 33 |
#' |
|
| 34 |
#' @examples |
|
| 35 |
#' s_proportion_diff( |
|
| 36 |
#' df = subset(dta, grp == "A"), |
|
| 37 |
#' .var = "rsp", |
|
| 38 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 39 |
#' .in_ref_col = FALSE, |
|
| 40 |
#' conf_level = 0.90, |
|
| 41 |
#' method = "ha" |
|
| 42 |
#' ) |
|
| 43 |
#' |
|
| 44 |
#' # CMH example with strata |
|
| 45 |
#' s_proportion_diff( |
|
| 46 |
#' df = subset(dta, grp == "A"), |
|
| 47 |
#' .var = "rsp", |
|
| 48 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 49 |
#' .in_ref_col = FALSE, |
|
| 50 |
#' variables = list(strata = c("f1", "f2")),
|
|
| 51 |
#' conf_level = 0.90, |
|
| 52 |
#' method = "cmh" |
|
| 53 |
#' ) |
|
| 54 |
#' |
|
| 55 |
#' @export |
|
| 56 |
s_proportion_diff <- function(df, |
|
| 57 |
.var, |
|
| 58 |
.ref_group, |
|
| 59 |
.in_ref_col, |
|
| 60 |
variables = list(strata = NULL), |
|
| 61 |
conf_level = 0.95, |
|
| 62 |
method = c( |
|
| 63 |
"waldcc", "wald", "cmh", |
|
| 64 |
"ha", "newcombe", "newcombecc", |
|
| 65 |
"strat_newcombe", "strat_newcombecc" |
|
| 66 |
), |
|
| 67 |
weights_method = "cmh", |
|
| 68 |
...) {
|
|
| 69 | 11x |
method <- match.arg(method) |
| 70 | 11x |
if (is.null(variables$strata) && checkmate::test_subset(method, c("cmh", "strat_newcombe", "strat_newcombecc"))) {
|
| 71 | ! |
stop(paste( |
| 72 | ! |
"When performing an unstratified analysis, methods 'cmh', 'strat_newcombe', and 'strat_newcombecc' are not", |
| 73 | ! |
"permitted. Please choose a different method." |
| 74 |
)) |
|
| 75 |
} |
|
| 76 | 11x |
y <- list(diff = numeric(), diff_ci = numeric()) |
| 77 | ||
| 78 | 11x |
if (!.in_ref_col) {
|
| 79 | 7x |
rsp <- c(.ref_group[[.var]], df[[.var]]) |
| 80 | 7x |
grp <- factor( |
| 81 | 7x |
rep( |
| 82 | 7x |
c("ref", "Not-ref"),
|
| 83 | 7x |
c(nrow(.ref_group), nrow(df)) |
| 84 |
), |
|
| 85 | 7x |
levels = c("ref", "Not-ref")
|
| 86 |
) |
|
| 87 | ||
| 88 | 7x |
if (!is.null(variables$strata)) {
|
| 89 | 3x |
strata_colnames <- variables$strata |
| 90 | 3x |
checkmate::assert_character(strata_colnames, null.ok = FALSE) |
| 91 | 3x |
strata_vars <- stats::setNames(as.list(strata_colnames), strata_colnames) |
| 92 | ||
| 93 | 3x |
assert_df_with_variables(df, strata_vars) |
| 94 | 3x |
assert_df_with_variables(.ref_group, strata_vars) |
| 95 | ||
| 96 |
# Merging interaction strata for reference group rows data and remaining |
|
| 97 | 3x |
strata <- c( |
| 98 | 3x |
interaction(.ref_group[strata_colnames]), |
| 99 | 3x |
interaction(df[strata_colnames]) |
| 100 |
) |
|
| 101 | 3x |
strata <- as.factor(strata) |
| 102 |
} |
|
| 103 | ||
| 104 |
# Defining the std way to calculate weights for strat_newcombe |
|
| 105 | 7x |
if (!is.null(variables$weights_method)) {
|
| 106 | ! |
weights_method <- variables$weights_method |
| 107 |
} else {
|
|
| 108 | 7x |
weights_method <- "cmh" |
| 109 |
} |
|
| 110 | ||
| 111 | 7x |
y <- switch(method, |
| 112 | 7x |
"wald" = prop_diff_wald(rsp, grp, conf_level, correct = FALSE), |
| 113 | 7x |
"waldcc" = prop_diff_wald(rsp, grp, conf_level, correct = TRUE), |
| 114 | 7x |
"ha" = prop_diff_ha(rsp, grp, conf_level), |
| 115 | 7x |
"newcombe" = prop_diff_nc(rsp, grp, conf_level, correct = FALSE), |
| 116 | 7x |
"newcombecc" = prop_diff_nc(rsp, grp, conf_level, correct = TRUE), |
| 117 | 7x |
"strat_newcombe" = prop_diff_strat_nc(rsp, |
| 118 | 7x |
grp, |
| 119 | 7x |
strata, |
| 120 | 7x |
weights_method, |
| 121 | 7x |
conf_level, |
| 122 | 7x |
correct = FALSE |
| 123 |
), |
|
| 124 | 7x |
"strat_newcombecc" = prop_diff_strat_nc(rsp, |
| 125 | 7x |
grp, |
| 126 | 7x |
strata, |
| 127 | 7x |
weights_method, |
| 128 | 7x |
conf_level, |
| 129 | 7x |
correct = TRUE |
| 130 |
), |
|
| 131 | 7x |
"cmh" = prop_diff_cmh(rsp, grp, strata, conf_level)[c("diff", "diff_ci")]
|
| 132 |
) |
|
| 133 | ||
| 134 | 7x |
y$diff <- setNames(y$diff * 100, paste0("diff_", method))
|
| 135 | 7x |
y$diff_ci <- setNames(y$diff_ci * 100, paste0("diff_ci_", method, c("_l", "_u")))
|
| 136 |
} |
|
| 137 | ||
| 138 | 11x |
attr(y$diff, "label") <- "Difference in Response rate (%)" |
| 139 | 11x |
attr(y$diff_ci, "label") <- d_proportion_diff( |
| 140 | 11x |
conf_level, method, |
| 141 | 11x |
long = FALSE |
| 142 |
) |
|
| 143 | ||
| 144 | 11x |
y |
| 145 |
} |
|
| 146 | ||
| 147 |
#' @describeIn prop_diff Formatted analysis function which is used as `afun` in `estimate_proportion_diff()`. |
|
| 148 |
#' |
|
| 149 |
#' @return |
|
| 150 |
#' * `a_proportion_diff()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 151 |
#' |
|
| 152 |
#' @examples |
|
| 153 |
#' a_proportion_diff( |
|
| 154 |
#' df = subset(dta, grp == "A"), |
|
| 155 |
#' .stats = c("diff"),
|
|
| 156 |
#' .var = "rsp", |
|
| 157 |
#' .ref_group = subset(dta, grp == "B"), |
|
| 158 |
#' .in_ref_col = FALSE, |
|
| 159 |
#' conf_level = 0.90, |
|
| 160 |
#' method = "ha" |
|
| 161 |
#' ) |
|
| 162 |
#' |
|
| 163 |
#' @export |
|
| 164 |
a_proportion_diff <- function(df, |
|
| 165 |
..., |
|
| 166 |
.stats = NULL, |
|
| 167 |
.stat_names = NULL, |
|
| 168 |
.formats = NULL, |
|
| 169 |
.labels = NULL, |
|
| 170 |
.indent_mods = NULL) {
|
|
| 171 | 9x |
dots_extra_args <- list(...) |
| 172 | ||
| 173 |
# Check if there are user-defined functions |
|
| 174 | 9x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 175 | 9x |
.stats <- default_and_custom_stats_list$all_stats |
| 176 | 9x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 177 | ||
| 178 |
# Adding automatically extra parameters to the statistic function (see ?rtables::additional_fun_params) |
|
| 179 | 9x |
extra_afun_params <- retrieve_extra_afun_params( |
| 180 | 9x |
names(dots_extra_args$.additional_fun_parameters) |
| 181 |
) |
|
| 182 | 9x |
dots_extra_args$.additional_fun_parameters <- NULL # After extraction we do not need them anymore |
| 183 | ||
| 184 |
# Main statistical functions application |
|
| 185 | 9x |
x_stats <- .apply_stat_functions( |
| 186 | 9x |
default_stat_fnc = s_proportion_diff, |
| 187 | 9x |
custom_stat_fnc_list = custom_stat_functions, |
| 188 | 9x |
args_list = c( |
| 189 | 9x |
df = list(df), |
| 190 | 9x |
extra_afun_params, |
| 191 | 9x |
dots_extra_args |
| 192 |
) |
|
| 193 |
) |
|
| 194 | ||
| 195 |
# Fill in with stats defaults if needed |
|
| 196 | 9x |
.stats <- get_stats("estimate_proportion_diff",
|
| 197 | 9x |
stats_in = .stats, |
| 198 | 9x |
custom_stats_in = names(custom_stat_functions) |
| 199 |
) |
|
| 200 | ||
| 201 | 9x |
x_stats <- x_stats[.stats] |
| 202 | ||
| 203 |
# Fill in formats/indents/labels with custom input and defaults |
|
| 204 | 9x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 205 | 9x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 206 | 9x |
if (is.null(.labels)) {
|
| 207 | 9x |
.labels <- sapply(x_stats, attr, "label") |
| 208 | 9x |
.labels <- .labels[nzchar(.labels) & !sapply(.labels, is.null) & !is.na(.labels)] |
| 209 |
} |
|
| 210 | 9x |
.labels <- get_labels_from_stats(.stats, .labels) |
| 211 | ||
| 212 |
# Auto format handling |
|
| 213 | 9x |
.formats <- apply_auto_formatting( |
| 214 | 9x |
.formats, |
| 215 | 9x |
x_stats, |
| 216 | 9x |
extra_afun_params$.df_row, |
| 217 | 9x |
extra_afun_params$.var |
| 218 |
) |
|
| 219 | ||
| 220 |
# Get and check statistical names from defaults |
|
| 221 | 9x |
.stat_names <- get_stat_names(x_stats, .stat_names) # note is x_stats |
| 222 | ||
| 223 | 9x |
in_rows( |
| 224 | 9x |
.list = x_stats, |
| 225 | 9x |
.formats = .formats, |
| 226 | 9x |
.names = names(.labels), |
| 227 | 9x |
.stat_names = .stat_names, |
| 228 | 9x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 229 | 9x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 230 |
) |
|
| 231 |
} |
|
| 232 | ||
| 233 |
#' @describeIn prop_diff Layout-creating function which can take statistics function arguments |
|
| 234 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 235 |
#' |
|
| 236 |
#' @return |
|
| 237 |
#' * `estimate_proportion_diff()` returns a layout object suitable for passing to further layouting functions, |
|
| 238 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 239 |
#' the statistics from `s_proportion_diff()` to the table layout. |
|
| 240 |
#' |
|
| 241 |
#' @examples |
|
| 242 |
#' ## "Mid" case: 4/4 respond in group A, 1/2 respond in group B. |
|
| 243 |
#' nex <- 100 # Number of example rows |
|
| 244 |
#' dta <- data.frame( |
|
| 245 |
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE), |
|
| 246 |
#' "grp" = sample(c("A", "B"), nex, TRUE),
|
|
| 247 |
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
|
|
| 248 |
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
|
|
| 249 |
#' stringsAsFactors = TRUE |
|
| 250 |
#' ) |
|
| 251 |
#' |
|
| 252 |
#' l <- basic_table() %>% |
|
| 253 |
#' split_cols_by(var = "grp", ref_group = "B") %>% |
|
| 254 |
#' estimate_proportion_diff( |
|
| 255 |
#' vars = "rsp", |
|
| 256 |
#' conf_level = 0.90, |
|
| 257 |
#' method = "ha" |
|
| 258 |
#' ) |
|
| 259 |
#' |
|
| 260 |
#' build_table(l, df = dta) |
|
| 261 |
#' |
|
| 262 |
#' @export |
|
| 263 |
#' @order 2 |
|
| 264 |
estimate_proportion_diff <- function(lyt, |
|
| 265 |
vars, |
|
| 266 |
variables = list(strata = NULL), |
|
| 267 |
conf_level = 0.95, |
|
| 268 |
method = c( |
|
| 269 |
"waldcc", "wald", "cmh", |
|
| 270 |
"ha", "newcombe", "newcombecc", |
|
| 271 |
"strat_newcombe", "strat_newcombecc" |
|
| 272 |
), |
|
| 273 |
weights_method = "cmh", |
|
| 274 |
var_labels = vars, |
|
| 275 |
na_str = default_na_str(), |
|
| 276 |
nested = TRUE, |
|
| 277 |
show_labels = "hidden", |
|
| 278 |
table_names = vars, |
|
| 279 |
section_div = NA_character_, |
|
| 280 |
..., |
|
| 281 |
na_rm = TRUE, |
|
| 282 |
.stats = c("diff", "diff_ci"),
|
|
| 283 |
.stat_names = NULL, |
|
| 284 |
.formats = c(diff = "xx.x", diff_ci = "(xx.x, xx.x)"), |
|
| 285 |
.labels = NULL, |
|
| 286 |
.indent_mods = c(diff = 0L, diff_ci = 1L)) {
|
|
| 287 |
# Depending on main functions |
|
| 288 | 4x |
extra_args <- list( |
| 289 | 4x |
"na_rm" = na_rm, |
| 290 | 4x |
"variables" = variables, |
| 291 | 4x |
"conf_level" = conf_level, |
| 292 | 4x |
"method" = method, |
| 293 | 4x |
"weights_method" = weights_method, |
| 294 |
... |
|
| 295 |
) |
|
| 296 | ||
| 297 |
# Needed defaults |
|
| 298 | 4x |
if (!is.null(.stats)) extra_args[[".stats"]] <- .stats |
| 299 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 300 | 4x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 301 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 302 | 4x |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 303 | ||
| 304 |
# Adding all additional information from layout to analysis functions (see ?rtables::additional_fun_params) |
|
| 305 | 4x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 306 | 4x |
formals(a_proportion_diff) <- c( |
| 307 | 4x |
formals(a_proportion_diff), |
| 308 | 4x |
extra_args[[".additional_fun_parameters"]] |
| 309 |
) |
|
| 310 | ||
| 311 |
# Main {rtables} structural call
|
|
| 312 | 4x |
analyze( |
| 313 | 4x |
lyt = lyt, |
| 314 | 4x |
vars = vars, |
| 315 | 4x |
var_labels = var_labels, |
| 316 | 4x |
afun = a_proportion_diff, |
| 317 | 4x |
na_str = na_str, |
| 318 | 4x |
inclNAs = !na_rm, |
| 319 | 4x |
nested = nested, |
| 320 | 4x |
extra_args = extra_args, |
| 321 | 4x |
show_labels = show_labels, |
| 322 | 4x |
table_names = table_names, |
| 323 | 4x |
section_div = section_div |
| 324 |
) |
|
| 325 |
} |
|
| 326 | ||
| 327 |
#' Check proportion difference arguments |
|
| 328 |
#' |
|
| 329 |
#' Verifies that and/or convert arguments into valid values to be used in the |
|
| 330 |
#' estimation of difference in responder proportions. |
|
| 331 |
#' |
|
| 332 |
#' @inheritParams prop_diff |
|
| 333 |
#' @inheritParams prop_diff_wald |
|
| 334 |
#' |
|
| 335 |
#' @keywords internal |
|
| 336 |
check_diff_prop_ci <- function(rsp, |
|
| 337 |
grp, |
|
| 338 |
strata = NULL, |
|
| 339 |
conf_level, |
|
| 340 |
correct = NULL) {
|
|
| 341 | 26x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
| 342 | 26x |
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2) |
| 343 | 26x |
checkmate::assert_number(conf_level, lower = 0, upper = 1) |
| 344 | 26x |
checkmate::assert_flag(correct, null.ok = TRUE) |
| 345 | ||
| 346 | 26x |
if (!is.null(strata)) {
|
| 347 | 12x |
checkmate::assert_factor(strata, len = length(rsp)) |
| 348 |
} |
|
| 349 | ||
| 350 | 26x |
invisible() |
| 351 |
} |
|
| 352 | ||
| 353 |
#' Description of method used for proportion comparison |
|
| 354 |
#' |
|
| 355 |
#' @description `r lifecycle::badge("stable")`
|
|
| 356 |
#' |
|
| 357 |
#' This is an auxiliary function that describes the analysis in |
|
| 358 |
#' [s_proportion_diff()]. |
|
| 359 |
#' |
|
| 360 |
#' @inheritParams s_proportion_diff |
|
| 361 |
#' @param long (`flag`)\cr whether a long (`TRUE`) or a short (`FALSE`, default) description is required. |
|
| 362 |
#' |
|
| 363 |
#' @return A `string` describing the analysis. |
|
| 364 |
#' |
|
| 365 |
#' @seealso [prop_diff] |
|
| 366 |
#' |
|
| 367 |
#' @export |
|
| 368 |
d_proportion_diff <- function(conf_level, |
|
| 369 |
method, |
|
| 370 |
long = FALSE) {
|
|
| 371 | 11x |
label <- paste0(conf_level * 100, "% CI") |
| 372 | 11x |
if (long) {
|
| 373 | ! |
label <- paste( |
| 374 | ! |
label, |
| 375 | ! |
ifelse( |
| 376 | ! |
method == "cmh", |
| 377 | ! |
"for adjusted difference", |
| 378 | ! |
"for difference" |
| 379 |
) |
|
| 380 |
) |
|
| 381 |
} |
|
| 382 | ||
| 383 | 11x |
method_part <- switch(method, |
| 384 | 11x |
"cmh" = "CMH, without correction", |
| 385 | 11x |
"waldcc" = "Wald, with correction", |
| 386 | 11x |
"wald" = "Wald, without correction", |
| 387 | 11x |
"ha" = "Anderson-Hauck", |
| 388 | 11x |
"newcombe" = "Newcombe, without correction", |
| 389 | 11x |
"newcombecc" = "Newcombe, with correction", |
| 390 | 11x |
"strat_newcombe" = "Stratified Newcombe, without correction", |
| 391 | 11x |
"strat_newcombecc" = "Stratified Newcombe, with correction", |
| 392 | 11x |
stop(paste(method, "does not have a description")) |
| 393 |
) |
|
| 394 | 11x |
paste0(label, " (", method_part, ")")
|
| 395 |
} |
|
| 396 | ||
| 397 |
#' Helper functions to calculate proportion difference |
|
| 398 |
#' |
|
| 399 |
#' @description `r lifecycle::badge("stable")`
|
|
| 400 |
#' |
|
| 401 |
#' @inheritParams argument_convention |
|
| 402 |
#' @inheritParams prop_diff |
|
| 403 |
#' @param grp (`factor`)\cr vector assigning observations to one out of two groups |
|
| 404 |
#' (e.g. reference and treatment group). |
|
| 405 |
#' |
|
| 406 |
#' @return A named `list` of elements `diff` (proportion difference) and `diff_ci` |
|
| 407 |
#' (proportion difference confidence interval). |
|
| 408 |
#' |
|
| 409 |
#' @seealso [prop_diff()] for implementation of these helper functions. |
|
| 410 |
#' |
|
| 411 |
#' @name h_prop_diff |
|
| 412 |
NULL |
|
| 413 | ||
| 414 |
#' @describeIn h_prop_diff The Wald interval follows the usual textbook |
|
| 415 |
#' definition for a single proportion confidence interval using the normal |
|
| 416 |
#' approximation. It is possible to include a continuity correction for Wald's |
|
| 417 |
#' interval. |
|
| 418 |
#' |
|
| 419 |
#' @param correct (`flag`)\cr whether to include the continuity correction. For further |
|
| 420 |
#' information, see [stats::prop.test()]. |
|
| 421 |
#' |
|
| 422 |
#' @examples |
|
| 423 |
#' # Wald confidence interval |
|
| 424 |
#' set.seed(2) |
|
| 425 |
#' rsp <- sample(c(TRUE, FALSE), replace = TRUE, size = 20) |
|
| 426 |
#' grp <- factor(c(rep("A", 10), rep("B", 10)))
|
|
| 427 |
#' |
|
| 428 |
#' prop_diff_wald(rsp = rsp, grp = grp, conf_level = 0.95, correct = FALSE) |
|
| 429 |
#' |
|
| 430 |
#' @export |
|
| 431 |
prop_diff_wald <- function(rsp, |
|
| 432 |
grp, |
|
| 433 |
conf_level = 0.95, |
|
| 434 |
correct = FALSE) {
|
|
| 435 | 8x |
if (isTRUE(correct)) {
|
| 436 | 5x |
mthd <- "waldcc" |
| 437 |
} else {
|
|
| 438 | 3x |
mthd <- "wald" |
| 439 |
} |
|
| 440 | 8x |
grp <- as_factor_keep_attributes(grp) |
| 441 | 8x |
check_diff_prop_ci( |
| 442 | 8x |
rsp = rsp, grp = grp, conf_level = conf_level, correct = correct |
| 443 |
) |
|
| 444 | ||
| 445 |
# check if binary response is coded as logical |
|
| 446 | 8x |
checkmate::assert_logical(rsp, any.missing = FALSE) |
| 447 | 8x |
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2) |
| 448 | ||
| 449 | 8x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
| 450 |
# x1 and n1 are non-reference groups. |
|
| 451 | 8x |
diff_ci <- desctools_binom( |
| 452 | 8x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
| 453 | 8x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
| 454 | 8x |
conf.level = conf_level, |
| 455 | 8x |
method = mthd |
| 456 |
) |
|
| 457 | ||
| 458 | 8x |
list( |
| 459 | 8x |
"diff" = unname(diff_ci[, "est"]), |
| 460 | 8x |
"diff_ci" = unname(diff_ci[, c("lwr.ci", "upr.ci")])
|
| 461 |
) |
|
| 462 |
} |
|
| 463 | ||
| 464 |
#' @describeIn h_prop_diff Anderson-Hauck confidence interval. |
|
| 465 |
#' |
|
| 466 |
#' @examples |
|
| 467 |
#' # Anderson-Hauck confidence interval |
|
| 468 |
#' ## "Mid" case: 3/4 respond in group A, 1/2 respond in group B. |
|
| 469 |
#' rsp <- c(TRUE, FALSE, FALSE, TRUE, TRUE, TRUE) |
|
| 470 |
#' grp <- factor(c("A", "B", "A", "B", "A", "A"), levels = c("B", "A"))
|
|
| 471 |
#' |
|
| 472 |
#' prop_diff_ha(rsp = rsp, grp = grp, conf_level = 0.90) |
|
| 473 |
#' |
|
| 474 |
#' ## Edge case: Same proportion of response in A and B. |
|
| 475 |
#' rsp <- c(TRUE, FALSE, TRUE, FALSE) |
|
| 476 |
#' grp <- factor(c("A", "A", "B", "B"), levels = c("A", "B"))
|
|
| 477 |
#' |
|
| 478 |
#' prop_diff_ha(rsp = rsp, grp = grp, conf_level = 0.6) |
|
| 479 |
#' |
|
| 480 |
#' @export |
|
| 481 |
prop_diff_ha <- function(rsp, |
|
| 482 |
grp, |
|
| 483 |
conf_level) {
|
|
| 484 | 4x |
grp <- as_factor_keep_attributes(grp) |
| 485 | 4x |
check_diff_prop_ci(rsp = rsp, grp = grp, conf_level = conf_level) |
| 486 | ||
| 487 | 4x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
| 488 |
# x1 and n1 are non-reference groups. |
|
| 489 | 4x |
ci <- desctools_binom( |
| 490 | 4x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
| 491 | 4x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
| 492 | 4x |
conf.level = conf_level, |
| 493 | 4x |
method = "ha" |
| 494 |
) |
|
| 495 | 4x |
list( |
| 496 | 4x |
"diff" = unname(ci[, "est"]), |
| 497 | 4x |
"diff_ci" = unname(ci[, c("lwr.ci", "upr.ci")])
|
| 498 |
) |
|
| 499 |
} |
|
| 500 | ||
| 501 |
#' @describeIn h_prop_diff Newcombe confidence interval. It is based on |
|
| 502 |
#' the Wilson score confidence interval for a single binomial proportion. |
|
| 503 |
#' |
|
| 504 |
#' @examples |
|
| 505 |
#' # Newcombe confidence interval |
|
| 506 |
#' |
|
| 507 |
#' set.seed(1) |
|
| 508 |
#' rsp <- c( |
|
| 509 |
#' sample(c(TRUE, FALSE), size = 40, prob = c(3 / 4, 1 / 4), replace = TRUE), |
|
| 510 |
#' sample(c(TRUE, FALSE), size = 40, prob = c(1 / 2, 1 / 2), replace = TRUE) |
|
| 511 |
#' ) |
|
| 512 |
#' grp <- factor(rep(c("A", "B"), each = 40), levels = c("B", "A"))
|
|
| 513 |
#' table(rsp, grp) |
|
| 514 |
#' |
|
| 515 |
#' prop_diff_nc(rsp = rsp, grp = grp, conf_level = 0.9) |
|
| 516 |
#' |
|
| 517 |
#' @export |
|
| 518 |
prop_diff_nc <- function(rsp, |
|
| 519 |
grp, |
|
| 520 |
conf_level, |
|
| 521 |
correct = FALSE) {
|
|
| 522 | 2x |
if (isTRUE(correct)) {
|
| 523 | ! |
mthd <- "scorecc" |
| 524 |
} else {
|
|
| 525 | 2x |
mthd <- "score" |
| 526 |
} |
|
| 527 | 2x |
grp <- as_factor_keep_attributes(grp) |
| 528 | 2x |
check_diff_prop_ci(rsp = rsp, grp = grp, conf_level = conf_level) |
| 529 | ||
| 530 | 2x |
p_grp <- tapply(rsp, grp, mean) |
| 531 | 2x |
diff_p <- unname(diff(p_grp)) |
| 532 | 2x |
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE))) |
| 533 | 2x |
ci <- desctools_binom( |
| 534 |
# x1 and n1 are non-reference groups. |
|
| 535 | 2x |
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]), |
| 536 | 2x |
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]), |
| 537 | 2x |
conf.level = conf_level, |
| 538 | 2x |
method = mthd |
| 539 |
) |
|
| 540 | 2x |
list( |
| 541 | 2x |
"diff" = unname(ci[, "est"]), |
| 542 | 2x |
"diff_ci" = unname(ci[, c("lwr.ci", "upr.ci")])
|
| 543 |
) |
|
| 544 |
} |
|
| 545 | ||
| 546 |
#' @describeIn h_prop_diff Calculates the weighted difference. This is defined as the difference in |
|
| 547 |
#' response rates between the experimental treatment group and the control treatment group, adjusted |
|
| 548 |
#' for stratification factors by applying Cochran-Mantel-Haenszel (CMH) weights. For the CMH chi-squared |
|
| 549 |
#' test, use [stats::mantelhaen.test()]. |
|
| 550 |
#' |
|
| 551 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`. |
|
| 552 |
#' |
|
| 553 |
#' @examples |
|
| 554 |
#' # Cochran-Mantel-Haenszel confidence interval |
|
| 555 |
#' |
|
| 556 |
#' set.seed(2) |
|
| 557 |
#' rsp <- sample(c(TRUE, FALSE), 100, TRUE) |
|
| 558 |
#' grp <- sample(c("Placebo", "Treatment"), 100, TRUE)
|
|
| 559 |
#' grp <- factor(grp, levels = c("Placebo", "Treatment"))
|
|
| 560 |
#' strata_data <- data.frame( |
|
| 561 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
| 562 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
| 563 |
#' stringsAsFactors = TRUE |
|
| 564 |
#' ) |
|
| 565 |
#' |
|
| 566 |
#' prop_diff_cmh( |
|
| 567 |
#' rsp = rsp, grp = grp, strata = interaction(strata_data), |
|
| 568 |
#' conf_level = 0.90 |
|
| 569 |
#' ) |
|
| 570 |
#' |
|
| 571 |
#' @export |
|
| 572 |
prop_diff_cmh <- function(rsp, |
|
| 573 |
grp, |
|
| 574 |
strata, |
|
| 575 |
conf_level = 0.95) {
|
|
| 576 | 8x |
grp <- as_factor_keep_attributes(grp) |
| 577 | 8x |
strata <- as_factor_keep_attributes(strata) |
| 578 | 8x |
check_diff_prop_ci( |
| 579 | 8x |
rsp = rsp, grp = grp, conf_level = conf_level, strata = strata |
| 580 |
) |
|
| 581 | ||
| 582 | 8x |
if (any(tapply(rsp, strata, length) < 5)) {
|
| 583 | 1x |
warning("Less than 5 observations in some strata.")
|
| 584 |
} |
|
| 585 | ||
| 586 |
# first dimension: FALSE, TRUE |
|
| 587 |
# 2nd dimension: CONTROL, TX |
|
| 588 |
# 3rd dimension: levels of strata |
|
| 589 |
# rsp as factor rsp to handle edge case of no FALSE (or TRUE) rsp records |
|
| 590 | 8x |
t_tbl <- table( |
| 591 | 8x |
factor(rsp, levels = c("FALSE", "TRUE")),
|
| 592 | 8x |
grp, |
| 593 | 8x |
strata |
| 594 |
) |
|
| 595 | 8x |
n1 <- colSums(t_tbl[1:2, 1, ]) |
| 596 | 8x |
n2 <- colSums(t_tbl[1:2, 2, ]) |
| 597 | 8x |
p1 <- t_tbl[2, 1, ] / n1 |
| 598 | 8x |
p2 <- t_tbl[2, 2, ] / n2 |
| 599 |
# CMH weights |
|
| 600 | 8x |
use_stratum <- (n1 > 0) & (n2 > 0) |
| 601 | 8x |
n1 <- n1[use_stratum] |
| 602 | 8x |
n2 <- n2[use_stratum] |
| 603 | 8x |
p1 <- p1[use_stratum] |
| 604 | 8x |
p2 <- p2[use_stratum] |
| 605 | 8x |
wt <- (n1 * n2 / (n1 + n2)) |
| 606 | 8x |
wt_normalized <- wt / sum(wt) |
| 607 | 8x |
est1 <- sum(wt_normalized * p1) |
| 608 | 8x |
est2 <- sum(wt_normalized * p2) |
| 609 | 8x |
estimate <- c(est1, est2) |
| 610 | 8x |
names(estimate) <- levels(grp) |
| 611 | 8x |
se1 <- sqrt(sum(wt_normalized^2 * p1 * (1 - p1) / n1)) |
| 612 | 8x |
se2 <- sqrt(sum(wt_normalized^2 * p2 * (1 - p2) / n2)) |
| 613 | 8x |
z <- stats::qnorm((1 + conf_level) / 2) |
| 614 | 8x |
err1 <- z * se1 |
| 615 | 8x |
err2 <- z * se2 |
| 616 | 8x |
ci1 <- c((est1 - err1), (est1 + err1)) |
| 617 | 8x |
ci2 <- c((est2 - err2), (est2 + err2)) |
| 618 | 8x |
estimate_ci <- list(ci1, ci2) |
| 619 | 8x |
names(estimate_ci) <- levels(grp) |
| 620 | 8x |
diff_est <- est2 - est1 |
| 621 | 8x |
se_diff <- sqrt(sum(((p1 * (1 - p1) / n1) + (p2 * (1 - p2) / n2)) * wt_normalized^2)) |
| 622 | 8x |
diff_ci <- c(diff_est - z * se_diff, diff_est + z * se_diff) |
| 623 | ||
| 624 | 8x |
list( |
| 625 | 8x |
prop = estimate, |
| 626 | 8x |
prop_ci = estimate_ci, |
| 627 | 8x |
diff = diff_est, |
| 628 | 8x |
diff_ci = diff_ci, |
| 629 | 8x |
weights = wt_normalized, |
| 630 | 8x |
n1 = n1, |
| 631 | 8x |
n2 = n2 |
| 632 |
) |
|
| 633 |
} |
|
| 634 | ||
| 635 |
#' @describeIn h_prop_diff Calculates the stratified Newcombe confidence interval and difference in response |
|
| 636 |
#' rates between the experimental treatment group and the control treatment group, adjusted for stratification |
|
| 637 |
#' factors. This implementation follows closely the one proposed by \insertCite{Yan2010-jt;textual}{tern}.
|
|
| 638 |
#' Weights can be estimated from the heuristic proposed in [prop_strat_wilson()] or from CMH-derived weights |
|
| 639 |
#' (see [prop_diff_cmh()]). |
|
| 640 |
#' |
|
| 641 |
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `rsp`. |
|
| 642 |
#' @param weights_method (`string`)\cr weights method. Can be either `"cmh"` or `"heuristic"` |
|
| 643 |
#' and directs the way weights are estimated. |
|
| 644 |
#' |
|
| 645 |
#' @references |
|
| 646 |
#' \insertRef{Yan2010-jt}{tern}
|
|
| 647 |
#' |
|
| 648 |
#' @examples |
|
| 649 |
#' # Stratified Newcombe confidence interval |
|
| 650 |
#' |
|
| 651 |
#' set.seed(2) |
|
| 652 |
#' data_set <- data.frame( |
|
| 653 |
#' "rsp" = sample(c(TRUE, FALSE), 100, TRUE), |
|
| 654 |
#' "f1" = sample(c("a", "b"), 100, TRUE),
|
|
| 655 |
#' "f2" = sample(c("x", "y", "z"), 100, TRUE),
|
|
| 656 |
#' "grp" = sample(c("Placebo", "Treatment"), 100, TRUE),
|
|
| 657 |
#' stringsAsFactors = TRUE |
|
| 658 |
#' ) |
|
| 659 |
#' |
|
| 660 |
#' prop_diff_strat_nc( |
|
| 661 |
#' rsp = data_set$rsp, grp = data_set$grp, strata = interaction(data_set[2:3]), |
|
| 662 |
#' weights_method = "cmh", |
|
| 663 |
#' conf_level = 0.90 |
|
| 664 |
#' ) |
|
| 665 |
#' |
|
| 666 |
#' prop_diff_strat_nc( |
|
| 667 |
#' rsp = data_set$rsp, grp = data_set$grp, strata = interaction(data_set[2:3]), |
|
| 668 |
#' weights_method = "wilson_h", |
|
| 669 |
#' conf_level = 0.90 |
|
| 670 |
#' ) |
|
| 671 |
#' |
|
| 672 |
#' @export |
|
| 673 |
prop_diff_strat_nc <- function(rsp, |
|
| 674 |
grp, |
|
| 675 |
strata, |
|
| 676 |
weights_method = c("cmh", "wilson_h"),
|
|
| 677 |
conf_level = 0.95, |
|
| 678 |
correct = FALSE) {
|
|
| 679 | 4x |
weights_method <- match.arg(weights_method) |
| 680 | 4x |
grp <- as_factor_keep_attributes(grp) |
| 681 | 4x |
strata <- as_factor_keep_attributes(strata) |
| 682 | 4x |
check_diff_prop_ci( |
| 683 | 4x |
rsp = rsp, grp = grp, conf_level = conf_level, strata = strata |
| 684 |
) |
|
| 685 | 4x |
checkmate::assert_number(conf_level, lower = 0, upper = 1) |
| 686 | 4x |
checkmate::assert_flag(correct) |
| 687 | 4x |
if (any(tapply(rsp, strata, length) < 5)) {
|
| 688 | ! |
warning("Less than 5 observations in some strata.")
|
| 689 |
} |
|
| 690 | ||
| 691 | 4x |
rsp_by_grp <- split(rsp, f = grp) |
| 692 | 4x |
strata_by_grp <- split(strata, f = grp) |
| 693 | ||
| 694 |
# Finding the weights |
|
| 695 | 4x |
weights <- if (identical(weights_method, "cmh")) {
|
| 696 | 3x |
prop_diff_cmh(rsp = rsp, grp = grp, strata = strata)$weights |
| 697 | 4x |
} else if (identical(weights_method, "wilson_h")) {
|
| 698 | 1x |
prop_strat_wilson(rsp, strata, conf_level = conf_level, correct = correct)$weights |
| 699 |
} |
|
| 700 | 4x |
weights[levels(strata)[!levels(strata) %in% names(weights)]] <- 0 |
| 701 | ||
| 702 |
# Calculating lower (`l`) and upper (`u`) confidence bounds per group. |
|
| 703 | 4x |
strat_wilson_by_grp <- Map( |
| 704 | 4x |
prop_strat_wilson, |
| 705 | 4x |
rsp = rsp_by_grp, |
| 706 | 4x |
strata = strata_by_grp, |
| 707 | 4x |
weights = list(weights, weights), |
| 708 | 4x |
conf_level = conf_level, |
| 709 | 4x |
correct = correct |
| 710 |
) |
|
| 711 | ||
| 712 | 4x |
ci_ref <- strat_wilson_by_grp[[1]] |
| 713 | 4x |
ci_trt <- strat_wilson_by_grp[[2]] |
| 714 | 4x |
l_ref <- as.numeric(ci_ref$conf_int[1]) |
| 715 | 4x |
u_ref <- as.numeric(ci_ref$conf_int[2]) |
| 716 | 4x |
l_trt <- as.numeric(ci_trt$conf_int[1]) |
| 717 | 4x |
u_trt <- as.numeric(ci_trt$conf_int[2]) |
| 718 | ||
| 719 |
# Estimating the diff and n_ref, n_trt (it allows different weights to be used) |
|
| 720 | 4x |
t_tbl <- table( |
| 721 | 4x |
factor(rsp, levels = c("FALSE", "TRUE")),
|
| 722 | 4x |
grp, |
| 723 | 4x |
strata |
| 724 |
) |
|
| 725 | 4x |
n_ref <- colSums(t_tbl[1:2, 1, ]) |
| 726 | 4x |
n_trt <- colSums(t_tbl[1:2, 2, ]) |
| 727 | 4x |
use_stratum <- (n_ref > 0) & (n_trt > 0) |
| 728 | 4x |
n_ref <- n_ref[use_stratum] |
| 729 | 4x |
n_trt <- n_trt[use_stratum] |
| 730 | 4x |
p_ref <- t_tbl[2, 1, use_stratum] / n_ref |
| 731 | 4x |
p_trt <- t_tbl[2, 2, use_stratum] / n_trt |
| 732 | 4x |
est1 <- sum(weights * p_ref) |
| 733 | 4x |
est2 <- sum(weights * p_trt) |
| 734 | 4x |
diff_est <- est2 - est1 |
| 735 | ||
| 736 | 4x |
lambda1 <- sum(weights^2 / n_ref) |
| 737 | 4x |
lambda2 <- sum(weights^2 / n_trt) |
| 738 | 4x |
z <- stats::qnorm((1 + conf_level) / 2) |
| 739 | ||
| 740 | 4x |
lower <- diff_est - z * sqrt(lambda2 * l_trt * (1 - l_trt) + lambda1 * u_ref * (1 - u_ref)) |
| 741 | 4x |
upper <- diff_est + z * sqrt(lambda1 * l_ref * (1 - l_ref) + lambda2 * u_trt * (1 - u_trt)) |
| 742 | ||
| 743 | 4x |
list( |
| 744 | 4x |
"diff" = diff_est, |
| 745 | 4x |
"diff_ci" = c("lower" = lower, "upper" = upper)
|
| 746 |
) |
|
| 747 |
} |
| 1 |
#' Kaplan-Meier plot |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' From a survival model, a graphic is rendered along with tabulated annotation |
|
| 6 |
#' including the number of patient at risk at given time and the median survival |
|
| 7 |
#' per group. |
|
| 8 |
#' |
|
| 9 |
#' @inheritParams argument_convention |
|
| 10 |
#' @param variables (named `list`)\cr variable names. Details are: |
|
| 11 |
#' * `tte` (`numeric`)\cr variable indicating time-to-event duration values. |
|
| 12 |
#' * `is_event` (`logical`)\cr event variable. `TRUE` if event, `FALSE` if time to event is censored. |
|
| 13 |
#' * `arm` (`factor`)\cr the treatment group variable. |
|
| 14 |
#' * `strata` (`character` or `NULL`)\cr variable names indicating stratification factors. |
|
| 15 |
#' @param control_surv (`list`)\cr parameters for comparison details, specified by using |
|
| 16 |
#' the helper function [control_surv_timepoint()]. Some possible parameter options are: |
|
| 17 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for survival rate. |
|
| 18 |
#' * `conf_type` (`string`)\cr `"plain"` (default), `"log"`, `"log-log"` for confidence interval type, |
|
| 19 |
#' see more in [survival::survfit()]. Note that the option "none" is no longer supported. |
|
| 20 |
#' @param col (`character`)\cr lines colors. Length of a vector should be equal |
|
| 21 |
#' to number of strata from [survival::survfit()]. |
|
| 22 |
#' @param lty (`numeric`)\cr line type. If a vector is given, its length should be equal to the number of strata from |
|
| 23 |
#' [survival::survfit()]. |
|
| 24 |
#' @param lwd (`numeric`)\cr line width. If a vector is given, its length should be equal to the number of strata from |
|
| 25 |
#' [survival::survfit()]. |
|
| 26 |
#' @param censor_show (`flag`)\cr whether to show censored observations. |
|
| 27 |
#' @param pch (`string`)\cr name of symbol or character to use as point symbol to indicate censored cases. |
|
| 28 |
#' @param size (`numeric(1)`)\cr size of censored point symbols. |
|
| 29 |
#' @param max_time (`numeric(1)`)\cr maximum value to show on x-axis. Only data values less than or up to |
|
| 30 |
#' this threshold value will be plotted (defaults to `NULL`). |
|
| 31 |
#' @param xticks (`numeric` or `NULL`)\cr numeric vector of tick positions or a single number with spacing |
|
| 32 |
#' between ticks on the x-axis. If `NULL` (default), [labeling::extended()] is used to determine |
|
| 33 |
#' optimal tick positions on the x-axis. |
|
| 34 |
#' @param xlab (`string`)\cr x-axis label. |
|
| 35 |
#' @param yval (`string`)\cr type of plot, to be plotted on the y-axis. Options are `Survival` (default) and `Failure` |
|
| 36 |
#' probability. |
|
| 37 |
#' @param ylab (`string`)\cr y-axis label. |
|
| 38 |
#' @param title (`string`)\cr plot title. |
|
| 39 |
#' @param footnotes (`string`)\cr plot footnotes. |
|
| 40 |
#' @param font_size (`numeric(1)`)\cr font size to use for all text. |
|
| 41 |
#' @param ci_ribbon (`flag`)\cr whether the confidence interval should be drawn around the Kaplan-Meier curve. |
|
| 42 |
#' @param annot_at_risk (`flag`)\cr compute and add the annotation table reporting the number of patient at risk |
|
| 43 |
#' matching the main grid of the Kaplan-Meier curve. |
|
| 44 |
#' @param annot_at_risk_title (`flag`)\cr whether the "Patients at Risk" title should be added above the `annot_at_risk` |
|
| 45 |
#' table. Has no effect if `annot_at_risk` is `FALSE`. Defaults to `TRUE`. |
|
| 46 |
#' @param annot_surv_med (`flag`)\cr compute and add the annotation table on the Kaplan-Meier curve estimating the |
|
| 47 |
#' median survival time per group. |
|
| 48 |
#' @param annot_coxph (`flag`)\cr whether to add the annotation table from a [survival::coxph()] model. |
|
| 49 |
#' @param annot_stats (`string` or `NULL`)\cr statistics annotations to add to the plot. Options are |
|
| 50 |
#' `median` (median survival follow-up time) and `min` (minimum survival follow-up time). |
|
| 51 |
#' @param annot_stats_vlines (`flag`)\cr add vertical lines corresponding to each of the statistics |
|
| 52 |
#' specified by `annot_stats`. If `annot_stats` is `NULL` no lines will be added. |
|
| 53 |
#' @param control_coxph_pw (`list`)\cr parameters for comparison details, specified using the helper function |
|
| 54 |
#' [control_coxph()]. Some possible parameter options are: |
|
| 55 |
#' * `pval_method` (`string`)\cr p-value method for testing hazard ratio = 1. |
|
| 56 |
#' Default method is `"log-rank"`, can also be set to `"wald"` or `"likelihood"`. |
|
| 57 |
#' * `ties` (`string`)\cr method for tie handling. Default is `"efron"`, |
|
| 58 |
#' can also be set to `"breslow"` or `"exact"`. See more in [survival::coxph()] |
|
| 59 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for HR. |
|
| 60 |
#' @param ref_group_coxph (`string` or `NULL`)\cr level of arm variable to use as reference group in calculations for |
|
| 61 |
#' `annot_coxph` table. If `NULL` (default), uses the first level of the arm variable. |
|
| 62 |
#' @param control_annot_surv_med (`list`)\cr parameters to control the position and size of the annotation table added |
|
| 63 |
#' to the plot when `annot_surv_med = TRUE`, specified using the [control_surv_med_annot()] function. Parameter |
|
| 64 |
#' options are: `x`, `y`, `w`, `h`, and `fill`. See [control_surv_med_annot()] for details. |
|
| 65 |
#' @param control_annot_coxph (`list`)\cr parameters to control the position and size of the annotation table added |
|
| 66 |
#' to the plot when `annot_coxph = TRUE`, specified using the [control_coxph_annot()] function. Parameter |
|
| 67 |
#' options are: `x`, `y`, `w`, `h`, `fill`, and `ref_lbls`. See [control_coxph_annot()] for details. |
|
| 68 |
#' @param legend_pos (`numeric(2)` or `NULL`)\cr vector containing x- and y-coordinates, respectively, for the legend |
|
| 69 |
#' position relative to the KM plot area. If `NULL` (default), the legend is positioned in the bottom right corner of |
|
| 70 |
#' the plot, or the middle right of the plot if needed to prevent overlapping. |
|
| 71 |
#' @param rel_height_plot (`proportion`)\cr proportion of total figure height to allocate to the Kaplan-Meier plot. |
|
| 72 |
#' Relative height of patients at risk table is then `1 - rel_height_plot`. If `annot_at_risk = FALSE` or |
|
| 73 |
#' `as_list = TRUE`, this parameter is ignored. |
|
| 74 |
#' @param ggtheme (`theme`)\cr a graphical theme as provided by `ggplot2` to format the Kaplan-Meier plot. |
|
| 75 |
#' @param as_list (`flag`)\cr whether the two `ggplot` objects should be returned as a list when `annot_at_risk = TRUE`. |
|
| 76 |
#' If `TRUE`, a named list with two elements, `plot` and `table`, will be returned. If `FALSE` (default) the patients |
|
| 77 |
#' at risk table is printed below the plot via [cowplot::plot_grid()]. |
|
| 78 |
#' @param draw `r lifecycle::badge("deprecated")` This function no longer generates `grob` objects.
|
|
| 79 |
#' @param newpage `r lifecycle::badge("deprecated")` This function no longer generates `grob` objects.
|
|
| 80 |
#' @param gp `r lifecycle::badge("deprecated")` This function no longer generates `grob` objects.
|
|
| 81 |
#' @param vp `r lifecycle::badge("deprecated")` This function no longer generates `grob` objects.
|
|
| 82 |
#' @param name `r lifecycle::badge("deprecated")` This function no longer generates `grob` objects.
|
|
| 83 |
#' @param annot_coxph_ref_lbls `r lifecycle::badge("deprecated")` Please use the `ref_lbls` element of
|
|
| 84 |
#' `control_annot_coxph` instead. |
|
| 85 |
#' @param position_coxph `r lifecycle::badge("deprecated")` Please use the `x` and `y` elements of
|
|
| 86 |
#' `control_annot_coxph` instead. |
|
| 87 |
#' @param position_surv_med `r lifecycle::badge("deprecated")` Please use the `x` and `y` elements of
|
|
| 88 |
#' `control_annot_surv_med` instead. |
|
| 89 |
#' @param width_annots `r lifecycle::badge("deprecated")` Please use the `w` element of `control_annot_surv_med`
|
|
| 90 |
#' (for `surv_med`) and `control_annot_coxph` (for `coxph`)." |
|
| 91 |
#' |
|
| 92 |
#' @return A `ggplot` Kaplan-Meier plot and (optionally) summary table. |
|
| 93 |
#' |
|
| 94 |
#' @examples |
|
| 95 |
#' library(dplyr) |
|
| 96 |
#' |
|
| 97 |
#' df <- tern_ex_adtte %>% |
|
| 98 |
#' filter(PARAMCD == "OS") %>% |
|
| 99 |
#' mutate(is_event = CNSR == 0) |
|
| 100 |
#' variables <- list(tte = "AVAL", is_event = "is_event", arm = "ARMCD") |
|
| 101 |
#' |
|
| 102 |
#' # Basic examples |
|
| 103 |
#' g_km(df = df, variables = variables) |
|
| 104 |
#' g_km(df = df, variables = variables, yval = "Failure") |
|
| 105 |
#' |
|
| 106 |
#' # Examples with customization parameters applied |
|
| 107 |
#' g_km( |
|
| 108 |
#' df = df, |
|
| 109 |
#' variables = variables, |
|
| 110 |
#' control_surv = control_surv_timepoint(conf_level = 0.9), |
|
| 111 |
#' col = c("grey25", "grey50", "grey75"),
|
|
| 112 |
#' annot_at_risk_title = FALSE, |
|
| 113 |
#' lty = 1:3, |
|
| 114 |
#' font_size = 8 |
|
| 115 |
#' ) |
|
| 116 |
#' g_km( |
|
| 117 |
#' df = df, |
|
| 118 |
#' variables = variables, |
|
| 119 |
#' annot_stats = c("min", "median"),
|
|
| 120 |
#' annot_stats_vlines = TRUE, |
|
| 121 |
#' max_time = 3000, |
|
| 122 |
#' ggtheme = ggplot2::theme_minimal() |
|
| 123 |
#' ) |
|
| 124 |
#' |
|
| 125 |
#' # Example with pairwise Cox-PH analysis annotation table, adjusted annotation tables |
|
| 126 |
#' g_km( |
|
| 127 |
#' df = df, variables = variables, |
|
| 128 |
#' annot_coxph = TRUE, |
|
| 129 |
#' control_coxph = control_coxph(pval_method = "wald", ties = "exact", conf_level = 0.99), |
|
| 130 |
#' control_annot_coxph = control_coxph_annot(x = 0.26, w = 0.35), |
|
| 131 |
#' control_annot_surv_med = control_surv_med_annot(x = 0.8, y = 0.9, w = 0.35) |
|
| 132 |
#' ) |
|
| 133 |
#' |
|
| 134 |
#' @aliases kaplan_meier |
|
| 135 |
#' @export |
|
| 136 |
g_km <- function(df, |
|
| 137 |
variables, |
|
| 138 |
control_surv = control_surv_timepoint(), |
|
| 139 |
col = NULL, |
|
| 140 |
lty = NULL, |
|
| 141 |
lwd = 0.5, |
|
| 142 |
censor_show = TRUE, |
|
| 143 |
pch = 3, |
|
| 144 |
size = 2, |
|
| 145 |
max_time = NULL, |
|
| 146 |
xticks = NULL, |
|
| 147 |
xlab = "Days", |
|
| 148 |
yval = c("Survival", "Failure"),
|
|
| 149 |
ylab = paste(yval, "Probability"), |
|
| 150 |
ylim = NULL, |
|
| 151 |
title = NULL, |
|
| 152 |
footnotes = NULL, |
|
| 153 |
font_size = 10, |
|
| 154 |
ci_ribbon = FALSE, |
|
| 155 |
annot_at_risk = TRUE, |
|
| 156 |
annot_at_risk_title = TRUE, |
|
| 157 |
annot_surv_med = TRUE, |
|
| 158 |
annot_coxph = FALSE, |
|
| 159 |
annot_stats = NULL, |
|
| 160 |
annot_stats_vlines = FALSE, |
|
| 161 |
control_coxph_pw = control_coxph(), |
|
| 162 |
ref_group_coxph = NULL, |
|
| 163 |
control_annot_surv_med = control_surv_med_annot(), |
|
| 164 |
control_annot_coxph = control_coxph_annot(), |
|
| 165 |
legend_pos = NULL, |
|
| 166 |
rel_height_plot = 0.75, |
|
| 167 |
ggtheme = NULL, |
|
| 168 |
as_list = FALSE, |
|
| 169 |
draw = lifecycle::deprecated(), |
|
| 170 |
newpage = lifecycle::deprecated(), |
|
| 171 |
gp = lifecycle::deprecated(), |
|
| 172 |
vp = lifecycle::deprecated(), |
|
| 173 |
name = lifecycle::deprecated(), |
|
| 174 |
annot_coxph_ref_lbls = lifecycle::deprecated(), |
|
| 175 |
position_coxph = lifecycle::deprecated(), |
|
| 176 |
position_surv_med = lifecycle::deprecated(), |
|
| 177 |
width_annots = lifecycle::deprecated()) {
|
|
| 178 |
# Deprecated argument warnings |
|
| 179 | 10x |
if (lifecycle::is_present(draw)) {
|
| 180 | 1x |
lifecycle::deprecate_warn( |
| 181 | 1x |
"0.9.4", "g_km(draw)", |
| 182 | 1x |
details = "This argument is no longer used since the plot is now generated as a `ggplot2` object." |
| 183 |
) |
|
| 184 |
} |
|
| 185 | 10x |
if (lifecycle::is_present(newpage)) {
|
| 186 | 1x |
lifecycle::deprecate_warn( |
| 187 | 1x |
"0.9.4", "g_km(newpage)", |
| 188 | 1x |
details = "This argument is no longer used since the plot is now generated as a `ggplot2` object." |
| 189 |
) |
|
| 190 |
} |
|
| 191 | 10x |
if (lifecycle::is_present(gp)) {
|
| 192 | 1x |
lifecycle::deprecate_warn( |
| 193 | 1x |
"0.9.4", "g_km(gp)", |
| 194 | 1x |
details = "This argument is no longer used since the plot is now generated as a `ggplot2` object." |
| 195 |
) |
|
| 196 |
} |
|
| 197 | 10x |
if (lifecycle::is_present(vp)) {
|
| 198 | 1x |
lifecycle::deprecate_warn( |
| 199 | 1x |
"0.9.4", "g_km(vp)", |
| 200 | 1x |
details = "This argument is no longer used since the plot is now generated as a `ggplot2` object." |
| 201 |
) |
|
| 202 |
} |
|
| 203 | 10x |
if (lifecycle::is_present(name)) {
|
| 204 | 1x |
lifecycle::deprecate_warn( |
| 205 | 1x |
"0.9.4", "g_km(name)", |
| 206 | 1x |
details = "This argument is no longer used since the plot is now generated as a `ggplot2` object." |
| 207 |
) |
|
| 208 |
} |
|
| 209 | 10x |
if (lifecycle::is_present(annot_coxph_ref_lbls)) {
|
| 210 | 1x |
lifecycle::deprecate_warn( |
| 211 | 1x |
"0.9.4", "g_km(annot_coxph_ref_lbls)", |
| 212 | 1x |
details = "Please specify this setting using the 'ref_lbls' element of control_annot_coxph." |
| 213 |
) |
|
| 214 | 1x |
control_annot_coxph[["ref_lbls"]] <- annot_coxph_ref_lbls |
| 215 |
} |
|
| 216 | 10x |
if (lifecycle::is_present(position_coxph)) {
|
| 217 | 1x |
lifecycle::deprecate_warn( |
| 218 | 1x |
"0.9.4", "g_km(position_coxph)", |
| 219 | 1x |
details = "Please specify this setting using the 'x' and 'y' elements of control_annot_coxph." |
| 220 |
) |
|
| 221 | 1x |
control_annot_coxph[["x"]] <- position_coxph[1] |
| 222 | 1x |
control_annot_coxph[["y"]] <- position_coxph[2] |
| 223 |
} |
|
| 224 | 10x |
if (lifecycle::is_present(position_surv_med)) {
|
| 225 | 1x |
lifecycle::deprecate_warn( |
| 226 | 1x |
"0.9.4", "g_km(position_surv_med)", |
| 227 | 1x |
details = "Please specify this setting using the 'x' and 'y' elements of control_annot_surv_med." |
| 228 |
) |
|
| 229 | 1x |
control_annot_surv_med[["x"]] <- position_surv_med[1] |
| 230 | 1x |
control_annot_surv_med[["y"]] <- position_surv_med[2] |
| 231 |
} |
|
| 232 | 10x |
if (lifecycle::is_present(width_annots)) {
|
| 233 | 1x |
lifecycle::deprecate_warn( |
| 234 | 1x |
"0.9.4", "g_km(width_annots)", |
| 235 | 1x |
details = paste( |
| 236 | 1x |
"Please specify widths of annotation tables relative to the plot area using the 'w' element of", |
| 237 | 1x |
"control_annot_surv_med (for surv_med) and control_annot_coxph (for coxph)." |
| 238 |
) |
|
| 239 |
) |
|
| 240 | 1x |
control_annot_surv_med[["w"]] <- as.numeric(width_annots[["surv_med"]]) |
| 241 | 1x |
control_annot_coxph[["w"]] <- as.numeric(width_annots[["coxph"]]) |
| 242 |
} |
|
| 243 | ||
| 244 | 10x |
checkmate::assert_list(variables) |
| 245 | 10x |
checkmate::assert_subset(c("tte", "arm", "is_event"), names(variables))
|
| 246 | 10x |
checkmate::assert_logical(censor_show, len = 1) |
| 247 | 10x |
checkmate::assert_numeric(size, len = 1) |
| 248 | 10x |
checkmate::assert_numeric(max_time, len = 1, null.ok = TRUE) |
| 249 | 10x |
checkmate::assert_numeric(xticks, null.ok = TRUE) |
| 250 | 10x |
checkmate::assert_character(xlab, len = 1, null.ok = TRUE) |
| 251 | 10x |
checkmate::assert_character(yval) |
| 252 | 10x |
checkmate::assert_character(ylab, null.ok = TRUE) |
| 253 | 10x |
checkmate::assert_numeric(ylim, finite = TRUE, any.missing = FALSE, len = 2, sorted = TRUE, null.ok = TRUE) |
| 254 | 10x |
checkmate::assert_character(title, len = 1, null.ok = TRUE) |
| 255 | 10x |
checkmate::assert_character(footnotes, len = 1, null.ok = TRUE) |
| 256 | 10x |
checkmate::assert_numeric(font_size, len = 1) |
| 257 | 10x |
checkmate::assert_logical(ci_ribbon, len = 1) |
| 258 | 10x |
checkmate::assert_logical(annot_at_risk, len = 1) |
| 259 | 10x |
checkmate::assert_logical(annot_at_risk_title, len = 1) |
| 260 | 10x |
checkmate::assert_logical(annot_surv_med, len = 1) |
| 261 | 10x |
checkmate::assert_logical(annot_coxph, len = 1) |
| 262 | 10x |
checkmate::assert_subset(annot_stats, c("median", "min"))
|
| 263 | 10x |
checkmate::assert_logical(annot_stats_vlines) |
| 264 | 10x |
checkmate::assert_list(control_coxph_pw) |
| 265 | 10x |
checkmate::assert_character(ref_group_coxph, len = 1, null.ok = TRUE) |
| 266 | 10x |
checkmate::assert_list(control_annot_surv_med) |
| 267 | 10x |
checkmate::assert_list(control_annot_coxph) |
| 268 | 10x |
checkmate::assert_numeric(legend_pos, finite = TRUE, any.missing = FALSE, len = 2, null.ok = TRUE) |
| 269 | 10x |
assert_proportion_value(rel_height_plot) |
| 270 | 10x |
checkmate::assert_logical(as_list) |
| 271 | ||
| 272 | 10x |
tte <- variables$tte |
| 273 | 10x |
is_event <- variables$is_event |
| 274 | 10x |
arm <- variables$arm |
| 275 | 10x |
assert_valid_factor(df[[arm]]) |
| 276 | 10x |
armval <- as.character(unique(df[[arm]])) |
| 277 | 10x |
assert_df_with_variables(df, list(tte = tte, is_event = is_event, arm = arm)) |
| 278 | 10x |
checkmate::assert_logical(df[[is_event]], min.len = 1) |
| 279 | 10x |
checkmate::assert_numeric(df[[tte]], min.len = 1) |
| 280 | 10x |
checkmate::assert_vector(col, len = length(armval), null.ok = TRUE) |
| 281 | 10x |
checkmate::assert_vector(lty, null.ok = TRUE) |
| 282 | 10x |
checkmate::assert_numeric(lwd, len = 1, null.ok = TRUE) |
| 283 | ||
| 284 | 10x |
if (annot_coxph && length(armval) < 2) {
|
| 285 | ! |
stop(paste( |
| 286 | ! |
"When `annot_coxph` = TRUE, `df` must contain at least 2 levels of `variables$arm`", |
| 287 | ! |
"in order to calculate the hazard ratio." |
| 288 |
)) |
|
| 289 |
} |
|
| 290 | ||
| 291 |
# process model |
|
| 292 | 10x |
yval <- match.arg(yval) |
| 293 | 10x |
formula <- stats::as.formula(paste0("survival::Surv(", tte, ", ", is_event, ") ~ ", arm))
|
| 294 | 10x |
fit_km <- survival::survfit( |
| 295 | 10x |
formula = formula, |
| 296 | 10x |
data = df, |
| 297 | 10x |
conf.int = control_surv$conf_level, |
| 298 | 10x |
conf.type = control_surv$conf_type |
| 299 |
) |
|
| 300 | 10x |
data <- h_data_plot(fit_km, armval = armval, max_time = max_time) |
| 301 | ||
| 302 |
# calculate x-ticks |
|
| 303 | 10x |
xticks <- h_xticks(data = data, xticks = xticks, max_time = max_time) |
| 304 | ||
| 305 |
# change estimates of survival to estimates of failure (1 - survival) |
|
| 306 | 10x |
if (yval == "Failure") {
|
| 307 | ! |
data[c("estimate", "conf.low", "conf.high", "censor")] <- list(
|
| 308 | ! |
1 - data$estimate, 1 - data$conf.low, 1 - data$conf.high, 1 - data$censor |
| 309 |
) |
|
| 310 |
} |
|
| 311 | ||
| 312 |
# derive y-axis limits |
|
| 313 | 10x |
if (is.null(ylim)) {
|
| 314 | 10x |
if (!is.null(max_time)) {
|
| 315 | 1x |
y_lwr <- min(data[data$time < max_time, ][["estimate"]]) |
| 316 | 1x |
y_upr <- max(data[data$time < max_time, ][["estimate"]]) |
| 317 |
} else {
|
|
| 318 | 9x |
y_lwr <- min(data[["estimate"]]) |
| 319 | 9x |
y_upr <- max(data[["estimate"]]) |
| 320 |
} |
|
| 321 | 10x |
ylim <- c(y_lwr, y_upr) |
| 322 |
} |
|
| 323 | ||
| 324 |
# initialize ggplot |
|
| 325 | 10x |
gg_plt <- ggplot( |
| 326 | 10x |
data = data, |
| 327 | 10x |
mapping = aes( |
| 328 | 10x |
x = .data[["time"]], |
| 329 | 10x |
y = .data[["estimate"]], |
| 330 | 10x |
ymin = .data[["conf.low"]], |
| 331 | 10x |
ymax = .data[["conf.high"]], |
| 332 | 10x |
color = .data[["strata"]], |
| 333 | 10x |
fill = .data[["strata"]] |
| 334 |
) |
|
| 335 |
) + |
|
| 336 | 10x |
theme_bw(base_size = font_size) + |
| 337 | 10x |
scale_y_continuous(limits = ylim, expand = c(0.025, 0)) + |
| 338 | 10x |
labs(title = title, x = xlab, y = ylab, caption = footnotes) + |
| 339 | 10x |
theme( |
| 340 | 10x |
axis.text = element_text(size = font_size), |
| 341 | 10x |
axis.title = element_text(size = font_size), |
| 342 | 10x |
legend.title = element_blank(), |
| 343 | 10x |
legend.text = element_text(size = font_size), |
| 344 | 10x |
legend.box.background = element_rect(fill = "white", linewidth = 0.5), |
| 345 | 10x |
legend.background = element_blank(), |
| 346 | 10x |
legend.position = "inside", |
| 347 | 10x |
legend.spacing.y = unit(-0.02, "npc"), |
| 348 | 10x |
panel.grid.major = element_blank(), |
| 349 | 10x |
panel.grid.minor = element_blank() |
| 350 |
) |
|
| 351 | ||
| 352 |
# derive x-axis limits |
|
| 353 | 10x |
if (!is.null(max_time) && !is.null(xticks)) {
|
| 354 | 1x |
gg_plt <- gg_plt + scale_x_continuous( |
| 355 | 1x |
breaks = xticks, limits = c(min(0, xticks), max(c(xticks, max_time))), expand = c(0.025, 0) |
| 356 |
) |
|
| 357 | 9x |
} else if (!is.null(xticks)) {
|
| 358 | 9x |
if (max(data$time) <= max(xticks)) {
|
| 359 | 9x |
gg_plt <- gg_plt + scale_x_continuous( |
| 360 | 9x |
breaks = xticks, limits = c(min(0, min(xticks)), max(xticks)), expand = c(0.025, 0) |
| 361 |
) |
|
| 362 |
} else {
|
|
| 363 | ! |
gg_plt <- gg_plt + scale_x_continuous(breaks = xticks, expand = c(0.025, 0)) |
| 364 |
} |
|
| 365 | ! |
} else if (!is.null(max_time)) {
|
| 366 | ! |
gg_plt <- gg_plt + scale_x_continuous(limits = c(0, max_time), expand = c(0.025, 0)) |
| 367 |
} |
|
| 368 | ||
| 369 |
# set legend position |
|
| 370 | 10x |
if (!is.null(legend_pos)) {
|
| 371 | 2x |
gg_plt <- gg_plt + theme(legend.position.inside = legend_pos) |
| 372 |
} else {
|
|
| 373 | 8x |
max_time2 <- sort( |
| 374 | 8x |
data$time, |
| 375 | 8x |
partial = nrow(data) - length(armval) - 1 |
| 376 | 8x |
)[nrow(data) - length(armval) - 1] |
| 377 | ||
| 378 | 8x |
y_rng <- ylim[2] - ylim[1] |
| 379 | ||
| 380 | 8x |
if (yval == "Survival" && all(data$estimate[data$time == max_time2] > ylim[1] + 0.09 * y_rng) && |
| 381 | 8x |
all(data$estimate[data$time == max_time2] < ylim[1] + 0.5 * y_rng)) { # nolint
|
| 382 | 1x |
gg_plt <- gg_plt + |
| 383 | 1x |
theme( |
| 384 | 1x |
legend.position.inside = c(1, 0.5), |
| 385 | 1x |
legend.justification = c(1.1, 0.6) |
| 386 |
) |
|
| 387 |
} else {
|
|
| 388 | 7x |
gg_plt <- gg_plt + |
| 389 | 7x |
theme( |
| 390 | 7x |
legend.position.inside = c(1, 0), |
| 391 | 7x |
legend.justification = c(1.1, -0.4) |
| 392 |
) |
|
| 393 |
} |
|
| 394 |
} |
|
| 395 | ||
| 396 |
# add lines |
|
| 397 | 10x |
gg_plt <- if (is.null(lty)) {
|
| 398 | 9x |
gg_plt + geom_step(linewidth = lwd, na.rm = TRUE) |
| 399 | 10x |
} else if (length(lty) == 1) {
|
| 400 | ! |
gg_plt + geom_step(linewidth = lwd, lty = lty, na.rm = TRUE) |
| 401 |
} else {
|
|
| 402 | 1x |
gg_plt + |
| 403 | 1x |
geom_step(aes(lty = .data[["strata"]]), linewidth = lwd, na.rm = TRUE) + |
| 404 | 1x |
scale_linetype_manual(values = lty) |
| 405 |
} |
|
| 406 | ||
| 407 |
# add censor marks |
|
| 408 | 10x |
if (censor_show) {
|
| 409 | 10x |
gg_plt <- gg_plt + geom_point( |
| 410 | 10x |
data = data[data$n.censor != 0, ], |
| 411 | 10x |
aes(x = .data[["time"]], y = .data[["censor"]], shape = "Censored"), |
| 412 | 10x |
size = size, |
| 413 | 10x |
na.rm = TRUE |
| 414 |
) + |
|
| 415 | 10x |
scale_shape_manual(name = NULL, values = pch) + |
| 416 | 10x |
guides(fill = guide_legend(override.aes = list(shape = NA))) |
| 417 |
} |
|
| 418 | ||
| 419 |
# add ci ribbon |
|
| 420 | 1x |
if (ci_ribbon) gg_plt <- gg_plt + geom_ribbon(alpha = 0.3, lty = 0, na.rm = TRUE) |
| 421 | ||
| 422 |
# control aesthetics |
|
| 423 | 10x |
if (!is.null(col)) {
|
| 424 | 1x |
gg_plt <- gg_plt + |
| 425 | 1x |
scale_color_manual(values = col) + |
| 426 | 1x |
scale_fill_manual(values = col) |
| 427 |
} |
|
| 428 | ! |
if (!is.null(ggtheme)) gg_plt <- gg_plt + ggtheme |
| 429 | ||
| 430 |
# annotate with stats (text/vlines) |
|
| 431 | 10x |
if (!is.null(annot_stats)) {
|
| 432 | ! |
if ("median" %in% annot_stats) {
|
| 433 | ! |
fit_km_all <- survival::survfit( |
| 434 | ! |
formula = stats::as.formula(paste0("survival::Surv(", tte, ", ", is_event, ") ~ ", 1)),
|
| 435 | ! |
data = df, |
| 436 | ! |
conf.int = control_surv$conf_level, |
| 437 | ! |
conf.type = control_surv$conf_type |
| 438 |
) |
|
| 439 | ! |
gg_plt <- gg_plt + |
| 440 | ! |
annotate( |
| 441 | ! |
"text", |
| 442 | ! |
size = font_size / .pt, col = 1, lineheight = 0.95, |
| 443 | ! |
x = stats::median(fit_km_all) + 0.07 * max(data$time), |
| 444 | ! |
y = ifelse(yval == "Survival", 0.65, 0.35), |
| 445 | ! |
label = paste("Median F/U:\n", round(stats::median(fit_km_all), 1), tolower(df$AVALU[1]))
|
| 446 |
) |
|
| 447 | ! |
if (annot_stats_vlines) {
|
| 448 | ! |
gg_plt <- gg_plt + |
| 449 | ! |
annotate( |
| 450 | ! |
"segment", |
| 451 | ! |
x = stats::median(fit_km_all), xend = stats::median(fit_km_all), y = -Inf, yend = Inf, |
| 452 | ! |
linetype = 2, col = "darkgray" |
| 453 |
) |
|
| 454 |
} |
|
| 455 |
} |
|
| 456 | ! |
if ("min" %in% annot_stats) {
|
| 457 | ! |
min_fu <- min(df[[tte]]) |
| 458 | ! |
gg_plt <- gg_plt + |
| 459 | ! |
annotate( |
| 460 | ! |
"text", |
| 461 | ! |
size = font_size / .pt, col = 1, lineheight = 0.95, |
| 462 | ! |
x = min_fu + max(data$time) * 0.07, |
| 463 | ! |
y = ifelse(yval == "Survival", 0.96, 0.05), |
| 464 | ! |
label = paste("Min. F/U:\n", round(min_fu, 1), tolower(df$AVALU[1]))
|
| 465 |
) |
|
| 466 | ! |
if (annot_stats_vlines) {
|
| 467 | ! |
gg_plt <- gg_plt + |
| 468 | ! |
annotate( |
| 469 | ! |
"segment", |
| 470 | ! |
linetype = 2, col = "darkgray", |
| 471 | ! |
x = min_fu, xend = min_fu, y = Inf, yend = -Inf |
| 472 |
) |
|
| 473 |
} |
|
| 474 |
} |
|
| 475 | ! |
gg_plt <- gg_plt + guides(fill = guide_legend(override.aes = list(shape = NA, label = ""))) |
| 476 |
} |
|
| 477 | ||
| 478 |
# add at risk annotation table |
|
| 479 | 10x |
if (annot_at_risk) {
|
| 480 | 9x |
annot_tbl <- summary(fit_km, times = xticks, extend = TRUE) |
| 481 | 9x |
annot_tbl <- if (is.null(fit_km$strata)) {
|
| 482 | ! |
data.frame( |
| 483 | ! |
n.risk = annot_tbl$n.risk, |
| 484 | ! |
time = annot_tbl$time, |
| 485 | ! |
strata = armval |
| 486 |
) |
|
| 487 |
} else {
|
|
| 488 | 9x |
strata_lst <- strsplit(sub("=", "equals", levels(annot_tbl$strata)), "equals")
|
| 489 | 9x |
levels(annot_tbl$strata) <- matrix(unlist(strata_lst), ncol = 2, byrow = TRUE)[, 2] |
| 490 | 9x |
data.frame( |
| 491 | 9x |
n.risk = annot_tbl$n.risk, |
| 492 | 9x |
time = annot_tbl$time, |
| 493 | 9x |
strata = annot_tbl$strata |
| 494 |
) |
|
| 495 |
} |
|
| 496 | ||
| 497 | 9x |
at_risk_tbl <- as.data.frame(tidyr::pivot_wider(annot_tbl, names_from = "time", values_from = "n.risk")[, -1]) |
| 498 | 9x |
at_risk_tbl[is.na(at_risk_tbl)] <- 0 |
| 499 | 9x |
rownames(at_risk_tbl) <- levels(annot_tbl$strata) |
| 500 | ||
| 501 | 9x |
gg_at_risk <- df2gg( |
| 502 | 9x |
at_risk_tbl, |
| 503 | 9x |
font_size = font_size, col_labels = FALSE, hline = FALSE, |
| 504 | 9x |
colwidths = rep(1, ncol(at_risk_tbl)) |
| 505 |
) + |
|
| 506 | 9x |
labs(title = if (annot_at_risk_title) "Patients at Risk:" else NULL, x = xlab) + |
| 507 | 9x |
theme_bw(base_size = font_size) + |
| 508 | 9x |
theme( |
| 509 | 9x |
plot.title = element_text(size = font_size, vjust = 3, face = "bold"), |
| 510 | 9x |
panel.border = element_blank(), |
| 511 | 9x |
panel.grid = element_blank(), |
| 512 | 9x |
axis.title.y = element_blank(), |
| 513 | 9x |
axis.ticks.y = element_blank(), |
| 514 | 9x |
axis.text.y = element_text(size = font_size, face = "italic", hjust = 1), |
| 515 | 9x |
axis.text.x = element_text(size = font_size), |
| 516 | 9x |
axis.line.x = element_line() |
| 517 |
) + |
|
| 518 | 9x |
coord_cartesian(clip = "off", ylim = c(0.5, nrow(at_risk_tbl))) |
| 519 | 9x |
gg_at_risk <- suppressMessages( |
| 520 | 9x |
gg_at_risk + |
| 521 | 9x |
scale_x_continuous(expand = c(0.025, 0), breaks = seq_along(at_risk_tbl) - 0.5, labels = xticks) + |
| 522 | 9x |
scale_y_continuous(labels = rev(levels(annot_tbl$strata)), breaks = seq_len(nrow(at_risk_tbl))) |
| 523 |
) |
|
| 524 | ||
| 525 | 9x |
if (!as_list) {
|
| 526 | 8x |
gg_plt <- cowplot::plot_grid( |
| 527 | 8x |
gg_plt, |
| 528 | 8x |
gg_at_risk, |
| 529 | 8x |
align = "v", |
| 530 | 8x |
axis = "tblr", |
| 531 | 8x |
ncol = 1, |
| 532 | 8x |
rel_heights = c(rel_height_plot, 1 - rel_height_plot) |
| 533 |
) |
|
| 534 |
} |
|
| 535 |
} |
|
| 536 | ||
| 537 |
# add median survival time annotation table |
|
| 538 | 10x |
if (annot_surv_med) {
|
| 539 | 8x |
surv_med_tbl <- h_tbl_median_surv(fit_km = fit_km, armval = armval) |
| 540 | 8x |
bg_fill <- if (isTRUE(control_annot_surv_med[["fill"]])) "#00000020" else control_annot_surv_med[["fill"]] |
| 541 | ||
| 542 | 8x |
gg_surv_med <- df2gg(surv_med_tbl, font_size = font_size, colwidths = c(1, 1, 2), bg_fill = bg_fill) + |
| 543 | 8x |
theme( |
| 544 | 8x |
axis.text.y = element_text(size = font_size, face = "italic", hjust = 1), |
| 545 | 8x |
plot.margin = margin(0, 2, 0, 5) |
| 546 |
) + |
|
| 547 | 8x |
coord_cartesian(clip = "off", ylim = c(0.5, nrow(surv_med_tbl) + 1.5)) |
| 548 | 8x |
gg_surv_med <- suppressMessages( |
| 549 | 8x |
gg_surv_med + |
| 550 | 8x |
scale_x_continuous(expand = c(0.025, 0)) + |
| 551 | 8x |
scale_y_continuous(labels = rev(rownames(surv_med_tbl)), breaks = seq_len(nrow(surv_med_tbl))) |
| 552 |
) |
|
| 553 | ||
| 554 | 8x |
gg_plt <- cowplot::ggdraw(gg_plt) + |
| 555 | 8x |
cowplot::draw_plot( |
| 556 | 8x |
gg_surv_med, |
| 557 | 8x |
control_annot_surv_med[["x"]], |
| 558 | 8x |
control_annot_surv_med[["y"]], |
| 559 | 8x |
width = control_annot_surv_med[["w"]], |
| 560 | 8x |
height = control_annot_surv_med[["h"]], |
| 561 | 8x |
vjust = 0.5, |
| 562 | 8x |
hjust = 0.5 |
| 563 |
) |
|
| 564 |
} |
|
| 565 | ||
| 566 |
# add coxph annotation table |
|
| 567 | 10x |
if (annot_coxph) {
|
| 568 | 1x |
coxph_tbl <- h_tbl_coxph_pairwise( |
| 569 | 1x |
df = df, |
| 570 | 1x |
variables = variables, |
| 571 | 1x |
ref_group_coxph = ref_group_coxph, |
| 572 | 1x |
control_coxph_pw = control_coxph_pw, |
| 573 | 1x |
annot_coxph_ref_lbls = control_annot_coxph[["ref_lbls"]] |
| 574 |
) |
|
| 575 | 1x |
bg_fill <- if (isTRUE(control_annot_coxph[["fill"]])) "#00000020" else control_annot_coxph[["fill"]] |
| 576 | ||
| 577 | 1x |
gg_coxph <- df2gg(coxph_tbl, font_size = font_size, colwidths = c(1.1, 1, 3), bg_fill = bg_fill) + |
| 578 | 1x |
theme( |
| 579 | 1x |
axis.text.y = element_text(size = font_size, face = "italic", hjust = 1), |
| 580 | 1x |
plot.margin = margin(0, 2, 0, 5) |
| 581 |
) + |
|
| 582 | 1x |
coord_cartesian(clip = "off", ylim = c(0.5, nrow(coxph_tbl) + 1.5)) |
| 583 | 1x |
gg_coxph <- suppressMessages( |
| 584 | 1x |
gg_coxph + |
| 585 | 1x |
scale_x_continuous(expand = c(0.025, 0)) + |
| 586 | 1x |
scale_y_continuous(labels = rev(rownames(coxph_tbl)), breaks = seq_len(nrow(coxph_tbl))) |
| 587 |
) |
|
| 588 | ||
| 589 | 1x |
gg_plt <- cowplot::ggdraw(gg_plt) + |
| 590 | 1x |
cowplot::draw_plot( |
| 591 | 1x |
gg_coxph, |
| 592 | 1x |
control_annot_coxph[["x"]], |
| 593 | 1x |
control_annot_coxph[["y"]], |
| 594 | 1x |
width = control_annot_coxph[["w"]], |
| 595 | 1x |
height = control_annot_coxph[["h"]], |
| 596 | 1x |
vjust = 0.5, |
| 597 | 1x |
hjust = 0.5 |
| 598 |
) |
|
| 599 |
} |
|
| 600 | ||
| 601 | 10x |
if (as_list) {
|
| 602 | 1x |
list(plot = gg_plt, table = gg_at_risk) |
| 603 |
} else {
|
|
| 604 | 9x |
gg_plt |
| 605 |
} |
|
| 606 |
} |
| 1 |
#' Analyze a pairwise Cox-PH model |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The analyze function [coxph_pairwise()] creates a layout element to analyze a pairwise Cox-PH model. |
|
| 6 |
#' |
|
| 7 |
#' This function can return statistics including p-value, hazard ratio (HR), and HR confidence intervals from both |
|
| 8 |
#' stratified and unstratified Cox-PH models. The variable(s) to be analyzed is specified via the `vars` argument and |
|
| 9 |
#' any stratification factors via the `strata` argument. |
|
| 10 |
#' |
|
| 11 |
#' @inheritParams argument_convention |
|
| 12 |
#' @inheritParams s_surv_time |
|
| 13 |
#' @param strata (`character` or `NULL`)\cr variable names indicating stratification factors. |
|
| 14 |
#' @param strat `r lifecycle::badge("deprecated")` Please use the `strata` argument instead.
|
|
| 15 |
#' @param control (`list`)\cr parameters for comparison details, specified by using the helper function |
|
| 16 |
#' [control_coxph()]. Some possible parameter options are: |
|
| 17 |
#' * `pval_method` (`string`)\cr p-value method for testing the null hypothesis that hazard ratio = 1. Default |
|
| 18 |
#' method is `"log-rank"` which comes from [survival::survdiff()], can also be set to `"wald"` or `"likelihood"` |
|
| 19 |
#' (from [survival::coxph()]). |
|
| 20 |
#' * `ties` (`string`)\cr specifying the method for tie handling. Default is `"efron"`, |
|
| 21 |
#' can also be set to `"breslow"` or `"exact"`. See more in [survival::coxph()]. |
|
| 22 |
#' * `conf_level` (`proportion`)\cr confidence level of the interval for HR. |
|
| 23 |
#' @param .stats (`character`)\cr statistics to select for the table. |
|
| 24 |
#' |
|
| 25 |
#' Options are: ``r shQuote(get_stats("coxph_pairwise"), type = "sh")``
|
|
| 26 |
#' |
|
| 27 |
#' @name survival_coxph_pairwise |
|
| 28 |
#' @order 1 |
|
| 29 |
NULL |
|
| 30 | ||
| 31 |
#' @describeIn survival_coxph_pairwise Statistics function which analyzes HR, CIs of HR, and p-value of a Cox-PH model. |
|
| 32 |
#' |
|
| 33 |
#' @return |
|
| 34 |
#' * `s_coxph_pairwise()` returns the statistics: |
|
| 35 |
#' * `pvalue`: p-value to test the null hypothesis that hazard ratio = 1. |
|
| 36 |
#' * `hr`: Hazard ratio. |
|
| 37 |
#' * `hr_ci`: Confidence interval for hazard ratio. |
|
| 38 |
#' * `n_tot`: Total number of observations. |
|
| 39 |
#' * `n_tot_events`: Total number of events. |
|
| 40 |
#' |
|
| 41 |
#' @keywords internal |
|
| 42 |
s_coxph_pairwise <- function(df, |
|
| 43 |
.ref_group, |
|
| 44 |
.in_ref_col, |
|
| 45 |
.var, |
|
| 46 |
is_event, |
|
| 47 |
strata = NULL, |
|
| 48 |
strat = lifecycle::deprecated(), |
|
| 49 |
control = control_coxph(), |
|
| 50 |
...) {
|
|
| 51 | 110x |
if (lifecycle::is_present(strat)) {
|
| 52 | ! |
lifecycle::deprecate_warn("0.9.4", "s_coxph_pairwise(strat)", "s_coxph_pairwise(strata)")
|
| 53 | ! |
strata <- strat |
| 54 |
} |
|
| 55 | ||
| 56 | 110x |
checkmate::assert_string(.var) |
| 57 | 110x |
checkmate::assert_numeric(df[[.var]]) |
| 58 | 110x |
checkmate::assert_logical(df[[is_event]]) |
| 59 | 110x |
assert_df_with_variables(df, list(tte = .var, is_event = is_event)) |
| 60 | 110x |
pval_method <- control$pval_method |
| 61 | 110x |
ties <- control$ties |
| 62 | 110x |
conf_level <- control$conf_level |
| 63 | ||
| 64 | 110x |
if (.in_ref_col) {
|
| 65 | 6x |
return( |
| 66 | 6x |
list( |
| 67 | 6x |
pvalue = formatters::with_label(numeric(), paste0("p-value (", pval_method, ")")),
|
| 68 | 6x |
hr = formatters::with_label(numeric(), "Hazard Ratio"), |
| 69 | 6x |
hr_ci = formatters::with_label(numeric(), f_conf_level(conf_level)), |
| 70 | 6x |
hr_ci_3d = formatters::with_label(numeric(), paste0("Hazard Ratio (", f_conf_level(conf_level), ")")),
|
| 71 | 6x |
n_tot = formatters::with_label(numeric(), "Total n"), |
| 72 | 6x |
n_tot_events = formatters::with_label(numeric(), "Total events") |
| 73 |
) |
|
| 74 |
) |
|
| 75 |
} |
|
| 76 | 104x |
data <- rbind(.ref_group, df) |
| 77 | 104x |
group <- factor(rep(c("ref", "x"), c(nrow(.ref_group), nrow(df))), levels = c("ref", "x"))
|
| 78 | ||
| 79 | 104x |
df_cox <- data.frame( |
| 80 | 104x |
tte = data[[.var]], |
| 81 | 104x |
is_event = data[[is_event]], |
| 82 | 104x |
arm = group |
| 83 |
) |
|
| 84 | 104x |
if (is.null(strata)) {
|
| 85 | 91x |
formula_cox <- survival::Surv(tte, is_event) ~ arm |
| 86 |
} else {
|
|
| 87 | 13x |
formula_cox <- stats::as.formula( |
| 88 | 13x |
paste0( |
| 89 | 13x |
"survival::Surv(tte, is_event) ~ arm + strata(",
|
| 90 | 13x |
paste(strata, collapse = ","), |
| 91 |
")" |
|
| 92 |
) |
|
| 93 |
) |
|
| 94 | 13x |
df_cox <- cbind(df_cox, data[strata]) |
| 95 |
} |
|
| 96 | 104x |
cox_fit <- survival::coxph( |
| 97 | 104x |
formula = formula_cox, |
| 98 | 104x |
data = df_cox, |
| 99 | 104x |
ties = ties |
| 100 |
) |
|
| 101 | 104x |
sum_cox <- summary(cox_fit, conf.int = conf_level, extend = TRUE) |
| 102 | 104x |
orginal_survdiff <- survival::survdiff( |
| 103 | 104x |
formula_cox, |
| 104 | 104x |
data = df_cox |
| 105 |
) |
|
| 106 | 104x |
log_rank_pvalue <- 1 - pchisq(orginal_survdiff$chisq, length(orginal_survdiff$n) - 1) |
| 107 | ||
| 108 | 104x |
pval <- switch(pval_method, |
| 109 | 104x |
"wald" = sum_cox$waldtest["pvalue"], |
| 110 | 104x |
"log-rank" = log_rank_pvalue, # pvalue from original log-rank test survival::survdiff() |
| 111 | 104x |
"likelihood" = sum_cox$logtest["pvalue"] |
| 112 |
) |
|
| 113 | 104x |
list( |
| 114 | 104x |
pvalue = formatters::with_label(unname(pval), paste0("p-value (", pval_method, ")")),
|
| 115 | 104x |
hr = formatters::with_label(sum_cox$conf.int[1, 1], "Hazard Ratio"), |
| 116 | 104x |
hr_ci = formatters::with_label(unname(sum_cox$conf.int[1, 3:4]), f_conf_level(conf_level)), |
| 117 | 104x |
hr_ci_3d = formatters::with_label( |
| 118 | 104x |
c(sum_cox$conf.int[1, 1], unname(sum_cox$conf.int[1, 3:4])), |
| 119 | 104x |
paste0("Hazard Ratio (", f_conf_level(conf_level), ")")
|
| 120 |
), |
|
| 121 | 104x |
n_tot = formatters::with_label(sum_cox$n, "Total n"), |
| 122 | 104x |
n_tot_events = formatters::with_label(sum_cox$nevent, "Total events") |
| 123 |
) |
|
| 124 |
} |
|
| 125 | ||
| 126 |
#' @describeIn survival_coxph_pairwise Formatted analysis function which is used as `afun` in `coxph_pairwise()`. |
|
| 127 |
#' |
|
| 128 |
#' @return |
|
| 129 |
#' * `a_coxph_pairwise()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 130 |
#' |
|
| 131 |
#' @keywords internal |
|
| 132 |
a_coxph_pairwise <- function(df, |
|
| 133 |
..., |
|
| 134 |
.stats = NULL, |
|
| 135 |
.stat_names = NULL, |
|
| 136 |
.formats = NULL, |
|
| 137 |
.labels = NULL, |
|
| 138 |
.indent_mods = NULL) {
|
|
| 139 |
# Check for additional parameters to the statistics function |
|
| 140 | 18x |
dots_extra_args <- list(...) |
| 141 | 18x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 142 | 18x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 143 | ||
| 144 |
# Check for user-defined functions |
|
| 145 | 18x |
default_and_custom_stats_list <- .split_std_from_custom_stats(.stats) |
| 146 | 18x |
.stats <- default_and_custom_stats_list$all_stats |
| 147 | 18x |
custom_stat_functions <- default_and_custom_stats_list$custom_stats |
| 148 | ||
| 149 |
# Apply statistics function |
|
| 150 | 18x |
x_stats <- .apply_stat_functions( |
| 151 | 18x |
default_stat_fnc = s_coxph_pairwise, |
| 152 | 18x |
custom_stat_fnc_list = custom_stat_functions, |
| 153 | 18x |
args_list = c( |
| 154 | 18x |
df = list(df), |
| 155 | 18x |
extra_afun_params, |
| 156 | 18x |
dots_extra_args |
| 157 |
) |
|
| 158 |
) |
|
| 159 | ||
| 160 |
# Fill in formatting defaults |
|
| 161 | 18x |
.stats <- get_stats("coxph_pairwise",
|
| 162 | 18x |
stats_in = .stats, |
| 163 | 18x |
custom_stats_in = names(custom_stat_functions) |
| 164 |
) |
|
| 165 | 18x |
x_stats <- x_stats[.stats] |
| 166 | 18x |
.formats <- get_formats_from_stats(.stats, .formats) |
| 167 | 18x |
.labels <- get_labels_from_stats( |
| 168 | 18x |
.stats, .labels, |
| 169 | 18x |
tern_defaults = c(lapply(x_stats, attr, "label"), tern_default_labels) |
| 170 |
) |
|
| 171 | 18x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) |
| 172 | ||
| 173 |
# Auto format handling |
|
| 174 | 18x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 175 | ||
| 176 |
# Get and check statistical names |
|
| 177 | 18x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 178 | ||
| 179 | 18x |
in_rows( |
| 180 | 18x |
.list = x_stats, |
| 181 | 18x |
.formats = .formats, |
| 182 | 18x |
.names = .labels %>% .unlist_keep_nulls(), |
| 183 | 18x |
.stat_names = .stat_names, |
| 184 | 18x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 185 | 18x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 186 |
) |
|
| 187 |
} |
|
| 188 | ||
| 189 |
#' @describeIn survival_coxph_pairwise Layout-creating function which can take statistics function arguments |
|
| 190 |
#' and additional format arguments. This function is a wrapper for [rtables::analyze()]. |
|
| 191 |
#' |
|
| 192 |
#' @return |
|
| 193 |
#' * `coxph_pairwise()` returns a layout object suitable for passing to further layouting functions, |
|
| 194 |
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing |
|
| 195 |
#' the statistics from `s_coxph_pairwise()` to the table layout. |
|
| 196 |
#' |
|
| 197 |
#' @examples |
|
| 198 |
#' library(dplyr) |
|
| 199 |
#' |
|
| 200 |
#' adtte_f <- tern_ex_adtte %>% |
|
| 201 |
#' filter(PARAMCD == "OS") %>% |
|
| 202 |
#' mutate(is_event = CNSR == 0) |
|
| 203 |
#' |
|
| 204 |
#' df <- adtte_f %>% filter(ARMCD == "ARM A") |
|
| 205 |
#' df_ref_group <- adtte_f %>% filter(ARMCD == "ARM B") |
|
| 206 |
#' |
|
| 207 |
#' basic_table() %>% |
|
| 208 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>% |
|
| 209 |
#' add_colcounts() %>% |
|
| 210 |
#' coxph_pairwise( |
|
| 211 |
#' vars = "AVAL", |
|
| 212 |
#' is_event = "is_event", |
|
| 213 |
#' var_labels = "Unstratified Analysis" |
|
| 214 |
#' ) %>% |
|
| 215 |
#' build_table(df = adtte_f) |
|
| 216 |
#' |
|
| 217 |
#' basic_table() %>% |
|
| 218 |
#' split_cols_by(var = "ARMCD", ref_group = "ARM A") %>% |
|
| 219 |
#' add_colcounts() %>% |
|
| 220 |
#' coxph_pairwise( |
|
| 221 |
#' vars = "AVAL", |
|
| 222 |
#' is_event = "is_event", |
|
| 223 |
#' var_labels = "Stratified Analysis", |
|
| 224 |
#' strata = "SEX", |
|
| 225 |
#' control = control_coxph(pval_method = "wald") |
|
| 226 |
#' ) %>% |
|
| 227 |
#' build_table(df = adtte_f) |
|
| 228 |
#' |
|
| 229 |
#' @export |
|
| 230 |
#' @order 2 |
|
| 231 |
coxph_pairwise <- function(lyt, |
|
| 232 |
vars, |
|
| 233 |
strata = NULL, |
|
| 234 |
control = control_coxph(), |
|
| 235 |
na_str = default_na_str(), |
|
| 236 |
nested = TRUE, |
|
| 237 |
..., |
|
| 238 |
var_labels = "CoxPH", |
|
| 239 |
show_labels = "visible", |
|
| 240 |
table_names = vars, |
|
| 241 |
.stats = c("pvalue", "hr", "hr_ci"),
|
|
| 242 |
.stat_names = NULL, |
|
| 243 |
.formats = NULL, |
|
| 244 |
.labels = NULL, |
|
| 245 |
.indent_mods = NULL) {
|
|
| 246 |
# Process standard extra arguments |
|
| 247 | 6x |
extra_args <- list(".stats" = .stats)
|
| 248 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 249 | 1x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 250 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 251 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 252 | ||
| 253 |
# Process additional arguments to the statistic function |
|
| 254 | 6x |
extra_args <- c( |
| 255 | 6x |
extra_args, |
| 256 | 6x |
strata = list(strata), control = list(control), |
| 257 |
... |
|
| 258 |
) |
|
| 259 | ||
| 260 |
# Append additional info from layout to the analysis function |
|
| 261 | 6x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 262 | 6x |
formals(a_coxph_pairwise) <- c(formals(a_coxph_pairwise), extra_args[[".additional_fun_parameters"]]) |
| 263 | ||
| 264 | 6x |
analyze( |
| 265 | 6x |
lyt = lyt, |
| 266 | 6x |
vars = vars, |
| 267 | 6x |
afun = a_coxph_pairwise, |
| 268 | 6x |
na_str = na_str, |
| 269 | 6x |
nested = nested, |
| 270 | 6x |
extra_args = extra_args, |
| 271 | 6x |
var_labels = var_labels, |
| 272 | 6x |
show_labels = show_labels, |
| 273 | 6x |
table_names = table_names |
| 274 |
) |
|
| 275 |
} |
| 1 |
#' Tabulate survival duration by subgroup |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The [tabulate_survival_subgroups()] function creates a layout element to tabulate survival duration by subgroup, |
|
| 6 |
#' returning statistics including median survival time and hazard ratio for each population subgroup. The table is |
|
| 7 |
#' created from `df`, a list of data frames returned by [extract_survival_subgroups()], with the statistics to include |
|
| 8 |
#' specified via the `vars` parameter. |
|
| 9 |
#' |
|
| 10 |
#' A forest plot can be created from the resulting table using the [g_forest()] function. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams argument_convention |
|
| 13 |
#' @inheritParams survival_coxph_pairwise |
|
| 14 |
#' @param df (`list`)\cr list of data frames containing all analysis variables. List should be |
|
| 15 |
#' created using [extract_survival_subgroups()]. |
|
| 16 |
#' @param vars (`character`)\cr the names of statistics to be reported among: |
|
| 17 |
#' * `n_tot_events`: Total number of events per group. |
|
| 18 |
#' * `n_events`: Number of events per group. |
|
| 19 |
#' * `n_tot`: Total number of observations per group. |
|
| 20 |
#' * `n`: Number of observations per group. |
|
| 21 |
#' * `median`: Median survival time. |
|
| 22 |
#' * `hr`: Hazard ratio. |
|
| 23 |
#' * `ci`: Confidence interval of hazard ratio. |
|
| 24 |
#' * `pval`: p-value of the effect. |
|
| 25 |
#' Note, one of the statistics `n_tot` and `n_tot_events`, as well as both `hr` and `ci` |
|
| 26 |
#' are required. |
|
| 27 |
#' @param time_unit (`string`)\cr label with unit of median survival time. Default `NULL` skips displaying unit. |
|
| 28 |
#' |
|
| 29 |
#' @details These functions create a layout starting from a data frame which contains |
|
| 30 |
#' the required statistics. Tables typically used as part of forest plot. |
|
| 31 |
#' |
|
| 32 |
#' @seealso [extract_survival_subgroups()] |
|
| 33 |
#' |
|
| 34 |
#' @examples |
|
| 35 |
#' library(dplyr) |
|
| 36 |
#' |
|
| 37 |
#' adtte <- tern_ex_adtte |
|
| 38 |
#' |
|
| 39 |
#' # Save variable labels before data processing steps. |
|
| 40 |
#' adtte_labels <- formatters::var_labels(adtte) |
|
| 41 |
#' |
|
| 42 |
#' adtte_f <- adtte %>% |
|
| 43 |
#' filter( |
|
| 44 |
#' PARAMCD == "OS", |
|
| 45 |
#' ARM %in% c("B: Placebo", "A: Drug X"),
|
|
| 46 |
#' SEX %in% c("M", "F")
|
|
| 47 |
#' ) %>% |
|
| 48 |
#' mutate( |
|
| 49 |
#' # Reorder levels of ARM to display reference arm before treatment arm. |
|
| 50 |
#' ARM = droplevels(forcats::fct_relevel(ARM, "B: Placebo")), |
|
| 51 |
#' SEX = droplevels(SEX), |
|
| 52 |
#' AVALU = as.character(AVALU), |
|
| 53 |
#' is_event = CNSR == 0 |
|
| 54 |
#' ) |
|
| 55 |
#' labels <- c( |
|
| 56 |
#' "ARM" = adtte_labels[["ARM"]], |
|
| 57 |
#' "SEX" = adtte_labels[["SEX"]], |
|
| 58 |
#' "AVALU" = adtte_labels[["AVALU"]], |
|
| 59 |
#' "is_event" = "Event Flag" |
|
| 60 |
#' ) |
|
| 61 |
#' formatters::var_labels(adtte_f)[names(labels)] <- labels |
|
| 62 |
#' |
|
| 63 |
#' df <- extract_survival_subgroups( |
|
| 64 |
#' variables = list( |
|
| 65 |
#' tte = "AVAL", |
|
| 66 |
#' is_event = "is_event", |
|
| 67 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
| 68 |
#' ), |
|
| 69 |
#' label_all = "Total Patients", |
|
| 70 |
#' data = adtte_f |
|
| 71 |
#' ) |
|
| 72 |
#' df |
|
| 73 |
#' |
|
| 74 |
#' df_grouped <- extract_survival_subgroups( |
|
| 75 |
#' variables = list( |
|
| 76 |
#' tte = "AVAL", |
|
| 77 |
#' is_event = "is_event", |
|
| 78 |
#' arm = "ARM", subgroups = c("SEX", "BMRKR2")
|
|
| 79 |
#' ), |
|
| 80 |
#' data = adtte_f, |
|
| 81 |
#' groups_lists = list( |
|
| 82 |
#' BMRKR2 = list( |
|
| 83 |
#' "low" = "LOW", |
|
| 84 |
#' "low/medium" = c("LOW", "MEDIUM"),
|
|
| 85 |
#' "low/medium/high" = c("LOW", "MEDIUM", "HIGH")
|
|
| 86 |
#' ) |
|
| 87 |
#' ) |
|
| 88 |
#' ) |
|
| 89 |
#' df_grouped |
|
| 90 |
#' |
|
| 91 |
#' @name survival_duration_subgroups |
|
| 92 |
#' @order 1 |
|
| 93 |
NULL |
|
| 94 | ||
| 95 |
#' Prepare survival data for population subgroups in data frames |
|
| 96 |
#' |
|
| 97 |
#' @description `r lifecycle::badge("stable")`
|
|
| 98 |
#' |
|
| 99 |
#' Prepares estimates of median survival times and treatment hazard ratios for population subgroups in |
|
| 100 |
#' data frames. Simple wrapper for [h_survtime_subgroups_df()] and [h_coxph_subgroups_df()]. Result is a `list` |
|
| 101 |
#' of two `data.frame`s: `survtime` and `hr`. `variables` corresponds to the names of variables found in `data`, |
|
| 102 |
#' passed as a named `list` and requires elements `tte`, `is_event`, `arm` and optionally `subgroups` and `strata`. |
|
| 103 |
#' `groups_lists` optionally specifies groupings for `subgroups` variables. |
|
| 104 |
#' |
|
| 105 |
#' @inheritParams argument_convention |
|
| 106 |
#' @inheritParams survival_duration_subgroups |
|
| 107 |
#' @inheritParams survival_coxph_pairwise |
|
| 108 |
#' |
|
| 109 |
#' @return A named `list` of two elements: |
|
| 110 |
#' * `survtime`: A `data.frame` containing columns `arm`, `n`, `n_events`, `median`, `subgroup`, `var`, |
|
| 111 |
#' `var_label`, and `row_type`. |
|
| 112 |
#' * `hr`: A `data.frame` containing columns `arm`, `n_tot`, `n_tot_events`, `hr`, `lcl`, `ucl`, `conf_level`, |
|
| 113 |
#' `pval`, `pval_label`, `subgroup`, `var`, `var_label`, and `row_type`. |
|
| 114 |
#' |
|
| 115 |
#' @seealso [survival_duration_subgroups] |
|
| 116 |
#' |
|
| 117 |
#' @export |
|
| 118 |
extract_survival_subgroups <- function(variables, |
|
| 119 |
data, |
|
| 120 |
groups_lists = list(), |
|
| 121 |
control = control_coxph(), |
|
| 122 |
label_all = "All Patients") {
|
|
| 123 | 12x |
if ("strat" %in% names(variables)) {
|
| 124 | ! |
warning( |
| 125 | ! |
"Warning: the `strat` element name of the `variables` list argument to `extract_survival_subgroups() ", |
| 126 | ! |
"was deprecated in tern 0.9.4.\n ", |
| 127 | ! |
"Please use the name `strata` instead of `strat` in the `variables` argument." |
| 128 |
) |
|
| 129 | ! |
variables[["strata"]] <- variables[["strat"]] |
| 130 |
} |
|
| 131 | ||
| 132 | 12x |
df_survtime <- h_survtime_subgroups_df( |
| 133 | 12x |
variables, |
| 134 | 12x |
data, |
| 135 | 12x |
groups_lists = groups_lists, |
| 136 | 12x |
label_all = label_all |
| 137 |
) |
|
| 138 | 12x |
df_hr <- h_coxph_subgroups_df( |
| 139 | 12x |
variables, |
| 140 | 12x |
data, |
| 141 | 12x |
groups_lists = groups_lists, |
| 142 | 12x |
control = control, |
| 143 | 12x |
label_all = label_all |
| 144 |
) |
|
| 145 | ||
| 146 | 12x |
list(survtime = df_survtime, hr = df_hr) |
| 147 |
} |
|
| 148 | ||
| 149 |
#' @describeIn survival_duration_subgroups Formatted analysis function which is used as |
|
| 150 |
#' `afun` in `tabulate_survival_subgroups()`. |
|
| 151 |
#' |
|
| 152 |
#' @return |
|
| 153 |
#' * `a_survival_subgroups()` returns the corresponding list with formatted [rtables::CellValue()]. |
|
| 154 |
#' |
|
| 155 |
#' @keywords internal |
|
| 156 |
a_survival_subgroups <- function(df, |
|
| 157 |
labelstr = "", |
|
| 158 |
..., |
|
| 159 |
.stats = NULL, |
|
| 160 |
.stat_names = NULL, |
|
| 161 |
.formats = NULL, |
|
| 162 |
.labels = NULL, |
|
| 163 |
.indent_mods = NULL) {
|
|
| 164 |
# Check for additional parameters to the statistics function |
|
| 165 | 335x |
dots_extra_args <- list(...) |
| 166 | 335x |
extra_afun_params <- retrieve_extra_afun_params(names(dots_extra_args$.additional_fun_parameters)) |
| 167 | 335x |
dots_extra_args$.additional_fun_parameters <- NULL |
| 168 | 335x |
cur_col_stat <- extra_afun_params$.var %||% .stats |
| 169 | ||
| 170 |
# Uniquely name & label rows |
|
| 171 | 335x |
var_lvls <- if ("biomarker" %in% names(dots_extra_args) && "biomarker" %in% names(df)) {
|
| 172 | 126x |
if ("overall" %in% names(dots_extra_args)) { # label rows for (nested) biomarker tables - e.g. "AGE", "BMRKR1"
|
| 173 | 54x |
as.character(df$biomarker) |
| 174 | 335x |
} else { # data rows for (nested) biomarker tables - e.g. "AGE.LOW", "BMRKR1.Total Patients"
|
| 175 | 72x |
paste(as.character(df$biomarker), as.character(df$subgroup), sep = ".") |
| 176 |
} |
|
| 177 | 335x |
} else { # data rows for non-biomarker tables - e.g. "Total Patients", "F", "M"
|
| 178 | 209x |
make.unique(as.character(df$subgroup)) |
| 179 |
} |
|
| 180 | ||
| 181 |
# if empty, return NA |
|
| 182 | 335x |
if (nrow(df) == 0) {
|
| 183 | 1x |
return(in_rows(.list = list(NA) %>% stats::setNames(cur_col_stat))) |
| 184 |
} |
|
| 185 | ||
| 186 |
# Main statistics taken from df |
|
| 187 | 334x |
x_stats <- as.list(df) |
| 188 | ||
| 189 |
# Fill in formatting defaults |
|
| 190 | 334x |
.stats <- get_stats("tabulate_survival_subgroups", stats_in = cur_col_stat)
|
| 191 | 334x |
levels_per_stats <- rep(list(var_lvls), length(.stats)) %>% setNames(.stats) |
| 192 | 334x |
.formats <- get_formats_from_stats(.stats, .formats, levels_per_stats) |
| 193 | 334x |
.labels <- get_labels_from_stats( |
| 194 | 334x |
.stats, .labels, levels_per_stats, |
| 195 |
# default labels are pre-determined in extract_*() function |
|
| 196 | 334x |
tern_defaults = as.list(as.character(df$subgroup)) %>% setNames(var_lvls) |
| 197 |
) |
|
| 198 | 334x |
.indent_mods <- get_indents_from_stats(.stats, .indent_mods, levels_per_stats) |
| 199 | ||
| 200 | 334x |
x_stats <- lapply( |
| 201 | 334x |
.stats, |
| 202 | 334x |
function(x) x_stats[[x]] %>% stats::setNames(var_lvls) |
| 203 |
) %>% |
|
| 204 | 334x |
stats::setNames(.stats) %>% |
| 205 | 334x |
.unlist_keep_nulls() |
| 206 | ||
| 207 |
# Auto format handling |
|
| 208 | 334x |
.formats <- apply_auto_formatting(.formats, x_stats, extra_afun_params$.df_row, extra_afun_params$.var) |
| 209 | ||
| 210 |
# Get and check statistical names |
|
| 211 | 334x |
.stat_names <- get_stat_names(x_stats, .stat_names) |
| 212 | ||
| 213 | 334x |
in_rows( |
| 214 | 334x |
.list = x_stats, |
| 215 | 334x |
.formats = .formats, |
| 216 | 334x |
.names = names(.labels), |
| 217 | 334x |
.stat_names = .stat_names, |
| 218 | 334x |
.labels = .labels %>% .unlist_keep_nulls(), |
| 219 | 334x |
.indent_mods = .indent_mods %>% .unlist_keep_nulls() |
| 220 |
) |
|
| 221 |
} |
|
| 222 | ||
| 223 |
#' @describeIn survival_duration_subgroups Table-creating function which creates a table |
|
| 224 |
#' summarizing survival by subgroup. This function is a wrapper for [rtables::analyze_colvars()] |
|
| 225 |
#' and [rtables::summarize_row_groups()]. |
|
| 226 |
#' |
|
| 227 |
#' @param label_all `r lifecycle::badge("deprecated")`\cr please assign the `label_all` parameter within the
|
|
| 228 |
#' [extract_survival_subgroups()] function when creating `df`. |
|
| 229 |
#' @param riskdiff (`list`)\cr if a risk (proportion) difference column should be added, a list of settings to apply |
|
| 230 |
#' within the column. See [control_riskdiff()] for details. If `NULL`, no risk difference column will be added. If |
|
| 231 |
#' `riskdiff$arm_x` and `riskdiff$arm_y` are `NULL`, the first level of `df$survtime$arm` will be used as `arm_x` |
|
| 232 |
#' and the second level as `arm_y`. |
|
| 233 |
#' |
|
| 234 |
#' @return An `rtables` table summarizing survival by subgroup. |
|
| 235 |
#' |
|
| 236 |
#' @examples |
|
| 237 |
#' ## Table with default columns. |
|
| 238 |
#' basic_table() %>% |
|
| 239 |
#' tabulate_survival_subgroups(df, time_unit = adtte_f$AVALU[1]) |
|
| 240 |
#' |
|
| 241 |
#' ## Table with a manually chosen set of columns: adding "pval". |
|
| 242 |
#' basic_table() %>% |
|
| 243 |
#' tabulate_survival_subgroups( |
|
| 244 |
#' df = df, |
|
| 245 |
#' vars = c("n_tot_events", "n_events", "median", "hr", "ci", "pval"),
|
|
| 246 |
#' time_unit = adtte_f$AVALU[1] |
|
| 247 |
#' ) |
|
| 248 |
#' |
|
| 249 |
#' @export |
|
| 250 |
#' @order 2 |
|
| 251 |
tabulate_survival_subgroups <- function(lyt, |
|
| 252 |
df, |
|
| 253 |
vars = c("n_tot_events", "n_events", "median", "hr", "ci"),
|
|
| 254 |
groups_lists = list(), |
|
| 255 |
label_all = lifecycle::deprecated(), |
|
| 256 |
time_unit = NULL, |
|
| 257 |
riskdiff = NULL, |
|
| 258 |
na_str = default_na_str(), |
|
| 259 |
..., |
|
| 260 |
.stat_names = NULL, |
|
| 261 |
.formats = NULL, |
|
| 262 |
.labels = NULL, |
|
| 263 |
.indent_mods = NULL) {
|
|
| 264 | 11x |
checkmate::assert_list(riskdiff, null.ok = TRUE) |
| 265 | 11x |
checkmate::assert_true(any(c("n_tot", "n_tot_events") %in% vars))
|
| 266 | 11x |
checkmate::assert_true(all(c("hr", "ci") %in% vars))
|
| 267 | 11x |
if ("pval" %in% vars && !"pval" %in% names(df$hr)) {
|
| 268 | ! |
warning( |
| 269 | ! |
'The "pval" statistic has been selected but is not present in "df" so it will not be included in the output ', |
| 270 | ! |
'table. To include the "pval" statistic, please specify a p-value test when generating "df" via ', |
| 271 | ! |
'the "method" argument to `extract_survival_subgroups()`. If method = "cmh", strata must also be specified via ', |
| 272 | ! |
'the "variables" argument to `extract_survival_subgroups()`.' |
| 273 |
) |
|
| 274 |
} |
|
| 275 | ||
| 276 | 11x |
if (lifecycle::is_present(label_all)) {
|
| 277 | 1x |
lifecycle::deprecate_warn( |
| 278 | 1x |
"0.9.5", "tabulate_survival_subgroups(label_all)", |
| 279 | 1x |
details = |
| 280 | 1x |
"Please assign the `label_all` parameter within the `extract_survival_subgroups()` function when creating `df`." |
| 281 |
) |
|
| 282 |
} |
|
| 283 | ||
| 284 |
# Process standard extra arguments |
|
| 285 | 11x |
extra_args <- list(".stats" = vars)
|
| 286 | ! |
if (!is.null(.stat_names)) extra_args[[".stat_names"]] <- .stat_names |
| 287 | 1x |
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats |
| 288 | ! |
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels |
| 289 | ! |
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods |
| 290 | ||
| 291 |
# Create "ci" column from "lcl" and "ucl" |
|
| 292 | 11x |
df$hr$ci <- combine_vectors(df$hr$lcl, df$hr$ucl) |
| 293 | ||
| 294 |
# Extract additional parameters from df |
|
| 295 | 11x |
conf_level <- df$hr$conf_level[1] |
| 296 | 11x |
method <- if ("pval_label" %in% names(df$hr)) df$hr$pval_label[1] else NULL
|
| 297 | 11x |
colvars <- d_survival_subgroups_colvars(vars, conf_level = conf_level, method = method, time_unit = time_unit) |
| 298 | 11x |
survtime_vars <- intersect(colvars$vars, c("n", "n_events", "median"))
|
| 299 | 11x |
hr_vars <- intersect(names(colvars$labels), c("n_tot", "n_tot_events", "hr", "ci", "pval"))
|
| 300 | 11x |
colvars_survtime <- list(vars = survtime_vars, labels = colvars$labels[survtime_vars]) |
| 301 | 11x |
colvars_hr <- list(vars = hr_vars, labels = colvars$labels[hr_vars]) |
| 302 | ||
| 303 |
# Process additional arguments to the statistic function |
|
| 304 | 11x |
extra_args <- c( |
| 305 | 11x |
extra_args, |
| 306 | 11x |
groups_lists = list(groups_lists), conf_level = conf_level, method = method, |
| 307 |
... |
|
| 308 |
) |
|
| 309 | ||
| 310 |
# Adding additional info from layout to analysis function |
|
| 311 | 11x |
extra_args[[".additional_fun_parameters"]] <- get_additional_afun_params(add_alt_df = FALSE) |
| 312 | 11x |
formals(a_survival_subgroups) <- c(formals(a_survival_subgroups), extra_args[[".additional_fun_parameters"]]) |
| 313 | ||
| 314 |
# Add risk difference column |
|
| 315 | 11x |
if (!is.null(riskdiff)) {
|
| 316 | 2x |
if (is.null(riskdiff$arm_x)) riskdiff$arm_x <- levels(df$survtime$arm)[1] |
| 317 | 2x |
if (is.null(riskdiff$arm_y)) riskdiff$arm_y <- levels(df$survtime$arm)[2] |
| 318 | 2x |
colvars_hr$vars <- c(colvars_hr$vars, "riskdiff") |
| 319 | 2x |
colvars_hr$labels <- c(colvars_hr$labels, riskdiff = riskdiff$col_label) |
| 320 | 2x |
arm_cols <- paste(rep(c("n_events", "n_events", "n", "n")), c(riskdiff$arm_x, riskdiff$arm_y), sep = "_")
|
| 321 | ||
| 322 | 2x |
df_prop_diff <- df$survtime %>% |
| 323 | 2x |
dplyr::select(-"median") %>% |
| 324 | 2x |
tidyr::pivot_wider( |
| 325 | 2x |
id_cols = c("subgroup", "var", "var_label", "row_type"),
|
| 326 | 2x |
names_from = "arm", |
| 327 | 2x |
values_from = c("n", "n_events")
|
| 328 |
) %>% |
|
| 329 | 2x |
dplyr::rowwise() %>% |
| 330 | 2x |
dplyr::mutate( |
| 331 | 2x |
riskdiff = stat_propdiff_ci( |
| 332 | 2x |
x = as.list(.data[[arm_cols[1]]]), |
| 333 | 2x |
y = as.list(.data[[arm_cols[2]]]), |
| 334 | 2x |
N_x = .data[[arm_cols[3]]], |
| 335 | 2x |
N_y = .data[[arm_cols[4]]], |
| 336 | 2x |
pct = riskdiff$pct |
| 337 |
) |
|
| 338 |
) %>% |
|
| 339 | 2x |
dplyr::select(-dplyr::all_of(arm_cols)) |
| 340 | ||
| 341 | 2x |
df$hr <- df$hr %>% |
| 342 | 2x |
dplyr::left_join( |
| 343 | 2x |
df_prop_diff, |
| 344 | 2x |
by = c("subgroup", "var", "var_label", "row_type")
|
| 345 |
) |
|
| 346 |
} |
|
| 347 | ||
| 348 |
# Add columns from table_survtime (optional) |
|
| 349 | 11x |
if (length(colvars_survtime$vars) > 0) {
|
| 350 | 10x |
lyt_survtime <- split_cols_by(lyt = lyt, var = "arm") |
| 351 | 10x |
lyt_survtime <- split_cols_by_multivar( |
| 352 | 10x |
lyt = lyt_survtime, |
| 353 | 10x |
vars = colvars_survtime$vars, |
| 354 | 10x |
varlabels = colvars_survtime$labels |
| 355 |
) |
|
| 356 | ||
| 357 |
# Add "All Patients" row |
|
| 358 | 10x |
lyt_survtime <- split_rows_by( |
| 359 | 10x |
lyt = lyt_survtime, |
| 360 | 10x |
var = "row_type", |
| 361 | 10x |
split_fun = keep_split_levels("content"),
|
| 362 | 10x |
nested = FALSE, |
| 363 | 10x |
child_labels = "hidden" |
| 364 |
) |
|
| 365 | 10x |
lyt_survtime <- analyze_colvars( |
| 366 | 10x |
lyt = lyt_survtime, |
| 367 | 10x |
afun = a_survival_subgroups, |
| 368 | 10x |
na_str = na_str, |
| 369 | 10x |
extra_args = extra_args |
| 370 |
) |
|
| 371 | ||
| 372 |
# Add analysis rows |
|
| 373 | 10x |
if ("analysis" %in% df$survtime$row_type) {
|
| 374 | 9x |
lyt_survtime <- split_rows_by( |
| 375 | 9x |
lyt = lyt_survtime, |
| 376 | 9x |
var = "row_type", |
| 377 | 9x |
split_fun = keep_split_levels("analysis"),
|
| 378 | 9x |
nested = FALSE, |
| 379 | 9x |
child_labels = "hidden" |
| 380 |
) |
|
| 381 | 9x |
lyt_survtime <- split_rows_by(lyt = lyt_survtime, var = "var_label", nested = TRUE) |
| 382 | 9x |
lyt_survtime <- analyze_colvars( |
| 383 | 9x |
lyt = lyt_survtime, |
| 384 | 9x |
afun = a_survival_subgroups, |
| 385 | 9x |
na_str = na_str, |
| 386 | 9x |
inclNAs = TRUE, |
| 387 | 9x |
extra_args = extra_args |
| 388 |
) |
|
| 389 |
} |
|
| 390 | ||
| 391 | 10x |
table_survtime <- build_table(lyt_survtime, df = df$survtime) |
| 392 |
} else {
|
|
| 393 | 1x |
table_survtime <- NULL |
| 394 |
} |
|
| 395 | ||
| 396 |
# Add columns from table_hr ("n_tot_events" or "n_tot", "hr" and "ci" required)
|
|
| 397 | 11x |
lyt_hr <- split_cols_by(lyt = lyt, var = "arm") |
| 398 | 11x |
lyt_hr <- split_cols_by_multivar( |
| 399 | 11x |
lyt = lyt_hr, |
| 400 | 11x |
vars = colvars_hr$vars, |
| 401 | 11x |
varlabels = colvars_hr$labels |
| 402 |
) |
|
| 403 | ||
| 404 |
# Add "All Patients" row |
|
| 405 | 11x |
lyt_hr <- split_rows_by( |
| 406 | 11x |
lyt = lyt_hr, |
| 407 | 11x |
var = "row_type", |
| 408 | 11x |
split_fun = keep_split_levels("content"),
|
| 409 | 11x |
nested = FALSE, |
| 410 | 11x |
child_labels = "hidden" |
| 411 |
) |
|
| 412 | 11x |
lyt_hr <- analyze_colvars( |
| 413 | 11x |
lyt = lyt_hr, |
| 414 | 11x |
afun = a_survival_subgroups, |
| 415 | 11x |
na_str = na_str, |
| 416 | 11x |
extra_args = extra_args |
| 417 |
) %>% |
|
| 418 | 11x |
append_topleft("Baseline Risk Factors")
|
| 419 | ||
| 420 |
# Add analysis rows |
|
| 421 | 11x |
if ("analysis" %in% df$survtime$row_type) {
|
| 422 | 10x |
lyt_hr <- split_rows_by( |
| 423 | 10x |
lyt = lyt_hr, |
| 424 | 10x |
var = "row_type", |
| 425 | 10x |
split_fun = keep_split_levels("analysis"),
|
| 426 | 10x |
nested = FALSE, |
| 427 | 10x |
child_labels = "hidden" |
| 428 |
) |
|
| 429 | 10x |
lyt_hr <- split_rows_by(lyt = lyt_hr, var = "var_label", nested = TRUE) |
| 430 | 10x |
lyt_hr <- analyze_colvars( |
| 431 | 10x |
lyt = lyt_hr, |
| 432 | 10x |
afun = a_survival_subgroups, |
| 433 | 10x |
na_str = na_str, |
| 434 | 10x |
inclNAs = TRUE, |
| 435 | 10x |
extra_args = extra_args |
| 436 |
) |
|
| 437 |
} |
|
| 438 | ||
| 439 | 11x |
table_hr <- build_table(lyt_hr, df = df$hr) |
| 440 | ||
| 441 |
# Join tables, add forest plot attributes |
|
| 442 | 11x |
n_tot_ids <- grep("^n_tot", colvars_hr$vars)
|
| 443 | 11x |
if (is.null(table_survtime)) {
|
| 444 | 1x |
result <- table_hr |
| 445 | 1x |
hr_id <- match("hr", colvars_hr$vars)
|
| 446 | 1x |
ci_id <- match("ci", colvars_hr$vars)
|
| 447 |
} else {
|
|
| 448 | 10x |
result <- cbind_rtables(table_hr[, n_tot_ids], table_survtime, table_hr[, -n_tot_ids]) |
| 449 | 10x |
hr_id <- length(n_tot_ids) + ncol(table_survtime) + match("hr", colvars_hr$vars[-n_tot_ids])
|
| 450 | 10x |
ci_id <- length(n_tot_ids) + ncol(table_survtime) + match("ci", colvars_hr$vars[-n_tot_ids])
|
| 451 | 10x |
n_tot_ids <- seq_along(n_tot_ids) |
| 452 |
} |
|
| 453 | 11x |
structure( |
| 454 | 11x |
result, |
| 455 | 11x |
forest_header = paste0(rev(levels(df$survtime$arm)), "\nBetter"), |
| 456 | 11x |
col_x = hr_id, |
| 457 | 11x |
col_ci = ci_id, |
| 458 | 11x |
col_symbol_size = n_tot_ids[1] # for scaling the symbol sizes in forest plots |
| 459 |
) |
|
| 460 |
} |
|
| 461 | ||
| 462 |
#' Labels for column variables in survival duration by subgroup table |
|
| 463 |
#' |
|
| 464 |
#' @description `r lifecycle::badge("stable")`
|
|
| 465 |
#' |
|
| 466 |
#' Internal function to check variables included in [tabulate_survival_subgroups()] and create column labels. |
|
| 467 |
#' |
|
| 468 |
#' @inheritParams tabulate_survival_subgroups |
|
| 469 |
#' @inheritParams argument_convention |
|
| 470 |
#' @param method (`string`)\cr p-value method for testing hazard ratio = 1. |
|
| 471 |
#' |
|
| 472 |
#' @return A `list` of variables and their labels to tabulate. |
|
| 473 |
#' |
|
| 474 |
#' @note At least one of `n_tot` and `n_tot_events` must be provided in `vars`. |
|
| 475 |
#' |
|
| 476 |
#' @export |
|
| 477 |
d_survival_subgroups_colvars <- function(vars, |
|
| 478 |
conf_level, |
|
| 479 |
method, |
|
| 480 |
time_unit = NULL) {
|
|
| 481 | 18x |
checkmate::assert_character(vars) |
| 482 | 18x |
checkmate::assert_string(time_unit, null.ok = TRUE) |
| 483 | 18x |
checkmate::assert_subset(c("hr", "ci"), vars)
|
| 484 | 18x |
checkmate::assert_true(any(c("n_tot", "n_tot_events") %in% vars))
|
| 485 | 18x |
checkmate::assert_subset( |
| 486 | 18x |
vars, |
| 487 | 18x |
c("n", "n_events", "median", "n_tot", "n_tot_events", "hr", "ci", "pval")
|
| 488 |
) |
|
| 489 | ||
| 490 | 18x |
propcase_time_label <- if (!is.null(time_unit)) {
|
| 491 | 17x |
paste0("Median (", time_unit, ")")
|
| 492 |
} else {
|
|
| 493 | 1x |
"Median" |
| 494 |
} |
|
| 495 | ||
| 496 | 18x |
varlabels <- c( |
| 497 | 18x |
n = "n", |
| 498 | 18x |
n_events = "Events", |
| 499 | 18x |
median = propcase_time_label, |
| 500 | 18x |
n_tot = "Total n", |
| 501 | 18x |
n_tot_events = "Total Events", |
| 502 | 18x |
hr = "Hazard Ratio", |
| 503 | 18x |
ci = paste0(100 * conf_level, "% Wald CI"), |
| 504 | 18x |
pval = method |
| 505 |
) |
|
| 506 | ||
| 507 | 18x |
colvars <- vars |
| 508 | ||
| 509 | 18x |
list( |
| 510 | 18x |
vars = colvars, |
| 511 | 18x |
labels = varlabels[vars] |
| 512 |
) |
|
| 513 |
} |
| 1 |
#' Helper functions for subgroup treatment effect pattern (STEP) calculations |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' Helper functions that are used internally for the STEP calculations. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams argument_convention |
|
| 8 |
#' |
|
| 9 |
#' @name h_step |
|
| 10 |
#' @include control_step.R |
|
| 11 |
NULL |
|
| 12 | ||
| 13 |
#' @describeIn h_step Creates the windows for STEP, based on the control settings |
|
| 14 |
#' provided. |
|
| 15 |
#' |
|
| 16 |
#' @param x (`numeric`)\cr biomarker value(s) to use (without `NA`). |
|
| 17 |
#' @param control (named `list`)\cr output from `control_step()`. |
|
| 18 |
#' |
|
| 19 |
#' @return |
|
| 20 |
#' * `h_step_window()` returns a list containing the window-selection matrix `sel` |
|
| 21 |
#' and the interval information matrix `interval`. |
|
| 22 |
#' |
|
| 23 |
#' @export |
|
| 24 |
h_step_window <- function(x, |
|
| 25 |
control = control_step()) {
|
|
| 26 | 12x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
| 27 | 12x |
checkmate::assert_list(control, names = "named") |
| 28 | ||
| 29 | 12x |
sel <- matrix(FALSE, length(x), control$num_points) |
| 30 | 12x |
out <- matrix(0, control$num_points, 3) |
| 31 | 12x |
colnames(out) <- paste("Interval", c("Center", "Lower", "Upper"))
|
| 32 | 12x |
if (control$use_percentile) {
|
| 33 |
# Create windows according to percentile cutoffs. |
|
| 34 | 9x |
out <- cbind(out, out) |
| 35 | 9x |
colnames(out)[1:3] <- paste("Percentile", c("Center", "Lower", "Upper"))
|
| 36 | 9x |
xs <- seq(0, 1, length.out = control$num_points + 2)[-1] |
| 37 | 9x |
for (i in seq_len(control$num_points)) {
|
| 38 | 185x |
out[i, 2:3] <- c( |
| 39 | 185x |
max(xs[i] - control$bandwidth, 0), |
| 40 | 185x |
min(xs[i] + control$bandwidth, 1) |
| 41 |
) |
|
| 42 | 185x |
out[i, 5:6] <- stats::quantile(x, out[i, 2:3]) |
| 43 | 185x |
sel[, i] <- x >= out[i, 5] & x <= out[i, 6] |
| 44 |
} |
|
| 45 |
# Center is the middle point of the percentile window. |
|
| 46 | 9x |
out[, 1] <- xs[-control$num_points - 1] |
| 47 | 9x |
out[, 4] <- stats::quantile(x, out[, 1]) |
| 48 |
} else {
|
|
| 49 |
# Create windows according to cutoffs. |
|
| 50 | 3x |
m <- c(min(x), max(x)) |
| 51 | 3x |
xs <- seq(m[1], m[2], length.out = control$num_points + 2)[-1] |
| 52 | 3x |
for (i in seq_len(control$num_points)) {
|
| 53 | 11x |
out[i, 2:3] <- c( |
| 54 | 11x |
max(xs[i] - control$bandwidth, m[1]), |
| 55 | 11x |
min(xs[i] + control$bandwidth, m[2]) |
| 56 |
) |
|
| 57 | 11x |
sel[, i] <- x >= out[i, 2] & x <= out[i, 3] |
| 58 |
} |
|
| 59 |
# Center is the same as the point for predicting. |
|
| 60 | 3x |
out[, 1] <- xs[-control$num_points - 1] |
| 61 |
} |
|
| 62 | 12x |
list(sel = sel, interval = out) |
| 63 |
} |
|
| 64 | ||
| 65 |
#' @describeIn h_step Calculates the estimated treatment effect estimate |
|
| 66 |
#' on the linear predictor scale and corresponding standard error from a STEP `model` fitted |
|
| 67 |
#' on `data` given `variables` specification, for a single biomarker value `x`. |
|
| 68 |
#' This works for both `coxph` and `glm` models, i.e. for calculating log hazard ratio or log odds |
|
| 69 |
#' ratio estimates. |
|
| 70 |
#' |
|
| 71 |
#' @param model (`coxph` or `glm`)\cr the regression model object. |
|
| 72 |
#' |
|
| 73 |
#' @return |
|
| 74 |
#' * `h_step_trt_effect()` returns a vector with elements `est` and `se`. |
|
| 75 |
#' |
|
| 76 |
#' @export |
|
| 77 |
h_step_trt_effect <- function(data, |
|
| 78 |
model, |
|
| 79 |
variables, |
|
| 80 |
x) {
|
|
| 81 | 208x |
checkmate::assert_multi_class(model, c("coxph", "glm"))
|
| 82 | 208x |
checkmate::assert_number(x) |
| 83 | 208x |
assert_df_with_variables(data, variables) |
| 84 | 208x |
checkmate::assert_factor(data[[variables$arm]], n.levels = 2) |
| 85 | ||
| 86 | 208x |
newdata <- data[c(1, 1), ] |
| 87 | 208x |
newdata[, variables$biomarker] <- x |
| 88 | 208x |
newdata[, variables$arm] <- levels(data[[variables$arm]]) |
| 89 | 208x |
model_terms <- stats::delete.response(stats::terms(model)) |
| 90 | 208x |
model_frame <- stats::model.frame(model_terms, data = newdata, xlev = model$xlevels) |
| 91 | 208x |
mat <- stats::model.matrix(model_terms, data = model_frame, contrasts.arg = model$contrasts) |
| 92 | 208x |
coefs <- stats::coef(model) |
| 93 |
# Note: It is important to use the coef subset from matrix, otherwise intercept and |
|
| 94 |
# strata are included for coxph() models. |
|
| 95 | 208x |
mat <- mat[, names(coefs)] |
| 96 | 208x |
mat_diff <- diff(mat) |
| 97 | 208x |
est <- mat_diff %*% coefs |
| 98 | 208x |
var <- mat_diff %*% stats::vcov(model) %*% t(mat_diff) |
| 99 | 208x |
se <- sqrt(var) |
| 100 | 208x |
c( |
| 101 | 208x |
est = est, |
| 102 | 208x |
se = se |
| 103 |
) |
|
| 104 |
} |
|
| 105 | ||
| 106 |
#' @describeIn h_step Builds the model formula used in survival STEP calculations. |
|
| 107 |
#' |
|
| 108 |
#' @return |
|
| 109 |
#' * `h_step_survival_formula()` returns a model formula. |
|
| 110 |
#' |
|
| 111 |
#' @export |
|
| 112 |
h_step_survival_formula <- function(variables, |
|
| 113 |
control = control_step()) {
|
|
| 114 | 10x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
| 115 | ||
| 116 | 10x |
assert_list_of_variables(variables[c("arm", "biomarker", "event", "time")])
|
| 117 | 10x |
form <- paste0("Surv(", variables$time, ", ", variables$event, ") ~ ", variables$arm)
|
| 118 | 10x |
if (control$degree > 0) {
|
| 119 | 5x |
form <- paste0(form, " * stats::poly(", variables$biomarker, ", degree = ", control$degree, ", raw = TRUE)")
|
| 120 |
} |
|
| 121 | 10x |
if (!is.null(variables$covariates)) {
|
| 122 | 6x |
form <- paste(form, "+", paste(variables$covariates, collapse = "+")) |
| 123 |
} |
|
| 124 | 10x |
if (!is.null(variables$strata)) {
|
| 125 | 2x |
form <- paste0(form, " + strata(", paste0(variables$strata, collapse = ", "), ")")
|
| 126 |
} |
|
| 127 | 10x |
stats::as.formula(form) |
| 128 |
} |
|
| 129 | ||
| 130 |
#' @describeIn h_step Estimates the model with `formula` built based on |
|
| 131 |
#' `variables` in `data` for a given `subset` and `control` parameters for the |
|
| 132 |
#' Cox regression. |
|
| 133 |
#' |
|
| 134 |
#' @param formula (`formula`)\cr the regression model formula. |
|
| 135 |
#' @param subset (`logical`)\cr subset vector. |
|
| 136 |
#' |
|
| 137 |
#' @return |
|
| 138 |
#' * `h_step_survival_est()` returns a matrix of number of observations `n`, |
|
| 139 |
#' `events`, log hazard ratio estimates `loghr`, standard error `se`, |
|
| 140 |
#' and Wald confidence interval bounds `ci_lower` and `ci_upper`. One row is |
|
| 141 |
#' included for each biomarker value in `x`. |
|
| 142 |
#' |
|
| 143 |
#' @export |
|
| 144 |
h_step_survival_est <- function(formula, |
|
| 145 |
data, |
|
| 146 |
variables, |
|
| 147 |
x, |
|
| 148 |
subset = rep(TRUE, nrow(data)), |
|
| 149 |
control = control_coxph()) {
|
|
| 150 | 55x |
checkmate::assert_formula(formula) |
| 151 | 55x |
assert_df_with_variables(data, variables) |
| 152 | 55x |
checkmate::assert_logical(subset, min.len = 1, any.missing = FALSE) |
| 153 | 55x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
| 154 | 55x |
checkmate::assert_list(control, names = "named") |
| 155 | ||
| 156 |
# Note: `subset` in `coxph` needs to be an expression referring to `data` variables. |
|
| 157 | 55x |
data$.subset <- subset |
| 158 | 55x |
coxph_warnings <- NULL |
| 159 | 55x |
tryCatch( |
| 160 | 55x |
withCallingHandlers( |
| 161 | 55x |
expr = {
|
| 162 | 55x |
fit <- survival::coxph( |
| 163 | 55x |
formula = formula, |
| 164 | 55x |
data = data, |
| 165 | 55x |
subset = .subset, |
| 166 | 55x |
ties = control$ties |
| 167 |
) |
|
| 168 |
}, |
|
| 169 | 55x |
warning = function(w) {
|
| 170 | 1x |
coxph_warnings <<- c(coxph_warnings, w) |
| 171 | 1x |
invokeRestart("muffleWarning")
|
| 172 |
} |
|
| 173 |
), |
|
| 174 | 55x |
finally = {
|
| 175 |
} |
|
| 176 |
) |
|
| 177 | 55x |
if (!is.null(coxph_warnings)) {
|
| 178 | 1x |
warning(paste( |
| 179 | 1x |
"Fit warnings occurred, please consider using a simpler model, or", |
| 180 | 1x |
"larger `bandwidth`, less `num_points` in `control_step()` settings" |
| 181 |
)) |
|
| 182 |
} |
|
| 183 |
# Produce a matrix with one row per `x` and columns `est` and `se`. |
|
| 184 | 55x |
estimates <- t(vapply( |
| 185 | 55x |
X = x, |
| 186 | 55x |
FUN = h_step_trt_effect, |
| 187 | 55x |
FUN.VALUE = c(1, 2), |
| 188 | 55x |
data = data, |
| 189 | 55x |
model = fit, |
| 190 | 55x |
variables = variables |
| 191 |
)) |
|
| 192 | 55x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
| 193 | 55x |
cbind( |
| 194 | 55x |
n = fit$n, |
| 195 | 55x |
events = fit$nevent, |
| 196 | 55x |
loghr = estimates[, "est"], |
| 197 | 55x |
se = estimates[, "se"], |
| 198 | 55x |
ci_lower = estimates[, "est"] - q_norm * estimates[, "se"], |
| 199 | 55x |
ci_upper = estimates[, "est"] + q_norm * estimates[, "se"] |
| 200 |
) |
|
| 201 |
} |
|
| 202 | ||
| 203 |
#' @describeIn h_step Builds the model formula used in response STEP calculations. |
|
| 204 |
#' |
|
| 205 |
#' @return |
|
| 206 |
#' * `h_step_rsp_formula()` returns a model formula. |
|
| 207 |
#' |
|
| 208 |
#' @export |
|
| 209 |
h_step_rsp_formula <- function(variables, |
|
| 210 |
control = c(control_step(), control_logistic())) {
|
|
| 211 | 14x |
checkmate::assert_character(variables$covariates, null.ok = TRUE) |
| 212 | 14x |
assert_list_of_variables(variables[c("arm", "biomarker", "response")])
|
| 213 | 14x |
response_definition <- sub( |
| 214 | 14x |
pattern = "response", |
| 215 | 14x |
replacement = variables$response, |
| 216 | 14x |
x = control$response_definition, |
| 217 | 14x |
fixed = TRUE |
| 218 |
) |
|
| 219 | 14x |
form <- paste0(response_definition, " ~ ", variables$arm) |
| 220 | 14x |
if (control$degree > 0) {
|
| 221 | 8x |
form <- paste0(form, " * stats::poly(", variables$biomarker, ", degree = ", control$degree, ", raw = TRUE)")
|
| 222 |
} |
|
| 223 | 14x |
if (!is.null(variables$covariates)) {
|
| 224 | 8x |
form <- paste(form, "+", paste(variables$covariates, collapse = "+")) |
| 225 |
} |
|
| 226 | 14x |
if (!is.null(variables$strata)) {
|
| 227 | 5x |
strata_arg <- if (length(variables$strata) > 1) {
|
| 228 | 2x |
paste0("I(interaction(", paste0(variables$strata, collapse = ", "), "))")
|
| 229 |
} else {
|
|
| 230 | 3x |
variables$strata |
| 231 |
} |
|
| 232 | 5x |
form <- paste0(form, "+ strata(", strata_arg, ")")
|
| 233 |
} |
|
| 234 | 14x |
stats::as.formula(form) |
| 235 |
} |
|
| 236 | ||
| 237 |
#' @describeIn h_step Estimates the model with `formula` built based on |
|
| 238 |
#' `variables` in `data` for a given `subset` and `control` parameters for the |
|
| 239 |
#' logistic regression. |
|
| 240 |
#' |
|
| 241 |
#' @param formula (`formula`)\cr the regression model formula. |
|
| 242 |
#' @param subset (`logical`)\cr subset vector. |
|
| 243 |
#' |
|
| 244 |
#' @return |
|
| 245 |
#' * `h_step_rsp_est()` returns a matrix of number of observations `n`, log odds |
|
| 246 |
#' ratio estimates `logor`, standard error `se`, and Wald confidence interval bounds |
|
| 247 |
#' `ci_lower` and `ci_upper`. One row is included for each biomarker value in `x`. |
|
| 248 |
#' |
|
| 249 |
#' @export |
|
| 250 |
h_step_rsp_est <- function(formula, |
|
| 251 |
data, |
|
| 252 |
variables, |
|
| 253 |
x, |
|
| 254 |
subset = rep(TRUE, nrow(data)), |
|
| 255 |
control = control_logistic()) {
|
|
| 256 | 58x |
checkmate::assert_formula(formula) |
| 257 | 58x |
assert_df_with_variables(data, variables) |
| 258 | 58x |
checkmate::assert_logical(subset, min.len = 1, any.missing = FALSE) |
| 259 | 58x |
checkmate::assert_numeric(x, min.len = 1, any.missing = FALSE) |
| 260 | 58x |
checkmate::assert_list(control, names = "named") |
| 261 |
# Note: `subset` in `glm` needs to be an expression referring to `data` variables. |
|
| 262 | 58x |
data$.subset <- subset |
| 263 | 58x |
fit_warnings <- NULL |
| 264 | 58x |
tryCatch( |
| 265 | 58x |
withCallingHandlers( |
| 266 | 58x |
expr = {
|
| 267 | 58x |
fit <- if (is.null(variables$strata)) {
|
| 268 | 54x |
stats::glm( |
| 269 | 54x |
formula = formula, |
| 270 | 54x |
data = data, |
| 271 | 54x |
subset = .subset, |
| 272 | 54x |
family = stats::binomial("logit")
|
| 273 |
) |
|
| 274 |
} else {
|
|
| 275 |
# clogit needs coxph and strata imported |
|
| 276 | 4x |
survival::clogit( |
| 277 | 4x |
formula = formula, |
| 278 | 4x |
data = data, |
| 279 | 4x |
subset = .subset |
| 280 |
) |
|
| 281 |
} |
|
| 282 |
}, |
|
| 283 | 58x |
warning = function(w) {
|
| 284 | 19x |
fit_warnings <<- c(fit_warnings, w) |
| 285 | 19x |
invokeRestart("muffleWarning")
|
| 286 |
} |
|
| 287 |
), |
|
| 288 | 58x |
finally = {
|
| 289 |
} |
|
| 290 |
) |
|
| 291 | 58x |
if (!is.null(fit_warnings)) {
|
| 292 | 13x |
warning(paste( |
| 293 | 13x |
"Fit warnings occurred, please consider using a simpler model, or", |
| 294 | 13x |
"larger `bandwidth`, less `num_points` in `control_step()` settings" |
| 295 |
)) |
|
| 296 |
} |
|
| 297 |
# Produce a matrix with one row per `x` and columns `est` and `se`. |
|
| 298 | 58x |
estimates <- t(vapply( |
| 299 | 58x |
X = x, |
| 300 | 58x |
FUN = h_step_trt_effect, |
| 301 | 58x |
FUN.VALUE = c(1, 2), |
| 302 | 58x |
data = data, |
| 303 | 58x |
model = fit, |
| 304 | 58x |
variables = variables |
| 305 |
)) |
|
| 306 | 58x |
q_norm <- stats::qnorm((1 + control$conf_level) / 2) |
| 307 | 58x |
cbind( |
| 308 | 58x |
n = length(fit$y), |
| 309 | 58x |
logor = estimates[, "est"], |
| 310 | 58x |
se = estimates[, "se"], |
| 311 | 58x |
ci_lower = estimates[, "est"] - q_norm * estimates[, "se"], |
| 312 | 58x |
ci_upper = estimates[, "est"] + q_norm * estimates[, "se"] |
| 313 |
) |
|
| 314 |
} |
| 1 |
#' Horizontal waterfall plot |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' This basic waterfall plot visualizes a quantity `height` ordered by value with some markup. |
|
| 6 |
#' |
|
| 7 |
#' @param height (`numeric`)\cr vector containing values to be plotted as the waterfall bars. |
|
| 8 |
#' @param id (`character`)\cr vector containing identifiers to use as the x-axis label for the waterfall bars. |
|
| 9 |
#' @param col (`character`)\cr color(s). |
|
| 10 |
#' @param col_var (`factor`, `character`, or `NULL`)\cr categorical variable for bar coloring. `NULL` by default. |
|
| 11 |
#' @param xlab (`string`)\cr x label. Default is `"ID"`. |
|
| 12 |
#' @param ylab (`string`)\cr y label. Default is `"Value"`. |
|
| 13 |
#' @param title (`string`)\cr text to be displayed as plot title. |
|
| 14 |
#' @param col_legend_title (`string`)\cr text to be displayed as legend title. |
|
| 15 |
#' |
|
| 16 |
#' @return A `ggplot` waterfall plot. |
|
| 17 |
#' |
|
| 18 |
#' @examples |
|
| 19 |
#' library(dplyr) |
|
| 20 |
#' |
|
| 21 |
#' g_waterfall(height = c(3, 5, -1), id = letters[1:3]) |
|
| 22 |
#' |
|
| 23 |
#' g_waterfall( |
|
| 24 |
#' height = c(3, 5, -1), |
|
| 25 |
#' id = letters[1:3], |
|
| 26 |
#' col_var = letters[1:3] |
|
| 27 |
#' ) |
|
| 28 |
#' |
|
| 29 |
#' adsl_f <- tern_ex_adsl %>% |
|
| 30 |
#' select(USUBJID, STUDYID, ARM, ARMCD, SEX) |
|
| 31 |
#' |
|
| 32 |
#' adrs_f <- tern_ex_adrs %>% |
|
| 33 |
#' filter(PARAMCD == "OVRINV") %>% |
|
| 34 |
#' mutate(pchg = rnorm(n(), 10, 50)) |
|
| 35 |
#' |
|
| 36 |
#' adrs_f <- head(adrs_f, 30) |
|
| 37 |
#' adrs_f <- adrs_f[!duplicated(adrs_f$USUBJID), ] |
|
| 38 |
#' head(adrs_f) |
|
| 39 |
#' |
|
| 40 |
#' g_waterfall( |
|
| 41 |
#' height = adrs_f$pchg, |
|
| 42 |
#' id = adrs_f$USUBJID, |
|
| 43 |
#' col_var = adrs_f$AVALC |
|
| 44 |
#' ) |
|
| 45 |
#' |
|
| 46 |
#' g_waterfall( |
|
| 47 |
#' height = adrs_f$pchg, |
|
| 48 |
#' id = paste("asdfdsfdsfsd", adrs_f$USUBJID),
|
|
| 49 |
#' col_var = adrs_f$SEX |
|
| 50 |
#' ) |
|
| 51 |
#' |
|
| 52 |
#' g_waterfall( |
|
| 53 |
#' height = adrs_f$pchg, |
|
| 54 |
#' id = paste("asdfdsfdsfsd", adrs_f$USUBJID),
|
|
| 55 |
#' xlab = "ID", |
|
| 56 |
#' ylab = "Percentage Change", |
|
| 57 |
#' title = "Waterfall plot" |
|
| 58 |
#' ) |
|
| 59 |
#' |
|
| 60 |
#' @export |
|
| 61 |
g_waterfall <- function(height, |
|
| 62 |
id, |
|
| 63 |
col_var = NULL, |
|
| 64 |
col = getOption("ggplot2.discrete.colour"),
|
|
| 65 |
xlab = NULL, |
|
| 66 |
ylab = NULL, |
|
| 67 |
col_legend_title = NULL, |
|
| 68 |
title = NULL) {
|
|
| 69 | 2x |
if (!is.null(col_var)) {
|
| 70 | 1x |
check_same_n(height = height, id = id, col_var = col_var) |
| 71 |
} else {
|
|
| 72 | 1x |
check_same_n(height = height, id = id) |
| 73 |
} |
|
| 74 | ||
| 75 | 2x |
checkmate::assert_multi_class(col_var, c("character", "factor"), null.ok = TRUE)
|
| 76 | 2x |
checkmate::assert_character(col, null.ok = TRUE) |
| 77 | ||
| 78 | 2x |
xlabel <- deparse(substitute(id)) |
| 79 | 2x |
ylabel <- deparse(substitute(height)) |
| 80 | ||
| 81 | 2x |
col_label <- if (!missing(col_var)) {
|
| 82 | 1x |
deparse(substitute(col_var)) |
| 83 |
} |
|
| 84 | ||
| 85 | 2x |
xlab <- if (is.null(xlab)) xlabel else xlab |
| 86 | 2x |
ylab <- if (is.null(ylab)) ylabel else ylab |
| 87 | 2x |
col_legend_title <- if (is.null(col_legend_title)) col_label else col_legend_title |
| 88 | ||
| 89 | 2x |
plot_data <- data.frame( |
| 90 | 2x |
height = height, |
| 91 | 2x |
id = as.character(id), |
| 92 | 2x |
col_var = if (is.null(col_var)) "x" else to_n(col_var, length(height)), |
| 93 | 2x |
stringsAsFactors = FALSE |
| 94 |
) |
|
| 95 | ||
| 96 | 2x |
plot_data_ord <- plot_data[order(plot_data$height, decreasing = TRUE), ] |
| 97 | ||
| 98 | 2x |
p <- ggplot2::ggplot(plot_data_ord, ggplot2::aes(x = factor(id, levels = id), y = height)) + |
| 99 | 2x |
ggplot2::geom_col() + |
| 100 | 2x |
ggplot2::geom_text( |
| 101 | 2x |
label = format(plot_data_ord$height, digits = 2), |
| 102 | 2x |
vjust = ifelse(plot_data_ord$height >= 0, -0.5, 1.5) |
| 103 |
) + |
|
| 104 | 2x |
ggplot2::xlab(xlab) + |
| 105 | 2x |
ggplot2::ylab(ylab) + |
| 106 | 2x |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, hjust = 0, vjust = .5)) |
| 107 | ||
| 108 | 2x |
if (!is.null(col_var)) {
|
| 109 | 1x |
p <- p + |
| 110 | 1x |
ggplot2::aes(fill = col_var) + |
| 111 | 1x |
ggplot2::labs(fill = col_legend_title) + |
| 112 | 1x |
ggplot2::theme( |
| 113 | 1x |
legend.position = "bottom", |
| 114 | 1x |
legend.background = ggplot2::element_blank(), |
| 115 | 1x |
legend.title = ggplot2::element_text(face = "bold"), |
| 116 | 1x |
legend.box.background = ggplot2::element_rect(colour = "black") |
| 117 |
) |
|
| 118 |
} |
|
| 119 | ||
| 120 | 2x |
if (!is.null(col)) {
|
| 121 | 1x |
p <- p + |
| 122 | 1x |
ggplot2::scale_fill_manual(values = col) |
| 123 |
} |
|
| 124 | ||
| 125 | 2x |
if (!is.null(title)) {
|
| 126 | 1x |
p <- p + |
| 127 | 1x |
ggplot2::labs(title = title) + |
| 128 | 1x |
ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) |
| 129 |
} |
|
| 130 | ||
| 131 | 2x |
p |
| 132 |
} |
| 1 |
#' Tabulate biomarker effects on binary response by subgroup |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("stable")`
|
|
| 4 |
#' |
|
| 5 |
#' The [tabulate_rsp_biomarkers()] function creates a layout element to tabulate the estimated biomarker effects on a |
|
| 6 |
#' binary response endpoint across subgroups, returning statistics including response rate and odds ratio for each |
|
| 7 |
#' population subgroup. The table is created from `df`, a list of data frames returned by [extract_rsp_biomarkers()], |
|
| 8 |
#' with the statistics to include specified via the `vars` parameter. |
|
| 9 |
#' |
|
| 10 |
#' A forest plot can be created from the resulting table using the [g_forest()] function. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams argument_convention |
|
| 13 |
#' @param df (`data.frame`)\cr containing all analysis variables, as returned by |
|
| 14 |
#' [extract_rsp_biomarkers()]. |
|
| 15 |
#' @param vars (`character`)\cr the names of statistics to be reported among: |
|
| 16 |
#' * `n_tot`: Total number of patients per group. |
|
| 17 |
#' * `n_rsp`: Total number of responses per group. |
|
| 18 |
#' * `prop`: Total response proportion per group. |
|
| 19 |
#' * `or`: Odds ratio. |
|
| 20 |
#' * `ci`: Confidence interval of odds ratio. |
|
| 21 |
#' * `pval`: p-value of the effect. |
|
| 22 |
#' Note, the statistics `n_tot`, `or` and `ci` are required. |
|
| 23 |
#' |
|
| 24 |
#' @return An `rtables` table summarizing biomarker effects on binary response by subgroup. |
|
| 25 |
#' |
|
| 26 |
#' @details These functions create a layout starting from a data frame which contains |
|
| 27 |
#' the required statistics. The tables are then typically used as input for forest plots. |
|
| 28 |
#' |
|
| 29 |
#' @note In contrast to [tabulate_rsp_subgroups()] this tabulation function does |
|
| 30 |
#' not start from an input layout `lyt`. This is because internally the table is |
|
| 31 |
#' created by combining multiple subtables. |
|
| 32 |
#' |
|
| 33 |
#' @seealso [extract_rsp_biomarkers()] |
|
| 34 |
#' |
|
| 35 |
#' @examples |
|
| 36 |