[R] Surv object in data.frame and Design package
Dear All, there seems to be some strange influence of the Design package on data.frame. If I build a data.frame containing a Surv object without loading the package Design, the data frame is usable to coxph. If instead I just load Design and build a data.frame afterwards, the naming of the Surv object is different and it does not work with coxph. (In my real application I loaded Design to use the ie.setup function.) Even if I detach Design I cannot build a data.frame as before loading Design. If I include the Surv object with the I() function it seems to work, but then there appeare the problems discussed in the posting from yesterday Surv object in data.frame. The problem is solvable, but I was surprised of the unexpected difficulties, especially that detaching Design did not solve the problem. Is it wrong to expect that R works after detaching a package as before loading it? Comments? Thanks Heinz Tüchler ## example starttime - c(0, 0, 1.5, 0, 2.5) stoptime - c(1, 1.5, 2, 2.5, 3) event - c(1, 0, 1, 0, 0) ie.status - c(0, 0, 1, 0, 1) library(survival) survobj - Surv(starttime, stoptime, event) df.nodesign - data.frame(survobj, ie.status) # build df without loading Design df.nodesign library(Design) df.design - data.frame(survobj, ie.status) # build df after loading Design df.design all.equal(df.nodesign, df.design) detach(package:Design) detach(package:survival) df.afterdesign - data.frame(survobj, ie.status) # building df after detaching Design df.afterdesign library(survival) rm(survobj, ie.status) coxph(survobj~ie.status, data=df.nodesign) # works coxph(survobj~ie.status, data=df.design) # doesn't works coxph(survobj~ie.status, data=df.afterdesign) # doesn't works Windows98 SE Version 2.2.0 Patched (2005-10-31 r36100) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame and Design package
On Thu, 16 Mar 2006, Heinz Tuechler wrote: Dear All, there seems to be some strange influence of the Design package on data.frame. If I build a data.frame containing a Surv object without loading the package Design, the data frame is usable to coxph. If instead I just load Design and build a data.frame afterwards, the naming of the Surv object is different and it does not work with coxph. (In my real application I loaded Design to use the ie.setup function.) Even if I detach Design I cannot build a data.frame as before loading Design. If I include the Surv object with the I() function it seems to work, but then there appeare the problems discussed in the posting from yesterday Surv object in data.frame. The problem is solvable, but I was surprised of the unexpected difficulties, especially that detaching Design did not solve the problem. Is it wrong to expect that R works after detaching a package as before loading it? It's not as strange as all that. The Design package requires the Hmisc package, which you did not detach. Hmisc contains an implementation of as.data.frame.Surv that doesn't work with data.frame() Unfortunately the Hmisc implementation overrides the survival one, irrespective of the order in which the packages are loaded (at least from the viewpoint of getS3method()). For code internal to the survival package the namespace system ensures that survival:::as.data.frame.Surv is called, but data.frame() is not part of the survival package and sees the Hmisc version. I would have expected that S3 methods registered in NAMESPACE would override those based on the function name, but it seems to be the other way around. Also, when you detached Design you also detached survival. If Design rather than Hmisc had been the source of the problem this still wouldn't have worked as no as.data.frame method for Surv objects would have been available. -thomas __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame and Design package
At 11:13 16.03.2006 -0800, Thomas Lumley wrote: On Thu, 16 Mar 2006, Heinz Tuechler wrote: Dear All, there seems to be some strange influence of the Design package on data.frame. If I build a data.frame containing a Surv object without loading the package Design, the data frame is usable to coxph. If instead I just load Design and build a data.frame afterwards, the naming of the Surv object is different and it does not work with coxph. (In my real application I loaded Design to use the ie.setup function.) Even if I detach Design I cannot build a data.frame as before loading Design. If I include the Surv object with the I() function it seems to work, but then there appeare the problems discussed in the posting from yesterday Surv object in data.frame. The problem is solvable, but I was surprised of the unexpected difficulties, especially that detaching Design did not solve the problem. Is it wrong to expect that R works after detaching a package as before loading it? It's not as strange as all that. The Design package requires the Hmisc package, which you did not detach. Hmisc contains an implementation of as.data.frame.Surv that doesn't work with data.frame() Unfortunately the Hmisc implementation overrides the survival one, irrespective of the order in which the packages are loaded (at least from the viewpoint of getS3method()). For code internal to the survival package the namespace system ensures that survival:::as.data.frame.Surv is called, but data.frame() is not part of the survival package and sees the Hmisc version. I would have expected that S3 methods registered in NAMESPACE would override those based on the function name, but it seems to be the other way around. Also, when you detached Design you also detached survival. If Design rather than Hmisc had been the source of the problem this still wouldn't have worked as no as.data.frame method for Surv objects would have been available. -thomas Thanks a lot for this explanation. I tried all this also with Hmisc and woundered, why the effect of Hmisc was reversible by detaching it and that of Design was not. Now I see. Thanks again Heinz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Surv object in data.frame
Dear All, a Surv object I put in a data frame behaves somehow unexpected (see example). 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? Thanks 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@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame
On Wed, 15 Mar 2006, Heinz Tuechler wrote: Dear All, a Surv object I put in a data frame behaves somehow unexpected (see example). 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? Well, this is the sort of thing that happens when you use kludges like AsIs. The problem is with [.AsIs survobj[,1] is supposed to be a vector of times (that's what [.Surv returns), but [.AsIs sticks the original class attribute on to it. str(survobj[,1]) num [1:5] 0 0 0 0 0 str(I(survobj)[,1]) Classes 'AsIs', 'Surv' num [1:5] 0 0 0 0 0 The solution is not to use I() -- there's no problem with putting survival objects in a data frame df.right-data.frame(survobj,group) df.right survobj group 1 (0,1 ] 1 2 (0,2+] 1 3 (0,3 ] 1 4 (0,4 ] 2 5 (0,5 ] 2 -thomas __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame
This does work: coxph(survobj~group, data=df.test[[1]]) # this works like your original To get insight compare: str(survobj) str(df.test) str(df.test[[1]]) Then note the 2nd sentence of the following from ?coxph Arguments: 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. 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 example). 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? Thanks 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@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame
At 09:23 15.03.2006 -0800, Thomas Lumley wrote: On Wed, 15 Mar 2006, Heinz Tuechler wrote: Dear All, a Surv object I put in a data frame behaves somehow unexpected (see example). 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? Well, this is the sort of thing that happens when you use kludges like AsIs. The problem is with [.AsIs survobj[,1] is supposed to be a vector of times (that's what [.Surv returns), but [.AsIs sticks the original class attribute on to it. str(survobj[,1]) num [1:5] 0 0 0 0 0 str(I(survobj)[,1]) Classes 'AsIs', 'Surv' num [1:5] 0 0 0 0 0 The solution is not to use I() -- there's no problem with putting survival objects in a data frame df.right-data.frame(survobj,group) df.right survobj group 1 (0,1 ] 1 2 (0,2+] 1 3 (0,3 ] 1 4 (0,4 ] 2 5 (0,5 ] 2 -thomas Thank you, Thomas. You are right, it works, but why then I find on the help page for Surv{survival} the following sentence: 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. Heinz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame
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: str(survobj) str(df.test) str(df.test[[1]]) 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 Arguments: 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. Heinz 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 example). 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? Thanks 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@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Surv object in data.frame
On Thu, 16 Mar 2006, Heinz Tuechler wrote: Thank you, Thomas. You are right, it works, but why then I find on the help page for Surv{survival} the following sentence: 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. That was probably true when it was written, but that was a long time ago. I will change it. -thomas __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html