Search code examples
rsurvival-analysissurvival

Generating a risk table from survival package


Is there an inbuilt function that generates a risk table in the survival package? I want to report the number at Risk, number of events, and number of censors by time interval. If this is not available, is there a way to produce this table efficiently? I have seen other packages with some in-built functions but would prefer to work with the survival package.


Solution

  • To my limited knowledge there is no in-built function in survival package that would generate the risk-set tables, but you can write a simple function to generate it. Here is a sample code using the veteran data from survival package for Kaplan-Meier plot with the risk set,

    library(survival)
    data(veteran)
    
    RiskSetCount <- function(timeindex, survivaltime) {
      atrisk <- NULL
      for (t in timeindex)
      atrisk <- c(atrisk, sum(survivaltime >= t))
      return(atrisk)
    }
    
    fit <- survfit(Surv(time, status) ~ trt, data=veteran) 
    par(mfrow=c(1,1),mar = c(7,6,2,2)) # defining plot parameters
    plot(fit, xlab="Time",ylab="Survival probability",lwd=2,mark.time=T,col=c(1,2),xlim=c(0,500))
    legend("topright",col=c(1,2),lwd=2,legend=c("Control","Intervention"))
    mtext("Risk set:", 1, line=4, at=-90)
    grid <- seq(0,500,by=100)
    mtext(RiskSetCount(grid,veteran$time[veteran$trt==1]), side=1, line=4, at=grid)
    mtext(RiskSetCount(grid,veteran$time[veteran$trt==2]), side=1, line=5, at=grid)
    

    Here is the K-M plot,

    K-M plot

    You can add more counts such as number of events, number of censored observations etc. to the program.