jpugliese
jpugliese

Reputation: 261

R Markdown error with LaTeX - apparently not the $ $ problem

The second code part below is written in the Markdown section, while the first part of the code generating the objects that R calls. For the example, I'll set

homoscedastic_variance = 1
heterokesdastic_variance = 1

$$Var_{homo}(\hat{\tau}) = \frac{1}{N_tN_c}\sum_{i=1}^N (Y_i - \bar{Y}) = `r homoscedastic_variance`$$

While the heteroskedastic is estimated as:

$$Var_{hetero}(\hat{\tau}) = \frac{1}{N_t} s_t^2 + \frac{1}{N_c}s_c^2 = `r heterokesdastic_variance`$$

with $s_c = \frac{1}{N_c - 1}\sum_{i: W_i = 0} (Y_i - \bar{Y_c})^2 $ and analogously for $s_t$.

Then, we can make two confidence intervals:

$CI_{homo} = [`r treatment_effect` \pm 1.96*sqrt(`r homoscedastic_variance`)]$

$CI_{hetero} = [`r treatment_effect` \pm 1.96*sqrt(`r heterokesdastic_variance`)]$

When using the knit -> PDF option, I get this error:

! Missing $ inserted.
<inserted text> 
                $
l.108 with \$s\_c = \frac{1}{N_c - 1}
                                     \sum\_\{i: W\_i = 0\} (Y\_i - 

Try to find the following text in script_ps1.Rmd:
  with \$s\_c = \frac{1}{N_c - 1} 

You may need to add $ $ around a certain inline R expression `r ` in script_ps1.Rmd (see the above hint). 

See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Erro: LaTeX failed to compile script_ps1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See script_ps1.log for more info." 

I tried this in LaTeX and it compiles just fine with the values; anyone knows why I am getting this bug? I find it funny that it suggests the $ $ around inline R expressions, but the bug is not related to an expression that has any inline R expressions (if I remove the "with $s_c..." part, the knit works just fine!

Upvotes: 1

Views: 1273

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84719

Remove the space at the end here:

$s_c = \frac{1}{N_c - 1}\sum_{i: W_i = 0} (Y_i - \bar{Y_c})^2 $

i.e.

$s_c = \frac{1}{N_c - 1}\sum_{i: W_i = 0} (Y_i - \bar{Y_c})^2$

and this works.

Upvotes: 3

Related Questions