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

如何修改ggcoef_model中变量水平标签并隐藏指定水平?

问题解决方法

1. 将animal变量的“d”水平标签修改为“dog”

有两种实用方案,可按需选择:

方案一:修改数据框因子水平后重新拟合模型(推荐)

直接在数据层面修正animal的因子标签,模型系数名会同步更新,后续可视化无需额外调整:

# 重定义animal的因子水平与对应标签
df$animal <- factor(df$animal, 
                    levels = c("d", "cat", "cow"), 
                    labels = c("dog", "cat", "cow"))

# 重新拟合模型
testM <- glmer(hunger ~ animal + treat + animal*treat  + 
            (1 + animal + treat + animal*treat||owner), 
          data = df,  family="binomial", 
          glmerControl(optimizer="bobyqa", optCtrl = list(maxfun=1e5)))

# 可视化时animal的"d"已自动显示为"dog"
ggcoef_model(testM, include = !contains(c("owner","Residual.sd")))

方案二:不重新拟合模型,直接在可视化阶段修改标签

若不想重复拟合模型,可通过scale_x_discrete手动替换目标标签:

p <- ggcoef_model(testM, include = !contains(c("owner","Residual.sd")))
p + scale_x_discrete(labels = function(x) {
  gsub("animald", "animaldog", x)
})

2. 隐藏treat变量下“A1”水平的可视化

两种简单实现方式:

方式一:通过include参数精准排除

利用正则匹配目标项,在可视化函数中直接设置排除规则:

ggcoef_model(testM, 
             include = !contains(c("owner","Residual.sd")) & !grepl("treatA1", term))

方式二:生成图表后过滤条目

结合dplyr过滤掉不需要的系数项:

library(dplyr)

ggcoef_model(testM, include = !contains(c("owner","Residual.sd"))) %>%
  filter(!grepl("treatA1", term))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 03:20:01