使用marginaleffects::plot_predictions绘制GLMM交互预测遇矩阵列错误
使用marginaleffects绘制GLMM四元交互预测图时出错
概述
尝试使用marginaleffects::plot_predictions()绘制广义线性混合模型(GLMM)的预测结果,以下为最小可复现示例。
目标
希望绘制以下GLMM中scaled_exp * Agreement * Condition * Group四元交互的预测结果:
glmer(Acceptance ~ scaled_exp * Agreement * Condition * Group + scaled_age * Agreement * Group + (1 | Item) + (1 + Condition | Subject)
但发现若conditions列表未包含所有预测变量(scaled_age仅作为控制变量,无需纳入绘图),marginaleffects::plot_predictions()会出现异常——仅GLMM存在此问题,线性混合模型LMM无此问题,推测与模型类型相关。执行时触发如下错误:
警告:矩阵列不被支持作为预测变量,因此已被省略。这可能会影响感兴趣指标的计算。您可以自行构建预测数据集并显式传入
newdata参数。
警告:部分列为多列类型(如矩阵列):[2, 6]。setDT将保留这些列,但后续分组、连接等操作可能失败。请考虑使用as.data.table(),它会为每个嵌入列创建新列。
错误:无法使用此模型计算预测值。您可以尝试向newdata参数传入其他数据集。同时触发错误:object 'scaled_age' not found。
最小可复现示例(MWE)
# 加载必要的包 library(lme4) library(tidyverse) library(marginaleffects) # 创建示例数据集 set.seed(123) n <- 5000 sample_data <- tibble( Subject = as.factor(rep(1:500, each = 10)), # 增加被试数量 Group = factor(rep(c("Group1", "Group2"), each = n / 2)), Item = sample(1:120, n, replace = TRUE), Condition = factor(rep(c("Cond1", "Cond2"), times = n / 2)), Agreement = factor(sample(c("Agree", "Disagree"), n, replace = TRUE)), Acceptance = rbinom(n, 1, 0.5), scaled_age = scale(rnorm(n, 40, 15)), scaled_exp = scale(rnorm(n, 0.3, 1.4)) ) # 拟合广义线性混合模型 glmm_mod <- glmer(Acceptance ~ scaled_exp * Agreement * Condition * Group + scaled_age * Agreement * Group + (1 | Item) + (1 + Condition | Subject), data = sample_data, family = binomial, control = glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000))) # 使用plot_predictions()生成四元交互的预测图 plot_predictions( glmm_mod, condition = list("scaled_exp", "Agreement", "Condition", "Group"), re.form = NA ) + aes(linetype = Agreement) + # 将线型映射到Agreement facet_wrap(~ Group + Condition, ncol = 4) + scale_color_manual(values = c("Agree" = "blue", "Disagree" = "red")) + scale_fill_manual(values = c("Agree" = "blue", "Disagree" = "red")) + scale_linetype_manual(values = c("Agree" = "solid", "Disagree" = "longdash")) + theme_classic() + labs(x = "标准化经验值", y = "接受概率预测值")
内容的提问来源于stack exchange,提问作者user9974638
相关产品推荐
相关产品推荐

