Generates a gtsummary table from the pairwise comparison results created by get_cox_pairwise_df(). 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(pairwise_df)

Arguments

pairwise_df

(data.frame)
The results data.frame output generated by get_cox_pairwise_df(). Must contain rownames (comparison arms) and at least one statistic column: "HR", "95% CI", or "p-value (...)".

Value

A gtsummary object with additional class tbl_coxph.

Examples

# Setup sample survival data
library(survival)
surv_data <- 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(.)))

formula <- Surv(time, status) ~ arm

# Generate the pairwise statistics data.frame
pairwise_results <- get_cox_pairwise_df(
  model_formula = formula,
  data = surv_data,
  arm = "arm",
  ref_group = "A"
)

# Example 1: Full table
tbl_coxph(pairwise_df = pairwise_results)
B
p-value (log-rank) 0.4059
Hazard Ratio 0.82
    95% CI (0.52, 1.30)
C
p-value (log-rank) 0.2953
Hazard Ratio 0.80
    95% CI (0.52, 1.22)
# Example 2: Table with only HR and CI (p-value removed) pairwise_no_pval <- pairwise_results[, c("HR", "95% CI"), drop = FALSE] tbl_coxph(pairwise_df = pairwise_no_pval)
B
Hazard Ratio 0.82
    95% CI (0.52, 1.30)
C
Hazard Ratio 0.80
    95% CI (0.52, 1.22)
# Example 3: Table with only p-values pairwise_only_pval <- pairwise_results[, 3, drop = FALSE] tbl_coxph(pairwise_df = pairwise_only_pval)
B
p-value (log-rank) 0.4059
C
p-value (log-rank) 0.2953
# Example 4: Customize p-value precision # Pre-format the p-value column as character before passing to tbl_coxph(). # Character values are displayed as-is (no further formatting applied). pval_col <- grep("p-value", names(pairwise_results), value = TRUE) custom <- pairwise_results custom[[pval_col]] <- ifelse( custom[[pval_col]] < 0.001, "<0.001", sprintf("%.3f", custom[[pval_col]]) ) tbl_coxph(pairwise_df = custom)
B
p-value (log-rank) 0.406
Hazard Ratio 0.82
    95% CI (0.52, 1.30)
C
p-value (log-rank) 0.295
Hazard Ratio 0.80
    95% CI (0.52, 1.22)