其他
R语言画森林图系列3!
森林图应用范围很广泛,不仅是meta分析,还可用于很多回归模型的可视化。
画图无非就是准备好数据格式,然后选择合适的函数往里面加数据就好了。
“数就是图,图就是数
所以前面介绍的2种方法适合于所有情况,只要你把数据提取出来即可。
不过对于很多模型,我们有现成的工具,不需要自己造轮子!比如GGally
!
这个包能够快速可视化多种模型,主要是结合了broom
包和ggplot2
的优点,通过broom
把模型结果变为整洁形式,然后借助ggplot2
画图,相当于另外封装了,如果你有兴趣,完全可以自己通过这2个包搞定一切。
多元线性回归
logistic回归
多项式回归
自定义标签
更改主题
同时比较多个模型
分面
nnet::multinom
多元线性回归
library(GGally)
## 载入需要的程辑包:ggplot2
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
data(tips, package = "reshape")
mod_simple <- lm(tip ~ day + time + total_bill, data = tips)
ggcoef_model(mod_simple)
logistic回归
d_titanic <- as.data.frame(Titanic)
d_titanic$Survived <- factor(d_titanic$Survived, c("No", "Yes"))
mod_titanic <- glm(
Survived ~ Sex * Age + Class,
weights = Freq,
data = d_titanic,
family = binomial
)
ggcoef_model(
mod_titanic,
exponentiate = TRUE, # 显示OR值
show_p_values = FALSE, # P值
signif_stars = FALSE, # 显著性星号
add_reference_rows = TRUE,
intercept = TRUE, # 截距
categorical_terms_pattern = "{level} (ref: {reference_level})",
interaction_sep = " x "
) +
scale_y_discrete(labels = scales::label_wrap(15))
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
只显示部分变量:
ggcoef_model(mod_titanic, exponentiate = TRUE, include = c("Age", "Class"))
黑白版本:
ggcoef_model(
mod_titanic, exponentiate = TRUE,
colour = NULL, stripped_rows = FALSE
)
多项式回归
mod_poly <- lm(
tip ~ poly(total_bill, 3) + day,
data = tips,
)
ggcoef_model(mod_poly)
自定义标签
library(labelled)
tips_labelled <- tips %>%
set_variable_labels(
day = "Day of the week",
time = "Lunch or Dinner",
total_bill = "Bill's total"
)
mod_labelled <- lm(tip ~ day + time + total_bill, data = tips_labelled)
ggcoef_model(mod_labelled)
# 另一种方法,支持折叠标签
ggcoef_model(
mod_simple,
variable_labels = c(
day = "Week day",
time = "Time (lunch or dinner ?)",
total_bill = "Total of the bill"
),
facet_labeller = label_wrap_gen(10)
)
更改主题
# 借助ggplot2
ggcoef_model(mod_simple) +
xlab("Coefficients") +
ggtitle("Custom title") +
scale_color_brewer(palette = "Set1") +
theme(legend.position = "right")
## Scale for 'colour' is already present. Adding another scale for 'colour',
## which will replace the existing scale.
同时比较多个模型
mod1 <- lm(Fertility ~ ., data = swiss) # 多元线性回归mod2 <- step(mod1, trace = 0) # 逐步回归mod3 <- lm(Fertility ~ Agriculture + Education * Catholic, data = swiss) models <- list("Full model" = mod1, "Simplified model" = mod2, "With interaction" = mod3)ggcoef_compare(models)
分面
ggcoef_compare(models, type = "faceted")
nnet::multinom
library(nnet)data(happy)mod <- multinom(happy ~ age + degree + sex, data = happy)## # weights: 24 (14 variable)## initial value 50552.644463 ## iter 10 value 45030.930814## iter 20 value 43102.145726## final value 43101.422966 ## converged ggcoef_multinom( mod, type = "faceted", y.level_label = c( "pretty happy" = "pretty happy\n(ref: very happy)", "very happy" = "very happy" ) )
OK,说了这么多,你学会了吗?
今天讲的这些东西好像用的不是太多,你们专业会用到吗?
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎在评论区留言或直接添加我的微信!
欢迎关注公众号:医学和生信笔记
“医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!
往期回顾
2022-03-22
2022-03-25
2022-03-28
2022-03-19
2022-03-17