r - 如何在 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.
如何用置信区间绘制这种三向交互?
解决方案
您可以使用该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
推荐阅读
- cryptocurrency - 如何解码 solana 交易?
- payment - 取消付款后,发票不能再次过帐
- machine-learning - 同时开发以图像和数字为输入的神经网络
- excel - 在 Excel 中将文本字符串转换为公式
- typescript - 扩充 Vue ComponentOptions 时动态导入在 CI build-Jest 中中断
- python - 从 span 元素中抓取网页
- android - 如何在 Android Studio 中请求位置更新
- asp.net - 如何在 ASP.Net 的 gridView 中显示来自 Web 服务的数据
- python - 如何在为机器学习准备数据时定义一系列值
- java - 如何检查用户是否按下特定键,即 Enter Key Like python 的键盘模块?