Math and Equations in Rmarkdown
Introduction
I have asked that all of your assignments be submitted via R markdown which is ideal for presenting the results from data analysis. Rmarkdown can be used as a general scientific writing tool as well, meaning, you can easily write out all kinds of formula’s and equations. In this very short tutorial I will show you how use “math mode” within Rmarkdown and point you to some other tutorial resources.
Math Mode
Entering math mode in Rmarkdown is as simple as using a dollar sign
symbol ($). Anything placed in between a starting
$ symbol and closing $ symbol will be
interpreted via Rmarkdown’s math interpreter. Remember to not leave any
spaces between your opening and closing $ symbols. For
example
$ x + y $
is not valid while this expression
$x + y$
is valid. In this expression: $x + y$, Rmarkdown will
enter math mode and display this equation will all that stylings LateX
users are accustomed to. The expression wil render as \(x + y\).
Using a single set of dollar signs to enclose an equation is used for
inline styling. That is when you want to have an equation written as
part of a paragraph, like this one here \(y =
\alpha + \beta\times x\) (which was written as
$y = \alpha + \beta\times x$).
If you want to write a displayed equation, which renders on it’s own
line (and centered), you use 2 dollar signs on either side of your text.
For example, using our example from the previous paragraph,
$$y = \alpha + \beta\times x$$ will render as
\[y = \alpha + \beta\times x\]
This is helpful when we have more complicated equations that need be positioned prominently or need a little more space to be displayed properly.
Commonly used mathematical expressions
\(\frac{3}{5}\) \(3^5\) \(3^{25}\) \(\beta_1\)
Greek Letters
We often use Greek letters when describing a statistical model. These
can be rendered by using a backslash \ followed by the
spelling of the greek letter you want. You can capitalize the spelling
to get the uppercase version. For example:
$\alpha$- is \(\alpha\)$\beta$- is \(\beta\)$\delta$- is \(\delta\)$\gamma$- is \(\gamma\)$\sigma$- is \(\sigma\)$\Sigma$- is \(\Sigma\)
and so on.
Fractions
Writing fractions is also pretty straight forward. We just use
$\frac{}{}$ Where whatever we put in between the first set
of brackets ends up in the numerator while the stuff we put in the
second bracket goes to the denominator.
For example
$$\frac{(a + b)^2}{b^2}$$
will render as:
\[\frac{(a + b)^2}{b^2}\]
You may often have fractions within fractions:
$$\frac{(a + b)^2}{\Big(\frac{1}{2}a + \frac{1}{2}b\Big)}$$
\[\frac{(a + b)^2}{\Big(\frac{1}{2}a + \frac{1}{2}b\Big)}\]
Note the use of \Big( and \Big) for the
parentheses. This tells math mode to make these parentheses larger to
fit the equation we are making.
Sub Scripts and Super Scripts
You may have noticed the use of $b^2$ to make a
superscript 2. We can _ and ^ to create sub
and superscripts. Let’s try this out
$$a^2 + a^4 + a^8 + a^16$$
\[a^2 + a^4 + a^8 + a^{16}\]
hmmm, the last $a^16$ didn’t render properly. This is
between the sub and superscript notation only looks at the first value.
If you wish to make more than one value appear in a sub or super script
you can wrap the values in braces {}
$$a^2 + a^4 + a^8 + a^{16}$$
\[a^2 + a^4 + a^8 + a^{16}\]
Regular Text
Sometimes you want to write some regualar words that don’t have any styling applied to them in an equation. We can applish this with the use of the keyword. See the following example
$$\text{My Equation} = \alpha + \beta\text{my_variable}$$
\[\text{My Equation} = \alpha + \beta\times\text{my_variable}\]
Summations
Summations are very common, and also very easy making use of the
\sum keyword and the sub/superscript notation we just
learned.
$$\text{mean value} = \sum_{i=1}^{n}\frac{x_i}{n}$$
\[\text{mean value} = \sum_{i=1}^{n}\frac{x_i}{n}\]
Common Math Notation
Here are some commonly used math notations
- Less than:
$x \lt y$- is \(x \lt y\) - Greater than:
$x \gt y$- is \(x \gt y\) - Greater than or equal to
$x \ge y$- is \(x \ge y\) - Less than or equal to
$x \le y$- is \(x \le y\) - Not equal to
$x \ne y$- is \(x \ne y\) - Approximately equal to
$x \approx y$- is \(x \approx y\) - Is proportional to
$x \propto y$- is \(x \propto y\) - Is distributed as (squiggly line)
$y \sim N(0, 1)$- is \(y \sim N(0, 1)\) - A hat for an estimate
$\hat{y}$- is \(\hat{y}\) - A bar for an estimate
$\bar{y}$- is \(\bar{y}\) - N choose k
${n \choose k}$- \({n \choose k}\) - More general use binom:
$\binom{n}{k}$- \(\binom{n}{k}\)- for example
$\binom{n+1}{x + y + z}$- \(\binom{n+1}{x + y + z}\)
- for example
Exercise 1.
Write out the probability mass function for the binomial distribution
shown below. Try doing on your own. If you get stuck, you can see the
solution by looking at the source code for this file. You can make the
three dots between 2 and n as $\dots$ which renders as:
\(\ldots\)
\[P(x; p, n) = \binom{n}{x}(p)^x(1-p)^{(n-x)}, \text{ for } x = 0, 1,2, \ldots, n\]
Arrays and Matrices
You can create arrays of numbers the \begin{array} and
\end{array} keywords. For start an array with
\begin{array} and we end an array with
\end{array}. Within an array we add new columns with
& and we add new rows with \\. Beside the
begin array keyword we tell Rmarkdown the positioning of each column (c
for center, l for left and r for right. For example, the following array
has 3 rows, and 3 columns. Each entry is centered
({ccc})
$$\begin{array}{ccc}
x_{11} & x_{12} & x_{13}\\
x_{21} & x_{22} & x_{23} \\
x_{31} & x_{32} & x_{33} \\
\end{array}$$
This will render as
$$ \[\begin{array}{ccc} x_{11} & x_{12} & x_{13}\\ x_{21} & x_{22} & x_{23} \\ x_{31} & x_{32} & x_{33} \\ \end{array}\]$$
Instead of an array we can create a matrix with square brackets using
\begin{bmatrix} as:
$$X = \begin{bmatrix}
1 & x_{1}\\
1 & x_{2}\\
1 & x_{3}
\end{bmatrix}$$
\[X = \begin{bmatrix} 1 & x_{1}\\ 1 & x_{2}\\ 1 & x_{3} \end{bmatrix}\]
We can create a matrix with parentheses with
\begin{pmatrix}, as in
$$X = \begin{pmatrix}
1 & x_{1}\\
1 & x_{2}\\
1 & x_{3}
\end{pmatrix}$$
\[X = \begin{pmatrix} 1 & x_{1}\\ 1 & x_{2}\\ 1 & x_{3} \end{pmatrix}\]
We can create a system of equations with the
\being{aligned} keyword. Here we put an &
symbol in the spot we wish to align. For example
$$\begin{aligned}
X + y &= a\timesx + a^2\timesb \\
x &= a(x - a\times b) - y
\end{aligned}$$
\[\begin{aligned} X + y &= (a\times x) + (a^2\times b) \\ X &= a(x - (a\times b)) - y \end{aligned}\]
Exercise 2
Try your best to recreate this equation for a random intercept and random slop model. If you need help, you can look at the source code.
\[y_i \sim N(\alpha_{j[i]} + \beta_1{j[i]} x_{1,i} + \beta_2 x_{2,i}, \sigma_y^2)\]
\[\begin{pmatrix} \alpha_j \\ \beta_j \end{pmatrix} = N\Big( \begin{pmatrix} \mu_\alpha \\ \mu_\beta \end{pmatrix}, \begin{pmatrix} \sigma_{\alpha}^2 & \rho\sigma_{\alpha}\sigma_{\beta} \\ \rho\sigma_{\alpha}\sigma_{\beta} & \sigma_{\beta}^2 \end{pmatrix}\Big), \text{ for } j = 1, \ldots, J\]
Resources
That should be more than enough to get you started. Here are a couple of resources for more information.