如何使用 modelsummary 函数在一个系数以上的回归 table 中添加文本行?

How to add a text line to your regression table above one coefficient using the modelsummary function?

我是 R 的新手,我第一次格式化回归 tables。我想在下面的 table 中的“Working class”上方添加一行。此行应显示“参考类别:Upper-class”。有人可以指导我一下,也许可以建议一些有趣的 examples/links,在那里我可以使用 modelsummary 函数找到格式化的 tables 吗?

这是我的代码:

results <-list("Pooled OLS" = Pooled.ols, "Fixed effects" = fixed.effects)
# Coefficients
cm <- c( 'age' = 'Age', 'I(age^2)' = 'Age square', 'wc' = 'Working class','mc' = 'Lower-middle class')
# Output Table
modelsummary(results, stars = TRUE,statistic = 'std.error', type= "html", 
             fmt= '%.4f',coef_map=cm
)

您可以按如下方式使用 add_rows 参数:

library(modelsummary)

mod <- lm(hp ~ mpg + vs + drat, mtcars)

row <- data.frame("Coefficients" = "Reference category: Upper-class",
                  "Model 1" = "")
attr(row, "position") <- 5
modelsummary(mod, add_rows=row)

编辑:我最初误解了这个问题,并认为你想添加一条水平线来分隔行。因为这在我们想要将自定义行添加到 table 时通常很有用,所以我在下面留下我的(错误但相关的)答案。

截至今天(2020-12-03),modelsummary 可以生成与四个 table 绘图包兼容的模型对象:kableExtra(默认),gtflextablehuxtable。这些软件包中的每一个都允许您自定义 table 的外观,但它们各自有不同的方法。

例如,如果您想自定义由 kableExtra 生成的默认 HTML table,您可以将自定义 CSS 提供给 row_spec 函数:

library(modelsummary)

mod <- lm(hp ~ mpg + vs + drat, mtcars)

library(kableExtra)
modelsummary(mod, gof_omit=".*") %>%
  row_spec(2, extra_css = "border-bottom: 3px solid")

请注意,kableExtra 对 LaTeX/PDF 输出使用略有不同的方法。见 package documentation.

如果您更愿意使用 gt 包,您可以设置 output 参数,然后使用 gttab_style 函数:

library(gt)
modelsummary(mod, output="gt", gof_omit=".*") %>%
  tab_style(style = cell_borders(sides="bottom", weight=px(4)), 
            locations = cells_body(rows = 2))