if (require("survival", quietly = TRUE)) {
cfit <- coxph(Surv(time, status) ~ age + sex, lung)
sfit <- survfit(cfit)
head(tidy(sfit))
glance(sfit)
library(ggplot2)
ggplot(tidy(sfit), aes(time, estimate)) + geom_line() +
geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=.25)
# multi-state
fitCI <- survfit(Surv(stop, status * as.numeric(event), type = "mstate") ~ 1,
data = mgus1, subset = (start == 0))
td_multi <- tidy(fitCI)
head(td_multi)
tail(td_multi)
ggplot(td_multi, aes(time, estimate, group = state)) +
geom_line(aes(color = state)) +
geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25)
# perform simple bootstrapping
library(dplyr)
bootstraps <- lung %>% bootstrap(100) %>%
do(tidy(survfit(coxph(Surv(time, status) ~ age + sex, .))))
ggplot(bootstraps, aes(time, estimate, group = replicate)) +
geom_line(alpha = .25)
bootstraps_bytime <- bootstraps %>% group_by(time) %>%
summarize(median = median(estimate),
low = quantile(estimate, .025),
high = quantile(estimate, .975))
ggplot(bootstraps_bytime, aes(x = time, y = median)) + geom_line() +
geom_ribbon(aes(ymin = low, ymax = high), alpha = .25)
# bootstrap for median survival
glances <- lung %>% bootstrap(100) %>%
do(glance(survfit(coxph(Surv(time, status) ~ age + sex, .))))
qplot(glances$median, binwidth = 15)
quantile(glances$median, c(.025, .975))
}
Run the code above in your browser using DataLab