rdataframecox-regression

How to convert cox regression output into a dataframe?


I ran univariate cox regression model using following code.

res <- coxph(Surv(DFS, DFS.Event) ~ Grade, data = Grade)
summary(res)

It output as follows:

Call:
coxph(formula = Surv(DFS, DFS.Event) ~ Grade, data = Grade)

  n= 172, number of events= 38 
   (12 observations deleted due to missingness)

             coef exp(coef) se(coef)     z Pr(>|z|)
GradeGrade2 1.153     3.167    1.026 1.123    0.261
GradeGrade3 1.006     2.736    1.028 0.979    0.327

            exp(coef) exp(-coef) lower .95 upper .95
GradeGrade2     3.167     0.3158    0.4237     23.67
GradeGrade3     2.736     0.3655    0.3651     20.50

Concordance= 0.539  (se = 0.04 )
Likelihood ratio test= 1.82  on 2 df,   p=0.4
Wald test            = 1.34  on 2 df,   p=0.5
Score (logrank) test = 1.45  on 2 df,   p=0.5

I want to convert the output into a dataframe. My expected output is:

Variable    coef    exp(coef)   se(coef)    z   p   lower 0.95  upper 0.95
GradeGrade2 1.153   3.167   1.026   1.123   0.261   0.4237  23.67
GradeGrade3 1.006   2.736   1.028   0.979   0.327   0.3651  20.5

Solution

  • As you are looking for a method to extract information from output of summary(coxph(something)), I recommend a self-written function of the form

    CoxphToDF <- function(y) {
      stopifnot(class(y) == "summary.coxph")
      
      y <- 
        cbind(y[["coefficients"]], 
              `lower .95` = y[["conf.int"]][, "lower .95"], 
              `upper .95` = y[["conf.int"]][, "upper .95"]
        )
      
      cbind(Variable = rownames(y), as.data.frame(y))  
    }
    

    Toy data from ?coxph:

    library(survival)
    
    test2 <- list(start=c(1,2,5,2,1,7,3,4,8,8), 
                  stop=c(2,3,6,7,8,9,9,9,14,17), 
                  event=c(1,1,1,1,1,1,1,0,0,0), 
                  x=c(1,0,0,1,0,1,1,1,0,0)) 
    bladder1 <- bladder[bladder$enum < 5, ] 
    
    testobject <- summary(coxph(Surv(stop, event) ~ (rx + size + number) * strata(enum),
                                cluster = id, bladder1))
    

    The result looks odd as the row names are identical to the Variable column, but this mimics the expectations.

    CoxphToDF(testobject) |> head()
    #>                                    Variable        coef exp(coef)   se(coef)
    #> rx                                       rx -0.52598436 0.5909733 0.31582587
    #> size                                   size  0.06961334 1.0720936 0.10155903
    #> number                               number  0.23817961 1.2689371 0.07588471
    #> rx:strata(enum)enum=2 rx:strata(enum)enum=2 -0.10632654 0.8991310 0.50423983
    #> rx:strata(enum)enum=3 rx:strata(enum)enum=3 -0.17250719 0.8415522 0.55780009
    #> rx:strata(enum)enum=4 rx:strata(enum)enum=4 -0.10945460 0.8963229 0.65730033
    #>                        robust se          z    Pr(>|z|) lower .95 upper .95
    #> rx                    0.31523946 -1.6685232 0.095211902 0.3185925  1.096226
    #> size                  0.08863123  0.7854268 0.432203369 0.9011346  1.275486
    #> number                0.07458532  3.1933847 0.001406155 1.0963594  1.468680
    #> rx:strata(enum)enum=2 0.33395783 -0.3183831 0.750194332 0.4672589  1.730168
    #> rx:strata(enum)enum=3 0.39867743 -0.4326987 0.665233705 0.3852354  1.838383
    #> rx:strata(enum)enum=4 0.50636317 -0.2161583 0.828864362 0.3322365  2.418141
    

    Created on 2023-10-11 with reprex v2.0.2