After generating a model using cph()
from rms
, calling model$y
will return a survival object. Is there a function that will "undo" the survival object and return a data frame?
I would like to be able to use a survival object as a argument for a function I am writing, but I also need the response data. I am trying to avoid using the data as an argument and creating the model inside the function.
A minimal working example is provided below:
library(rms)
# generate data
time <- c(82, 73, 89, 79, 72, 87, 103, 83, 100, 79)
event <- c(0, 0, 1, 0, 1, 0, 0, 0, 1, 1)
covar <- c(15, 11, 11, 20, 12, 13, 10, 11, 10, 14)
df <- data.frame(time, event, covar)
# Cox model
dd <- datadist(df)
options(datadist = 'dd')
model <- cph(Surv(time, event) ~ covar, x=TRUE, y=TRUE, surv=TRUE, data=df)
# returns a survival object
model$y
# what I want is a data frame
want <- data.frame(time, event)
want