Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape=FALSE not functioning for panel titles with fixest_multi objects when exporting table in latex_tabular #622

Closed
chickymonkeys opened this issue Apr 11, 2023 · 1 comment

Comments

@chickymonkeys
Copy link

Hi!

I would like to share a potential issue with you as I have tried to circumvent it in several ways without any success. In a nutshell, the escape=FALSE option for modelsummary seems to ignore the titles of panels when using rbind or rcollapse, that keep to be escaped in case of latex content in there.

Here is a minimal reproducible example with mtcars:

library(modelsummary)
library(fixest)

# regression with fixest package
reg <- fixest::feols(
    c(mpg, hp) ~ sw(log(qsec), log(drat)) | gear + vs, data = mtcars
)

# grep panels 
panels_reg <- list(
    "Panel A. $\\log(qsec)_{gt}" = reg[grepl("qsec", names(reg))],
    "Panel B. $\\Delta(drat)_{gt}" = reg[grepl("qsec", names(reg))]
)

# some latex math mode for the coefficients 
coef_labels_reg <- rep(
        "$\\log({SO_{X}})_{g} \\times Post_{t}$",
    length(panels_reg[[1]])
)
names(coef_labels_reg) <- c("log(qsec)", "(logdrat)")

# adding some rows at the end (these are not escaped)
rows_reg <- tibble(
    a = c("\\hspace{1em}Gear FE", rep("$\\checkmark$", 2)),
    b = c("\\hspace{1em}VS FE", rep("$\\checkmark$", 2))
)
rows_reg <- data.frame(t(rows_reg))

# run modelsummary with `escape=FALSE` option on
modelsummary::modelsummary(
    panels_reg,
    output = "latex_tabular",
    coef_map = coef_labels_reg,
    shape = "rbind",
    stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
    col.names = NULL,
    add_rows = rows_reg,
    escape = FALSE
)

Here is my sessionInfo() output:

R version 4.2.3 (2023-03-15)
Platform: aarch64-apple-darwin22.3.0 (64-bit)
Running under: macOS Ventura 13.2.1

Matrix products: default
LAPACK: /opt/homebrew/Cellar/r/4.2.3/lib/R/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/POSIX/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] fstcore_0.9.14          tmaptools_3.1-1        
 [3] tmap_3.3-3              terra_1.7-21           
 [5] sf_1.0-11               rio_0.5.29             
 [7] fst_0.9.8               broom_1.0.4            
 [9] did_2.1.2               fixest_0.11.1          
[11] data.table_1.14.8       modelsummary_1.3.0.9013
[13] ggplot2_3.4.1           collapse_1.9.3         
[15] lubridate_1.9.2         stringr_1.5.0          
[17] tidyr_1.3.0             dplyr_1.1.0            
[19] here_1.0.1              pacman_0.5.1           

loaded via a namespace (and not attached):
  [1] readxl_1.4.2         backports_1.4.1     
  [3] systemfonts_1.0.4    lwgeom_0.2-11       
  [5] sp_1.6-0             crosstalk_1.2.0     
  [7] leaflet_2.1.2        httpgd_1.3.1        
  [9] digest_0.6.31        htmltools_0.5.4     
 [11] fansi_1.0.4          magrittr_2.0.3      
 [13] checkmate_2.1.0      tzdb_0.3.0          
 [15] openxlsx_4.2.5.2     BMisc_1.4.5         
 [17] readr_2.1.4          vroom_1.6.1         
 [19] sandwich_3.0-2       svglite_2.1.1       
 [21] timechange_0.2.0     colorspace_2.1-0    
 [23] rvest_1.0.3          haven_2.5.2         
 [25] xfun_0.37            leafem_0.2.0        
 [27] crayon_1.5.2         jsonlite_1.8.4      
 [29] DRDID_1.0.4          zoo_1.8-11          
 [31] glue_1.6.2           kableExtra_1.3.4    
 [33] stars_0.6-0          gtable_0.3.2        
 [35] webshot_0.5.4        car_3.1-1           
 [37] abind_1.4-5          scales_1.2.1        
 [39] DBI_1.1.3            rstatix_0.7.2       
 [41] Rcpp_1.0.10          viridisLite_0.4.1   
 [43] performance_0.10.2.6 units_0.8-1         
 [45] foreign_0.8-84       bit_4.0.5           
 [47] proxy_0.4-27         Formula_1.2-5       
 [49] datawizard_0.6.5     htmlwidgets_1.6.2   
 [51] httr_1.4.5           RColorBrewer_1.1-3  
 [53] wk_0.7.2             ellipsis_0.3.2      
 [55] pkgconfig_2.0.3      XML_3.99-0.14       
 [57] farver_2.1.1         utf8_1.2.3          
 [59] tidyselect_1.2.0     labeling_0.4.2      
 [61] rlang_1.1.0          later_1.3.0         
 [63] munsell_0.5.0        cellranger_1.1.0    
 [65] tools_4.2.3          cli_3.6.0           
 [67] generics_0.1.3       evaluate_0.20       
 [69] fastmap_1.1.1        tables_0.9.10       
 [71] leafsync_0.1.0       knitr_1.42          
 [73] bit64_4.0.5          zip_2.2.2           
 [75] purrr_1.0.1          s2_1.1.2            
 [77] nlme_3.1-162         xml2_1.3.3          
 [79] compiler_4.2.3       rstudioapi_0.14     
 [81] curl_5.0.0           png_0.1-8           
 [83] e1071_1.7-13         ggsignif_0.6.4      
 [85] tibble_3.2.0         stringi_1.7.12      
 [87] parameters_0.20.2.10 forcats_1.0.0       
 [89] lattice_0.20-45      Matrix_1.5-3        
 [91] classInt_0.4-9       vctrs_0.6.0         
 [93] pillar_1.8.1         lifecycle_1.0.3     
 [95] trust_0.1-8          insight_0.19.1      
 [97] raster_3.6-20        R6_2.5.1            
 [99] KernSmooth_2.23-20   codetools_0.2-19    
[101] dichromat_2.0-0.1    rprojroot_2.0.3     
[103] withr_2.5.0          bayestestR_0.13.0   
[105] parallel_4.2.3       hms_1.1.2           
[107] dreamerr_1.2.3       grid_4.2.3          
[109] class_7.3-21         rmarkdown_2.20      
[111] carData_3.0-5        ggpubr_0.6.0        
[113] numDeriv_2016.8-1.1  base64enc_0.1-3  

I hope it is relevant! Or if it is not, please let me know whether I am doing something wrong :)

vincentarelbundock added a commit that referenced this issue Apr 24, 2023
@vincentarelbundock
Copy link
Owner

Thanks a lot for your detailed and useful report! I am sorry for the delay; my “real” job has been keeping me very busy.

Version 1.3.0.9017 on Github should fix the issue. Please let me know if it works for you.

library(modelsummary)
tmp <- transform(mtcars, a_b = qsec, b_c = hp)
panels <- list(
"Panel B: $\\log(3)^2$" = list(
    "A_B" = lm(mpg ~ a_b, data = tmp),
    "B_C" = lm(mpg ~ a_b + b_c, data = tmp)),
"Panel B: $\\log(3)_2$" = list(
    "A_B" = lm(disp ~ a_b, data = tmp),
    "B_C" = lm(disp ~ a_b + b_c, data = tmp))
)
modelsummary(panels, output = "latex_tabular", shape = "rbind", title = "$\\beta_1$", escape = FALSE)
# \begin{table}
# 
# \caption{\label{tab:unnamed-chunk-2}$\beta_1$}
# \centering
# \begin{tabular}[t]{lcc}
# \toprule
#   & A_B & B_C\\
# \midrule
# \addlinespace[0.5em]
# \multicolumn{3}{l}{\textit{Panel B: $\log(3)^2$}}\\
# \midrule \hspace{1em}(Intercept) & -5.114 & 48.324\\
# \hspace{1em} & (10.030) & (11.103)\\
# \hspace{1em}a_b & 1.412 & -0.887\\
# \hspace{1em} & (0.559) & (0.535)\\
# \hspace{1em}b_c &  & -0.085\\
# \hspace{1em} &  & (0.014)\\
# \hspace{1em}Num.Obs. & 32 & \vphantom{1} 32\\
# \hspace{1em}R2 & 0.175 & 0.637\\
# \hspace{1em}R2 Adj. & 0.148 & 0.612\\
# \hspace{1em}AIC & 204.6 & 180.3\\
# \hspace{1em}BIC & 209.0 & 186.2\\
# \hspace{1em}Log.Lik. & -99.294 & -86.170\\
# \hspace{1em}F & 6.377 & 25.431\\
# \hspace{1em}RMSE & 5.39 & 3.57\\
# \addlinespace[0.5em]
# \multicolumn{3}{l}{\textit{Panel B: $\log(3)_2$}}\\
# \midrule \hspace{1em}(Intercept) & 767.619 & -340.780\\
# \hspace{1em} & (204.642) & (221.689)\\
# \hspace{1em}a_b & -30.080 & 17.599\\
# \hspace{1em} & (11.410) & (10.674)\\
# \hspace{1em}b_c &  & 1.755\\
# \hspace{1em} &  & (0.278)\\
# \hspace{1em}Num.Obs. & 32 & 32\\
# \hspace{1em}R2 & 0.188 & 0.658\\
# \hspace{1em}R2 Adj. & 0.161 & 0.634\\
# \hspace{1em}AIC & 397.6 & 372.0\\
# \hspace{1em}BIC & 402.0 & 377.8\\
# \hspace{1em}Log.Lik. & -195.797 & -181.979\\
# \hspace{1em}F & 6.950 & 27.859\\
# \hspace{1em}RMSE & 109.92 & 71.37\\
# \bottomrule
# \end{tabular}
# \end{table}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants