我使用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))