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.
get_cox_pairwise_df(model_formula, data, arm, ref_group = NULL)(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.
A data.frame with the results of the pairwise comparisons. The columns include:
arm: (rownames of the data.frame) The comparison arm (group) being tested against the reference group.
hr: The Hazard Ratio (HR) for the comparison arm vs. the reference arm, formatted to two decimal places.
ci: The 95% confidence interval for the HR, presented as a string in the format "(lower, upper)", with
values formatted to two decimal places.
pval: The log-rank p-value for the comparison.
The function iterates through each unique arm (excluding the reference group). For each iteration, it filters the data to include only the current comparison arm and the reference arm, and then:
Fits a Cox model using survival::coxph.
Performs a log-rank test using survival::survdiff.
The Hazard Ratio and its 95% confidence interval are extracted from the Cox model summary, and the p-value is extracted from the log-rank test.
annotate_gg_km(), gg_km(), and the survival package functions survival::coxph and
survival::survdiff.
# Example data setup (assuming 'time' is event time, 'status' is event indicator (1=event),
# and 'arm' is the treatment group)
library(dplyr) # For better data handling
#>
#> 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
# Prepare data in a modern dplyr-friendly way
surv_data <- survival::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 <- survival::Surv(time, status) ~ arm
results_tbl <- get_cox_pairwise_df(
model_formula = formula,
data = surv_data,
arm = "arm",
ref_group = "A"
)
print(results_tbl)
#> HR 95% CI p-value (log-rank)
#> B 0.85 (0.55, 1.32) 0.4710072
#> C NA (NA, NA) 0.7912945