1

我使用modelsummary()withkableExtra()在 Rmd 文件中生成回归表(最终输出格式:LaTex 和 HTML)。

我对几个变量组合和模型规范进行回归。回归通过变量组合在表中分组kable::add_header_above()

对于不同的变量组合,我运行相同的模型(例如 OLS 和 Poisson 或其他)。因此,为了提高可读性,我想简单地命名模型,例如

names(models) <- c("OLS", "Poisson", "OLS", "Poisson", ...)

代替

names(models) <- c("OLS 1", "Poisson 1", "OLS 2", "Poisson 2", ...)

但是,modelsummary()不知何故不允许将回归命名为相同,从而导致以下错误:

Error: Can't bind data because some arguments have the same name
Backtrace:
  1. modelsummary::msummary(...)
  2. modelsummary::extract(...)
 10. dplyr::mutate(., group = "gof")
 12. dplyr:::mutate_cols(.data, ...)
 13. DataMask$new(.data, caller_env())
 14. .subset2(public_bind_env, "initialize")(...)
 17. rlang::env_bind_lazy(...)
 18. rlang:::env_bind_impl(.env, exprs, "env_bind_lazy()", TRUE, binder)

 Error in htmlTable_add_header_above(kable_input, header, bold, italic,  : 
 The new header row you provided has a different total number of columns with the original `kabel()` output.

MWE:

library(modelsummary)
library(kableExtra)

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)

models <- list()
models[['OLS']] <- lm(Crime_prop ~ Literacy, data = dat)
models[['Poisson']] <- glm(Crime_prop ~ Literacy + Clergy, family = poisson, data = dat)
models[['OLS']] <- lm(Crime_pers ~ Literacy, data = dat)
models[['Poisson']] <- glm(Crime_pers ~ Literacy + Clergy, family = poisson, data = dat)

# build table with `modelsummary` 
cm <- c( '(Intercept)' = 'Constant', 'Literacy' = 'Literacy (%)', 'Clergy' = 'Priests/capita')
cap <- 'A modelsummary table customized with kableExtra'

tab <- msummary(models, output = 'kableExtra',
                coef_map = cm, stars = TRUE,
                title = cap, gof_omit = 'IC|Log|Adj')

# customize table with `kableExtra`
tab %>%
  
  # column labels
  add_header_above(c(" " = 1, "Crimes (property)" = 2, "Crimes (person)" = 2))

添加在:

一种解决方法是在模型名称中添加一个空格" ",然后再使用以下命令构建表modelsummary

names(models) <- c("OLS", "Poisson", "OLS ", "Poisson ", ...)

对于少数模型规格和变量组合,手动操作很容易实现。但是,可以动态适应给定设置的解决方案将是首选,即也适合以下情况:

names(models) <- c("OLS", "Poisson", "GLM", "Poisson", ...)

代替

names(models) <- c("OLS 1", "Poisson 1", "GLM 2", "Poisson 2", ...)

更新:

通过@Vincent 提供的更新包版本,具有相同名称的模型的回归表也可以轻松地用于存储在嵌套列表中的模型,例如,如果它们被添加到循环中的子列表或通过 lapply(..., FUN )。

models <- NA
models <- list()
models[["a"]][["OLS"]] <- lm(Crime_prop ~ Literacy, data = dat)
models[["a"]][["Poisson"]] <- glm(Crime_prop ~ Literacy + Clergy, family = poisson, data = dat)
models[["b"]][["OLS"]] <- lm(Crime_pers ~ Literacy, data = dat)
models[["b"]][["Poisson"]] <- glm(Crime_pers ~ Literacy + Clergy, family = poisson, data = day)
# ...

models_unlisted <- unlist(models, recursive=FALSE)
names(models_unlisted) <- c('ols', 'poisson', 'ols', 'poisson')

cm <- c( '(Intercept)' = 'Constant', 'Literacy' = 'Literacy (%)', 'Clergy' = 'Priests/capita')

msummary(models_unlisted, output = 'kableExtra', statistic_vertical = FALSE,
         coef_map = cm, stars = TRUE, gof_omit = 'IC|Log|Adj') %>%
  add_header_above(c(" " = 1, "Crimes (property)" = 2, "Crimes (person)" = 2))

模型汇总回归表输出

4

2 回答 2

1

目前,您的 MWE 中的第 3 和第 4 个模型会覆盖前两个模型,因此models列表中只有两个元素,这会给您带来different total number of columns错误。

如果只是为了便于阅读,您可以在第 3 和第 4 个模型中的名称后添加一个空格,其余的应该可以很好地显示。

models[['OLS ']] <- lm(Crime_pers ~ Literacy, data = dat)
models[['Poisson ']] <- glm(Crime_pers ~ Literacy + Clergy, family = poisson, data = dat)
于 2020-06-27T16:52:00.773 回答
1

谢谢你的问题。另一张海报是对的:你在 MWE 下的解决方案永远不会奏效,因为它与R语言的基本特征有关。分配给列表中的相同名称会覆盖先前的值。看:

a <- list()
a['blah'] <- 1
a['blah'] <- 2
a

我知道的最简单的技巧是已经提出的技巧:在名称后添加一个空格。这有一个主要缺点:使用按名称选择列来自定义它们变得更加困难gtor kableExtra。但除此之外,它是无害的,因为所有的制表包在显示表格之前都会去除空白区域。

阅读您的问题后,我自动添加了一行代码来modelsummary“填充”模型名称。如果你从 Github 安装(我将很快发布到 CRAN),你应该能够运行这个:

library(remotes)
install_github('vincentarelbundock/modelsummary')

library(modelsummary)
library(kableExtra)

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)

models <- list()
models[[1]] <- lm(Crime_prop ~ Literacy, data = dat)
models[[2]] <- glm(Crime_prop ~ Literacy + Clergy, family = poisson, data = dat)
models[[3]] <- lm(Crime_pers ~ Literacy, data = dat)
models[[4]] <- glm(Crime_pers ~ Literacy + Clergy, family = poisson, data = dat)
names(models) <- c('ols', 'poisson', 'ols', 'poisson')

cm <- c( '(Intercept)' = 'Constant', 'Literacy' = 'Literacy (%)', 'Clergy' = 'Priests/capita')
cap <- 'A modelsummary table customized with kableExtra'

msummary(models, output = 'kableExtra',
         coef_map = cm, stars = TRUE,
         title = cap, gof_omit = 'IC|Log|Adj') %>%
       add_header_above(c(" " = 1, "Crimes (property)" = 2, "Crimes (person)" = 2))

PS:如果您有功能请求,请在 Github 上打开一个问题:https ://github.com/vincentarelbundock/modelsummary/issues

于 2020-06-29T12:37:47.760 回答