Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract P values from coxph

Tags:

r

I run the following code and get an object names res.cox, which when I print I can see its p values

res.cox <- coxph(
  Surv(DB$time,DB$event) ~
    age + gender + var1 + var2 + ... + varN
  , data =  DB)

i can easily extract the HR and the Conf interval by the following:

HR <- round(exp(coef(res.cox)), 2)
CI <- round(exp(confint(res.cox)), 2)

But whatever I try I can't extract the list of P values.

Any help would be greatly appreciated.

like image 311
Edi Itelman Avatar asked Oct 15 '25 23:10

Edi Itelman


1 Answers

Usually the statistics including the p value isn't yet available until a summary() method is called. (Calling res.cox directly yields a p value, but probably there's an invisible summary call involved.)

Example

library(survival)
res.cox <- coxph(Surv(time, status) ~ x + strata(sex), test1) 

Looking into the str()ucture reveals that there are only the betas but no statistics.

str(res.cox)

Whereas in the summary() they are available in the "coefficients".

str(summary(res.cox))

So let's get'em.

res.cox.sum <- summary(res.cox)$coefficients

The object you get is a "matrix", and, thus, can be treated like that.

class(res.cox.sum)
# [1] "matrix"

Since the desired p value is in the fifth column, do:

res.cox.sum[, 5]
# [1] 0.3292583

Or short:

summary(res.cox)$coefficients[, 5]
# [1] 0.3292583

Data

test1 <- structure(list(time = c(4, 3, 1, 1, 2, 2, 3), status = c(1, 1, 
1, 0, 1, 1, 0), x = c(0, 2, 1, 1, 1, 0, 0), sex = c(0, 0, 0, 
0, 1, 1, 1)), class = "data.frame", row.names = c(NA, -7L))
like image 119
jay.sf Avatar answered Oct 17 '25 13:10

jay.sf