generating a risk table from a survival package

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top