modelsummary重命名估计列时出现重复列的技术求助
解决modelsummary列重命名重复问题(波兰语表格适配)
问题原因
你代码里的statistic参数被错误用来定义估计值列,这个参数的作用是添加额外统计量行(比如标准误差、t值),而非重命名默认的估计值列,所以导致估计值列重复出现。
解决方案1:在modelsummary内直接处理
先正常生成表格,再修改默认列名:
library(modelsummary) library(tinytable) N <- 1e4 x <- rnorm(N) x2 <- rnorm(N, 1.3, .8) y <- rnorm(N, 2 + .3*x - .6*x2) df_to_lm <- data.frame(x, x2, y) lm_totable_1 <- lm(y ~ x, df_to_lm) lm_totable_2 <- lm(y ~ x + x2, df_to_lm) lm_totable_3 <- lm(y ~ x*x2, df_to_lm) # 生成基础表格,仅在statistic中指定标准误差的波兰语名称 tab <- modelsummary( models = list("Model 1" = lm_totable_1, "Model 2" = lm_totable_2, "Model 3" = lm_totable_3), fmt = 2, statistic = c("Błąd Standardowy" = "{std.error}"), coef_omit = "Intercept", shape = term ~ model + statistic, gof_map = "r.squared" ) # 将默认的"Estimate"列名替换为波兰语"B" colnames(tab) <- gsub("Estimate", "B", colnames(tab)) tab
解决方案2:用tinytable后期修改列名
指定输出为tinytable对象后,直接修改列名:
# 生成tinytable格式的表格 tab <- modelsummary( models = list("Model 1" = lm_totable_1, "Model 2" = lm_totable_2, "Model 3" = lm_totable_3), fmt = 2, statistic = c("Błąd Standardowy" = "{std.error}"), coef_omit = "Intercept", shape = term ~ model + statistic, gof_map = "r.squared", output = "tinytable" ) # 替换列名中的"Estimate"为"B" tab <- tab |> modify_cols( colnames = gsub("Estimate", "B", colnames(tab)) ) tab
内容的提问来源于stack exchange,提问作者Honestiore
相关产品推荐
相关产品推荐

