Heinz Tuechler
2009-Jul-13 15:35 UTC
[R] survSplit with data.frame containing a Surv object
Dear All, since years I am struggling with Surv objects in data.frames. The following seems to have to do with it. See below the modified example from the help page of survSplit. The original works, as expected. If, however, a Surv object is added to the data.frame, each record gets doubled. Is there some solution other than avoiding Surv objects in data.frames? Thanks, Heinz require(survival) ## from the help page aml3<-survSplit(aml,cut=c(5,10,50),end="time",start="start", event="status",episode="i") summary(aml) summary(aml3) coxph(Surv(time,status)~x,data=aml) ## the same coxph(Surv(start,time,status)~x,data=aml3) ## added to show doubling of records aml.so <- aml aml.so$surv.object <- with(aml, Surv(time, status)) aml3.so <- survSplit(aml.so ,cut=c(5,10,50),end="time",start="start", event="status",episode="i") summary(aml3.so) sessionInfo('survival') R version 2.9.1 Patched (2009-07-07 r48910) i386-pc-mingw32 locale: LC_COLLATE=German_Switzerland.1252;LC_CTYPE=German_Switzerland.1252;LC_MONETARY=German_Switzerland.1252;LC_NUMERIC=C;LC_TIME=German_Switzerland.1252 attached base packages: character(0) other attached packages: [1] survival_2.35-4 loaded via a namespace (and not attached): [1] base_2.9.1 graphics_2.9.1 grDevices_2.9.1 methods_2.9.1 [5] splines_2.9.1 stats_2.9.1 utils_2.9.1
Charles C. Berry
2009-Jul-13 18:18 UTC
[R] survSplit with data.frame containing a Surv object
On Mon, 13 Jul 2009, Heinz Tuechler wrote:> Dear All, > > since years I am struggling with Surv objects in data.frames. The following > seems to have to do with it. > See below the modified example from the help page of survSplit. The original > works, as expected. If, however, a Surv object is added to the data.frame, > each record gets doubled. > Is there some solution other than avoiding Surv objects in data.frames?I think you can modify survSplit so that it will properly handle Surv objects. Change this line: newdata <- lapply(data, rep, ntimes + 1) to this: newdata <- lapply(data, function(x) { x <- as.matrix(x) x[rep(1:nrow(x), ntimes + 1),] }) or something similar that results Surv objects being rep()'ed rowwise rather than elementwise and returned as objects of the right dimension (rather than as a vector). Caveat: This works in the example you give, but I've not tested this extensively. HTH, Chuck> > Thanks, > Heinz > > > require(survival) > > ## from the help page > aml3<-survSplit(aml,cut=c(5,10,50),end="time",start="start", > event="status",episode="i") > > summary(aml) > summary(aml3) > > coxph(Surv(time,status)~x,data=aml) > ## the same > coxph(Surv(start,time,status)~x,data=aml3) > > ## added to show doubling of records > aml.so <- aml > aml.so$surv.object <- with(aml, Surv(time, status)) > > aml3.so <- survSplit(aml.so ,cut=c(5,10,50),end="time",start="start", > event="status",episode="i") > summary(aml3.so) > > sessionInfo('survival') > R version 2.9.1 Patched (2009-07-07 r48910) > i386-pc-mingw32 > > locale: > LC_COLLATE=German_Switzerland.1252;LC_CTYPE=German_Switzerland.1252;LC_MONETARY=German_Switzerland.1252;LC_NUMERIC=C;LC_TIME=German_Switzerland.1252 > > attached base packages: > character(0) > > other attached packages: > [1] survival_2.35-4 > > loaded via a namespace (and not attached): > [1] base_2.9.1 graphics_2.9.1 grDevices_2.9.1 methods_2.9.1 > [5] splines_2.9.1 stats_2.9.1 utils_2.9.1 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901