如何调整tab_model输出的MLM系数顺序并指定显示项?
解决tab_model系数顺序不生效的问题
核心问题原因
你设置的auto.label = TRUE会覆盖自定义的系数顺序和标签,这个参数会自动从模型中提取变量标签和默认顺序,导致你指定的terms和pred.labels失效。
具体解决步骤
- 关闭自动标签功能:把
auto.label = TRUE改成auto.label = FALSE,这样tab_model才会遵循你指定的顺序和自定义标签。 - 对齐pred.labels和terms的内容:你的
coefs里包含了mother_dummy_12011,但terms里没列这个变量,建议把pred.labels调整为只包含terms里的变量,且顺序和terms完全一致,避免标签错乱。 - (可选)提前设置因子顺序:如果
mother_l1_recode是因子变量,在拟合模型前就用factor()函数指定因子水平顺序,比如:
这样模型拟合后,默认输出顺序就是你想要的,后续用tab_model时更省心。df$mother_l1_recode <- factor(df$mother_l1_recode, levels = c("mother_l1_recode1", "mother_l1_recode2", ..., "mother_l1_recodenot_provided"))
修改后的代码示例
# 调整pred.labels,只保留terms里的变量,顺序和terms一致 coefs <- c( "mother_l1_recode1" = "Northern European", "mother_l1_recode2" = "Southern European", "mother_l1_recode3" = "Eastern European", "mother_l1_recode4" = "Southwest and Central Asian", "mother_l1_recode5" = "Southern Asian", "mother_l1_recode6" = "Southeast Asian", "mother_l1_recode7" = "Eastern Asian", "mother_l1_recode9" = "Other", "mother_l1_recodenot_provided" = "Not provided" ) tab_model(mlm1, mlm1_non1201, title = " ", pred.labels = coefs, terms = c( "mother_l1_recode1", "mother_l1_recode2", "mother_l1_recode3", "mother_l1_recode4", "mother_l1_recode5", "mother_l1_recode6", "mother_l1_recode7", "mother_l1_recode9", "mother_l1_recodenot_provided" ), show.intercept = FALSE, show.ci = FALSE, show.se = TRUE, string.se = "S.E.", dv.labels = c( paste("Year", parse_number(Year_Level_2_use), sep = " "), paste("Year", parse_number(Year_Level_2_use)+4, sep = " ") ), string.pred = as.character(year), string.est = "Coefficient", p.style = "stars", show.icc = FALSE, show.re.var = FALSE, show.obs = FALSE, show.r2 = FALSE, show.ngroups = FALSE, auto.label = FALSE # 关键修改:关闭自动标签 )
内容的提问来源于stack exchange,提问作者m45ha
相关产品推荐
相关产品推荐

