vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
912 stars 75 forks source link

List of `fixest_multi` objects #568

Closed astraetech closed 1 year ago

astraetech commented 1 year ago

Hi,

Thank you for continuing to develop modelsummary package! I was wondering if you could look into supporting fixest_multi from fixest? I guess it's when multiple DVs are used in a model.

Here is a reprex:

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,wt, gear )~1)

modelsummary(list(fit, fit_1, fit_2))

I get an error " Error: `modelsummary could not extract the required information from a model of class "fixest_multi". The package tried a sequence of 2 helper functions to extract estimates: .. "

Thank you!

vincentarelbundock commented 1 year ago

This should already work. Can you update both packages, restart R and try again? Also, please copy the output of sessionInfo() to show which versions of the packages your are using.

astraetech commented 1 year ago

Hi,

Updated the packages, restarted R and the machine, still gives the same error: "sessionInfo() R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] modelsummary_1.1.0 broom_1.0.1 forcats_0.5.2 stringr_1.4.1
[5] dplyr_1.0.99.9000 purrr_0.3.5 readr_2.1.3 tidyr_1.2.1
[9] tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2 fixest_0.11.0

loaded via a namespace (and not attached): [1] TH.data_1.1-1 googledrive_2.0.0 colorspace_2.0-3
[4] ellipsis_0.3.2 estimability_1.4.1 flextable_0.8.2
[7] parameters_0.19.0.10 base64enc_0.1-3 fs_1.5.2.9000
[10] rstudioapi_0.14 listenv_0.8.0 remotes_2.4.2
[13] mvtnorm_1.1-3 fansi_1.0.3 lubridate_1.8.0
[16] xml2_1.3.3 splines_4.2.1 codetools_0.2-18
[19] cachem_1.0.6 knitr_1.40 pkgload_1.3.0
[22] Formula_1.2-4 jsonlite_1.8.3 dbplyr_2.2.1
[25] shiny_1.7.2 compiler_4.2.1 httr_1.4.4
[28] emmeans_1.8.1-1 backports_1.4.1-9000 assertthat_0.2.1
[31] Matrix_1.5-1 fastmap_1.1.0 gargle_1.2.0
[34] lfe_2.8-8 cli_3.4.1 later_1.3.0
[37] htmltools_0.5.3 prettyunits_1.1.1 tools_4.2.1
[40] coda_0.19-4 gtable_0.3.1 glue_1.6.2.9000
[43] dreamerr_1.2.3 tables_0.9.10 Rcpp_1.0.9
[46] cellranger_1.1.0 vctrs_0.5.0.9000 nlme_3.1-160
[49] stargazer_5.2.3 insight_0.18.6 xfun_0.34
[52] globals_0.16.1 ps_1.7.2 rvest_1.0.3
[55] mime_0.12 miniUI_0.1.1.1 lifecycle_1.0.3
[58] devtools_2.4.4 googlesheets4_1.0.0 future_1.28.0
[61] MASS_7.3-58.1 zoo_1.8-11 scales_1.2.1
[64] hms_1.1.2 promises_1.2.0.1 parallel_4.2.1
[67] sandwich_3.0-2 huxtable_5.5.0.9000 memoise_2.0.1
[70] gdtools_0.2.4 stringi_1.7.8 bayestestR_0.13.0
[73] fastDummies_1.6.3 checkmate_2.1.0 pkgbuild_1.3.1
[76] zip_2.2.1 rlang_1.0.6.9000 pkgconfig_2.0.3
[79] systemfonts_1.0.4 evaluate_0.17 lattice_0.20-45
[82] htmlwidgets_1.5.4.9000 processx_3.8.0 tidyselect_1.2.0
[85] parallelly_1.32.1 magrittr_2.0.3.9000 R6_2.5.1.9000
[88] generics_0.1.3 profvis_0.3.7 multcomp_1.4-20
[91] DBI_1.1.3.9000 pillar_1.8.1 haven_2.5.1
[94] withr_2.5.0 survival_3.4-0 datawizard_0.6.3
[97] performance_0.10.0.3 future.apply_1.9.1 modelr_0.1.8
[100] crayon_1.5.2 uuid_1.1-0 utf8_1.2.2
[103] tzdb_0.3.0 rmarkdown_2.17 officer_0.4.4.001
[106] urlchecker_1.0.1 usethis_2.1.6.9000 grid_4.2.1
[109] readxl_1.4.1 data.table_1.14.5 callr_3.7.3
[112] reprex_2.0.1 digest_0.6.30 xtable_1.8-4
[115] httpuv_1.6.6 numDeriv_2016.8-1.1 munsell_0.5.0
[118] sessioninfo_1.2.2 "

The error: "> modelsummary(list(fit, fit_1, fit_2)) Error: `modelsummary could not extract the required information from a model of class "fixest_multi". The package tried a sequence of 2 helper functions to extract estimates:

parameters::parameters(model) broom::tidy(model)

To draw a table, one of these commands must return a data.frame with a column named "term". The modelsummary website explains how to summarize unsupported models or add support for new models yourself: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html

These errors messages were generated during extraction:

parameters::parameters(model) did not return a valid data.frame. broom::tidy(model) did not return a valid data.frame. In addition: Warning message: `modelsummary could not extract goodness-of-fit statistics from a model of class "fixest_multi". The package tried a sequence of 2 helper functions:

performance::model_performance(model) broom::glance(model)

One of these functions must return a one-row data.frame. The modelsummary website explains how to summarize unsupported models or add support for new models yourself:

https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html "

vincentarelbundock commented 1 year ago

Oh, I missed the solution the first time I looked. Here it is.

When you ask feols to fit multiple models, it returns a list of single models. If you plug that list directly in modelsummary, it should work without problem.

The issue here, is that you wrapped what was already a list inside another list, and modelsummary doesn't know what to do with a nested list of list of models. Instead of a nested list of lists, what you want to do is combine the three objects in a single list of models. Instead of:

modelsummary(list(fit, fit_1, fit_2))

Use the concatenation function c():

modelsummary(c(fit, fit_1, fit_2))
astraetech commented 1 year ago

Hi,

Good point! However, this does not differentiate between models? It just puts them all in a row. Using lists allows to separate models.

image

vincentarelbundock commented 1 year ago

You can rename the models in each fixest_multi ahead of time. Or do three steps: concatenate the list and rename and send to modelsummary.

Or you could use something like kableExtra::add_header_above

Tons of easy options

astraetech commented 1 year ago

Understood! Thank you!