![alt](https://i-blog.csdnimg.cn/blog_migrate/1f459b463ebc824270a7927c717222c3.png)
1写在前面
本期继续介绍git
上的神包,autoReg
,如果你认为它只能像table1
一样的完成统计表
的输出, 那你就小看它啦!~
它更为重要的应用就是单因素回归分析
和多因素回归分析
等等。
应用场景:危险因素探索、预后因素探索等。
2用到的包
rm(list = ls())
#devtools::install_github("cardiomoon/autoReg")
library(autoReg)
library(tidyverse)
library(ggsci)
3示例数据
本期示例数据为
survival
包中的cancer
数据集。
library(survival)
data(cancer)
![alt](https://i-blog.csdnimg.cn/blog_migrate/d0a5c9518f42b17b5861b8ecfe12b435.png)
4多因素logistic回归
这里我们以logistic回归
为例,进行演示,大家在应用的时候,依据结局变量的不同选择不同方法建模即可。
4.1 建模
fit <- glm(status~rx+sex+age+obstruct+perfor+nodes,
data=colon,
family="binomial")
summary(fit)
![alt](https://i-blog.csdnimg.cn/blog_migrate/d6f10a29fb40be43ceecf200928dba5d.png)
4.2 整理数据为table
这里我们快速整理成方便阅读的表格形式,配合myft()
直接输出成发表级。
autoReg(fit) %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/339f99142a55c1904f7ba575ba5fce69.png)
4.3 更改label
上面的表格显示为0
和1
,并不方便阅读,当然你可以选择在word中更改。
这里我们将其转为factor
,并设置label
。
colon$status.factor <- factor(colon$status,labels=c("Alive","Died"))
colon$obstruct.factor <- factor(colon$obstruct,labels=c("No","Yes"))
colon$perfor.factor <- factor(colon$perfor,labels=c("No","Yes"))
colon$sex.factor <- factor(colon$sex,labels=c("Female","Male"))
fit <- glm(status.factor~rx+sex.factor+age+obstruct.factor+perfor.factor+nodes,
data=colon,
family="binomial")
result <- autoReg(fit)
result %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/7063b6943693dfbdc32c34a0a6b65197.png)
4.4 设置变量的label
给变量名
加label
的话,我们就用setLabel
函数吧。
colon$status.factor <- setLabel(colon$status.factor,"Mortality")
colon$rx <- setLabel(colon$rx,"Treatment")
colon$age <- setLabel(colon$age,"Age(Years)")
colon$sex.factor <- setLabel(colon$sex.factor,"Sex")
colon$obstruct.factor <- setLabel(colon$obstruct.factor,"Obstruction")
colon$perfor.factor <- setLabel(colon$perfor.factor,"Perforation")
colon$nodes <- setLabel(colon$nodes,"Positive nodes")
fit <- glm(status.factor~rx+sex.factor+age+obstruct.factor+perfor.factor+nodes,
data=colon,
family="binomial")
result <- autoReg(fit)
result %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/a0099f5128e3ec0275577ff93b1397cd.png)
4.5 不显示reference
在回归分析中,有一组会作为我们的参照组,即reference
,如果你并想它显示的话, 可以这样做:
shorten(result) %>% myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/83f4490a2383784565634ee546f95e21.png)
5单+多因素logistic回归
5.1 加入单因素回归
这里我们加上单因素logistic回归
的结果。
autoReg(fit, uni=TRUE) %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/b9cb90a26e7855c0ee75bb83f8fe8a50.png)
5.2 纳入所有变量
我们将threshold
设定为1,纳入所有变量进入多因素分析。你可以根据自己的需要设置threshold
。
autoReg(fit, uni=TRUE,threshold=1) %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/8a00a1586cea49d8b993753c57b62bb9.png)
5.3 建立最终模型
我们将通过筛选的变量,纳入最终的模型吧,即final
设为T
。
autoReg(fit, uni=TRUE,threshold=1, final=TRUE) %>%
myft()
![alt](https://i-blog.csdnimg.cn/blog_migrate/31a405b043e8029dab4eb291325f3ae3.png)
6回归结果的可视化
6.1 单一结果可视化
这里我们用森林图
的形式,先可视化一下多因素logistic回归
的结果吧。
颜值也就是凑活吧。。。。😂
modelPlot(fit,change.pointsize = T)
![alt](https://i-blog.csdnimg.cn/blog_migrate/d7f257800de011e362ed26f9ca78c046.png)
6.2 多结果可视化
接着我们同时展示单因素logistic回归
和多因素logistic回归
的结果吧。
顺便我们改一下颜色吧,提高一下颜值~🤒
p2 <- modelPlot(fit,uni=TRUE,
threshold=1,
show.ref=FALSE,
change.pointsize = T)
p2$p <- p2$p+
scale_fill_nejm()+
scale_color_nejm()
p2
![alt](https://i-blog.csdnimg.cn/blog_migrate/9489753acca1588e1ae448c981e667e1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b1b8ed51d9d2a305629626275ac8e939.png)
点个在看吧各位~ ✐.ɴɪᴄᴇ ᴅᴀʏ 〰
![alt](https://i-blog.csdnimg.cn/blog_migrate/63850fb1fa37f926ca26ced4acb0d1d6.png)
本文由 mdnice 多平台发布