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.
Asked
Active
Viewed 1,182 times
1 Answers
2
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,
You can add more counts such as number of events, number of censored observations etc. to the program.

ssaha
- 459
- 2
- 10
-
Thanks for your answer. Another question: can you explain how n.risk summarizes at risk data? I realised that n.risk is not necessarily equal to the N - ( number of events + censored events ) prior to ith event. – user1916067 Nov 20 '20 at 00:00
-
If I understood your question correctly, are you asking how we construct risk set at each time point? If yes, then risk set is the number of individuals at risk at each time points. This includes both individuals who will experience the outcome and will be censored later on. Therefore, the risk set at time 0 will correspond to N (since no event occurred yet). For risk set in future time points, say at t, we have to subtract total number of event + total number of censored event until t from N. – ssaha Nov 20 '20 at 16:41