今天介绍一个非常实用的R包,可帮助各位快速拟合Cox回归模型,制作表格,画出生存曲线以及HR森林图!进入实战部分,首先安装并且载入所需R包:
install.packages("finalfit")
install.packages("flextable")
library(finalfit)
library(flextable)
下一步,查看R包{finalfit}中带有的数据集‘colon_s’:
summary(colon_s)
下一步,定义自变量和因变量的信息,制作一条生存曲线,代码如下:
x <- "perfor.factor"
y <- "Surv(time, status)"
colon_s %>%
surv_plot(dependent = y, explanatory= x, pval = TRUE)
下一步,定义自变量和因变量,拟合Cox回归,并且将模型结果制作成表格,代码如下:
x <- c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
y <- "Surv(time, status)"
# 建立Cox回归并且做表
colon_s %>%
finalfit.coxph(dependent = y, explanatory= x) %>%
flextable(cwidth = 2)
可以将上述的表格导出到Word作进一步的修饰,代码如下:
colon_s %>%
finalfit(dependent = y, explanatory= x) %>%
flextable(cwidth = 2) %>%
save_as_docx(path = "table.docx")
运行上述代码后,在各位的工作路径中会出现一个名为‘table.docx’的word文档,说明导出成功!
再比如,有小伙伴希望将结果以森林图的形式呈现(这个是多因素分析的森林树),那可以这么做:
colon_s %>%
hr_plot(dependent = y, explanatory= x)
全部代码如下:
install.packages("finalfit")
install.packages("flextable")
library(finalfit)
library(flextable)
summary(colon_s)
x <- "perfor.factor"
y <- "Surv(time, status)"
colon_s %>%
surv_plot(dependent = y, explanatory= x, pval = TRUE)
x <- c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
y <- "Surv(time, status)"
# 建立Cox回归并且做表
colon_s %>%
finalfit.coxph(dependent = y, explanatory= x) %>%
flextable(cwidth = 2)
colon_s %>%
finalfit(dependent = y, explanatory= x) %>%
flextable(cwidth = 2) %>%
save_as_docx(path = "table.docx")
colon_s %>%
hr_plot(dependent = y, explanatory= x)
特别申明:本文为转载文章,转载自 R语言和统计,不代表贪吃的夜猫子立场,如若转载,请注明出处:https://mp.weixin.qq.com/s/wl5SKlAMMTWBwaapLnU-sA