0

Again, thanks to Laurent for answering questions and supporting the modelsummarypackage.

library(tidyverse)
library(fixest)
library(modelsummary)
fit<-mtcars %>% feols(c(mpg,hp )~1)
fit_1 <- mtcars %>% feols(c(mpg,hp,wt )~1)
fit_2 <- mtcars %>% feols(c(mpg,hp,gear, wt )~1)
modelsummary(c(fit, fit_1, fit_2), shape=model + statistic ~ term, output="flextable")

We obtain the long column of estimates from all 3 models (which are simple averages) as in:

original

So is there a way to rearrange the columns and the rows either using internal modelsummary functions or external work to the following format:

goal

The biggest problem is moving the terms around so that they are aligned on the same line (note that the order of terms between fit_1 and fit_2 is changed) and the rest is filled with NA. Would really appreciate any help! It's a part of a larger problem I've been trying to solve unsuccessfully for the last 3 weeks.

  • 1
    Hi! I need to correct a fact: It's `modelsummary` that supports `fixest` and not the other way around :-) Vincent has been doing a fantastic job in so doing. As regards the question: no idea, sorry! Good luck! – Laurent Bergé Nov 03 '22 at 08:12
  • Hi Laurent! Sorry I've been going between your and Laurent's packages a lot in the last few months and got confused. Thank you both for your contributions to science! – astrae_research Nov 04 '22 at 02:26

1 Answers1

1

One option is to output to a data frame and reshape manually:

library(tidyverse)
library(fixest)
library(modelsummary)
library(flextable)
fit<-mtcars %>% feols(c(mpg,hp )~1)
fit_1 <- mtcars %>% feols(c(mpg,hp,wt )~1)
fit_2 <- mtcars %>% feols(c(mpg,hp,gear, wt )~1)
models <- c(fit, fit_1, fit_2)

modelsummary(
    models,
    output = "dataframe",
    shape = model + statistic ~ term) |>
    mutate(
      fit = c(rep(1, 4), rep(2, 6), rep(3, 8)),
      model = trimws(model)) |>
    pivot_wider(names_from = "fit", values_from = "(Intercept)") |>
    select(-statistic, -part, ` ` = model) |>
    flextable()

enter image description here

This is a very customized shape and modeling context, so I can't currently think of a way to achieve that purely using internal modelsummary functions arguments.

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    This is great! Thank you Vincent! I spent several days on building a different approach but a pivot_wider works great here. I extracted the rep factors from the original fixest_multi models and made `fit = c(rep(1, 4), rep(2, 6), rep(3, 8))` driven by the model themselves. – astrae_research Nov 04 '22 at 06:49