Using panelsummary with fixest

This vignette focuses on using the panelsummary package with the fixest package. fixest has become one of the most popular regression packages for economists, and therefore further capabilities have been implemented in panelsummary to work seamlessly with fixest. There will be two main capabilities that this vignette covers:

  1. Adding the mean of the dependent variable.
  2. Collapsing fixed effects.

Adding the Mean of the Dependent Variable

To start, load in the fixest, panelsummary, and kableExtra packages.

library(panelsummary)
library(fixest) ## for regressions
library(kableExtra) ## for further table customization

For demonstration purposes, four regressions are estimated with the fixest package using the feols function and the mtcars data:

## estimating two models for mpg
mpg_1 <- mtcars |>
    feols(mpg ~  cyl | gear + carb, cluster = ~hp)

mpg_2 <- mtcars |>
    feols(mpg ~  cyl | gear + carb + am, cluster = ~hp)


## estimating the same two models for disp
disp_1 <- mtcars |> 
  feols(disp ~ cyl | gear + carb, cluster = ~hp)

disp_2 <- mtcars |> 
  feols(disp ~ cyl | gear + carb + am, cluster = ~hp)

To create a regression table with two panels (Panel A: MPG/Panel B: Displacement) and two models in each, the lists list(mpg_1, mpg_2) and list(disp_1, disp_2) can be passed into the arguments for .... However, since each of these models is of the class fixest (i.e., created with the fixest package), the mean_dependent argument can be set to TRUE to show the means of the dependent variable in each column:

panelsummary(list(mpg_1, mpg_2), list(disp_1, disp_2), 
             mean_dependent = TRUE,
             panel_labels = c("Panel A: MPG", "Panel B: Displacement"),
             caption = "Automated Mean of Dependent Variable") |> 
  kable_classic(full_width = F, html_font = "Cambria") 
Automated Mean of Dependent Variable
Panel A: MPG
cyl -0.907 -1.093
(0.789) (0.801)
Mean of Dependent Variable 20.091 20.091
Num.Obs. 32 32
R2 0.818 0.835
R2 Adj. 0.755 0.768
R2 Within 0.058 0.088
R2 Within Adj. 0.017 0.046
AIC 168.3 167.1
BIC 181.4 181.8
RMSE 2.53 2.41
Std.Errors by: hp by: hp
FE: gear X X
FE: carb X X
FE: am X
Panel B: Displacement
cyl 43.306 43.905
(13.075) (13.489)
Mean of Dependent Variable 230.722 230.722
Num.Obs. 32 32
R2 0.909 0.910
R2 Adj. 0.878 0.873
R2 Within 0.399 0.401
R2 Within Adj. 0.373 0.373
AIC 339.5 341.4
BIC 352.7 356.0
RMSE 36.76 36.68
Std.Errors by: hp by: hp
FE: gear X X
FE: carb X X
FE: am X

Notice that a new row was placed before the “Num.Obs.” row in each panel with the mean of the dependent variable. This is the default setting. If you would like to change where the mean is placed, you will need to customize using the panelsummary::panelsummary_raw function (see the Adding Rows to a panelsummary Table vignette for more details). Now, using gof_map (see the modelsummary website) the goodness-of-fit statistics will be mapped/renamed. Note that the mean of the dependent variable is not included in the mapped statistics and cannot be reordered without using panelsummary::panelsummary_raw.

## creating a renaming tibble to pass into panelsummary 
gm <- tibble::tribble(
        ~raw,      ~clean,          ~fmt,  ~omit,
        "nobs", "Observations", 0,  FALSE,
        "FE: gear", "FE: Gear", 0, FALSE,
        "FE: carb", "FE: Carb", 0, FALSE,
        "FE: am", "FE: AM", 0, FALSE
)

## creating the regression table
panelsummary(list(mpg_1, mpg_2), list(disp_1, disp_2),
             mean_dependent = TRUE,
             panel_labels = c("Panel A: MPG", "Panel B: Displacement"),
             gof_map = gm,
             caption = "Automated Mean of Dependent Variable-Renamed and Reordered") |> 
  kable_classic(full_width = F, html_font = "Cambria") 
Automated Mean of Dependent Variable-Renamed and Reordered
Panel A: MPG
cyl -0.907 -1.093
(0.789) (0.801)
Mean of Dependent Variable 20.091 20.091
Observations 32 32
FE: Gear X X
FE: Carb X X
FE: AM X
Panel B: Displacement
cyl 43.306 43.905
(13.075) (13.489)
Mean of Dependent Variable 230.722 230.722
Observations 32 32
FE: Gear X X
FE: Carb X X
FE: AM X

Lastly, as an important aside, any models that are not of class fixest will not return a dependent variable mean. There are plans to accommodate more models for this feature in the future.

Collapsing Fixed Effects

Notice that in the tables above that the fixed effects in columns (1) and (2) are the same across each panel. To reduce the number of rows, the collapse_fe argument can be used to show a separate panel containing only the fixed effects. Observe that there is also a warning message returned when this command is used; the warning message is telling the user that the function assumes both panels have the same fixed effects for the same models.

## collapsing fixed effects with collapse_fe = T
panelsummary(list(mpg_1, mpg_2), list(disp_1, disp_2),
             mean_dependent = TRUE,
             panel_labels = c("Panel A: MPG", "Panel B: Displacement"),
             gof_map = gm,
             caption = "Collapsed Fixed Effects",
             collapse_fe = T) |> 
  kable_classic(full_width = F, html_font = "Cambria") |> 
  footnote("Collapsing the fixed effects assumes the fixed effects in all panels are the same!")
#> Warning in panelsummary(list(mpg_1, mpg_2), list(disp_1, disp_2),
#> mean_dependent = TRUE, : panelsummary does not check if the fixed effects in
#> each panel match. It always assumes they do!
Collapsed Fixed Effects
Panel A: MPG
cyl -0.907 -1.093
(0.789) (0.801)
Mean of Dependent Variable 20.091 20.091
Observations 32 32
Panel B: Displacement
cyl 43.306 43.905
(13.075) (13.489)
Mean of Dependent Variable 230.722 230.722
Observations 32 32
FE: Gear X X
FE: Carb X X
FE: AM X
Note:
Collapsing the fixed effects assumes the fixed effects in all panels are the same!

Although it does not appear in HTML, a LaTeX version of this table will include a horizontal line separating the fixed effects from the other panels. This can be switched off using the hline_before_fe argument.