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

not properly escaped FE #546

Closed
Laurent-Smeets-GSS-Account opened this issue Aug 23, 2022 · 10 comments
Closed

not properly escaped FE #546

Laurent-Smeets-GSS-Account opened this issue Aug 23, 2022 · 10 comments

Comments

@Laurent-Smeets-GSS-Account
Copy link

When using the modelsummary command to make a latex table in a Markdown document, the modelsummary does not properly escape the "_" in the name of fixed effects in a fixest model

For example, if I run this markdown file
---
title: "Untitled"
header-includes:
\usepackage{booktabs}
\usepackage{siunitx}
\newcolumntype{d}{S[input-symbols = ()]}
output: pdf_document
---

my_data <- dplyr::tibble(Company_ID = rep(letters[1:2], 10),
                  x = 1:20,
                  y = runif(20, 1,10))

model <- fixest::feols(x ~ y|Company_ID,
               data = my_data)


modelsummary::modelsummary(summary(model),
                            output = 'latex',
                    stars = TRUE,
                    escape = TRUE)

I get this error code

! Missing $ inserted.
<inserted text> 
                $
l.173 FE: Company_
                  ID & X\\ 

Try to find the following text in non_escaped_FE.Rmd:
  FE: Company_ 

You may need to add $ $ around a certain inline R expression `r ` in non_escaped_FE.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile non_escaped_FE.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See non_escaped_FE.log for more info.
Execution halted

and the latex code looks like

Show in New Window
\begin{table}
\centering
\begin{tabular}[t]{lc}
\toprule
  & Model 1\\
\midrule
y & \num{-0.917}\\
 & (\num{0.845})\\
\midrule
Num.Obs. & \num{20}\\
R2 & \num{0.142}\\
R2 Adj. & \num{0.041}\\
RMSE & \num{5.34}\\
Std.Errors & by: Company\_ID\\
FE: Company_ID & X\\
\bottomrule
\multicolumn{2}{l}{\rule{0pt}{1em}+ p $<$ 0.1, * p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001}\\
\end{tabular}
\end{table}

All the underscores are escaped, except from the "FE: Company_ID & X\\" line one

@vincentarelbundock
Copy link
Owner

Thanks a lot for the report!

I just pushed a modification. I'd really appreciate it if you could install from Github, restart your R session completely, and try again. Thanks!

@Laurent-Smeets-GSS-Account
Copy link
Author

Thanks. The error has disappeared and this now works for me.

@shreyasgm
Copy link

Hi, thank you for this wonderful package. This error seems to have reappeared. I was able to reproduce the error with the example provided by @Laurent-Smeets-GSS-Account . The error is in both 1.4.1 on CRAN as well as the current version on Github.

@vincentarelbundock
Copy link
Owner

Thanks for the comment. I'll check it out as soon as I can.

@vincentarelbundock
Copy link
Owner

Thanks a lot for reviving this issue. Should be fixed on Github now, but please be aware that things are a bit in flux on the development repo right now, so it may be best to wait until the next release on CRAN to update your installation (probably in the next 2 weeks).

@baarthur
Copy link

hi there,

first of all, thank you for this awesome package, it's helping me a lot!

i am facing the same issue with package did, but on the Std.Errors part. In the example below, i changed the variable "countyreal" to "county_real" to reproduce the error i am facing. note that with standard lm() models there is no problem with using underscore in list names. however, if i change names to att_1 and att_2 in the second chunk, it also throws an error ---i focused in the standard errors because that's what is bugging me.

---
title: "debug"

format: 
  pdf:
    keep-tex: true
---

```{r}
#| label: setup
#| message: false

library(did)
library(dplyr)
library(modelsummary)
```

Works okay
```{r}
set.seed(1)

df <- tibble(
  x_1 = rnorm(100, 10, 3),
  x_2 = rnorm(100, 7, 2.5),
  u = rnorm(100, 0, 0.02),
  y = 1 + 2*x_1 - 3*x_2 + u
)

lm <- list(
  lm_1 = lm(y ~ x_1, data = df),
  lm_2 = lm(y ~ x_1 + x_2, data = df)
)

modelsummary(lm)
```

Not okay
```{r}
data("mpdta")

mpdta <- mpdta %>% 
  rename(county_real = countyreal)

att <- list(
  att1 = att_gt(yname = "lemp", tname = "year", idname = "county_real", gname = "first.treat", 
                 data = mpdta),
  att2 = att_gt(yname = "lemp", tname = "year", idname = "county_real", gname = "first.treat",
                 xformla = ~ lpop, data = mpdta)
)

agg <- purrr::map(att, aggte)

agg %>% 
  modelsummary(
    statistic = "conf.int", gof_omit = "type|ngroup|ntime|control.group|est.method",
    escape = F,
    output = "kableExtra"
  )
```

@vincentarelbundock
Copy link
Owner

@baarthur thanks for the report.

One of the main reasons I changed the default output format in modelsummary from kableExtra to tinytable is because escaping was so difficult to implement correctly and debug.

If you run into the same issue with the default tinytable output, I will be happy to take a look.

Realistically, though, I am unlikely to invest time in debugging this for kableExtra, because I have already spent way more time than I was hoping for in that stuff. Of course, if someone submits a Pull Request with a kableExtra fix, I'll be happy to review and merge.

@baarthur
Copy link

hi @vincentarelbundock,

I didn't know you changed from kableExtra to tinytable! Currently, I am very familiar with kableExtra since I need to customize my tables due to my university's guidelines.

I just tried the same reprex above but changing output to "tinytable" and got a similar error but in a different line.

When output = "kableExtra":

Missing $ inserted.
<inserted text> 
                $
l.321 Std.Errors & by: county_
                              real & by: county_real\\ 

You may need to $ $ around an expression in this file.

Associated table in the .tex file:

\begin{table}
\centering
\begin{tabular}[t]{lcc}
\toprule
  & att1 & att2\\
\midrule
ATT(Average) & \num{-0.031} & \num{-0.033}\\
 & {}[\num{-0.055}, \num{-0.007}] & {}[\num{-0.056}, \num{-0.010}]\\
ATT(2004) & \num{-0.080} & \num{-0.085}\\
 & {}[\num{-0.139}, \num{-0.021}] & {}[\num{-0.142}, \num{-0.027}]\\
ATT(2006) & \num{-0.023} & \num{-0.020}\\
 & {}[\num{-0.061}, \num{0.015}] & {}[\num{-0.056}, \num{0.016}]\\
ATT(2007) & \num{-0.026} & \num{-0.029}\\
 & {}[\num{-0.062}, \num{0.010}] & {}[\num{-0.065}, \num{0.008}]\\
\midrule
Num.Obs. & \num{500} & \num{500}\\
Std.Errors & by: county_real & by: county_real\\
\bottomrule
\end{tabular}
\end{table}

When output = "tinytable" or if I omit the argument:

Missing $ inserted.
<inserted text> 
                $
l.306 \end
          {tblr} 

You may need to $ $ around an expression in this file.

Associated table in the .tex file:

\begin{table}
\centering
\begin{tblr}[         %% tabularray outer open
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]Q[]},
column{1}={halign=l,},
column{2}={halign=c,},
column{3}={halign=c,},
hline{10}={1,2,3}{solid, 0.05em, black},
}                     %% tabularray inner close
\toprule
& att1 & att2 \\ \midrule %% TinyTableHeader
ATT(Average) & \num{-0.031}                  & \num{-0.033}                  \\
& [\num{-0.055}, \num{-0.007}] & [\num{-0.056}, \num{-0.010}] \\
ATT(2004)    & \num{-0.080}                  & \num{-0.085}                  \\
& [\num{-0.139}, \num{-0.021}] & [\num{-0.142}, \num{-0.027}] \\
ATT(2006)    & \num{-0.023}                  & \num{-0.020}                  \\
& [\num{-0.061}, \num{0.015}]  & [\num{-0.056}, \num{0.016}]  \\
ATT(2007)    & \num{-0.026}                  & \num{-0.029}                  \\
& [\num{-0.062}, \num{0.010}]  & [\num{-0.065}, \num{0.008}]  \\
Num.Obs.     & \num{500}                     & \num{500}                     \\
Std.Errors   & by: county_real                & by: county_real                \\
\bottomrule
\end{tblr}
\end{table}

@vincentarelbundock
Copy link
Owner

In your reprex you explicitly request escape=F, and then you include values with underscores _, which are illegal characters that will break LaTeX unless they are escaped. Either remove the underscores or escape them...

@baarthur
Copy link

right, I removed escape and it worked. Thanks, you can close this issue now I think!

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

4 participants