```{r}
# install.packages("palmerpenguins")
library(palmerpenguins)
favstats(data = penguins, ~ bill_length_mm) |> kable()
```
min | Q1 | median | Q3 | max | mean | sd | n | missing | |
---|---|---|---|---|---|---|---|---|---|
32.1 | 39.225 | 44.45 | 48.5 | 59.6 | 43.92193 | 5.459584 | 342 | 2 |
align*
environment: multiline sequence of equationsarray
environment – tables the manual waycases
environment – piecewise definitionsRyan McShane, Ph.D.
The University of Chicago
Oct. 2nd, 2024
Oct. 25th, 2024
**Bold-face**
, *Italic*
, ~~Striking Through Text~~
Here is the start of the next paragraph where this text is bold, whereas this text is in italics. You can even strike through text.
This Markdown syntax also works on GitHub (“GitHub-Flavored Markdown”).
Table Header | Second Header |
---|---|
Table Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
You can center, right-, or left-justify text in your tables as you like.
Centered | Right-Justified | Left-Justified
:-------:|----------------:|:----------------
A | 24 | My Friend's Name
B | 5 | My Name
CC | 167 | Firstname Lastname
Centered | Right-Justified | Left-Justified |
---|---|---|
A | 24 | My Friend’s Name |
B | 5 | My Name |
CC | 167 | Firstname Lastname |
This Markdown syntax also works on GitHub (“GitHub-Flavored Markdown”).
Best practice is to write your code so that you don’t overflow the margins.
We’ll discuss code style more later!
You can resize your plots by adjusting fig.width
and fig.height
in the code chunk. The unit of fig.width
and fig.height
are in inches. Recall that letter size paper is 8.5 \(\times\) 11 inches.
```{r}
#| fig-width: 4.5
#| fig-height: 2.5
#| warning: false
gf_density(penguins, ~ bill_length_mm)
```
Please ALWAYS adjust the sizes of plots appropriately in your homework submission. The default settings are usually fine, but sometimes you may need to resize them for readability.
One can adjust the alignment of plots by specifying fig.align
to be "center"
, "right"
or "left"
. The default alignment is left.
We generally don’t have the two-column format in reveal.js
available to us as in Section 2.2.5, so we may need to cowplot
package to combine plots:
Read here for R Markdown-style code chunk options, and read here for Quarto style code chunk (“cell”) options. Note! Many of these options are output format-dependent. We can’t produce animations in PDFs, for example, and fig-pos
is only meaningful in PDF documents.
On Section 2.2.8, we learned how to reference a section header (often a problem number in this course).
\({}\)
On Section 2.1.2, we discussed Markdown tables.
\({}\)
Read more on Quarto and all of the things you can cross-reference (table, equations, sections, code listings, theorems and proofs, OH MY).
Quarto supports \(\LaTeX\) math symbols & expressions and can produce output with pretty math equations like \[f(x)= \frac{1}{\sqrt{2\pi}\sigma}\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right),\quad -\infty<x<\infty.\]
$x$
\(x\)
$x $
or $ x$
or $ x $
(fails to render or shows math as \(\LaTeX{}\) code)
Inline math symbols & expressions are placed between $...$
.
You can write Greek letters: $\alpha$
, $\beta$
, $\delta$
, $\epsilon$
, $\varepsilon$
\[\alpha, \;\beta, \;\delta, \;\epsilon, \; \varepsilon ...\]
Subscripts are written as $a_b$
\(a_b\) and superscripts as $a^b$
\(a^b\).
Note that multiple characters in a subscript need to be grouped by {...}
: $x_ij$
vs $x_{ij}$
\[x_ij \text{ vs } x_{ij}\]
Simple linear regression model: $Y_{ij}=\beta_0+\beta_1 X +\varepsilon_{ij}$
\[Y_{ij}=\beta_0+\beta_1 X +\varepsilon_{ij}\]
Place two dollar signs around math expressions in display mode like $$... $$
Fractions in inline math mode: $\frac{a}{b}$
\(\frac{a}{b}\) and in display math mode: $$\frac{a}{b}$$
\[\frac{a}{b}\]
Display fractions in inline math mode: $\dfrac{a}{b}$
\(\dfrac{a}{b}\)
Logistic regression model, $$P(Y=1) = \frac{e^{\alpha+\beta x}}{1+e^{\alpha+\beta x}}$$
\[P(Y=1) = \frac{e^{\alpha+\beta x}}{1+e^{\alpha+\beta x}}\]
Use \text{}
: $f(x) = 0 \text{ for all } x$
\[f(x) = 0 \text{ for all } x\]
And use \quad
or \;
to add space for readability
$f(x) = 0 \; \text{ for all } x$
$f(x) = 0 \quad \text{ for all } x$
:
\[f(x) = 0 \; \text{ for all } x\] \[f(x) = 0 \quad \text{ for all } x\]
$\log(x)$
, $\exp(x)$
, $\sqrt{x}$
\[\log(x), \exp(x), \sqrt{x}\]
\({}\)
Be sure to place the back-slash \
, or they would look like this rendered output:
\[log(x), exp(x), sqrt{x}\]
binom
environment$\binom{n}{k}$
\[\binom{n}{k}\]
You may sometimes see new shortcut definitions at the top of a .qmd
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\({}\)
These define helpful shortcuts. E.g., type $\E[X]$
instead of $\mathrm{E}[X]$
:
\[\mathrm{E}[X]\]
This can be used for “ill” as well:
\newcommand{\Loki}{\mathsf{L}\mathbb{O}\mathscr{K}\mathbf{I}}
\[\text{Loki: }\mathsf{L}\mathbb{O}\mathscr{K}\mathbf{I}\]
\newcommand{\Twitter}{\colorbox{black}{\textcolor{white}{\reflectbox{$\mathbb{X}$}}}}
(Not quite the Twitter logo, but many speculated it was \(\LaTeX\) at the time of the switch to “X”)
align*
environment: multiline sequence of equations\begin{align*}
(5x + 3y) - (4x - 7y +3y) &= (5x + 3y) - (4x - 4y)\\
&= 5x + 3y - 4x + 4y \\
&= x + 7y
\end{align*}
\[\begin{align*} (5x + 3y) - (4x - 7y +3y) &= (5x + 3y) - (4x - 4y)\\ &= 5x + 3y - 4x + 4y \\ &= x + 7y \end{align*}\]
\begin{align*}
(5x + 3y) - (4x - 7y +3y) &= (5x + 3y) - (4x - 4y) & \text{grouping like terms}\\
&= 5x + 3y - 4x + 4y & \text{distributing negative}\\
&= x + 7y & \text{grouping like terms}
\end{align*}
\[\begin{align*} (5x + 3y) - (4x - 7y +3y) &= (5x + 3y) - (4x - 4y) & \text{grouping like terms}\\ &= 5x + 3y - 4x + 4y & \text{distributing negative}\\ &= x + 7y & \text{grouping like terms} \end{align*}\]
array
environment – tables the manual way$$
\begin{array}{c|ccccc}
x & 0 & 1 & 2 & 3 & \text{o/w} \\\hline
p(x) & 0.6 & 0.25 & 0.1 & 0.05 & 0
\end{array}
$$
\[ \begin{array}{c|ccccc} x & 0 & 1 & 2 & 3 & \text{o/w} \\\hline p(x) & 0.6 & 0.25 & 0.1 & 0.05 & 0 \end{array} \]
cases
environment – piecewise definitions$$
p(x)=
\begin{cases}
0.6 & \text{if }x =0 \\
0.25 & \text{if }x =1 \\
0.1 & \text{if }x =2 \\
0.05 & \text{if }x =3 \\
0 & \text{if } x\notin \{0, 1, 2, 3\}
\end{cases}
$$
\[ p(x)= \begin{cases} 0.6 & \text{if }x =0 \\ 0.25 & \text{if }x =1 \\ 0.1 & \text{if }x =2 \\ 0.05 & \text{if }x =3 \\ 0 & \text{if } x\notin \{0, 1, 2, 3\} \end{cases} \]
$\sum_{i=1}^{n} x_ip(x_i)$
\(\sum_{i=1}^{n} x_ip(x_i)\)
\({}\)
\({}\)
$\int_a^b xf(x)dx$
\(\int_a^b xf(x)dx\)
$$\sum_{i=1}^{n} x_ip(x_i)$$
\[\sum_{i=1}^{n} x_ip(x_i)\]
\({}\)
$$\int_a^b xf(x)dx$$
\[\int_a^b xf(x)dx\]
Instead of \((\dfrac{5}{3})(\dfrac{3}{8})\), we can use \left(
and \right)
to produce \(\left( \dfrac{5}{3}\right)\left(\dfrac{3}{8} \right)\)!
$\left( \dfrac{5}{3}\right)$
\(\left( \dfrac{5}{3}\right)\)
\dot
$X ~ \dot \sim ~ \mathrm{Bin}(n, p)$
\(X ~ \dot \sim ~ \mathrm{Bin}(n, p)\)
\({}\)
\bar
$\bar{x}$
\(\bar{x}\)
\overline
$\frac{1}{7} = 0.\overline{142857}$
\(\frac{1}{7} = 0.\overline{142857}\)
\({}\)
\hat
$\hat \mu$
\(\hat \mu\)
\({}\)
Here’s my \(\LaTeX\) for R Markdown (and thus Quarto) cheatsheet. It summarizes everything we’ve discussed here.
Important notes from the chapter:
x = 5
or x <- 5
x=5
or x<-5
snake_case
for variable names, rather than camelCase
or PascalCase
or using.periods
, etc. We will stick to snake_case
where possible, although not every programmer (e.g., the former students that wrote the Qwixx program) follows these conventions, and sometimes (e.g., with kable
), it is difficult to make use of snake_case
.seq(from = 1, to = 10)
seq(1, 10)
Much of this advice is germane, although I suggest the following sequence for troubleshooting:
help(FUNCTION_NAME)
firsthelp
pages (see, for example, any package that uses the pkgdown
package to organize their web documentation: dplyr
, bayesplot
, mkin
, and many, many more).pkgdown
pages helpfully links to the public-facing GitHub repo (usually top right).
dplyr
’s issues. Here, we can see (as of this writing), 60 open issues and 4,886 closed issues. If you’re looking for help, the closed issues are often more informative than the open ones – just click “4,886 Closed
” and change the search query to is:issue is:closed
.R
folder. From here, package organization varies, but you’ll find the exported functions in one or more of the *.R
scripts here!reprex
(reproducible example) as described in Ch 8. Here’s a successful example for mosaic::xqt
..qmd
is essentially a miniature R script.install.packages
) in the Console is fine, but it’s better to keep track of code in a reproducible format. We’ve been using .QMD
s (and will continue to do so), but one can also write longer sets of code in R Scripts..R
file extension) with R code in them.Here is an example script called my_script.R
:
Tools -> Global Options...
.usethis::use_blank_slate()
first to make this choice effective.A new window is now open! In the files
tab, you should see all of the GitHub repo files as well as a new file with the .Rproj
extension.
Now, when you want to open a repo (after you’ve closed the RStudio window), you should:
*.Rproj
file!This will open a new RStudio window dedicated to just this repo!