Search code examples
rplotinterpolationsurvival

Survival function - interpolation in R


I have constructed a survival function such as:

  [![plot(survfit(Surv(inf.time,infection)~1),xlab="Time (days)",ylab="Survival Probability",
                        main="Time Until Staphylococcus Infection",
                        conf.int = F)][1]][1]

enter image description here

Now i would like to determine by which time has 40% of the patients had an infection? How can this be written in R? Either as a command that gives me a numerical value as result or directly plotted into the survival function graph.

I am aware that the survival function depicts the probability of not having an infection, therefore i can read from the graph that survival probability of 0.6 corresponds to ca. 45 days? How can i plot this into the graph?

i tried (with no luck):

inf.time[which(survfit(Surv(inf.time,infection)~1) == 0.6)]

Solution

  • I suppose, you are using survival package, right? So you can get data frame of all points of the survival curve from "survfit" object or, easily, from "summary.survfit because it is list type under the hood. Then you can filter the df by survival probability. Some example:

    library(survival)
    
    s <- Surv(lung$time, lung$status)
    
    sfit <- survfit(s~1)  #get usual "survfit" object
    
    summary_sfit = summary(sfit)
    
    surv_df = data.frame(summary_sfit[2:6])  #somehow coerce it to data.frame
    
    head(surv_df)
    
    ##    time n.risk n.event n.censor      surv
    ##  1    5    228       1        0 0.9956140
    ##  2   11    227       3        0 0.9824561
    
    
    surv_df[max(which(surv_df$surv > 0.60)), ] 
    ##     time n.risk n.event n.censor      surv
    ##  68  245    117       1        2 0.6018576
    

    Or you can just subset survfit$time without creating data frame

    sfit$time[max(which(sfit$surv > 0.6))]
    
    ## [1] 245