Computes pairwise Cox proportional hazards tests and returns a gtsummary object. The table splits the results by comparison arms, presenting the p-value, Hazard Ratio, and 95% Confidence Interval in a stacked layout where statistics form the rows of the table.

tbl_coxph(data, model_formula, arm, ref_group = NULL)

Arguments

data

(data.frame)
A data.frame containing the survival data, including time, status, and the arm variable.

model_formula

(formula)
A formula object specifying the survival model, typically in the form Surv(time, status) ~ arm + covariates.

arm

(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.

ref_group

(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.

Value

A gtsummary object summarizing the pairwise Cox PH results.

Examples

# Setup sample survival data with 3 arms to test pairwise comparisons
library(survival)
surv_data <- survival::lung |>
  dplyr::mutate(
    arm = factor(sample(c("A", "B", "C"), dplyr::n(), replace = TRUE)),
    status = status - 1
  ) |>
  dplyr::filter(dplyr::if_all(dplyr::everything(), ~ !is.na(.)))

# Generate the gtsummary table
tbl_coxph(
  data = surv_data,
  model_formula = survival::Surv(time, status) ~ arm,
  arm = "arm",
  ref_group = "A"
)
B vs A
p-value (log-rank) 0.3499
Hazard Ratio 0.80
    95% CI (0.51, 1.27)
C vs A
p-value (log-rank) 0.3729
Hazard Ratio 0.82
    95% CI (0.54, 1.26)
# Setup sample survival data with 2 arms to test if table drops comparison # column surv_data_2arm <- survival::lung |> dplyr::mutate( arm = factor(sample(c("A", "B"), dplyr::n(), replace = TRUE)), status = status - 1 ) |> dplyr::filter(dplyr::if_all(dplyr::everything(), ~ !is.na(.))) # Generate the 2-arm gtsummary table tbl_coxph( data = surv_data_2arm, model_formula = survival::Surv(time, status) ~ arm, arm = "arm", ref_group = "A" )
p-value (log-rank) 0.2806
Hazard Ratio 1.22
    95% CI (0.85, 1.75)