[R] Surv object in data.frame

Heinz Tuechler tuechler at gmx.at
Thu Mar 16 01:39:45 CET 2006

At 11:59 15.03.2006 -0600, Robert Baer wrote:
>This does work:
>coxph(survobj~group, data=df.test[[1]]) # this works like your original
>To get insight compare:

Thank you for your answer. It seems to me that your solution only works, as
long as the original objects are in the search path. If I do rm(survobj,
group) then coxph(survobj~group, data=df.test[[1]]) does not work, because
it is not found in data=df.test[[1]]. As long as the objects are present also
data=data.frame(NULL) works.

## example data
starttime <- rep(0,5)
stoptime  <- 1:5
event     <- c(1,0,1,1,1)
group     <- c(1,1,1,2,2)
## Surv object
survobj   <- Surv(starttime, stoptime, event)
## put Surv object in data.frame
df.test <- data.frame(survobj=I(survobj), group)

## following Robert Baer
coxph(survobj~group, data=df.test[[1]]) # this works like your original

coxph(survobj~group, data=data.frame(NULL)) # give an empty data frame

## remove objects to verify that df.test is used
rm(starttime, stoptime, event, group, survobj)
coxph(survobj~group, data=df.test[[1]]) # now it doesn't work

>Then note the 2nd sentence of the  following from ?coxph
> formula: a formula object, with the response on the left of a '~'
>          operator, and the terms on the right.  The response must be a
>          survival object as returned by the 'Surv' function.
I know that the "response must be a survival object as returned by the
'Surv' function". The following sentence on the help page for Surv{survival}:
"To include a survival object inside a data frame, use the I() function.
Surv objects are implemented as a matrix of 2 or 3 columns." gave me the
impression that a survival object retains its class if it is included via
I() in a data frame. I was in error.


>Robert W. Baer, Ph.D.
>Associate Professor
>Department of Physiology
>A. T. Still University of Health Science
>800 W. Jefferson St.
>Kirksville, MO 63501-1497 USA
>Dear All,
>a Surv object I put in a data frame behaves somehow unexpected (see
>If I do a Cox regression on the original Surv object it works. If I put it
>in a data.frame and do the regression on the data frame it does not work.
>Seemingly it has to do with the class attribute, because if I change the
>class attribute to let "Surv" appeare first, again it works.
>Is this known? Should I have found information on it?
>Any comments?
>Heinz Tüchler
>## example data
>starttime <- rep(0,5)
>stoptime  <- 1:5
>event     <- c(1,0,1,1,1)
>group     <- c(1,1,1,2,2)
>## Surv object
>survobj   <- Surv(starttime, stoptime, event)
>## Cox-regression
>coxph(survobj~group) # this works
>## put Surv object in data.frame
>df.test <- data.frame(survobj=I(survobj), group)
>## Cox-regression on data.frame
>coxph(survobj~group, data=df.test) # this does not work
>attr(df.test$survobj, 'class') # survobject has class "AsIs", "Surv"
>attr(df.test$survobj, 'class') <- c('Surv', 'AsIs') # put Surv first
>attr(df.test$survobj, 'class') # survobject has class "Surv", "AsIs"
>coxph(survobj~group, data=df.test) # now it works
>R-help at stat.math.ethz.ch mailing list
>PLEASE do read the posting guide!

More information about the R-help mailing list