Chapter 9
9.1 生存对象
library(survival)
data(ovarian)
str(ovarian)
ovarian$resid.ds <- factor(ovarian$resid.ds,
levels = c(1, 2),
labels = c("no", "yes"))
ovarian$rx <- factor(ovarian$rx,
levels = c(1, 2),
labels = c("A", "B"))
ovarian$ecog.ps <- factor(ovarian$ecog.ps,
levels = c("1", "2"),
labels = c("good", "bad"))
hist(ovarian$age)
ovarian$agegr <- cut(ovarian$age,
breaks = c(0, 50, 75),
labels = c("<=50", ">50"))
table(ovarian$agegr)
surv.obj <- Surv(time = ovarian$futime, event = ovarian$fustat)
surv.obj
9.2 生存率的估计与生存曲线
survfit(surv.obj ~ 1)
surv.all <- survfit(surv.obj ~ 1)
summary(surv.all)
summary(surv.all, censored = TRUE)
plot(surv.all, mark.time = TRUE,conf.int = FALSE)
abline(h = .5, lty = 2, col = "red")
9.3 生存率的比较(这里加入了自变量)
surv.treat <- survfit(surv.obj ~ rx, data = ovarian)
summary(surv.treat)
plot(surv.treat, mark.time = T, conf.int = TRUE,
lty = c(1, 2), col = c("blue", "red"))
legend(60, .3, legend = c("A", "B"),
lty = c(1, 2), col = c("blue", "red"))
install.packages("survminer")
library(survminer)
ggsurvplot(surv.treat, data = ovarian, pval = TRUE)
survdiff(surv.obj ~ rx, data = ovarian)
9.4 Cox回归
9.4.1 建立Cox回归模型
cox1 <- coxph(surv.obj ~ rx + resid.ds + agegr + ecog.ps,
data = ovarian)
summary(cox1)
drop1(cox1, test = "Chisq")
cox2 <- coxph(surv.obj ~ rx + resid.ds + agegr, data = ovarian)
step.cox <- step(cox1)
9.4.2 比例风险假定的检验
cox.zph(cox2)
summary(cox2)
9.4.3 生存的预测
newdata <- data.frame(rx = c("A" , "B"),
resid.ds = c("no", "no"),
agegr = c(">50", ">50"))
newdata
hr <- predict(cox2, newdata = newdata, type = "risk")
hr
hr[1]/hr[2]
cox.fit <- survfit(cox2, newdata = newdata, type = "kaplan-meier")
plot(cox.fit, lty = c(1, 2), col = c(2, 4))
title(main="Cox survival curves by treatment
for age > 50, no residual disease patients",
xlab="Duration in days",
ylab="Survival probability",
las = 1)
legend(5, 0.3, c("Treatment A", "Treatment B"),
lty = c(1, 2), col = c(2, 4))