I am working with the fixest package in R to run a regression model, and I am using etable to display the results. I want to split the coefficients into two different columns based on whether they contain the string “main” or “second”. Here is a reproducible example of the data and the model:
library(data.table)
library(fixest)
# Create a fake dataset
set.seed(123)
n <- 1000
dt <- data.table(
y = rnorm(n),
height_main_1 = rnorm(n),
height_second_1 = rnorm(n),
height_main_2 = rnorm(n),
height_second_2 = rnorm(n),
weight_main_1 = rnorm(n),
weight_main_2 = rnorm(n),
weight_second_1 = rnorm(n),
weight_second_2 = rnorm(n),
second = sample(0:1, n, replace = TRUE),
control1 = rnorm(n),
control2 = rnorm(n),
id = sample(1:100, n, replace = TRUE),
FE2 = sample(1:50, n, replace = TRUE)
)
# Run the regression model
m2 <- feols(y ~ height_main_1 + height_second_1 + height_main_2 + height_second_2 + weight_main_1 + weight_second_1 + weight_main_2 + weight_second_2 + second + control1 + control2 | id + FE2, data = dt)
# Display the summary of the model
summary(m2)
How can I do that? Ideally, I want the variables that differ only in the “main”/”second” part of the name to be labeled in the same way, so that the coefficients will be on the same line but in two different columns.
Note that I want to export the results in latex.
Thanks for your help!