Skip to content
New issue

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

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

Already on GitHub? # to your account

Not escaping underscores in all variable names #202

Closed
Joe-Wasserman opened this issue Oct 15, 2021 · 6 comments · Fixed by #205
Closed

Not escaping underscores in all variable names #202

Joe-Wasserman opened this issue Oct 15, 2021 · 6 comments · Fixed by #205

Comments

@Joe-Wasserman
Copy link

Joe-Wasserman commented Oct 15, 2021

I'm attempting to output an equatiomatic equation as an image that can be embedded in a README.md for github via texPreview::tex_preview().

I believe the issue is caused by equatiomatic not escaping underscores in some of the variable names, in this case just the random grouping factors of the merMod object.

Reprex for the issue:

httr::GET("https://github.com/mymil/covid-19-united-states-county-quarterly-excess-deaths/blob/main/results/united_states_county_quarterly_model.RDS?raw=true",
    httr::write_disk(tf <- tempfile(fileext = ".RDS"))
)

lmer_model <- readRDS(file = tf)

equatiomatic::extract_eq(
  lmer_model,
  wrap = TRUE,
  terms_per_line = 2
) %>%
  texPreview::tex_preview(returnType = "html" )

Produces this error:

Error in tex_preview.default(obj, tex_lines, stem, overwrite, keep_pdf,  : 
   
! Missing $ inserted.
<inserted text> 
 $
l.8 \text{, for region_code j = 1,}
 \dots \text{,J} \\ 
!  ==> Fatal error occurred, no output PDF file produced!

The results of equatiomatic::extract_eq():

$$
\begin{aligned}
  \operatorname{total\_deaths\_per\_day}_{i}  &\sim N \left(\mu, \sigma^2 \right) \\
    \mu &=\alpha_{j[i],k[i],l[i]} + \beta_{1}(\operatorname{population\_z})\ + \\
&\quad \beta_{2}(\operatorname{year\_zero}) + \beta_{3}(\operatorname{quarter}_{\operatorname{2}})\ + \\
&\quad \beta_{4}(\operatorname{quarter}_{\operatorname{3}}) + \beta_{5}(\operatorname{quarter}_{\operatorname{4}}) \\
    \alpha_{j}  &\sim N \left(\mu_{\alpha_{j}}, \sigma^2_{\alpha_{j}} \right)
    \text{, for region_code j = 1,} \dots \text{,J} \\
    \alpha_{k}  &\sim N \left(\mu_{\alpha_{k}}, \sigma^2_{\alpha_{k}} \right)
    \text{, for county_set_code k = 1,} \dots \text{,K} \\
    \alpha_{l}  &\sim N \left(\mu_{\alpha_{l}}, \sigma^2_{\alpha_{l}} \right)
    \text{, for census_division l = 1,} \dots \text{,L}
\end{aligned}
$$
sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

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

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

other attached packages:
[1] lmerTest_3.1-3     lme4_1.1-26        Matrix_1.3-2       texPreview_1.5    
[5] tinytex_0.34       equatiomatic_0.3.0

loaded via a namespace (and not attached):
 [1] httr_1.4.2            svgPanZoom_0.3.4      jsonlite_1.7.2       
 [4] splines_4.0.4         here_1.0.1            rdocsyntax_0.4.1.9000
 [7] shiny_1.6.0           assertthat_0.2.1      statmod_1.4.35       
[10] numDeriv_2016.8-1.1   pillar_1.6.0          lattice_0.20-41      
[13] glue_1.4.2            digest_0.6.27         promises_1.2.0.1     
[16] rvest_1.0.0           minqa_1.2.4           colorspace_2.0-0     
[19] htmltools_0.5.1.1     httpuv_1.6.0          details_0.2.1        
[22] clipr_0.7.1           pkgconfig_2.0.3       magick_2.7.3         
[25] purrr_0.3.4           xtable_1.8-4          scales_1.1.1         
[28] whisker_0.4           later_1.2.0           tibble_3.1.0         
[31] generics_0.1.0        ggplot2_3.3.5         ellipsis_0.3.1       
[34] cachem_1.0.4          withr_2.4.2           magrittr_2.0.1       
[37] crayon_1.4.1          mime_0.10             memoise_2.0.0        
[40] fs_1.5.0              fansi_0.4.2           nlme_3.1-152         
[43] MASS_7.3-53           xml2_1.3.2            tools_4.0.4          
[46] lifecycle_1.0.0       V8_3.4.0              munsell_0.5.0        
[49] packrat_0.6.0         compiler_4.0.4        rlang_0.4.10         
[52] grid_4.0.4            nloptr_1.2.2.2        rstudioapi_0.13      
[55] base64enc_0.1-3       boot_1.3-26           gtable_0.3.0         
[58] DBI_1.1.1             curl_4.3              rematch2_2.1.2       
[61] R6_2.5.0              knitr_1.31            dplyr_1.0.5          
[64] fastmap_1.1.0         utf8_1.2.1            rprojroot_2.0.2      
[67] desc_1.3.0            Rcpp_1.0.7            vctrs_0.3.7          
[70] png_0.1-7             tidyselect_1.1.0      xfun_0.26  
@datalorax
Copy link
Owner

Hmm... I don't see any underscores that are not escaped. Which variables do you think are causing the issue?

@Joe-Wasserman
Copy link
Author

Joe-Wasserman commented Oct 15, 2021

Sorry, I should've indicated that: region_code, county_set_code, and census_division

I am not very familiar with latex, so my understanding comes from here: yonicd/texPreview#51 (comment)

@datalorax
Copy link
Owner

Ah... I see. Okay, I will try to dig into this tomorrow. Also, just an FYI, I'm going to be updating the merMod output soon so it looks more like #196 (comment). The current notation has some issues, as discussed in that thread. It's a minor-ish update, but you might want to keep it in mind to update this equation after I update that code.

@Joe-Wasserman
Copy link
Author

Great, thank you! I appreciate it. I did read through that discussion, and I'm looking forward to the update, since I prefer that notation style as well.

datalorax pushed a commit that referenced this issue Oct 19, 2021
@datalorax datalorax mentioned this issue Oct 19, 2021
datalorax pushed a commit that referenced this issue Oct 19, 2021
@datalorax
Copy link
Owner

Okay, this should be fixed. If you install from GitHub and try again it should work for you.

@Joe-Wasserman
Copy link
Author

Confirming that it does indeed work now! Thank you very much for the update.

image

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

Successfully merging a pull request may close this issue.

2 participants