rlatex

R etable: how can I split the coefficients of a fixest object into two different columns?


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)


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)
)


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)

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!


Solution

  • I think I found a solution, it's a bit long maybe but it does the job.

    I first create two data frames containing the results of the model m2, selecting only for the first only the "main" coefficients and for the second dataframe only the "second" ones. I then combine the two data frames, and after some formatting I export the dataset as a latex table using the package xtable. Here is the code:

    library(xtable)
    
    ## create the first column - only "main" coefficients
    setFixest_dict(dict=c("height_main_1"="height 1", "weight_main_1"="weight 1", 
                          "height_main_2"="height 2", "weight_main_2"="weight 2",
                          "height_second_1"="todrop", "height_second_2"="todrop2",
                          "weight_second_1"="todrop3", "weight_second_2"="todrop4",
                          "second"="todrop_"
    ))
    
    order <- c("^height$", "^weight$", "!2", "!1", "!weight")
    
    # transform the regression results in a dataframe
    t2_main <- etable(m2, order=order, tex=FALSE,se.below=TRUE,
                        drop=c("control", "todrop"))
    colnames(t2_main) <- c("Vars", "Main")
    setDT(t2_main)[, counter := 1:.N]
    
    
    # second column - only "second" coefficients
    setFixest_dict(dict=c("height_main_1"="todrop", "weight_main_1"="todrop2", 
                          "height_main_2"="todrop3", "weight_main_2"="todrop4",
                          "height_second_1"="height 1", "height_second_2"="height 2",
                          "weight_second_1"="weight 1", "weight_second_2"="weight 2",
                          "second"="todrop_"
    ))
    
    order <- c("^height$", "^weight$", "!2", "!1", "!weight")
    
    t2_second <- etable(m2, order=order, tex=FALSE,se.below=TRUE,
                           drop=c("control", "todrop"))
    colnames(t2_second) <- c("Vars", "Second")
    
    
    # merge the two datasets and export them as a latex table using table
    table <- cbind(t2_main, t2_second %>% select(-Vars))
    
    
    # select only the relevant variables
    table <- table[counter  %in% c(c(3:10), 15,11,12) ]
    setkey(table,counter)[, counter:=NULL]
    
    
    
    addtorow <- list()
    addtorow$pos <- list(0,0,8,8)
    addtorow$command <- c("& \\multicolumn{2}{c}{Outcome variable} \\\\\n",
                          " & (1) & (2) \\\\\n",
                          "& & \\\\\n",
                          "context & main & second \\\\\n")
    
    print(xtable(table), compress = TRUE,include.rownames = FALSE, include.colnames=FALSE,
          hline.after = c(-1, 0, nrow(xtable(table))), 
          floating=FALSE, add.to.row = addtorow, booktabs=TRUE,
          file=paste0(PATH, "Regtable.tex"))