sjPlot的plot_model无法显示交互项名称问题求助
解决plot_model交互项名称显示异常及tab_model标记混用问题
问题背景
运行含多因子交互项的广义线性混合模型后,使用sjPlot包的plot_model()绘制森林图时,Y轴所有交互项均显示为“conditional”,添加auto.labels=T参数无改善;tab_model()输出中交互项标记混用X与冒号:,该问题出现于R、RStudio及所有包更新之后。
模型代码:
modelFINAL<- glmer(treatment ~ (1|Household_ID)+(1|Case)+(1|Villages)+ ProductionSystem*disease+ ProductionSystem*costs+ ProductionSystem*education+ ProductionSystem*age+ costs*income+ age+ income, data = GLM_data, family = binomial(link='logit'), control=glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=2e5)))
绘图代码:
plot_model(modelFINAL, show.values = TRUE,type='est',sort.est = T,transform=NULL, vline.color = "black",title="TITLE")
解决办法
1. 手动自定义交互项标签
直接通过rename参数为每个交互项指定显示名称,覆盖默认标签:
plot_model(modelFINAL, show.values = TRUE, type='est', sort.est = TRUE, transform=NULL, vline.color = "black", title="TITLE", rename = c( "ProductionSystem:disease" = "ProductionSystem × disease", "ProductionSystem:costs" = "ProductionSystem × costs", "ProductionSystem:education" = "ProductionSystem × education", "ProductionSystem:age" = "ProductionSystem × age", "costs:income" = "costs × income", "age" = "age", "income" = "income" ))
2. 手动指定模型术语
提取模型中的固定效应术语,通过terms参数传递给plot_model(),避免默认标签识别错误:
# 提取模型中的固定效应术语(排除截距项) fixed_terms <- names(fixef(modelFINAL))[-1] plot_model(modelFINAL, terms = fixed_terms, show.values = TRUE, type='est', sort.est = TRUE, transform=NULL, vline.color = "black", title="TITLE")
3. 统一tab_model的交互项标记
对于tab_model()的标记混用问题,使用transform参数将冒号替换为统一的交互符号:
tab_model(modelFINAL, transform = function(x) gsub(":", " × ", x))
也可通过term.labels参数完全自定义所有术语标签:
tab_model(modelFINAL, term.labels = c( "(Intercept)" = "截距", "ProductionSystem" = "ProductionSystem", "disease" = "disease", "ProductionSystem:disease" = "ProductionSystem × disease", "costs" = "costs", "ProductionSystem:costs" = "ProductionSystem × costs", "education" = "education", "ProductionSystem:education" = "ProductionSystem × education", "age" = "age", "ProductionSystem:age" = "ProductionSystem × age", "income" = "income", "costs:income" = "costs × income" ))
4. 回退至稳定版本
若确定为包更新导致的bug,可回退至sjPlot或lme4的旧版本:
# 先安装devtools install.packages("devtools") # 回退至sjPlot 2.8.10版本(示例,可根据实际情况调整) devtools::install_version("sjPlot", version = "2.8.10", repos = "http://cran.us.r-project.org")
内容的提问来源于stack exchange,提问作者Kathrin Loosli
相关产品推荐
相关产品推荐

