1

Suppose I have a table summarizing regressions using the modelsummary package such as the following:

library(fixest)
library(modelsummary)

reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)
I plot the summary of the two regressions in a table such as the following:

tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),stars = TRUE, statistic = c("s.e. = {std.error}"), estimate = "estimate", fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")))

I now will add a column of values to the table using the argument add_columns(data.frame(c("x","","x","","",""))) This works, however it will give me an arbitrary column name. How can I rename this added column within the modelsummary if I rename the vector/dataframe outside the modelsummary this would not be ideal as I need to add multiple columns with shared names but different values.

Also, how can one add multiple columns in a different order within the add_columns argument? For example, if I wanted new_col_1 after the original first regression and another column new_col_2 after the second regression, how can I do this?

flâneur
  • 633
  • 2
  • 8

2 Answers2

1

For anyone who encounters the same question I did, this does the trick:

library(fixest)
library(modelsummary)

reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)

#theoretical columns to add
new_col_1 <- c("x","","x","","","")
new_col_2 <- c("x","","","","x","")
#make dataframe
cols <-data.frame(new_col_1,new_col_2)
#add desired names
names(cols) <- c("name1","name2")
#set position attribute as needed
attr(cols, "position") <- c(3,5)

tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),stars = TRUE, statistic = c("s.e. = {std.error}"), estimate = "estimate", fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")), add_columns = cols)

If the position is after the last column in the existing regression table, just leave that number blank when setting the attribute. modelsummary is a brilliant package!

flâneur
  • 633
  • 2
  • 8
1

You can also consider the following approach :

library(modelsummary)
library(fixest)
library(marginaleffects)
library(rvest)
library(dplyr)
library(rmarkdown)

reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)

tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3), stars = TRUE)
table <- as.data.frame(read_html(tab[[1]]) %>% html_table())

new_Col <- c(NA, "X", rep(NA, 11))
table <- bind_cols(table[, 1 : 3], new_Col, table[, 4])
tab <- knitr::kable(table)                    

You can convert the html table to a data.frame. After, you add a column to the data.frame and then, you convert the data.frame to a html table.

Emmanuel Hamel
  • 1,769
  • 7
  • 19
  • 1
    There is also the `datasummary_df()` function which accepts a data frame and returns a table in any of the formats supported by `modelsummary`. The benefit is you can output to `kableExtra`, `gt`, and friends with exactly the same syntax and arguments used by the core `modelsummary` functions. – Vincent May 15 '23 at 00:06