library(tidyverse)
library(gtsummary)
library(survival)
# Example 1 ----------------------------------
# 直接用survfit()拟合,计算12及24个月的生存概率及可信区间
tbl_survfit_ex1 <- tbl_survfit(
survfit(Surv(ttdeath, death) ~ trt, data = trial),#拟合生存方程
times = c(12, 24),#时间点
label_header = "**{time} Month**"#时间单位显示设定
)
# Example 2 ----------------------------------
# 计算多个分组的中位生存时间
tbl_survfit_ex2 <- tbl_survfit(
trial,#数据集
y = Surv(ttdeath, death),#生存对象
include = c(trt, grade),#分组变量,可以是多个
probs = 0.5,#计算中位生存时间,也可以返回其他生存概率的时间点
label_header = "**Median Survival**"#题目显示
)
tbl_survfit_ex2
# Example 3 ----------------------------------
# 可以利用surfit分别组成不对的对象后构成列表进行分析
tbl_survfit_ex3 <-
list(
survfit(Surv(ttdeath, death) ~ 1, trial),
survfit(Surv(ttdeath, death) ~ trt, trial)
) %>%
tbl_survfit(times = c(12, 24))
tbl_survfit_ex3
# Example 4 竞争风险模型计算 ---------
# 构建竞争风险数据
trial2 <- trial %>%
mutate(
death_cr = case_when(
death == 0 ~ "censor",
runif(n()) < 0.5 ~ "death from cancer",
TRUE ~ "death other causes"
) %>% factor()
)
#计算结果并展示
survfit_cr_ex4 <-
survfit(Surv(ttdeath, death_cr) ~ grade, data = trial2) %>%
tbl_survfit(times = c(12, 24), label = "Tumor Grade")
trial2$death_cr
survfit_cr_ex4
# Example 5 ----------------------------------
# 同时对竞争分析模型的两种结局拟合并计算时间点生存率
tbl_survfit_ex3 <-
list(
survfit(Surv(ttdeath, death_cr == "death from cancer") ~ grade, data = trial2),
survfit(Surv(ttdeath, death_cr == "death other causes") ~ grade, data = trial2)
) %>%
tbl_survfit(times = c(12, 24))
tbl_survfit_ex3