[1] 4
1.5 — Optimize Workflow
ECON 480 • Econometrics • Fall 2022
Dr. Ryan Safner
Associate Professor of Economics
safner@hood.edu
ryansafner/metricsF22
metricsF22.classes.ryansafner.com
Writing text/documents
Managing citations and bibliographies
Performing data analysis
Making figures and tables
Saving files for future use
Monitoring changes in documents
Collaborating and sharing with others
Combining into a deliverable (report, paper, presentation, etc.)

Writing text/documents
Managing citations and bibliographies
Performing data analysis
Making figures and tables
Saving files for future use
Monitoring changes in documents
Collaborating and sharing with others
Combining into a deliverable (report, paper, presentation, etc.)

A lot of copy/paste
A lot of:



Source: Science Magazine

Source: Bloomberg
"'Reproducible research' is a redundant term. 'Irreproducible research' just used to be known as 'bullshit'." - @fperez_org ::slow clap::
— Kaitlin Thaney 💁🏻 (@kaythaney) May 8, 2014

This is how I make my…
I have not used any MS Office products since 2011 (good riddance!)
This stuff is optional

Meet Quarto, which can do all of this in one pipeline
Writing text/documents
Managing citations and bibliographies
Performing data analysis
Making figures and tables
Saving files for future use
Monitoring changes in documents
Collaborating and sharing with others
Combining into a deliverable (report, paper, presentation, etc.)

Open Source: free, useable forever, often very small file size
.doc file from Microsoft Word 1997?Automate and Minimize Errors, especially in repetitive processes
Can be used with version control (see below)

Quarto file (.qmd) is the “real” part of your analysis, everything can live in this plain-text file!
Document text in markdown
R code executed in “chunks”
Plots and tables generated from R code
Citations and bibliography automated with .bib file


Source: The Atlantic
File -> New File -> Quarto Document...

html: renders a webpage, viewable in any browser
pdf: renders a PDF document
LaTeX distribution to render (more on that soon)word: create a Micosoft Word document

Entire document is written in a single file with three types of content:
YAML header for metadatamarkdownR chunks for data analysis, plots, figures, tables, statistics, as necessary
Top of a document contains the YAML1 separated by three dashes --- above and below
Contains the metadata of the document, such as:
format must be specified, everything else can be left blank, and other options can be added as necessary
In most cases, you can safely ignore other things in the yaml until you are ready
---
title: Distributing Patronage^[I would like to thank the Board of Associates of Hood College...]
subtitle: Intellectual Property in the Transition from Natural State to Open Access Order
date: \today
author:
- Ryan Safner^[Hood College, Department of Economics and Business Administration; safner@hood.edu]
abstract: |
| "This paper explores the emergence of the modern forms of copyright and patent in ...
| *JEL Classification:* O30, O43, N43
| *Keywords:* Copyright, intellectual property, economic history, freedom of the press, economic development
bibliography: patronage.bib
geometry: margin = 1in
fontsize: 12pt
mainfont: Fira Sans Condensed
output:
pdf_document:
latex_engine: xelatex
number_sections: true
fig_caption: yes
header-includes:
- \usepackage{booktabs}
---R code with three backticks1 above and below your code# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
#| at the top of the chunkYAML header:`tidyverse` or `gapminder`, it formats the text as in-line code.R code to output something in the middle of a sentence, put r as the first character inside the backticks, and then run the actual code such as pi is equal to 3.1415927.pi is equal to `r pi`.
pi is equal to 3.1415927.
The average GDP per capita is ` r dollar(mean(gapminder$gdpPercap)) ` with a standard deviation of ` r dollar(sd(gapminder$gdpPercap)) `.
The average GDP per capita is $7,215.33 with a standard deviation of $9,857.45.
| Markdown Syntax | Output |
|---|---|
|
italics and bold |
|
superscript2 / subscript2 |
|
|
|
verbatim code |
<!-- Unprinted comments here --> (will not print in output; this comes from html)| Markdown Syntax | Output |
|---|---|
|
|
|
|
|
|
| Markdown Syntax | Output |
|---|---|
|
Header 1 |
|
Header 2 |
|
Header 3 |
|
Header 4 |
|
Header 5 |
|
Header 6 |
You can embed named hyperlinks, direct urls like https://metricsF22.classes.ryansafner.com/, and links to other places — like the previous slide in the document.


| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
: Table Example {tbl-colwidths="[25,25,25,25]"}
| Right | Left | Default | Center |
|---|---|---|---|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
kableExtra package is great for this see Documentation| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
kableExtra package is great for this see Documentation| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
$ tag before and after the math, two $$ before/after for a centered equation$1^2=\frac{\sqrt{16}}{4}$ produces 12=√164 in my text$$ \hat{\beta_1}=\frac{\displaystyle \sum_{i=1}^n (X_i-\bar{X})(Y_i-\bar{Y})}{\displaystyle \sum_{i=1}^n (X_i-\bar{X})^2} $$
^β1=n∑i=1(Xi−ˉX)(Yi−ˉY)n∑i=1(Xi−ˉX)2
R and markdownpdf output actually converts markdown files into TEX first!| Input | Output |
|---|---|
$\alpha$ |
α |
$\pi$ |
π |
$\frac{1}{2}$ |
12 |
$\hat{x}$ |
ˆx |
$\bar{y}$ |
ˉy |
$x_{1,2}$ |
x1,2 |
x^{a-1}$ |
xa−1 |
$\lim_{x \to \infty}$ |
limx→∞ |
$A=\begin{bmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \\ \end{bmatrix}$ |
A=[a1,1a1,2a2,1a2,2] |
.bib files.bib file to list all of your references in
R via: File -> New File -> Text File (and save with .bib at the end)examplebib.bib in this repository used in this documentYAML header in the main document, add bibliography: examplebib.bib so R knows to pull references from this file.bib file, like so:.bib FileA .bib file is a plain text file with entries like this
Classes for @article, @book, @collectedwork, @unpublished, etc.
editor, publisher, address)First input after the @article is your citation key (e.g. safner2016)
Whenever you want to cite a work in your text, call up the citation key with @, like so: @safner2016[], which produces (Safner, 2016)
You can customize citations, e.g.:
| Write | Produces |
|---|---|
[@Safner2016] |
(Safner, 2016) |
@Safner2016 |
Safner 2016 |
-@Safner2016 |
(2016) |
@Safner2016[p. 743-744] |
(Safner, 2016, p.743-744) |
For more information and examples, see Quarto’s Documentation on Citations
Lot of programs can help you manage references and export complete .bib files to use with R Markdown
Markdown files are plain text files and can be edited in any text editor
Any good editor will have syntax highlighting and coloring when you use tags (like bold, italic, code, and code #comments).

VS Code

Empty space is very important in markdown
Lines that begin with a space may not render properly
Math that contains spaces between the dollar-signs may not render properly
Moving from one type of content to another (e.g. a heading to a list to text to an equation to text) requires blank lines between them to work
When you are ready, you “redner” your markdown and code into an output format using:
knitr1, an R package that “knits” your R code and markdown .qmd into a .md file for:
pandoc is a “swiss-army knife” utility that can convert between dozens of document types
All you need to do is click the Render button at the top of the text editor!

Producing a PDF uses LATEX
You will need a full distribution of LATEX on your computer, OR
tinytex to install a mini-distro of LATEX inside of R:R Project is a way of systematically organizing your R history, working directory, and related files in a single, self-contained directory


README of this repository on GitHub for instructions (automatically shows on the main page)example_paper.qmd
.R scripts from scripts folderbibexample.bib from bibliography folderSource: PhD Comics
Source: PhD Comics
Source: PhD Comics
Keep your files backed up
Track changes
Collaborate on the same files with others
Edit files on one computer and then open and continue working on another?

Register an account for free
Set up a location on your computer for the Dropbox/ folder
Anything you put in this folder will sync to the cloud
Smart Sync - keep some files online only for space


Git is an “open source distributed version control system” widely used in the software development industry
Track changes on steroids (if MS Word’s Track Changes and Dropbox had a baby)
"repository")commit”) with “comments”push these to the cloudpull changes to (other) computers as neededGitHub is a popular (not the only!) cloud destination for these repositories

versions) of files with comments
fork or branch repository into multiple versions at oncerevert back to original versions as neededRequires some advanced set up, see this excellent guide
R Studio integrates git and github commands nicely


project # folder on my computer (the new working directory)
|
|- data/ # folder for data files
|- scripts/ # folder .R code
|- bibliography/ # folder for .bib files
|- figures/ # folder to plots and figures to
|- paper.qmd # write document here
paper.qmd, loading/saving files from/to various folders in project
df <- read_csv("data/my_data"); save plots like ggsave("figures/p.png")pdf or html.stage and commit changes with a description, push to GitHub.