julian.bothe at elitepartner.de
2013-Sep-23 11:45 UTC
[R] Bug in Survival - predict.coxph with collapse? (related to question "censor=FALSE and id options in survfit.coxph")
Hello everyone, Hello Terry, Trying to find workarounds for the bug described in http://r.789695.n4.nabble.com/censor-FALSE-and-id-options-in-survfit-coxph -td4670320.html, I found another issue which might or might not be related and which I think is a bug - unless I got the usage of the collapse argument in predict.coxph totally wrong. Using predict.coxph with a collapse vector, it gives the error the collapse-vector has the wrong length. Using the example data given in http://r.789695.n4.nabble.com/censor-FALSE-and-id-options-in-survfit-coxph -td4670320.html: predict = predict(mod, newdata=datnew, collapse=datnew$id) Error in predict.coxph(mod, newdata = datnew, collapse = datnew$id) : Collapse vector is the wrong length all the best Julian ########### CODE ############# # create data set.seed(20130625) n <- 100 # sample size x <- rbinom(n, 1, 0.5) # covariate z <- rep(0, n) # start time y <- rexp(n, exp(x)) # event time e <- y < 2 # censor at 2 y <- pmin(y, 2) # observation time dat <- data.frame(x,z,y,e) rm(x,z,y,e) # fit cox model with start/stop format library(survival) mod <- coxph(Surv(z, y, e)~x, data=dat) summary(mod) plot(survfit(mod)) # create prediction dataset with 3 individuals with # x = 0 on (0,2) # x = 1 on (0,2) # x = 0 on (0,1) and x = 1 on (1,2) datnew <- data.frame(x=c(0,1,0,1), z=c(0,0,0,1), y=c(2,2,1,2), e=rep(0,4), id=c(1,2,3,3)) datnew #### Prediction ###### predict(mod, newdata=datnew) ## works predict(mod, newdata=datnew, collapse=datnew$id) ##error predict(mod, newdata=dat[1:5,], collapse=c(1,2,3,4)) ## error [[alternative HTML version deleted]]