library(survival) survival.prob<-function(time,fired,time.threshold) { # If no data, then returen 0 so err on the side of making everyone fire. if (length(time)==0) { return(0) } S<-survfit(Surv(time,fired),type="kaplan-meier") ntimes<-length(S$time) if ((ntimes==1)||(min(S$time)>=time.threshold)) { return(0) } if (max(S$time)<=time.threshold) { return(S$surv[length(S$surv)]) } index1<-max(which(S$timety),] data[[3]]<-d.temp[(d.temp$x>tx)&(d.temp$ytx)&(d.temp$y>ty),] p.cells<-c( dim(data[[1]])[1], dim(data[[2]])[1], dim(data[[3]])[1], dim(data[[4]])[1]) p.cells<-p.cells/sum(p.cells) p.fire<-c() p.notfire<-c() for (i in seq(1,4)) { time<-data[[i]]$time fired<-data[[i]]$fired p<-survival.prob(time,fired,time.threshold) p.notfire<-c(p.notfire,p) p.fire<-c(p.fire,1-p) } p<-p.fire*p.cells p<-p/sum(p) q<-p.notfire*p.cells q<-q/sum(q) lr<-p/q perm<-order(lr, decreasing=TRUE) fprob<-rep(0,4) psum<-0 i<-1 while(psum+p[perm[i]]best.L$spec) { best.L<-list(fprob=fprob,spec=spec,tx=tx,ty=ty,p=p,q=q) } } } best.L }