You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R包ggeffects无法输出glmer模型分类预测的全部类别?

问题:ggeffects输出分类变量预测结果时缺失类别

我用glmer()构建了含12个水平分类预测变量Wshort的二项混合模型,summary()能显示全部12个水平的结果:

model.contcons <- data %>%
  glmer(bin_choice ~ Wshort + (1|id), data = ., family = binomial, control = glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=2e5)))

coef(summary(model.contcons))
#                      Estimate Std. Error     z value      Pr(>|z|)
# (Intercept)      -1.844979105 0.08002527 -23.0549555 1.311832e-117
# Wshortcutwelfare  0.028561627 0.10428477   0.2738811  7.841760e-01
# Wshortdiscipline -0.349133890 0.11034237  -3.1640963  1.555653e-03
# Wshortfreedom    -0.003641179 0.10245239  -0.0355402  9.716490e-01
# WshortineqincOK   0.107650142 0.10173642   1.0581278  2.899972e-01
# Wshortleader      0.125679107 0.10041647   1.2515786  2.107235e-01
# Wshortpolice      0.217560461 0.10133894   2.1468595  3.180447e-02
# Wshortpolitduty   0.177802391 0.09991568   1.7795244  7.515383e-02
# Wshortrefugees    0.109247617 0.10508525   1.0396094  2.985214e-01
# WshortRussia      0.115529761 0.10101235   1.1437192  2.527401e-01
# Wshorttaxesdown   0.176320660 0.10252782   1.7197347  8.548067e-02
# Wshortworse-off  -0.016075802 0.10455777  -0.1537504  8.778065e-01

但用ggeffects包的ggpredict()或ggemmeans()时,仅输出8个类别的预测结果,缺失了discipline、police、Russia、taxesdown这四个类别:

ggemmeans(model.contcons, "Wshort [all]")
# Predicted probabilities of bin_choice
# 
# Wshort     | Predicted |       95% CI
# -------------------------------------
# climate    |      0.14 | [0.12, 0.16]
# cutwelfare |      0.14 | [0.12, 0.16]
# freedom    |      0.14 | [0.12, 0.15]
# ineqincOK  |      0.15 | [0.13, 0.17]
# leader     |      0.15 | [0.13, 0.17]
# politduty  |      0.16 | [0.14, 0.18]
# refugees   |      0.15 | [0.13, 0.17]
# worse-off  |      0.13 | [0.12, 0.15]

如何让ggeffects输出全部12个类别的预测结果?


解决方法
  1. 检查因子水平完整性
    先确认原始数据中Wshort的因子水平是否包含全部12个类别:
levels(data$Wshort)

如果输出的水平列表缺失目标类别,需要重新将Wshort转换为包含所有水平的因子:

# 手动指定所有12个水平的向量
all_levels <- c("climate", "cutwelfare", "discipline", "freedom", "ineqincOK", "leader", "police", "politduty", "refugees", "Russia", "taxesdown", "worse-off")
data$Wshort <- factor(data$Wshort, levels = all_levels)
# 重新拟合模型
model.contcons <- data %>%
  glmer(bin_choice ~ Wshort + (1|id), data = ., family = binomial, control = glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=2e5)))
  1. 手动指定所有水平给ggeffects函数
    如果因子水平完整,但[all]参数未正确识别所有类别,可手动列出所有水平传入函数:
all_levels <- levels(data$Wshort)
# 生成包含所有水平的字符串参数
term_spec <- paste0("Wshort [", paste(all_levels, collapse = ", "), "]")
# 调用ggemmeans
ggemmeans(model.contcons, term_spec)
  1. 更新ggeffects包
    部分旧版本的ggeffects可能存在类别识别的bug,尝试更新到最新版本:
update.packages("ggeffects")
  1. 切换使用ggpredict并指定类型
    尝试使用ggpredict()并明确指定type = "fe"(固定效应预测),避免随机效应相关的筛选逻辑:
ggpredict(model.contcons, "Wshort [all]", type = "fe")

内容的提问来源于stack exchange,提问作者RandomMonitor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:50:43