#### suppose wt.loss is the marker of interest
if(requireNamespace("survival")) {
library(survival)
dat=subset(lung, !is.na(wt.loss) & !is.na(ph.ecog))
f1=Surv(time, status) ~ wt.loss + ph.ecog + age + sex
fit.risk = coxph(f1, data=dat)
ss=quantile(dat$wt.loss, seq(.05,.95,by=0.01))
t0=1000
prob = marginalized.risk(fit.risk, "wt.loss", dat, categorical.s=FALSE, t = t0, ss=ss)
plot(ss, prob, type="l", xlab="Weight loss", ylab=paste0("Probability of survival at day ", t0))
}
if (FALSE) {
#### Efron bootstrap to get confidence band
# store the current rng state
save.seed <- try(get(".Random.seed", .GlobalEnv), silent=TRUE)
if (class(save.seed)=="try-error") {set.seed(1); save.seed <- get(".Random.seed", .GlobalEnv) }
B=10 # bootstrap replicates, 1000 is good
numCores=1 # multiple cores can speed things up
library(doParallel)
out=mclapply(1:B, mc.cores = numCores, FUN=function(seed) {
set.seed(seed)
# a simple resampling scheme here. needs to be adapted to the sampling scheme
dat.tmp=dat[sample(row(dat), replace=TRUE),]
fit.risk = coxph(f1, data=dat)
marginalized.risk(fit.risk, "wt.loss", dat.tmp, categorical.s=FALSE, t = t0, ss=ss)
})
res=do.call(cbind, out)
# restore rng state
assign(".Random.seed", save.seed, .GlobalEnv)
# quantile bootstrap CI
ci.band=t(apply(res, 1, function(x) quantile(x, c(.025,.975))))
plot(ss, prob, type="l", xlab="Weight loss", ylab=paste0("Probability of survival at day ", t0),
ylim=range(ci.band))
lines(ss, ci.band[,1], lty=2)
lines(ss, ci.band[,2], lty=2)
}
Run the code above in your browser using DataLab