如何用sjplot的plot_model函数单独展示多分类逻辑回归的单个结果水平
解决多分类逻辑回归预测图按因变量水平单独生成的问题
核心方案:使用response.level参数
sjPlot的plot_model()函数针对多分类模型(如multinom类模型)提供了response.level参数,专门用于指定要绘制的因变量水平,直接生成对应水平的独立完整图表,无需额外调整布局或处理子集。
具体代码示例
假设你的因变量Veto Type三个水平为"Partial Veto"、"Full Veto"、"No Veto",也可以用水平索引1/2/3指定,以下是分水平绘图的代码:
绘制「Partial Veto」水平的预测图
plot_model( model9, type = "pred", terms = c("CoalitionPercentageAmorim", "CoalescenceAmorim [0,1]"), mdrt.values = "meansd", ci.lvl=0.00, response.level = "Partial Veto", # 指定目标因变量水平 title = "Figure: Predicted Probability of Partial Vetoes of PLs", axis.title = c("Coalition Size", "Predicted Probability"), legend.title = "COALITION COALESCENCE" )
绘制「Full Veto」水平的预测图
plot_model( model9, type = "pred", terms = c("CoalitionPercentageAmorim", "CoalescenceAmorim [0,1]"), mdrt.values = "meansd", ci.lvl=0.00, response.level = "Full Veto", title = "Figure: Predicted Probability of Full Vetoes of PLs", axis.title = c("Coalition Size", "Predicted Probability"), legend.title = "COALITION COALESCENCE" )
通过索引指定水平(适用于不确定水平名称的情况)
先查看因变量的水平列表,再用索引指定:
# 查看因变量所有水平 levels(model9$model$Veto_Type) # 绘制第一个水平(索引为1) plot_model( model9, type = "pred", terms = c("CoalitionPercentageAmorim", "CoalescenceAmorim [0,1]"), mdrt.values = "meansd", ci.lvl=0.00, response.level = 1, title = "Figure: Predicted Probability of Veto Type Level 1", axis.title = c("Coalition Size", "Predicted Probability"), legend.title = "COALITION COALESCENCE" )
批量生成并保存所有水平的图表
如果需要一次性生成所有水平的图并保存,可通过循环实现:
# 获取因变量的所有水平 veto_levels <- levels(model9$model$Veto_Type) # 循环生成每个水平的图表并保存 for (level in veto_levels) { p <- plot_model( model9, type = "pred", terms = c("CoalitionPercentageAmorim", "CoalescenceAmorim [0,1]"), mdrt.values = "meansd", ci.lvl=0.00, response.level = level, title = paste0("Figure: Predicted Probability of ", level, " of PLs"), axis.title = c("Coalition Size", "Predicted Probability"), legend.title = "COALITION COALESCENCE" ) # 保存图表,文件名用水平名称区分 ggsave(paste0("veto_pred_", level, ".png"), p, width = 8, height = 6) }
说明:为什么之前的方法无效?
你尝试的rm.terms参数用于移除预测变量,而非筛选因变量水平,因此无法实现需求;用gtable调整布局的方式会压缩图表尺寸,不如response.level参数直接高效。
内容的提问来源于stack exchange,提问作者Jair Moreira
相关产品推荐
相关产品推荐

