Last updated: 2021-05-04 08:48:15 EST

Go home

1 Overview

A great feature of R notebooks is that you can typeset math with a few keystrokes.

This is done through LaTeX, a typesetting language supported by R Markdown. It’s a whole kettle of fish. All we need from it are the commands to render math.

You can learn more about LaTeX math here and even more here (plus a handy list of Greek symbols here).

2 Inline math

Inline meaning “in the line of the sentence” (or something like that).

In the middle of a sentence just wrap the math bits with $...$.

For example, here is a my amazing formula: $y = f(x)$ \(y = f(x)\)

3 Formulas

In the lectures you’ve seen plenty of math formulas. They are always inside a “fence” of dollar signs:

$$
% math here
$$

For instance, y = f(x) = 2x:

\[ y = f(x) = 2x \]

3.1 Powers

a raised to the b:

$$
a^b
$$

If the power is more than one character (e.g., 20 instead of 2) just wrap the power in {}:

$$
a^{20}
$$

3.2 Underscores

a underscore b:

$$
a_b
$$

multiple characters:

a_{bc}

3.3 Greek symbols

Everybody knows your math looks better in Greek.

The most common ones are “betas” for regression coefficients

$$
\beta
$$

and “epsilons” for the error term:

$$
\epsilon
$$

For instance, a simple linear regression model with an intercept “beta nought” and a slope “beta one” (you need underscores here):

$$
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \epsilon
$$

\[ y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \epsilon \]

3.4 Fractions

\frac{numerator}{denominator}

For example:

$$
\frac{3x + y^2}{8log(x+3)}
$$

\[ \frac{3x + y^2}{8log(x+3)} \]

We can combine this with the partial derivative symbol \partial to write the partial derivative of \(f(x,y) = x^2 + y^2\):

$$
\frac{\partial f}{\partial x} = 2x = f'_x
$$

\[ \frac{\partial f}{\partial x} = 2x = f'_x \]

4 Aligned formulas

This is as complicated as it gets for us, and it’s still not that bad.

First, everything has to be in an aligned “environment”:

$$
\begin{aligned}
% math here
\end{aligned}
$$

then:

  • use & to indicate where you want alignment to happen (e.g., &= means “align on the equals sign”)
  • break each line with \\
$$
\begin{aligned}
 f(x) + h(x) + j(x) &= x + y \\
 g(x) &= z + b
\end{aligned}
$$

\[ \begin{aligned} f(x) + h(x) + j(x) + cat(x) &= x + y \\ g(x) &= z + b \end{aligned} \]

5 Full example

Let’s optimize

\[ f(x) = -x^2 + 2x + 4 \]

and put all the steps in an aligned environment, starting with the first-order condition:

$$
\begin{aligned}
 f'_x &= 0 \\
 -2x + 2 &= 0 \\
 -2x &= -2 \\
 x  &= \frac{-2}{-2} \\
 x^* &= 1
\end{aligned}
$$

\[ \begin{aligned} f'_x &= 0 \\ -2x + 2 &= 0 \\ -2x &= -2 \\ x &= \frac{-2}{-2} \\ x^* &= 1 \end{aligned} \]

OK, let’s confirm the results with numerical optimization.

First define the function:

f = function(x){
  -x^2 + 2*x + 4
}

Then optimize it:

optimize(f = f, lower = -100, upper = 100, maximum = TRUE)
$maximum
[1] 1

$objective
[1] 5

Verify the solution (the objective function should equal 5 at the optimum):

f(x = 1)
[1] 5
LS0tCnRpdGxlOiAiVHlwZXNldHRpbmcgTWF0aCIKc3VidGl0bGU6ICJTdWZmb2xrRWNvbiIKYXV0aG9yOiAiIgpvdXRwdXQ6IAogIGh0bWxfbm90ZWJvb2s6CiAgICBudW1iZXJfc2VjdGlvbnM6IHRydWUKICAgIHRoZW1lOiByZWFkYWJsZQogICAgaGlnaGxpZ2h0OiBweWdtZW50cwogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IAogICAgICBjb2xsYXBzZWQ6IHllcwotLS0KCgo8YSBocmVmPSJodHRwczovL2xyZGVnZWVzdC5naXRodWIuaW8vc3RhdHMvY29kZV9jaGVja3BvaW50cy5uYi5odG1sIiBjbGFzcz0iYnRuIGJ0bi13YXJuaW5nIj4qKkxhc3QgdXBkYXRlZDoqKiBgciBTeXMudGltZSgpYCBFU1QgPC9hPiAKCjxhIGhyZWY9Imh0dHBzOi8vc3VmZm9sa2Vjb24uZ2l0aHViLmlvLyIgY2xhc3M9ImJ0biBidG4tZGVmYXVsdCI+R28gaG9tZTwvYT4KCiMgT3ZlcnZpZXcKCkEgZ3JlYXQgZmVhdHVyZSBvZiBSIG5v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