如何在同一绘图中展示三个多元回归模型的结果?
可行实现方法
方法一:用broom+ggplot2自定义绘图
通过broom整理模型结果,再用ggplot2在同一画布上展示所有模型的系数及置信区间:
# 加载依赖包 library(broom) library(ggplot2) # 合并三个模型的结果并添加模型标识 model_results <- bind_rows( tidy(fit1) %>% mutate(model = "safety"), tidy(fit2) %>% mutate(model = "belonging"), tidy(fit3) %>% mutate(model = "wellbeing") ) # 绘制合并系数图 ggplot(model_results, aes(x = term, y = estimate, color = model, group = model)) + geom_point(position = position_dodge(width = 0.5)) + # 点位置错开避免重叠 geom_errorbar( aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), width = 0.2, position = position_dodge(width = 0.5) ) + labs(x = "自变量", y = "回归系数", color = "因变量模型") + theme_minimal()
方法二:用sjPlot快速绘制合并系数图
sjPlot的plot_models函数可以直接将多个线性模型的结果合并到同一幅图中,操作更简便:
# 加载依赖包 library(sjPlot) # 绘制合并模型系数图 plot_models( fit1, fit2, fit3, axis.labels = c("截距", "negpol", "pospol"), # 自定义坐标轴标签 legend.title = "因变量模型", show.values = TRUE # 显示系数数值 )
内容的提问来源于stack exchange,提问作者user23504932
相关产品推荐
相关产品推荐

