I am using the feols command from the fixest package in R to estimate fixed effect regressions, in most cases with weights. In order to output my results, I am mainly using the modelsummary command from the modelsummary package. I have figured out how to use the add_rows feature to add further information about the models (in my cases, I'm using it to add indicators for the presence of control variables in a given model, in a similar fashion to how modelsummary can natively indicate the presence of fixed effects in a given model).
However, I would also like to add, to each model, the mean of the dependent variable of the model, estimated on the same observations as the given model is estimated (i.e. accounting for that some observations drop because of missing values, and that this can differ from model to model depending on the structure of missing values), and preferably only for observations with my treatment variable set to == 0
. How could I achieve this?
The basic regression structure I have is as follows (with some specifications being simpler, e.g. without the Municipality.ID):
model = feols(depvar ~ i(Year.factor, Treatment.dummy, ref ='2007') + Year.factor + Treatment.dummy + Control.var, data = subset(data.frame, condition < limit), weights = Weight.var, panel.id = c(Year.factor, Municipality.ID), cluster = ~Municipality.ID)
I loop these estimations to the list of models mdls. The modelsummary output code is more or less as follows:
mdl.stats <- list(
list("raw" = "nobs", "clean" = "Observations", "fmt" = 0),
list("raw" = "r.squared", "clean" = "R Squared", "fmt" = 3),
list("raw" = "r2", "clean" = "R Squared", "fmt" = 3),
list("raw" = "r2.within", "clean" = "R2", "fmt" = 3),
list("raw" = "within.r.squared", "clean" = "R2 (within)", "fmt" = 3),
list("raw" = "r.squared.within", "clean" = "R2 (within)", "fmt" = 3),
list("raw" = "FE: TERYT.f", "clean" = "Municipality FE", "fmt" = 0))
cntrls <- rep(c("", "X", ""), times=length(mdls)/3)
cntrls <- as.data.frame(t(c("Controls", cntrls)))
cntrls <- set_names(cntrls, c("Coefficients", names(mdls)))
modelsummary::modelsummary(mdls, gof_map = mdl.stats, stars = TRUE, output = "latex", booktabs = TRUE, add_rows = cntrls)
Sorry I cannot comment on Vincent's answer. Fixest provides the "mean of dependent variable" fitstat under "my", such that your glance_custom function can be simplified to:
and there is no need to depend on insight.