重命名modelsummary中的add_columns列并添加多个列。

huangapple go评论64阅读模式
英文:

Rename the add_columns columns in modelsummary and add multiple columns

问题

You can rename the added column within the modelsummary by specifying the add_columns argument with a named list. Here's how you can do it:

# Define the data frame with your values and desired column names
new_columns <- data.frame(new_col_1 = c("x", "", "x", "", ""),
                          new_col_2 = c("", "x", "", "", ""))

# Add the columns with specified names
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 = new_columns)

This will add two columns named "new_col_1" and "new_col_2" to the table with the specified values.

To add multiple columns in a different order within the add_columns argument, you can reorder the columns in your new_columns data frame accordingly. For example:

# Define the data frame with your values and desired column names in the desired order
new_columns <- data.frame(new_col_2 = c("", "x", "", "", ""),
                          new_col_1 = c("x", "", "x", "", ""))

# Add the columns with specified names and order
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 = new_columns)

This will add "new_col_2" after the first regression and "new_col_1" after the second regression, as per your desired order.

英文:

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

library(fixest)
library(modelsummary)

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

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

I now will add a column of values to the table using the argument add_columns(data.frame(c(&quot;x&quot;,&quot;&quot;,&quot;x&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;)))
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?

答案1

得分: 1

以下是您要翻译的代码部分:

如果位置在现有回归表中的最后一列之后,请在设置属性时将该数字留空。 "modelsummary" 是一个出色的包!
英文:

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

library(fixest)
library(modelsummary)

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

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

tab &lt;- modelsummary(list(&quot;HP&quot; = reg1, &quot;WT&quot; = reg2, &quot;GEAR&quot; = reg3),stars = TRUE, statistic = c(&quot;s.e. = {std.error}&quot;), estimate = &quot;estimate&quot;, fmt = fmt_statistic(p.value = fmt_sprintf(&quot;%.2e&quot;)), 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!

答案2

得分: 1

你可以考虑以下方法:

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)

你可以将HTML表格转换为数据框。之后,你可以向数据框添加一列,然后将数据框转换回HTML表格。

英文:

You can also consider the following approach :

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

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

tab &lt;- modelsummary(list(&quot;HP&quot; = reg1, &quot;WT&quot; = reg2, &quot;GEAR&quot; = reg3), stars = TRUE)
table &lt;- as.data.frame(read_html(tab[[1]]) %&gt;% html_table())

new_Col &lt;- c(NA, &quot;X&quot;, rep(NA, 11))
table &lt;- bind_cols(table[, 1 : 3], new_Col, table[, 4])
tab &lt;- 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.

huangapple
  • 本文由 发表于 2023年5月15日 02:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定