总结
我正在尝试使用 marginaleffects::plot_predictions() 来绘制 GLMM 的预测。下面提供了一个最小的工作示例。
目标
我试图仅在以下 GLMM 中绘制四向交互的预测 scaled_exp * 协议 * 条件 * 组:
glmer(Acceptance ~ scaled_exp * Agreement * Condition * Group +
scaled_age * Agreement * Group +
(1 | Item) +
(1 + Condition | Subject)
但是,如果不是每个预测变量都包含在条件列表中(即,我们对绘制 scaled_age 的影响不感兴趣),那么 margineffects::plot_predictions() 似乎会遇到问题(仅适用于 GLMM,不适用于 LMM) ,它只是为了控制老化对研究结果的影响),但如果我尝试在没有 scaled_age 的情况下运行它,我会收到以下错误。
警告:矩阵列不支持作为预测变量,因此被省略。这可能会妨碍数量的计算 出于兴趣。您可以构建自己的预测数据集并将其显式提供给
参数。警告:某些列是多列类型(例如矩阵列):[2, 6]。 setDT 将按原样保留这些列,但后续操作(如分组和联接)可能会失败。请考虑 as.data.table() ,它将为每个嵌入列创建一个新列。错误:无法使用此模型计算预测值。您可以尝试为newdata
参数提供不同的数据集。还引发了此错误:找不到对象“scaled_age”。错误跟踪器:https://github.com/vincentarelbundock/marginaleffects/issuesnewdata
我没有遇到具有类似固定和随机效应结构的相应 LMM 的这些问题,所以我怀疑这与此有关。
MWE
# Load necessary libraries
library(lme4)
library(tidyverse)
library(marginaleffects)
# Create a sample dataset
set.seed(123)
n <- 5000
sample_data <- tibble(
Subject = as.factor(rep(1:500, each = 10)), # Increase the number of subjects
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))
)
# Fit the generalized linear mixed model
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)))
# Generate predictions for the four-way interaction using plot_predictions()
plot_predictions(
glmm_mod,
condition = list("scaled_exp", "Agreement", "Condition", "Group"),
re.form = NA
) +
aes(linetype = Agreement) + # Map linetype to 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 = "Scaled Experience", y = "Predicted Probability of Acceptance")
如果您再次仔细阅读错误消息,您会发现其中很大一部分与
marginaleffects
不支持“矩阵列”有关。如果您检查数据集,您会注意到某些列确实属于“矩阵”类:
> class(sample_data$scaled_age)
[1] "matrix" "array"
事实证明
scale()
函数默认返回一个矩阵,而不是一个数值向量。要解决此问题,您可以使用 drop()
函数:
library(lme4)
library(tidyverse)
library(marginaleffects)
set.seed(123)
n <- 5000
sample_data <- tibble(
Subject = as.factor(rep(1:500, each = 10)), # Increase the number of subjects
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 = drop(scale(rnorm(n, 40, 15))),
scaled_exp = drop(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(
glmm_mod,
condition = list("scaled_exp", "Agreement", "Condition", "Group"),
re.form = NA
)