3

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
A Toll
  • 647
  • 7
  • 15
  • Try using `broom::tidy(model$y)` (untested) – Benjamin Feb 22 '16 at 09:28
  • Unfortunately that's not quite what I'm looking for, it returned a data frame of survival objects. – A Toll Feb 22 '16 at 15:53
  • 1
    I think I see my misunderstanding. Does `as.data.frame(unclass(model$y))` give you what you need? And for the full data frame, you could use `as.data.frame(cbind(unclass(model$y), model$x))` – Benjamin Feb 22 '16 at 15:59
  • Perfect, that's exactly what I am looking for! Thank you! – A Toll Feb 22 '16 at 17:50

1 Answers1

1

I think you can get what you want by using as.matrix:

pl <- as.matrix(model$y)
as.data.frame(pl)
Theodor
  • 986
  • 3
  • 7
  • 23