rdplyrmodelsummaryfixest

Multiple fixest_multi models and shape parameter - modelsummary package


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.


Solution

  • 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.