首页 > 解决方案 > 如何在 lmer 拟合的三向交互作用下使用 R 预测主预测器的斜率

问题描述

我有一个线性混合效应模型,具有由以下代码拟合的三向交互:

m <- lmer(cog ~ PRS*poly(Age, 2, raw=T)*Score
             + gender + Edu + fam + factor(Time)
             + (1|family/DBID), 
             data = test_all, REML = F)

在这个模型中,PRS、Score 和年龄的多项式项之间存在三向交互作用,具有两个度数(线性 + 二次)。对于这种三向交互,我怎样才能获得一个变量的边际效应(斜率),以其他变量为条件?例如,当年龄 = 50 且得分 = 1 时,PRS 的斜率是多少?

其次,我尝试使用以下代码来绘制这种三向交互:

plot <- ggpredict(m, ci.lvl=0.95, c("PRS [all]", "Age [60, 65, 70, 75, 80]", "Score[0, 0.321, 0.695, 1.492, 1.914, 3.252]"))
plot(m)

交互图最终显示,但 R 没有给出置信区间。错误信息是Error: Confidence intervals could not be computed.

如何用置信区间绘制这种三向交互?

标签: rpredictlme4interaction

解决方案


您可以使用marginaleffects软件包来做到这一点(免责声明:我是维护者):

library(marginaleffects)
library(lme4)

mod <- lmer(mpg ~ hp * am * vs + (1 | cyl), data = mtcars)

mfx <- marginaleffects(mod, newdata = typical(vs = 0, am = 1, cyl = 4))

summary(mfx)
## Average marginal effects 
##       type Term   Effect Std. Error z value  Pr(>|z|)    2.5 %   97.5 %
## 1 response   am  4.10167    2.13391  1.9221 0.0545884 -0.08072  8.28406
## 2 response   hp -0.03724    0.01378 -2.7016 0.0069001 -0.06426 -0.01022
## 3 response   vs -0.61237    3.52755 -0.1736 0.8621833 -7.52625  6.30151
## 
## Model type:  lmerMod 
## Prediction type:  response

推荐阅读