Utilise Surv object in ggplot or lattice

Posted by Misha on Stack Overflow See other posts from Stack Overflow or by Misha
Published on 2010-06-01T21:54:05Z Indexed on 2010/06/11 13:52 UTC
Read the original article Hit count: 318

Filed under:
|
|
|
|

Anyone know how to take advantage of ggplot or lattice in doing survival analysis? It would be nice to do trellis/facet like survival graphs.


So in the end I played around and sort of found a solution for a kaplan meier plot. Apologize for the messy code in taking the list elements into a dataframe, but I couldnt figure out another way.

Note: It only works with two levels of stratum. If anyone know how I can use x<-length(stratum) to do this please let me know (in stata I could append to a macro-unsure how this works in R)...

ggkm<-function(time,event,stratum) {


    m2s<-Surv(time,as.numeric(event))

    fit <- survfit(m2s ~ stratum)

    f$time<-fit$time

    f$surv<-fit$surv

    f$strata<-c(rep(names(fit$strata[1]),fit$strata[1]),rep(names(fit$strata[2]),fit$strata[2])) 

    f$upper<-fit$upper
    f$lower<-fit$lower

    r<-ggplot (f,aes(x=time,y=surv,fill=strata,group=strata))+geom_line()+geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3)
    return(r)
}

© Stack Overflow or respective owner

Related posts about r

    Related posts about graph