plot_model函数如何确定图中展示的lsvo值?能否自定义该值?
关于sjPlot::plot_model面板取值的问题解答
你的操作代码回顾
加载R包
library(dplyr) library(ggplot2) library(lme4) library(readxl) library(AICcmodavg) library(MuMIn) library(afex) library(sjPlot) library(ggeffects)
拟合GLMM模型
mod.top <- glmer(use ~ boxes.per.post + box.age + lsvo + (1|year), family = "binomial", data = WODU)
绘制预测图
plot_model(mod.top, type = "pred", terms = c("box.age [all] ","boxes.per.post", "lsvo"), pred.type = "fe", legend.title = "", axis.title = "Probability of Use", title = "")
核心问题解答
1. plot_model如何确定面板展示的lsvo值?
plot_model(type = "pred")底层依赖ggeffects包的ggpredict()函数生成预测数据。对于作为第三个变量的连续型变量(此处为lsvo),默认逻辑是选取该变量的25%、50%、75%分位数值来生成不同面板;如果是分类变量则展示所有水平。
你遇到的负数问题,大概率是函数内部对变量做了默认中心化/标准化处理,导致生成了超出原始数据范围的取值。
2. 能否修改这些面板的lsvo值?
完全可以,有两种灵活的方式:
方式一:直接在plot_model的terms参数中指定取值
在terms里给lsvo加上方括号,填入你想要展示的具体数值(用逗号分隔),比如想展示0、50、100这三个关键值:
plot_model(mod.top, type = "pred", terms = c("box.age [all]", "boxes.per.post", "lsvo [0,50,100]"), pred.type = "fe", legend.title = "", axis.title = "Probability of Use", title = "")
如果需要更多面板,比如展示0、25、50、75、100,直接把数值加入方括号即可。
方式二:先用ggpredict生成自定义预测数据,再手动绘图
这种方式能完全控制所有绘图细节,灵活性最高:
# 生成指定lsvo取值的预测数据 pred_data <- ggpredict(mod.top, terms = c("box.age [all]", "boxes.per.post", "lsvo [0,50,100]"), pred.type = "fe") # 用ggplot2自定义绘图 ggplot(pred_data, aes(x = x, y = predicted, color = group)) + geom_line(linewidth = 1) + facet_wrap(~facet) + labs(x = "Box Age", y = "Probability of Use") + theme_bw()
内容的提问来源于stack exchange,提问作者blake.struthers
相关产品推荐
相关产品推荐

