R/get_cox_pairwise_df.R
get_cox_pairwise_df.RdThis function performs pairwise comparisons of treatment arms using the Cox Proportional Hazards model and calculates the corresponding log-rank p-value. Each comparison tests a non-reference group against a specified reference group.
(formula)
A formula object specifying the survival model,
typically in the form Surv(time, status) ~ arm + covariates.
(data.frame)
A data.frame containing the survival data,
including time, status, and the arm variable.
(character)
A single character string specifying the name of the column in data
that contains the grouping/treatment
arm variable. This column must be a factor
for correct stratification and comparison.
(character or NULL)
A single character string specifying the level of the arm variable
to be used as the reference group for
all pairwise comparisons. If NULL (the default),
the first unique level of the arm column is automatically
selected as the reference group.
(character)
A string specifying the method for tie handling in the Cox model.
Must be one of "exact", "efron", or "breslow".
Default is "exact".
(character)
A string specifying the type of test to compute the p-value.
Must be one of "log-rank", "gehan-breslow" (wilcoxon), "tarone", "peto",
"prentice" (modified peto), "fleming-harrington", or "likelihood-ratio".
Additional arguments passed to survival::coxph() and summary()
two arguments are supported:
conf.int (default 0.95) to adjust the CI level;
robust = TRUE to compute robust standard errors;
A data.frame with one row per comparison arm (stored as rownames).
The columns are:
HR: The Hazard Ratio formatted to two decimal places.
conf.int (default 0.95): Adjusts the confidence interval level.
Note: Changing this value dynamically updates the corresponding
column name in the output (e.g., passing 0.99 renames the column
to "99% CI").
p-value (<test>): The p-value from the selected test, where
<test> is the title-cased test name (e.g., "p-value (log-rank)").
The function iterates through each non-reference arm, subsets the data to the current arm and the reference arm, and then:
Fits a Cox model using survival::coxph().
Computes a p-value, which dispatches
to coin::logrank_test() for weighted log-rank variants or to
a nested survival::coxph() LRT for the likelihood-ratio test.
When robust = TRUE is specified, the Hazard Ratio and Confidence Intervals are
computed using robust sandwich standard errors. However, the p-values across all
tests (including the likelihood-ratio test) are calculated using standard,
non-robust model variances.
# Example data setup (assuming 'time' is event time, 'status'
# is event indicator (1=event), and 'arm' is the treatment group)
# for data handling
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
library(survival)
# Prepare data in a modern dplyr-friendly way
surv_data <- lung |>
mutate(
arm = factor(sample(c("A", "B", "C"), n(), replace = TRUE)),
status = status - 1 # Convert status to 0/1
) |>
filter(if_all(everything(), ~ !is.na(.)))
formula <- Surv(time, status) ~ arm
# Example 1: Default usage (ties = "exact", test = "log-rank")
results_default <- get_cox_pairwise_df(
model_formula = formula,
data = surv_data,
arm = "arm",
ref_group = "A"
)
print(results_default)
#> HR 95% CI p-value (log-rank)
#> B 1.25 (0.82, 1.92) 0.2928890
#> C 1.27 (0.80, 2.02) 0.3011297
# Example 2: Using Breslow ties and the Gehan-Breslow test
results_wilcoxon <- get_cox_pairwise_df(
model_formula = formula,
data = surv_data,
arm = "arm",
ref_group = "A",
ties = "breslow",
test = "gehan-breslow"
)
print(results_wilcoxon)
#> HR 95% CI p-value (Gehan-Breslow)
#> B 1.25 (0.82, 1.92) 0.6724338
#> C 1.27 (0.80, 2.02) 0.4594753
# Example 3: Using Efron ties and the Likelihood-Ratio test
results_lr <- get_cox_pairwise_df(
model_formula = formula,
data = surv_data,
arm = "arm",
ref_group = "A",
ties = "efron",
test = "likelihood-ratio"
)
print(results_lr)
#> HR 95% CI p-value (Likelihood-Ratio)
#> B 1.25 (0.82, 1.92) 0.5390843
#> C 1.27 (0.80, 2.02) 0.4362390