diff --git a/main/coverage-report/index.html b/main/coverage-report/index.html index 1e9f8e02bc..b0d4852d67 100644 --- a/main/coverage-report/index.html +++ b/main/coverage-report/index.html @@ -94,7 +94,7 @@ font-size: 11px; }
lst <- list(...)
maxdim <- max(lengths(lst))
res <- lapply(lst, rep, length.out = maxdim)
attr(res, "maxdim") <- maxdim
return(res)
if (missing(sides)) {
sides <- match.arg(sides)
if (missing(method)) {
iBinomDiffCI <- function(x1, n1, x2, n2, conf.level, sides, method) { # nolint
if (sides != "two.sided") {
alpha <- 1 - conf.level
kappa <- stats::qnorm(1 - alpha / 2)
p1_hat <- x1 / n1
p2_hat <- x2 / n2
est <- p1_hat - p2_hat
switch(method,
wald = {
waldcc = {
vd <- p1_hat * (1 - p1_hat) / n1 + p2_hat * (1 - p2_hat) / n2
term2 <- kappa * sqrt(vd)
term2 <- term2 + 0.5 * (1 / n1 + 1 / n2)
ci_lwr <- max(-1, est - term2)
ci_upr <- min(1, est + term2)
ac = {
exact = {
score = {
scorecc = {
mee = {
blj = {
ha = {
mn = {
beal = {
hal = {
jp = {
ci <- c(
est = est, lwr.ci = min(ci_lwr, ci_upr),
upr.ci = max(ci_lwr, ci_upr)
if (sides == "left") {
} else if (sides == "right") {
return(ci)
method <- match.arg(arg = method, several.ok = TRUE)
sides <- match.arg(arg = sides, several.ok = TRUE)
lst <- h_recycle(
x1 = x1, n1 = n1, x2 = x2, n2 = n2, conf.level = conf.level,
sides = sides, method = method
res <- t(sapply(1:attr(lst, "maxdim"), function(i) {
iBinomDiffCI(
x1 = lst$x1[i],
n1 = lst$n1[i], x2 = lst$x2[i], n2 = lst$n2[i], conf.level = lst$conf.level[i],
sides = lst$sides[i], method = lst$method[i]
lgn <- h_recycle(x1 = if (is.null(names(x1))) {
paste("x1", seq_along(x1), sep = ".")
}, n1 = if (is.null(names(n1))) {
paste("n1", seq_along(n1), sep = ".")
}, x2 = if (is.null(names(x2))) {
paste("x2", seq_along(x2), sep = ".")
}, n2 = if (is.null(names(n2))) {
paste("n2", seq_along(n2), sep = ".")
}, conf.level = conf.level, sides = sides, method = method)
xn <- apply(as.data.frame(lgn[sapply(lgn, function(x) {
length(unique(x)) !=
1
})]), 1, paste, collapse = ":")
rownames(res) <- xn
return(res)
checkmate::assert_string(.var)
assert_df_with_variables(df, list(tte = .var, is_event = is_event))
checkmate::assert_numeric(df[[.var]], min.len = 1, any.missing = FALSE)
checkmate::assert_number(time_point)
checkmate::assert_logical(df[[is_event]], min.len = 1, any.missing = FALSE)
conf_type <- control$conf_type
conf_level <- control$conf_level
formula <- stats::as.formula(paste0("survival::Surv(", .var, ", ", is_event, ") ~ 1"))
srv_fit <- survival::survfit(
formula = formula,
data = df,
conf.int = conf_level,
conf.type = conf_type
s_srv_fit <- summary(srv_fit, times = time_point, extend = TRUE)
df_srv_fit <- as.data.frame(s_srv_fit[c("time", "n.risk", "surv", "lower", "upper", "std.err")])
if (df_srv_fit[["n.risk"]] == 0) {
pt_at_risk <- df_srv_fit$n.risk
event_free_rate <- df_srv_fit$surv
rate_se <- df_srv_fit$std.err
rate_ci <- c(df_srv_fit$lower, df_srv_fit$upper)
list(
pt_at_risk = formatters::with_label(pt_at_risk, "Patients remaining at risk"),
event_free_rate = formatters::with_label(event_free_rate * 100, "Event Free Rate (%)"),
rate_se = formatters::with_label(rate_se * 100, "Standard Error of Event Free Rate"),
rate_ci = formatters::with_label(rate_ci * 100, f_conf_level(conf_level))
c(rate_diff = 1L, rate_diff_ci = 2L, ztest_pval = 2L)
method <- match.arg(method)
checkmate::assert_string(table_names_suffix)
f <- list(
surv = c("pt_at_risk", "event_free_rate", "rate_se", "rate_ci"),
surv_diff = c("rate_diff", "rate_diff_ci", "ztest_pval")
.stats <- h_split_param(.stats, .stats, f = f)
.formats <- h_split_param(.formats, names(.formats), f = f)
.labels <- h_split_param(.labels, names(.labels), f = f)
.indent_mods <- h_split_param(.indent_mods, names(.indent_mods), f = f)
afun_surv <- make_afun(
a_surv_timepoint,
.stats = .stats$surv,
.formats = .formats$surv,
.labels = .labels$surv,
.indent_mods = .indent_mods$surv
afun_surv_diff <- make_afun(
a_surv_timepoint_diff,
.stats = .stats$surv_diff,
.formats = .formats$surv_diff,
.labels = .labels$surv_diff,
.indent_mods = .indent_mods$surv_diff
time_point <- list(...)$time_point
for (i in seq_along(time_point)) {
tpt <- time_point[i]
if (method %in% c("surv", "both")) {
lyt <- analyze(
lyt,
vars,
var_labels = paste(tpt, var_labels),
table_names = paste0("surv_", tpt, table_names_suffix),
show_labels = show_labels,
afun = afun_surv,
na_str = na_str,
nested = nested,
extra_args = list(
is_event = list(...)$is_event,
control = list(...)$control,
time_point = tpt
if (method %in% c("surv_diff", "both")) {
lyt <- analyze(
lyt,
vars,
var_labels = paste(tpt, var_labels),
table_names = paste0("surv_diff_", tpt, table_names_suffix),
show_labels = ifelse(method == "both", "hidden", show_labels),
afun = afun_surv_diff,
na_str = na_str,
nested = nested,
extra_args = list(
is_event = list(...)$is_event,
control = list(...)$control,
time_point = tpt
lyt
checkmate::assert_numeric(x)
if (finite) {
} else if (na.rm) {
if (length(x) == 0) {
rval <- c(min(x, na.rm = FALSE), max(x, na.rm = FALSE))
return(rval)
assert_proportion_value(conf_level)
paste0(conf_level * 100, "% CI")
checkmate::assert_numeric(test_mean, len = 1)
paste0("p-value (H0: mean = ", test_mean, ")")
checkmate::assert_numeric(x)
x / 30.4375
sum(!is.na(x))
y <- lapply(f, function(x) param[value %in% x])
lapply(y, function(x) if (length(x) == 0) NULL else x)
checkmate::assert_vector(quantiles, len = 2)
checkmate::assert_int(quantile_type, lower = 1, upper = 9)
checkmate::assert_numeric(test_mean)
lapply(quantiles, assert_proportion_value)
assert_proportion_value(conf_level)
list(conf_level = conf_level, quantiles = quantiles, quantile_type = quantile_type, test_mean = test_mean)
checkmate::assert_flag(na.rm)
UseMethod("s_summary", x)
checkmate::assert_numeric(x)
if (na.rm) {
x <- x[!is.na(x)]
y <- list()
y$n <- c("n" = length(x))
y$sum <- c("sum" = ifelse(length(x) == 0, NA_real_, sum(x, na.rm = FALSE)))
y$mean <- c("mean" = ifelse(length(x) == 0, NA_real_, mean(x, na.rm = FALSE)))
y$sd <- c("sd" = stats::sd(x, na.rm = FALSE))
y$se <- c("se" = stats::sd(x, na.rm = FALSE) / sqrt(length(stats::na.omit(x))))
y$mean_sd <- c(y$mean, "sd" = stats::sd(x, na.rm = FALSE))
y$mean_se <- c(y$mean, y$se)
mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE)
y$mean_ci <- formatters::with_label(mean_ci, paste("Mean", f_conf_level(control$conf_level)))
mean_sei <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE) / sqrt(y$n)
names(mean_sei) <- c("mean_sei_lwr", "mean_sei_upr")
y$mean_sei <- formatters::with_label(mean_sei, "Mean -/+ 1xSE")
mean_sdi <- y$mean[[1]] + c(-1, 1) * stats::sd(x, na.rm = FALSE)
names(mean_sdi) <- c("mean_sdi_lwr", "mean_sdi_upr")
y$mean_sdi <- formatters::with_label(mean_sdi, "Mean -/+ 1xSD")
mean_pval <- stat_mean_pval(x, test_mean = control$test_mean, na.rm = FALSE, n_min = 2)
y$mean_pval <- formatters::with_label(mean_pval, paste("Mean", f_pval(control$test_mean)))
y$median <- c("median" = stats::median(x, na.rm = FALSE))
y$mad <- c("mad" = stats::median(x - y$median, na.rm = FALSE))
median_ci <- stat_median_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE)
y$median_ci <- formatters::with_label(median_ci, paste("Median", f_conf_level(control$conf_level)))
q <- control$quantiles
if (any(is.na(x))) {
qnts <- stats::quantile(x, probs = q, type = control$quantile_type, na.rm = FALSE)
names(qnts) <- paste("quantile", q, sep = "_")
y$quantiles <- formatters::with_label(qnts, paste0(paste(paste0(q * 100, "%"), collapse = " and "), "-ile"))
y$iqr <- c("iqr" = ifelse(
any(is.na(x)),
NA_real_,
stats::IQR(x, na.rm = FALSE, type = control$quantile_type)
y$range <- stats::setNames(range_noinf(x, na.rm = FALSE), c("min", "max"))
y$min <- y$range[1]
y$max <- y$range[2]
y$median_range <- formatters::with_label(c(y$median, y$range), "Median (Min - Max)")
y$cv <- c("cv" = unname(y$sd) / unname(y$mean) * 100)
x_no_negative_vals <- x
x_no_negative_vals[x_no_negative_vals <= 0] <- NA
y$geom_mean <- c("geom_mean" = exp(mean(log(x_no_negative_vals), na.rm = FALSE)))
geom_mean_ci <- stat_mean_ci(x, conf_level = control$conf_level, na.rm = FALSE, gg_helper = FALSE, geom_mean = TRUE)
y$geom_mean_ci <- formatters::with_label(geom_mean_ci, paste("Geometric Mean", f_conf_level(control$conf_level)))
y$geom_cv <- c("geom_cv" = sqrt(exp(stats::sd(log(x_no_negative_vals), na.rm = FALSE) ^ 2) - 1) * 100) # styler: off
y
assert_valid_factor(x)
denom <- match.arg(denom)
if (na.rm) {
x <- x[!is.na(x)] %>% fct_discard("<Missing>")
y <- list()
y$n <- length(x)
y$count <- as.list(table(x, useNA = "ifany"))
dn <- switch(denom,
n = length(x),
N_row = .N_row,
N_col = .N_col
y$count_fraction <- lapply(
y$count,
function(x) {
c(x, ifelse(dn > 0, x / dn, 0))
y$n_blq <- sum(grepl("BLQ|LTR|<[1-9]|<PCLLOQ", x))
y
if (lifecycle::is_present(na_level)) {
if (is.numeric(x)) {
type <- "numeric"
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
.stats[grepl("^pval", .stats)] <- "pval" # tmp fix xxx
type <- "counts"
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
x_stats <- if (!compare) {
s_summary(x = x, .N_col = .N_col, .N_row = .N_row, na.rm = na.rm, ...)
s_compare(
x = x, .N_col = .N_col, .N_row = .N_row, na.rm = na.rm, .ref_group = .ref_group, .in_ref_col = .in_ref_col, ...
met_grp <- paste0(c("analyze_vars", type), collapse = "_")
.stats <- get_stats(met_grp, stats_in = .stats, add_pval = compare)
.formats <- get_formats_from_stats(.stats, .formats)
.labels <- get_labels_from_stats(.stats, .labels)
indent_mods_custom <- .indent_mods
.indent_mods <- stats::setNames(rep(0L, length(.stats)), .stats)
if (!is.null(indent_mods_custom)) {
x_stats <- x_stats[.stats]
if (is.numeric(x)) {
default_labels <- get_labels_from_stats(.stats)
for (i in intersect(.stats, c("mean_ci", "mean_pval", "median_ci", "quantiles"))) {
if (is.factor(x) || is.character(x)) {
x_ungrp <- ungroup_stats(x_stats, .formats, .labels, .indent_mods)
x_stats <- x_ungrp[["x"]]
.formats <- x_ungrp[[".formats"]]
.labels <- gsub("fill-na-level", "NA", x_ungrp[[".labels"]])
.indent_mods <- x_ungrp[[".indent_mods"]]
fmt_is_auto <- vapply(.formats, function(ii) is.character(ii) && ii == "auto", logical(1))
if (any(fmt_is_auto)) {
in_rows(
.list = x_stats,
.formats = .formats,
.names = .labels,
.labels = .labels,
.indent_mods = .indent_mods,
.format_na_strs = na_str
if (lifecycle::is_present(na_level)) {
extra_args <- list(.stats = .stats, na.rm = na.rm, na_str = na_str, ...)
analyze(
lyt = lyt,
vars = vars,
var_labels = var_labels,
afun = a_summary,
na_str = na_str,
nested = nested,
extra_args = extra_args,
inclNAs = TRUE,
show_labels = show_labels,
table_names = table_names,
section_div = section_div
checkmate::assert_atomic(x)
checkmate::assert_string(x_name)
checkmate::assert_string(na_level)
checkmate::assert_flag(verbose)
if (is.factor(x)) {
return(x)
checkmate::assert_factor(x)
checkmate::assert_character(discard, any.missing = FALSE)
new_obs <- x[!(x %in% discard)]
new_levels <- setdiff(levels(x), discard)
factor(new_obs, levels = new_levels)
checkmate::assert_list(x)
empty_pval <- "pval" %in% names(x) && length(x[["pval"]]) == 0
empty_pval_counts <- "pval_counts" %in% names(x) && length(x[["pval_counts"]]) == 0
x <- unlist(x, recursive = FALSE)
.stats <- names(x)
.formats <- lapply(.stats, function(x) {
.formats[[if (!grepl("\\.", x)) x else regmatches(x, regexpr("\\.", x), invert = TRUE)[[1]][1]]]
.indent_mods <- sapply(.stats, function(x) {
.indent_mods[[if (!grepl("\\.", x)) x else regmatches(x, regexpr("\\.", x), invert = TRUE)[[1]][1]]]
.labels <- sapply(.stats, function(x) {
if (!grepl("\\.", x)) .labels[[x]] else regmatches(x, regexpr("\\.", x), invert = TRUE)[[1]][2]
list(
x = x,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
afun <- make_afun(
a_coxph_pairwise,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
var_labels = var_labels,
show_labels = show_labels,
table_names = table_names,
afun = afun,
na_str = na_str,
nested = nested,
extra_args = list(...)
arm <- variables$arm
covariates <- variables$covariates
offset <- .df_row[[variables$offset]]
formula <- stats::as.formula(paste0(
.var, " ~ ",
paste(covariates, collapse = " + "),
arm
glm_fit <- stats::glm(
formula = formula,
offset = offset,
data = .df_row,
family = stats::poisson(link = "log")
emmeans_fit <- emmeans::emmeans(
glm_fit,
specs = arm,
data = .df_row,
type = "response",
offset = 0,
weights = weights
list(
glm_fit = glm_fit,
emmeans_fit = emmeans_fit
if (distribution == "negbin") {
switch(distribution,
poisson = h_glm_poisson(.var, .df_row, variables, weights),
afun <- make_afun(
a_glm_count,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
var_labels = var_labels,
show_labels = show_labels,
table_names = table_names,
afun = afun,
na_str = na_str,
nested = nested,
extra_args = list(...)
if (na.rm) {
n <- length(x)
if (!geom_mean) {
m <- mean(x)
negative_values_exist <- any(is.na(x[!is.na(x)]) <- x[!is.na(x)] <= 0)
if (negative_values_exist) {
x <- log(x)
m <- mean(x)
if (n < n_min || is.na(m)) {
hci <- stats::qt((1 + conf_level) / 2, df = n - 1) * stats::sd(x) / sqrt(n)
ci <- c(mean_ci_lwr = m - hci, mean_ci_upr = m + hci)
if (geom_mean) {
ci <- exp(ci)
if (gg_helper) {
return(ci)
x <- unname(x)
if (na.rm) {
n <- length(x)
med <- stats::median(x)
k <- stats::qbinom(p = (1 - conf_level) / 2, size = n, prob = 0.5, lower.tail = TRUE)
if (k == 0 || is.na(med)) {
x_sort <- sort(x)
ci <- c(median_ci_lwr = x_sort[k], median_ci_upr = x_sort[n - k + 1])
empir_conf_level <- 1 - 2 * stats::pbinom(k - 1, size = n, prob = 0.5)
if (gg_helper) {
attr(ci, "conf_level") <- empir_conf_level
return(ci)
if (na.rm) {
n <- length(x)
x_mean <- mean(x)
x_sd <- stats::sd(x)
if (n < n_min) {
x_se <- stats::sd(x) / sqrt(n)
ttest <- (x_mean - test_mean) / x_se
pv <- c(p_value = 2 * stats::pt(-abs(ttest), df = n - 1))
return(pv)
checkmate::assert_character(method_groups)
checkmate::assert_character(stats_in, null.ok = TRUE)
checkmate::assert_flag(add_pval)
if (any(method_groups == "analyze_vars")) {
type_tmp <- ifelse(any(grepl("counts", method_groups)), "counts", "numeric") # for pval checks
out <- NULL
for (mgi in method_groups) {
out_tmp <- switch(mgi,
"count_occurrences" = c("count", "count_fraction", "count_fraction_fixed_dp", "fraction"),
"summarize_num_patients" = c("unique", "nonunique", "unique_count"),
"analyze_vars_counts" = c("n", "count", "count_fraction", "n_blq"),
"analyze_vars_numeric" = c(
"n", "sum", "mean", "sd", "se", "mean_sd", "mean_se", "mean_ci", "mean_sei",
"mean_sdi", "mean_pval", "median", "mad", "median_ci", "quantiles", "iqr",
"range", "min", "max", "median_range", "cv", "geom_mean", "geom_mean_ci",
"geom_cv"
stop(
"The selected method group (", mgi, ") has no default statistical method."
out <- unique(c(out, out_tmp))
if (!is.null(stats_in) && any(grepl("^pval", stats_in))) {
stats_in_pval_value <- stats_in[grepl("^pval", stats_in)]
checkmate::assert_choice(stats_in_pval_value, c("pval", "pval_counts"))
if (any(grepl("counts", method_groups)) && stats_in_pval_value != "pval_counts" ||
any(grepl("numeric", method_groups)) && stats_in_pval_value != "pval") { # nolint
add_pval <- TRUE
if (isTRUE(add_pval)) {
if (any(grepl("counts", method_groups))) {
out <- unique(c(out, "pval"))
if (!is.null(stats_in)) {
out <- intersect(stats_in, out) # It orders them too
if (length(out) == 0) {
out
checkmate::assert_character(stats, min.len = 1)
if (checkmate::test_list(formats_in, null.ok = TRUE)) {
checkmate::assert_list(formats_in, null.ok = TRUE)
which_fmt <- match(stats, names(tern_default_formats))
ret <- vector("list", length = length(stats)) # Returning a list is simpler
ret[!is.na(which_fmt)] <- tern_default_formats[which_fmt[!is.na(which_fmt)]]
out <- setNames(ret, stats)
if (!is.null(formats_in)) {
out
checkmate::assert_character(stats, min.len = 1)
checkmate::assert_character(row_nms, null.ok = TRUE)
if (checkmate::test_list(labels_in, null.ok = TRUE)) {
checkmate::assert_list(labels_in, null.ok = TRUE)
if (!is.null(row_nms)) {
which_lbl <- match(stats, names(tern_default_labels))
ret <- vector("character", length = length(stats)) # it needs to be a character vector
ret[!is.na(which_lbl)] <- tern_default_labels[which_lbl[!is.na(which_lbl)]]
out <- setNames(ret, stats)
if (!is.null(labels_in)) {
out
UseMethod("s_compare", x)
checkmate::assert_numeric(x)
checkmate::assert_numeric(.ref_group)
checkmate::assert_flag(.in_ref_col)
y <- s_summary.numeric(x = x, ...)
y$pval <- if (!.in_ref_col && n_available(x) > 1 && n_available(.ref_group) > 1) {
stats::t.test(x, .ref_group)$p.value
character()
y
if (lifecycle::is_present(na_level)) {
extra_args <- list(.stats = .stats, na.rm = na.rm, na_str = na_str, compare = TRUE, ...)
analyze(
lyt = lyt,
vars = vars,
var_labels = var_labels,
afun = a_summary,
na_str = na_str,
nested = nested,
extra_args = extra_args,
inclNAs = TRUE,
show_labels = show_labels,
table_names = table_names,
section_div = section_div
checkmate::assert_string(.var)
checkmate::assert_list(variables)
checkmate::assert_subset(names(variables), c("arm", "covariates"))
assert_df_with_variables(.df_row, list(rsp = .var))
arm <- variables$arm
covariates <- variables$covariates
if (!is.null(covariates) && length(covariates) > 0) {
covariates_part <- paste(covariates, collapse = " + ")
if (covariates_part != "") {
formula <- stats::as.formula(paste0(.var, " ~ ", arm))
if (is.null(interaction_item)) {
specs <- arm
lm_fit <- stats::lm(
formula = formula,
data = .df_row
emmeans_fit <- emmeans::emmeans(
lm_fit,
specs = specs,
data = .df_row
emmeans_fit
afun <- make_afun(
a_ancova,
interaction_y = interaction_y,
interaction_item = interaction_item,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
var_labels = var_labels,
show_labels = show_labels,
table_names = table_names,
afun = afun,
na_str = na_str,
nested = nested,
extra_args = list(...)
afun <- make_afun(
a_proportion_diff,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
afun = afun,
var_labels = var_labels,
na_str = na_str,
nested = nested,
extra_args = list(...),
show_labels = show_labels,
table_names = table_names
checkmate::assert_logical(rsp, any.missing = FALSE)
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2)
checkmate::assert_number(conf_level, lower = 0, upper = 1)
checkmate::assert_flag(correct, null.ok = TRUE)
if (!is.null(strata)) {
invisible()
label <- paste0(conf_level * 100, "% CI")
if (long) {
method_part <- switch(method,
"cmh" = "CMH, without correction",
"waldcc" = "Wald, with correction",
"wald" = "Wald, without correction",
"ha" = "Anderson-Hauck",
"newcombe" = "Newcombe, without correction",
"newcombecc" = "Newcombe, with correction",
"strat_newcombe" = "Stratified Newcombe, without correction",
"strat_newcombecc" = "Stratified Newcombe, with correction",
stop(paste(method, "does not have a description"))
paste0(label, " (", method_part, ")")
if (isTRUE(correct)) {
mthd <- "waldcc"
grp <- as_factor_keep_attributes(grp)
check_diff_prop_ci(
rsp = rsp, grp = grp, conf_level = conf_level, correct = correct
checkmate::assert_logical(rsp, any.missing = FALSE)
checkmate::assert_factor(grp, len = length(rsp), any.missing = FALSE, n.levels = 2)
tbl <- table(grp, factor(rsp, levels = c(TRUE, FALSE)))
diff_ci <- desctools_binom(
x1 = tbl[2], n1 = sum(tbl[2], tbl[4]),
x2 = tbl[1], n2 = sum(tbl[1], tbl[3]),
conf.level = conf_level,
method = mthd
list(
"diff" = unname(diff_ci[, "est"]),
"diff_ci" = unname(diff_ci[, c("lwr.ci", "upr.ci")])
pval_method <- match.arg(pval_method)
ties <- match.arg(ties)
assert_proportion_value(conf_level)
list(pval_method = pval_method, ties = ties, conf_level = conf_level)
conf_type <- match.arg(conf_type)
assert_proportion_value(conf_level)
list(
conf_level = conf_level,
conf_type = conf_type
x <- Filter(Negate(is.null), x)
res <- checkmate::check_list(x,
names = "named",
min.len = 1,
any.missing = FALSE,
types = "character"
if (isTRUE(res)) {
res <- checkmate::check_character(unlist(x), min.chars = 1)
return(res)
checkmate::assert_data_frame(df)
assert_list_of_variables(variables)
err_flag <- all(unlist(variables) %in% colnames(df))
checkmate::assert_flag(err_flag)
if (isFALSE(err_flag)) {
if (!is.null(na_level)) {
return(TRUE)
checkmate::assert_int(min.levels, lower = 1)
res <- checkmate::check_factor(x,
min.levels = min.levels,
null.ok = null.ok,
max.levels = max.levels,
any.missing = any.missing,
n.levels = n.levels
if (isTRUE(res)) {
res <- checkmate::check_character(levels(x), min.chars = 1)
return(res)
res <- check_df_with_variables(df, variables, na_level)
if (isTRUE(res)) {
res <- lapply(
X = as.list(df)[unlist(variables)],
FUN = check_valid_factor,
min.levels = min.levels,
max.levels = max.levels,
any.missing = any.missing
res_lo <- unlist(vapply(res, Negate(isTRUE), logical(1)))
if (any(res_lo)) {
res <- TRUE
return(res)
checkmate::assert_number(x, lower = 0, upper = 1)
checkmate::assert_flag(include_boundaries)
if (isFALSE(include_boundaries)) {
checkmate::assert_true(x > 0)
checkmate::assert_true(x < 1)
afun <- make_afun(
a_odds_ratio,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
afun = afun,
na_str = na_str,
nested = nested,
extra_args = list(...),
show_labels = show_labels,
table_names = table_names
checkmate::assert_logical(data$rsp)
assert_proportion_value(conf_level)
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp"))
checkmate::assert_multi_class(data$grp, classes = c("factor", "character"))
data$grp <- as_factor_keep_attributes(data$grp)
assert_df_with_factors(data, list(val = "grp"), min.levels = 2, max.levels = 2)
formula <- stats::as.formula("rsp ~ grp")
model_fit <- stats::glm(
formula = formula, data = data,
family = stats::binomial(link = "logit")
or <- exp(stats::coef(model_fit)[-1])
or_ci <- exp(
stats::confint.default(model_fit, level = conf_level)[-1, , drop = FALSE]
values <- stats::setNames(c(or, or_ci), c("est", "lcl", "ucl"))
n_tot <- stats::setNames(nrow(model_fit$model), "n_tot")
list(or_ci = values, n_tot = n_tot)
checkmate::assert_string(method)
meth_part <- switch(method,
"schouten" = "Chi-Squared Test with Schouten Correction",
"chisq" = "Chi-Squared Test",
"cmh" = "Cochran-Mantel-Haenszel Test",
"fisher" = "Fisher's Exact Test",
stop(paste(method, "does not have a description"))
paste0("p-value (", meth_part, ")")
afun <- make_afun(
a_test_proportion_diff,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
analyze(
lyt,
vars,
afun = afun,
var_labels = var_labels,
na_str = na_str,
nested = nested,
extra_args = list(...),
show_labels = show_labels,
table_names = table_names
checkmate::assert_integer(c(ncol(tbl), nrow(tbl)), lower = 2, upper = 2)
tbl <- tbl[, c("TRUE", "FALSE")]
if (any(colSums(tbl) == 0)) {
stats::prop.test(tbl, correct = FALSE)$p.value
1 | ++ |
+ #' Custom Split Functions+ |
+
2 | ++ |
+ #'+ |
+
3 | ++ |
+ #' @description `r lifecycle::badge("stable")`+ |
+
4 | ++ |
+ #'+ |
+
5 | ++ |
+ #' Collection of useful functions that are expanding on the core list of functions+ |
+
6 | ++ |
+ #' provided by `rtables`. See [rtables::custom_split_funs] and [rtables::make_split_fun()]+ |
+
7 | ++ |
+ #' for more information on how to make a custom split function. All these functions+ |
+
8 | ++ |
+ #' work with [split_rows_by()] argument `split_fun` to modify the way the split+ |
+
9 | ++ |
+ #' happens. For other split functions, consider consulting [`rtables::split_funcs`].+ |
+
10 | ++ |
+ #'+ |
+
11 | ++ |
+ #' @seealso [rtables::make_split_fun()]+ |
+
12 | ++ |
+ #'+ |
+
13 | ++ |
+ #' @name utils_split_funs+ |
+
14 | ++ |
+ NULL+ |
+
15 | ++ | + + | +
16 | ++ |
+ #' @describeIn utils_split_funs split function to place reference group facet at a specific position+ |
+
17 | ++ |
+ #' during post-processing stage.+ |
+
18 | ++ |
+ #'+ |
+
19 | ++ |
+ #' @param position (`string` or `integer`)\cr should it be `"first"` or `"last"` or in a specific position?+ |
+
20 | ++ |
+ #'+ |
+
21 | ++ |
+ #' @return+ |
+
22 | ++ |
+ #' * `ref_group_position` returns an utility function that puts the reference group+ |
+
23 | ++ |
+ #' as first, last or at a certain position and needs to be assigned to `split_fun`.+ |
+
24 | ++ |
+ #'+ |
+
25 | ++ |
+ #' @examples+ |
+
26 | ++ |
+ #' library(dplyr)+ |
+
27 | ++ |
+ #'+ |
+
28 | ++ |
+ #' dat <- data.frame(+ |
+
29 | ++ |
+ #' x = factor(letters[1:5], levels = letters[5:1]),+ |
+
30 | ++ |
+ #' y = 1:5+ |
+
31 | ++ |
+ #' )+ |
+
32 | ++ |
+ #'+ |
+
33 | ++ |
+ #' # With rtables layout functions+ |
+
34 | ++ |
+ #' basic_table() %>%+ |
+
35 | ++ |
+ #' split_cols_by("x", ref_group = "c", split_fun = ref_group_position("last")) %>%+ |
+
36 | ++ |
+ #' analyze("y") %>%+ |
+
37 | ++ |
+ #' build_table(dat)+ |
+
38 | ++ |
+ #'+ |
+
39 | ++ |
+ #' # With tern layout funcitons+ |
+
40 | ++ |
+ #' adtte_f <- tern_ex_adtte %>%+ |
+
41 | ++ |
+ #' filter(PARAMCD == "OS") %>%+ |
+
42 | ++ |
+ #' mutate(+ |
+
43 | ++ |
+ #' AVAL = day2month(AVAL),+ |
+
44 | ++ |
+ #' is_event = CNSR == 0+ |
+
45 | ++ |
+ #' )+ |
+
46 | ++ |
+ #'+ |
+
47 | ++ |
+ #' basic_table() %>%+ |
+
48 | ++ |
+ #' split_cols_by(var = "ARMCD", ref_group = "ARM B", split_fun = ref_group_position("first")) %>%+ |
+
49 | ++ |
+ #' add_colcounts() %>%+ |
+
50 | ++ |
+ #' surv_time(+ |
+
51 | ++ |
+ #' vars = "AVAL",+ |
+
52 | ++ |
+ #' var_labels = "Survival Time (Months)",+ |
+
53 | ++ |
+ #' is_event = "is_event",+ |
+
54 | ++ |
+ #' ) %>%+ |
+
55 | ++ |
+ #' build_table(df = adtte_f)+ |
+
56 | ++ |
+ #'+ |
+
57 | ++ |
+ #' basic_table() %>%+ |
+
58 | ++ |
+ #' split_cols_by(var = "ARMCD", ref_group = "ARM B", split_fun = ref_group_position(2)) %>%+ |
+
59 | ++ |
+ #' add_colcounts() %>%+ |
+
60 | ++ |
+ #' surv_time(+ |
+
61 | ++ |
+ #' vars = "AVAL",+ |
+
62 | ++ |
+ #' var_labels = "Survival Time (Months)",+ |
+
63 | ++ |
+ #' is_event = "is_event",+ |
+
64 | ++ |
+ #' ) %>%+ |
+
65 | ++ |
+ #' build_table(df = adtte_f)+ |
+
66 | ++ |
+ #'+ |
+
67 | ++ |
+ #' @export+ |
+
68 | ++ |
+ ref_group_position <- function(position = "first") {+ |
+
69 | +7x | +
+ make_split_fun(+ |
+
70 | +7x | +
+ post = list(+ |
+
71 | +7x | +
+ function(splret, spl, fulldf) {+ |
+
72 | +19x | +
+ if (!"ref_group_value" %in% methods::slotNames(spl)) {+ |
+
73 | +1x | +
+ stop("Reference group is undefined.")+ |
+
74 | ++ |
+ }+ |
+
75 | ++ | + + | +
76 | +18x | +
+ spl_var <- rtables:::spl_payload(spl)+ |
+
77 | +18x | +
+ fulldf[[spl_var]] <- factor(fulldf[[spl_var]])+ |
+
78 | +18x | +
+ init_lvls <- levels(fulldf[[spl_var]])+ |
+
79 | ++ | + + | +
80 | +18x | +
+ if (!all(names(splret$values) %in% init_lvls)) {+ |
+
81 | +! | +
+ stop("This split function does not work with combination facets.")+ |
+
82 | ++ |
+ }+ |
+
83 | ++ | + + | +
84 | +18x | +
+ ref_group_pos <- which(init_lvls == rtables:::spl_ref_group(spl))+ |
+
85 | +18x | +
+ pos_choices <- c("first", "last")+ |
+
86 | +18x | +
+ if (checkmate::test_choice(position, pos_choices) && position == "first") {+ |
+
87 | +3x | +
+ pos <- 0+ |
+
88 | +15x | +
+ } else if (checkmate::test_choice(position, pos_choices) && position == "last") {+ |
+
89 | +12x | +
+ pos <- length(init_lvls)+ |
+
90 | +3x | +
+ } else if (checkmate::test_int(position, lower = 1, upper = length(init_lvls))) {+ |
+
91 | +3x | +
+ pos <- position - 1+ |
+
92 | ++ |
+ } else {+ |
+
93 | +! | +
+ stop("Wrong input for ref group position. It must be 'first', 'last', or a integer.")+ |
+
94 | ++ |
+ }+ |
+
95 | ++ | + + | +
96 | +18x | +
+ reord_lvls <- append(init_lvls[-ref_group_pos], init_lvls[ref_group_pos], after = pos)+ |
+
97 | +18x | +
+ ord <- match(reord_lvls, names(splret$values))+ |
+
98 | ++ | + + | +
99 | +18x | +
+ make_split_result(+ |
+
100 | +18x | +
+ splret$values[ord],+ |
+
101 | +18x | +
+ splret$datasplit[ord],+ |
+
102 | +18x | +
+ splret$labels[ord]+ |
+
103 | ++ |
+ )+ |
+
104 | ++ |
+ }+ |
+
105 | ++ |
+ )+ |
+
106 | ++ |
+ )+ |
+
107 | ++ |
+ }+ |
+
108 | ++ | + + | +
109 | ++ |
+ #' @describeIn utils_split_funs split function to change level order based on a `integer`+ |
+
110 | ++ |
+ #' vector or a `character` vector that represent the split variable's factor levels.+ |
+
111 | ++ |
+ #'+ |
+
112 | ++ |
+ #' @param order (`character` or `integer`)\cr vector of ordering indexes for the split facets.+ |
+
113 | ++ |
+ #'+ |
+
114 | ++ |
+ #' @return+ |
+
115 | ++ |
+ #' * `level_order` returns an utility function that changes the original levels' order,+ |
+
116 | ++ |
+ #' depending on input `order` and split levels.+ |
+
117 | ++ |
+ #'+ |
+
118 | ++ |
+ #' @examples+ |
+
119 | ++ |
+ #' # level_order --------+ |
+
120 | ++ |
+ #' # Even if default would bring ref_group first, the original order puts it last+ |
+
121 | ++ |
+ #' basic_table() %>%+ |
+
122 | ++ |
+ #' split_cols_by("Species", split_fun = level_order(c(1, 3, 2))) %>%+ |
+
123 | ++ |
+ #' analyze("Sepal.Length") %>%+ |
+
124 | ++ |
+ #' build_table(iris)+ |
+
125 | ++ |
+ #'+ |
+
126 | ++ |
+ #' # character vector+ |
+
127 | ++ |
+ #' new_order <- level_order(levels(iris$Species)[c(1, 3, 2)])+ |
+
128 | ++ |
+ #' basic_table() %>%+ |
+
129 | ++ |
+ #' split_cols_by("Species", ref_group = "virginica", split_fun = new_order) %>%+ |
+
130 | ++ |
+ #' analyze("Sepal.Length") %>%+ |
+
131 | ++ |
+ #' build_table(iris)+ |
+
132 | ++ |
+ #'+ |
+
133 | ++ |
+ #' @export+ |
+
134 | ++ |
+ level_order <- function(order) {+ |
+
135 | +2x | +
+ make_split_fun(+ |
+
136 | +2x | +
+ post = list(+ |
+
137 | +2x | +
+ function(splret, spl, fulldf) {+ |
+
138 | +4x | +
+ if (checkmate::test_integerish(order)) {+ |
+
139 | +1x | +
+ checkmate::assert_integerish(order, lower = 1, upper = length(splret$values))+ |
+
140 | +1x | +
+ ord <- order+ |
+
141 | ++ |
+ } else {+ |
+
142 | +3x | +
+ checkmate::assert_character(order, len = length(splret$values))+ |
+
143 | +3x | +
+ checkmate::assert_set_equal(order, names(splret$values), ordered = FALSE)+ |
+
144 | +3x | +
+ ord <- match(order, names(splret$values))+ |
+
145 | ++ |
+ }+ |
+
146 | +4x | +
+ make_split_result(+ |
+
147 | +4x | +
+ splret$values[ord],+ |
+
148 | +4x | +
+ splret$datasplit[ord],+ |
+
149 | +4x | +
+ splret$labels[ord]+ |
+
150 | ++ |
+ )+ |
+
151 | ++ |
+ }+ |
+
152 | ++ |
+ )+ |
+
153 | ++ |
+ )+ |
+
154 | ++ |
+ }+ |
+