修改modelsummary表格shape后拟合优度消失的解决方法问询
解决modelsummary修改shape后拟合优度指标消失的问题
当使用shape=model ~ term + statistic转置表格时,拟合优度(GOF)指标默认会被隐藏,以下是两种有效的解决方法:
方法一:使用gof_position参数直接保留GOF
通过设置gof_position="right",可以将GOF指标放置在表格右侧,与统计量列对齐,无需额外处理:
mod_custom <- lm(hp ~ mpg + drat, mtcars) modelsummary(mod_custom, shape = model ~ term + statistic, gof_position = "right" )
方法二:手动提取并合并GOF指标
如果需要更灵活的GOF展示格式,可以先提取GOF,再合并到转置后的表格中:
library(modelsummary) mod_custom <- lm(hp ~ mpg + drat, mtcars) # 生成转置后的系数表(不含GOF) coef_tab <- modelsummary(mod_custom, shape = model ~ term + statistic, output = "data.frame") # 提取GOF指标并整理格式 gof_data <- extract_gof(mod_custom) gof_tab <- data.frame( model = "mod_custom", term = gof_data$term, estimate = gof_data$value, std.error = NA, # GOF无标准误,留空占位 stringsAsFactors = FALSE ) # 合并系数表与GOF表,输出最终表格 final_table <- rbind(coef_tab, gof_tab) modelsummary(final_table, output = "gt") # 可替换为"kable"等其他输出格式
补充说明
之前尝试的gof_omit='NULL'和gof_map='all'无效的原因是:当使用shape=model ~ term + statistic时,GOF的显示逻辑由gof_position参数控制,而非gof_omit或gof_map,这两个参数仅在默认表格结构下生效。
内容的提问来源于stack exchange,提问作者ly li
相关产品推荐
相关产品推荐

