jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
162 stars 22 forks source link

Integration with fixest models #123

Closed mattysimonson closed 1 month ago

mattysimonson commented 2 years ago

The fixest package which came out last year has been a real boon to researchers using regression, and for the most part, it plays well with jtools (this is important because while fixest can make beautiful tables, it relies on base R for plotting which is a pain for those of us used to ggplot2). A key feature of fixest is that you can run multiple models at once (e.g., on a split sample, or swapping out different outcome variables with the same set of preditors). When you do this, the results get saved as a fixest_multi object, which, from what I can tell, appears to be a list of individual fixest model objects. The plot_summs function can handle individual fixest model objects just fine, but if they're stored in a fixest_multi, then you have to break them out manually:

modeling_3_outcomes <- fixest::feglm( c(support, favorability, trust) ~ income + gender + age, data = mydata) plot_summs(modeling_3_outcomes[[1]], modeling_3_outcomes[[2]], modeling_3_outcomes[[3]]

and realistically, you'd want to rename them after their DVs so the legend doesn't label each color as model 1, model 2, model 3:

plot_summs(support = modeling_3_outcomes[[1]], favorability = modeling_3_outcomes[[2]], trust = modeling_3_outcomes[[3]]

Ideally, I would be able to simply input plot_summs(my_fixest_multi) and plot_summs would unpack this list and extract the model names (e.g. the DV names) automatically.

There are probably other ways to make the integration of these two packages more seamless, but this is the first one that comes to mind, and it seems like reasonably low-hanging fruit to start with (at least the unpacking part is; less sure how easy it is to extract the DV names).

jacob-long commented 1 month ago

As I finally look into this, I think the solution turns out to be relatively straightforward. Normally, a list of models should be handled by plot_coefs() smoothly. But since the "fixest_multi" object doesn't identify itself as a list, for probably good reasons, plot_coefs() doesn't know it's a list and treats it like it's a single model. While I tend to prefer general solutions to specific ones, I am just going to add a check for "fixest_multi" objects and treat them like lists, which seems to work.

library(fixest)
library(jtools)
est_mult <- feols(c(Ozone, Solar.R) ~ Wind + Temp + csw0(Wind:Temp, Day), airquality)
plot_coefs(est_mult)

image