Add the test of the Proportional Hazards Assumption of a Cox Regression to gtsummary table

Viewed 103

I would like to add a column with the p-value of the proportional hazards assumption test of a Cox regression to a gtsummary table. I don't think this option is available directly with the package. Here is an example using the survival package :

tbl_regression_ex1 <-
    coxph(Surv(ttdeath, death) ~ age + marker, trial) %>%
    tbl_regression(exponentiate = TRUE)
zph <- coxph(Surv(ttdeath, death) ~ age + marker, trial)
cox.zph(zph, terms = FALSE, global = FALSE)

Thanks in advance

1 Answers

You can use the modify_table_body() function to merge any additional results you'd like into the main tbl_regression() table. Example below!

library(gtsummary)
library(survival)
packageVersion("gtsummary")
#> [1] '1.5.2'

mod <- coxph(Surv(ttdeath, death) ~ age + marker, trial)

# convert test results into data frame to merge with tbl_regressoin table
ph_test <- 
  cox.zph(mod, terms = FALSE, global = FALSE) %>%
  purrr::pluck("table") %>%
  as.data.frame() %>%
  tibble::rownames_to_column("variable") %>%
  tibble::as_tibble() %>%
  select(variable, ph.test_p.value = p) %>%
  dplyr::mutate(
    # add a column for merging with gtsummary table
    row_type = "label",
    # format p-value
    ph.test_p.value = style_pvalue(ph.test_p.value)
  )
ph_test
#> # A tibble: 2 x 3
#>   variable ph.test_p.value row_type
#>   <chr>    <chr>           <chr>   
#> 1 age      0.8             label   
#> 2 marker   0.6             label

mod %>%
  tbl_regression(exponentiate = TRUE) %>%
  # merge in PH test p-values
  modify_table_body(
    ~.x %>%
      dplyr::left_join(
        ph_test,
        by = c("variable", "row_type")
      )
  ) %>%
  # assign label to PH Test p-value
  modify_header(ph.test_p.value = "**PH Test**") %>%
  # convert to kable to display on StackOverflow
  as_kable()
Characteristic HR 95% CI p-value PH Test
Age 1.01 0.99, 1.02 0.4 0.8
Marker Level (ng/mL) 0.96 0.76, 1.21 0.7 0.6

Created on 2022-02-21 by the reprex package (v2.0.1)

Related