therneau / survival

Survival package for R
390 stars 106 forks source link

Enhancement: Accessors for Surv Class #216

Closed DarioS closed 2 years ago

DarioS commented 2 years ago

I would like to create a convenience function that converts a Surv object into the format required by xgboost for its survival:cox objective. Bizarrely, it only accepts a vector of numbers, with negative numbers indicating censored observations. If there were accessors such as time(surv) and event(surv) it would be easy to extract the vectors and convert the data into the silly xgboost format.

Or, could as.vector be changed to produce the silly xgboost format instead of what it does now?

> surv <- Surv(1:5, event = rep(0:1, length.out = 5))
> as.vector(surv)
  1 2 3 4 5 0 1 0 1 0
therneau commented 2 years ago

Read the help file, i.e. help(Surv). The 'value' section will tell you that a Surv object is, under the covers, a matrix. So your xgboost function is

> library(survival)
> temp <- Surv(1:6, c(1,0,1,0,1,1))
> temp
[1] 1  2+ 3  4+ 5  6 
> ifelse(temp[,2]==1, temp[,1], -temp[,1])
[1]  1 -2  3 -4  5  6