B Bookdown cheat sheet

# to see non-scientific notation a result
format(12e-17, scientific = FALSE)
#> [1] "0.00000000000000012"

B.1 Operation

R commands to do derivatives of a defined function Taking derivatives in R involves using the expression, D, and eval functions. You wrap the function you want to take the derivative of in expression(), then use D, then eval as follows.

simple example

#define a function
f=expression(sqrt(x))

#take the first derivative
df.dx=D(f,'x')
df.dx
#> 0.5 * x^-0.5

#take the second derivative
d2f.dx2=D(D(f,'x'),'x')
d2f.dx2
#> 0.5 * (-0.5 * x^-1.5)

Evaluate

  • The first argument passed to eval is the expression you want to evaluate
  • the second is a list containing the values of all quantities that are not defined elsewhere.
#evaluate the function at a given x
eval(f,list(x=3))
#> [1] 1.732051

#evaluate the first derivative at a given x
eval(df.dx,list(x=3))
#> [1] 0.2886751

#evaluate the second derivative at a given x
eval(d2f.dx2,list(x=3))
#> [1] -0.04811252

B.2 Math Expression/ Syntax

Full list

Aligning equations

\begin{aligned}
a & = b \\
X &\sim {Norm}(10, 3) \\
5 & \le 10
\end{aligned}

\[ \begin{aligned} a & = b \\ X &\sim {Norm}(10, 3) \\ 5 & \le 10 \end{aligned} \]

Cross-reference equation

\begin{equation} 
a = b
(\#eq:test)
\end{equation}
\[\begin{equation} a = b \tag{B.1} \end{equation}\]

to refer in a sentence (B.1) (\@ref(eq:test))

Math Syntax Notation
$\pm$ \(\pm\)
$\ge$ \(\ge\)
$\le$ \(\le\)
$\neq$ \(\neq\)
$\equiv$ \(\equiv\)
$^\circ$ \(^\circ\)
$\times$ \(\times\)
$\cdot$ \(\cdot\)
$\leq$ \(\leq\)
$\geq$ \(\geq\)
\propto \(\propto\)
$\subset$ \(\subset\)
$\subseteq$ \(\subseteq\)
$\leftarrow$ \(\leftarrow\)
$\rightarrow$ \(\rightarrow\)
$\Leftarrow$ \(\Leftarrow\)
$\Rightarrow$ \(\Rightarrow\)
$\approx$ \(\approx\)
$\mathbb{R}$ \(\mathbb{R}\)
$\sum_{n=1}^{10} n^2$ \(\sum_{n=1}^{10} n^2\)
$$\sum_{n=1}^{10} n^2$$ \[\sum_{n=1}^{10} n^2\]
$x^{n}$ \(x^{n}\)
$x_{n}$ \(x_{n}\)
$\overline{x}$ \(\overline{x}\)
$\hat{x}$ \(\hat{x}\)
$\tilde{x}$ \(\tilde{x}\)
\check{} \(\check{}\)
\underset{\gamma}{\operatorname{argmin}} \(\underset{\gamma}{\operatorname{argmin}}\)
$\frac{a}{b}$ \(\frac{a}{b}\)
$\frac{a}{b}$ \(\frac{a}{b}\)
$\displaystyle \frac{a}{b}$ \(\displaystyle \frac{a}{b}\)
$\binom{n}{k}$ \(\binom{n}{k}\)
$x_{1} + x_{2} + \cdots + x_{n}$ \(x_{1} + x_{2} + \cdots + x_{n}\)
$x_{1}, x_{2}, \dots, x_{n}$ \(x_{1}, x_{2}, \dots, x_{n}\)
\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle$ \(\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle\)
$x \in A$ \(x \in A\)
$|A|$ \(|A|\)
$x \in A$ \(x \in A\)
$x \subset B$ \(x \subset B\)
$x \subseteq B$ \(x \subseteq B\)
$A \cup B$ \(A \cup B\)
$A \cap B$ \(A \cap B\)
$X \sim Binom(n, \pi)$ \(X \sim Binom(n, \pi)\)
$\mathrm{P}(X \le x) = \text{pbinom}(x, n, \pi)$ \(\mathrm{P}(X \le x) = \text{pbinom}(x, n, \pi)\)
$P(A \mid B)$ \(P(A \mid B)\)
$\mathrm{P}(A \mid B)$ \(\mathrm{P}(A \mid B)\)
$\{1, 2, 3\}$ \(\{1, 2, 3\}\)
$\sin(x)$ \(\sin(x)\)
$\log(x)$ \(\log(x)\)
$\int_{a}^{b}$ \(\int_{a}^{b}\)
$\left(\int_{a}^{b} f(x) \; dx\right)$ \(\left(\int_{a}^{b} f(x) \; dx\right)\)
$\left[\int_{\-infty}^{\infty} f(x) \; dx\right]$ \(\left[\int_{-\infty}^{\infty} f(x) \; dx\right]\)
$\left. F(x) \right|_{a}^{b}$ \(\left. F(x) \right|_{a}^{b}\)
$\sum_{x = a}^{b} f(x)$ \(\sum_{x = a}^{b} f(x)\)
$\prod_{x = a}^{b} f(x)$ \(\prod_{x = a}^{b} f(x)\)
$\lim_{x \to \infty} f(x)$ \(\lim_{x \to \infty} f(x)\)
$\displaystyle \lim_{x \to \infty} f(x)$ \(\displaystyle \lim_{x \to \infty} f(x)\)
Greek Letters
$\alpha A$ \(\alpha A\)
$\beta B$ \(\beta B\)
$\gamma \Gamma$ \(\gamma \Gamma\)
$\delta \Delta$ \(\delta \Delta\)
$\epsilon \varepsilon E$ \(\epsilon \varepsilon E\)
$\zeta Z \sigma $ \(\zeta Z \sigma\)
$\eta H$ \(\eta H\)
$\theta \vartheta \Theta$ \(\theta \vartheta \Theta\)
$\iota I$ \(\iota I\)
$\kappa K$ \(\kappa K\)
$\lambda \Lambda$ \(\lambda \Lambda\)
$\mu M$ \(\mu M\)
$\nu N$ \(\nu N\)
$\xi\Xi$ \(\xi\Xi\)
$o O$ \(o O\)
$\pi \Pi$ \(\pi \Pi\)
$\rho\varrho P$ \(\rho\varrho P\)
$\sigma \Sigma$ \(\sigma \Sigma\)
$\tau T$ \(\tau T\)
$\upsilon \Upsilon$ \(\upsilon \Upsilon\)
$\phi \varphi \Phi$ \(\phi \varphi \Phi\)
$\chi X$ \(\chi X\)
$\psi \Psi$ \(\psi \Psi\)
$\omega \Omega$ \(\omega \Omega\)
$\cdot$ \(\cdot\)
$\cdots$ \(\cdots\)
$\ddots$ \(\ddots\)
$\ldots$ \(\ldots\)

Limit P(\lim_{n\to \infty}\bar{X}_n =\mu) =1

\[ P(\lim_{n\to \infty}\bar{X}_n =\mu) =1 \]

Matrices

$$\begin{array}
{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}
$$

\[ \begin{array} {rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array} \]

$$\mathbf{X} = \left[\begin{array}
{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]
$$

\[ \mathbf{X} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right] \]

Aligning Equations

Aligning Equations with Comments

\begin{aligned}
    3+x &=4 && \text{(Solve for} x \text{.)}\\
    x &=4-3 && \text{(Subtract 3 from both sides.)}\\
    x &=1   && \text{(Yielding the solution.)}
\end{aligned}

\[ \begin{aligned} 3+x &=4 & &\text{(Solve for} x \text{.)} \\ x &=4-3 && \text{(Subtract 3 from both sides.)} \\ x &=1 && \text{(Yielding the solution.)} \end{aligned} \]

B.2.1 Statistics Notation

$$
f(y|N,p) = \frac{N!}{y!(N-y)!}\cdot p^y \cdot (1-p)^{N-y} = {{N}\choose{y}} \cdot p^y \cdot (1-p)^{N-y}
$$

\[ f(y|N,p) = \frac{N!}{y!(N-y)!}\cdot p^y \cdot (1-p)^{N-y} = {{N}\choose{y}} \cdot p^y \cdot (1-p)^{N-y} \]

\begin{cases}
\frac{1}{b-a}&\text{for $x\in[a,b]$}\\
0&\text{otherwise}\\
\end{cases}

\[ \begin{cases} \frac{1}{b-a} & \text{for } x\in[a,b]\\ 0 & \text{otherwise}\\ \end{cases} \]

B.3 Table

+---------------+---------------+--------------------+
| Fruit         | Price         | Advantages         |
+===============+===============+====================+
| *Bananas*     | $1.34         | - built-in wrapper |
|               |               | - bright color     |
+---------------+---------------+--------------------+
| Oranges       | $2.10         | - cures scurvy     |
|               |               | - **tasty**        |
+---------------+---------------+--------------------+
Fruit Price Advantages
Bananas $1.34
  • built-in wrapper
  • bright color
Oranges $2.10
  • cures scurvy
  • tasty
(\mathbf{x}^T\mathbf{x})^{-1}\mathbf{x}^T\mathbf{y}

\((\mathbf{x}^T\mathbf{x})^{-1}\mathbf{x}^T\mathbf{y}\)