therneau / survival

Survival package for R
382 stars 104 forks source link

Index in `cox_zph` #213

Closed AlissonRP closed 2 years ago

AlissonRP commented 2 years ago

When we apply the cox_zph function it returns an object of the class cox.zph which is ultimately a list, but indexing doesn't seem to be working well (or is intentional???), because if I want to select the last line (GLOBAL), its output an error:

df <- survival::leukemia

fit <- survival::coxph(survival::Surv(time, status) ~ x, data = df, 
                       x = T, method = "breslow")

survival::cox.zph(fit)
#>         chisq df    p
#> x      0.0136  1 0.91
#> GLOBAL 0.0136  1 0.91
survival::cox.zph(fit)[2]
#> Error in x$y[, i, drop = FALSE]: subscript out of bounds

So in this pr I made a slight adjustment in this function:

df <- leukemia

fit <- coxph(survival::Surv(time, status) ~ x, data = df, 
                       x = T, method = "breslow")
cox.zph(fit)[2]

#>        chisq df    p
#> GLOBAL 0.0136  1 0.91

I didn't do a lot of testing, so reviews are welcome.

therneau commented 2 years ago

I don't think you have read the documentation. Subscripts of a cox.zph object do a very particular thing. It doesn't happen to be what you want to assume.

A primary use:
fit <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data= lung) zp <- cox.zph(fit) plot(zp[1])

This will draw a plot of the ph test for age.

Your example asks for the second variable, for a model that has only one. Of course it failed.

AlissonRP commented 2 years ago

Sorry, so the index is just for plot? but how to "collect" the values in the last line of cox.zph??

Edit: is in cox.zph$table