1bootkm <- function(S, q=.5, B=500, times, pr=TRUE) 2{ 3 tthere <- !missing(times) 4 if(tthere && length(times)>1) 5 stop('presently bootkm only works for a single time') 6 7 S <- S[!is.na(S),] 8 n <- nrow(S) 9 stratvar <- factor(rep(1,nrow(S))) 10 f <- survfitKM(stratvar, S) 11 tt <- c(0, f$time) 12 ss <- c(1, f$surv) 13 if(!tthere) { 14 if(ss[length(ss)] > q) 15 stop(paste('overall Kaplan-Meier estimate does not fall below',q)) 16 17 } else { 18 if(tt[length(tt)] < times) 19 stop(paste('overall Kaplan-Meier estimate not defined to time',times)) 20 } 21 22 ests <- if(.R.) 23 double(B) 24 else 25 single(B) 26 27 for(i in 1:B) { 28 if(pr && (i %% 10)==0) 29 cat(i,'\r') 30 31 f <- survfitKM(stratvar, S[sample(n,n,replace=TRUE),], 32 se.fit=FALSE, conf.type='none') 33 tt <- c(0, f$time) 34 ss <- c(1, f$surv) 35 ests[i] <- if(tthere) 36 approx(tt, ss, xout=times, method='constant', f=0)$y 37 else 38 min(tt[ss <= q]) #is NA if none 39 } 40 if(pr) 41 cat('\n') 42 43 ests 44} 45