library(stats) # needed for the stepfun method # For using the boot function: # samplemedian = function(x, d){ return(median(x[d])) } samplemean = function(x, d){ return(mean(x[d])) } make_kaplan_meir_survival_curve = function(DF){ # # Build/compute a S(t) curve from the formual given in 11.4.1 # # In calling this routine we assume that we have cosumn dataframe named: # 1) deceased: 0 => deceased and 1 => alive # 2) time: lifetime # #----- # Drop any d_i == 0 points (these don't affect the estimate of S(t)): # DF = DF[DF$deceased == 1, ] n = dim(DF)[1] # Order the dataset by time: # inds = order(DF$time) DF = DF[inds,] # Drop duplicate records (by time): # duped = duplicated( DF$time ) DF = DF[!duped, ] possible_t_values = DF$time n_ts = length(possible_t_values) # Compute the midpoints between each time: mids = ( possible_t_values[-1] + possible_t_values[-n_ts] )/2 is = 1:length(mids) cps = cumprod( ( (n_ts - is) / (n_ts - is + 1) ) ) xs = DF$time # the jump locations ys = c( 1, cps, 0 ) library(stats) sf = stepfun(xs, ys, right=TRUE) return(sf) } load_table_11_5_data = function(){ # The Treatment Time data: Table 11.5 # DF = data.frame( time=c( 1, 1, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23 ), age=c( 61, 65, 59, 52, 56, 67, 63, 58, 56, 58, 52, 49, 50, 55, 49, 62, 51, 49, 57, 52 ) ) DF$deceased = 1 DF$deceased[10] = 0 # patient is still alive DF }