计算
先构造两个多变量 Cox 分析模型
install.packages("ezcox")#先安装包
packageVersion("ezcox")#0.4.0版本
library(survival)
library(ezcox)
lung$ph.ecog <- factor(lung$ph.ecog)
zz <- ezcox(lung, covariates = c("sex", "ph.ecog"), controls = "age", return_models = TRUE)
#=> Processing variable sex
#==> Building Surv object...
#==> Building Cox model...
#==> Done.
#=> Processing variable ph.ecog
#==> Building Surv object...
#==> Building Cox model...
#==> Done.
zz
#$res
# A tibble: 6 x 12
# Variable is_control contrast_level ref_level n_contrast n_ref beta HR lower_95 upper_95 #p.value global.pval
# <chr> <lgl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #<dbl>
#1 sex FALSE sex sex 228 228 -0.513 0.599 0.431 0.831 0.00218 #0.000857
#2 sex TRUE age age 228 228 0.017 1.02 0.999 1.04 0.0646 #0.000857
#3 ph.ecog FALSE 1 0 113 63 0.359 1.43 0.969 2.11 0.0712 #0.000551
#4 ph.ecog FALSE 2 0 50 63 0.857 2.36 1.5 3.7 0.0002 0.000551
#5 ph.ecog FALSE 3 0 1 63 2.11 8.23 1.09 61.8 0.0406 0.000551
#6 ph.ecog TRUE age age 228 228 0.0108 1.01 0.992 1.03 0.251 #0.000551
#$models
# A tibble: 2 x 5
# Variable control model_file model status
<chr> <chr> <chr> <list> <lgl>
#1 sex age "C:\\Users\\ASUS\\AppData\\Local\\Temp\\Rtmp0GDxAP/ezcox\\ezcox_1c30cfa3a80" <coxph> TRUE
#2 ph.ecog age "C:\\Users\\ASUS\\AppData\\Local\\Temp\\Rtmp0GDxAP/ezcox\\ezcox_1c3060a35bfa" <coxph> TRUE
#attr(,"class")
#[1] "ezcox" "list"
#attr(,"controls")
#[1] "age"
#####获取模型
得到计算结果后,我们下一步提取模型。
mds <- get_models(zz)
查看模型结构,其实是列表而已
str(mds, max.level = 1)
#> List of 2
#> $ Surv ~ sex + age :List of 19
#> ..- attr(*, "class")= chr "coxph"
#> ..- attr(*, "Variable")= chr "sex"
#> $ Surv ~ ph.ecog + age:List of 22
#> ..- attr(*, "class")= chr "coxph"
#> ..- attr(*, "Variable")= chr "ph.ecog"
#> - attr(*, "class")= chr [1:2] "ezcox_models" "list"
#> - attr(*, "has_control")= logi TRUE
Show time 使用 show_models()。
#先安装forestmodel包
library(remotes)
remotes::install_github("ShixiangWang/forestmodel")
library("forestmodel")
show_models(mds)
```![png](https://img.haomeiwen.com/i21455872/1018a5d57e2bc8e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# Set model names
show_models(mds, model_names = paste0("Model ", 1:2))
.png
对比发现改变了model名字,内容是不变的.
png
让图形更精简一些,可以将模型合并并去掉控制变量。
# Merge all models and drop control variables
show_models(mds, merge_models = TRUE, drop_controls = TRUE)
png
一步生成森林图
show_forest(lung, covariates = c("sex", "ph.ecog"), controls = "age")
png
cox森林图也可用ggforest()画1,两者对比,看你自己喜欢哪个。
另外,非常感谢王诗翔老师的邮件回复,帮助我解决问题,让我能跟着文章做下去!
参考:王诗翔「r<-包」使用 ezcox 展示森林图
网友评论