Reputation: 53
I have created an r chunk in an r markdown document, in which I have calculated some parameter values that I am required to find for a homework. Now I would like for my knitted PDF-document to show the sentence "Our estimate of $\beta_1$ is -0.2186". However, the portion of the code for the greek letter beta ($\beta_1$) is being shown in the PDF the same way it's written here, not as the actual greek letter.
I have already tried installing LaTeX-packages in the document header (e.g. \usepackage{mathtools}), which made no difference.
cigs_mean <- mean(smoke$cigs) #find y-bar
educ_mean <- mean(smoke$educ) #find x-bar
beta1_hat <- (cov(smoke$educ,smoke$cigs))/(var(smoke$educ)) #find beta1-hat
beta0_hat <- (cigs_mean-(beta1_hat*educ_mean)) #find beta0-hat
print(paste0("Our estimate of $\beta_1$ is ", round(beta1_hat, digits=4)))
I just want for the document to show a greek letter beta with subscript 1, rather than replicating the code I have written ($\beta_1$)
Upvotes: 2
Views: 1202
Reputation: 545578
Backslashes in R character strings have a special meaning as escape characters, and must themselves be escaped. Otherwise, your string '$\beta$'
is read by R as '$'
‹backspace›
'e'
't'
'a'
'$'
.
Furthermore, print
is the wrong function to use here: its purpose is to provide output in the interactive R console, never for actual output to a document. Use cat
instead.
Finally, if you haven’t already done so, you need to tell knitr to interpret the results of this code chunk as-is instead of rendering them as a result:
```{r results = 'asis'}
…
cat(paste0("Our estimate of $\\beta_1$ is ", round(beta1_hat, digits=4), "\n"))
```
Upvotes: 3