1. What is R, RStudio and R Markdown?

  • R is a popular open-source statistical programming environment.
  • R is freely available for download and runs on Macintosh and Windows computers.
  • RStudio is another freely available software package that has numerous features to make data analysis easier.
  • RStudio is open-source and free program that greatly facilitates the use of R.
  • RStudio gives users a text editor where we write programs, a grapp viewer which displays graphics we make, R console where we execute our programs, a help section, and many other useful features.
  • R Markdown consists of R and Markdown and works on RStudio.
  • Markdown is a Markup language proposed by John Gruber and Aaron Swartz in 2004.
  • R Markdown enables us to document R code, statistical results, and comments into one file

Advantages to use R Markdown on RStudio

  • You can display your idea, R code, statistical results, and comments at the same time in html.
    → You can trace what you have done in your analysis
    → You can avoid human errors in empirical analysis
  • Output type: HTML, PDF, MSWord, PowerPoint

HTML output is highly recommended

What is R Markdown?
・You need to know the difference between plain text and rich text to understand the difference between Markdown and R Markdown

2 types of text   

1. plain text

・A document made up of text which does not include format information, such as the size and the color of characters.
Rmd file we use here is also a plain text.
・Examples of plain text:
- TextEdit (macOS)
- memo (Windows)
plain text does not include functions of formatting or inserting figures and graphs

2. rich text

・ A document made up of text which includes format information. ・ Examples of rich text:
- Microsoft Word
- Pages
- LibreOffice Writer
rich text includes functions of formatting or inserting figures and graphs.

What is Markup lauguage?

Markup language is a language which aims at making a rich text only using plain text
・ Examples of Markup language: HTML:(HyperText Markup Language)
html contains figures, tables, bold characters, and index in addition to plain text.
html is written only by plain text.
・You need a Web Browser which enables you to visualize the text.
・ Examples of Web Browser:
- Firefox
- Chrome
- Edge
・Web Browsers read text
→ Interpret it
→ Visualize it as an output

What is Markdown?

Markdown reconstructs Markup with simpler grammar.

What is R Markdown?

R Markdown is a Markdown which contains R code and its statistical results altogether.

2. Setting up R Markdown

Procedure What you do Steps
I. Make an R Project :Make a folder (ex: keiryo_2023 1 - 5
II. Make an Rmd file : Make a file (ex: first_trial.Rmd 6 - 10
III. Click Knit : Visualize the Rmd file (ex: first_trial.html 11 - 18

I. Make an R Project

  • Make an R Project equals making an R Project foler

Step_1:

  • Start RStusioFileNew Project

Step_2:

  • Choose New Directory in Create Project

Step_3:

  • Choose New Project in Project Type

Step_4:

  • If you choose New Project, they you will see the following display below:
  • Here you assign where you put your R Project folder.
  • You assign to put your R Project folder on Desktop
  • Click Browse  

 

Step_5:

  • If you click Browse, then you will see the following display below:
  • Click Desktop on the left upper side of the display
  • Click open

Step_6:

  • Now you come back to Step_6
  • Type keiryo_2023 in Directory name:
  • You should not use Japanese when you type directory name or file name
  • Click Create Project

 

What you should not do in typing at Directory name ・You should not use Japanese and space.
・If you want to use symbols, use under bar _ and period .
・It would be better for you not to use hyphens-
・Start with an alphabet character (Do not start with symbols or numbers)

Step_7:

  • Confirm the name of your R Project (keiryo_2023) displayed on the upper right
  • This is the name of your R Project

 

II. Make an Rmd file

Step_8:

  • Choose FileNew FileR Markdown

 

Step_9:

  • Type How to use R Markdown in Title:
  • Type your name at Author:
  • Type "r Sys.Date()" at Date:
    → RStudio automatically display the date
  • Check HTML in Default Output Format:

  • Click OK

Step_10:

  • Now you made an Rmd file
  • The name of the Rmd file you just made is Untitled1

III. Click Knit

Step_11:

  • Click Knit

Step_12:

  • Here, you put a name on the Rmd file you made
  • You should not type Japanese here
  • You should not use Japanese when you type directory name or file name
  • Type first_trial in Save As:
  • Click Save

What you should not do in typing at Save As: ・You should not use Japanese and space.
・If you want to use symbols, use under bar _ and period .
・It would be better for you not to use hyphens-
・Start with an alphabet character (Do not start with symbols or numbers)

注意:
When you visualize the first_trial.Rmd、you may see types of displays (either Step_13 or Step_14)

  • If you see the monitor like Step_13
    → No problem, go to Step_16
  • If you see the monitor like Step_14
    → Go to Step_15, and follow the instruction

Step_13:

  • You see your output on the lower right of the disllay
  • This is first_trial.html

Step_14:

  • You see the html display like this outside of the RStudio display

  • This is not convenient, so let the html file displayed on the lower right of the monitor within RStudio

Step_15

  • Click the gear icon ① in the upper middle of the display
  • Choose Preview in Viewer Pane

  • Click Knit
  • You see the output on the lower right of the display

Step_16:

  • Click the taller icon of the two on the extreme upper right corner
    → You can double the size of your output display

Step_17: Confiem inside of R Project folder

  • Click Files on the upper middle of the display
  • You see check inside of the R Project folder you named as keiryo_2023

  • You see the following 4 files in the R Project folder
File name
.Rhistory History file
first_trial.html Displayed on the right side pane (html file)
first_trial.Rmd Displayed on the left upper side pane (Rmd file)
keiryo_2023.Rproj You click RProject to start this project

Step_18:

  • If you see this, you got it right!

3. R Markdown Grammar

Three parts in R Markdown

  • Rmd file is made up of the following 3 parts:
Parts Function
YAML header :Settings
Chunk : R code is written within a chunk
Markdown : Main body of the document

R Markdown Grammar

items
1. Begin a new line
2. Emphasis: bold, itlic, underline, color, etc
3. Bullet points
4. Headlines
5. Dividing line
6. Tables
7. Pictures
8. Link
9. Footnotes
10. Mathematical formula
11. Citation
12. Comments
13. R code and chunk
14. Chunk option
15. Display / Non-display R code and statistical results
16. Plots
17. Another way of writing chunk option
18. Cosmetics on R code
19. Header options
20. How to add contents
21. How to highlight R code

3.1 Begin a new line

  • In Markdown, hitting return key does not mean beginning a new line

  • What you type in Rmd file

Get a good idea and stay with it. Dog it, and work at it until it’s done right. (Walt Disney) 
  • What you see in html output
Get a good idea and stay with it. Dog it, and work at it until it’s done right. (Walt Disney) 
  • Two ways of beginning a new line:
Insert two spaces and hit the return key
  • What you type in Rmd file
Get a good idea and stay with it.  # Insert two spaces and hit the return key
Dog it, and work at it until it’s done right.  # Insert two spaces and hit the return key
(Walt Disney) 
  • What you see in html output
Get a good idea and stay with it.  
Dog it, and work at it until it’s done right.  
(Walt Disney)   

3.2 Empasis: bold, italic, underline, strike-through line  

  • What you type in Rmd file
You can make it **bold**   

You can make it *italic*  

You can draw an <u>underline</u>   
  
You can ~~cancel what you wrote~~  

You can [paint letters in red]{style="color:red"} or [blue]{style="color:blue"}, for exmple
  • What you see in html output

You can make it bold

You can make a italic

You can draw an underline

You can cancel what you wrote

You can paint letters in red or blue, for example

3.3 Bullet points

  • The two ways of bullet points:
(1) Bullet points without order
  • Insert one space after hyphen, -
  • Insert two spaces at the end of each line and hit the return key
  • What you type in Rmd file
- Questions about Questions  
- The Experimental Ideal  
  - The Selection Problem  
  - Random Assignment Solves the Selection Problem  
  - Regression Analysis of Experiments  
- THE CORE  
- Making Regression Make Sense  
  - Regression Fundamentals  
  - Regression and Causality  
  - Heterogeneity and Nonlinearity
  • What you see in html output

  • Questions about Questions

  • The Experimental Ideal

    • The Selection Problem
    • Random Assignment Solves the Selection Problem
    • Regression Analysis of Experiments
  • THE CORE

  • Making Regression Make Sense

    • Regression Fundamentals
    • Regression and Causality
    • Heterogeneity and Nonlinearity
(2) Bullet points with order
  • Replace the hyphen with number and period, for instance, 1.
  • What you type in Rmd file
1. Questions about Questions  
2. The Experimental Ideal  
  2.1 The Selection Problem  
  2.2 Random Assignment Solves the Selection Problem  
  2.3 Regression Analysis of Experiments  
3. THE CORE  
4. Making Regression Make Sense  
  4.1 Regression Fundamentals  
  4.2 Regression and Causality  
  4.3 Heterogeneity and Nonlinearity
  • What you see in html output
  1. Questions about Questions
  2. The Experimental Ideal
    2.1 The Selection Problem
    2.2 Random Assignment Solves the Selection Problem
    2.3 Regression Analysis of Experiments
  3. THE CORE
  4. Making Regression Make Sense
    4.1 Regression Fundamentals
    4.2 Regression and Causality
    4.3 Heterogeneity and Nonlinearity

3.4 Headlines

  • Use # when you put the title of chapters and sections
  • # leads to the biggest characters, ## leads to the smaller ones, and so on
  • The number of sharps increases, the smaller the characters
  • The maximum number of shapes is 4

3.5 Dividing line

  • You can draw a line with ---

  • What you type in Rmd file

---
  • What you see in html output

3.6 Tables

  • You an make a table using | in Markdown

  • The first row is the header where variable names are displayed

  • You make an additional row by adding |---|

  • You can change the color of background to grey if you use ``

  • The 1st row is the header and it is shown as bold and centered automatically

  • The 1st and 2nd rows to N rows is automatically left aligned

  • You can change center aligned, right aligned:
    |:---|・・・left aligned (default)
    |:---:|・・・center aliged
    |---:|・・・right aligned

  • What you type in Rmd file

|variable names|explanation|
|:---|:---|:---|
| 1. Assign data|`ggplot(data = )`|Choose the data you use|
| 2. Assign variables|`aes()`|Choose the variables you use|
| 3. Assign the type of graphs|`geom_*()`|Choose the graph you use|
  • What you see in html output
variable names explanation
1. Assign data ggplot(data = ) Choose the data you use
2. Assign variables aes() Choose the variables you use
3. Assign the type of graphs geom_*() Choose the graph you use

3.7 Pictures

  • You type the following R code in your Chunk to insert pictures in R Markdown
![](ファイル名)
  • If the picture you want to insert is not inside the working directory (that is, R project folder, or keiryo_2023 here)

  • You need to assign the path to the picture you want to insert - You made your R project folder, named keiryo_2023
    → You don’t have to assign the path

  • All you have to do is to make a new folder in which you put your picture you want to insert

  • Let’s take a look at Step_17

Check inside of R Project folder

  • Click Files in the upper middle side of the screen
  • You can see what your R Project folder, named keiryo_2023 has in the folder

  • You see the following 4 files:
File names ****
.Rhistory history file
first_trial.html Output displayed in the right pane (html file)
first_trial.Rmd Rmd file displayed in the left upper pane
keiryo_2023.Rproj You click this icon when you want to open your project
  • Put tsubakuro_2000.JPG into keiryo_2023

  • Confirm that you see tsubakuro_2000.JPG in keiryo_2023

  • Type the following R code OUT OF the chunk」 and click Knit

  • What you type in Rmd file

![](tsubakuro_2000.JPG){width=30%} # You can adjust the size   
  • What you see in html output

  • If you want to center the picture, then type the following R code:
::: {style="text-align: center;"}
![](tsubakuro_2000.JPG){width=30%}
:::
  • What you see in html output

Make figure folder inside keiryo_2023 ・It would be convenient for you to make a folder, named, for instance, figure, within your R project folder and put your pictures in it
・This way, you don’t have to assign the path to the picture you want to insert
・It must be better for you to make figure folder in keiryo_2023 and put tsubakuro_2000.JPG in it
・If this is the case, then you use the following R code:

::: {style="text-align: center;"}
![](figure/tsubakuro_2000.JPG){width=30%}
:::
  • Remember that the path chagnes here:
    tsubakuro_2000.JPG

    figure/tsubakuro_2000.JPG

3.9 Footnotes

  • Suppose you want to insert “The Angrist-Lavy (1999) results turn up again in chapter 6, as an illustration of the quasi-experimental regression-discontinuity design.” at the end of the following sentence:

  • What you type in Rmd file
The hospital allegory of selection bias would therefore seem to apply to the class size question as well.
[^foot1]

[^foot1]: The Angrist-Lavy (1999) results turn up again in chapter 6, as an illustration of the quasi-experimental regression-discontinuity design. 
  • What you see in html output

How it works

  • [^foot1] ・・・Type this at the exact point you want to insert footnote
  • [^foot1]:・・・Type what you want to write in the footnote after the colon :
  • ^ is called a unique identifier

3.10 Mathematical formula

  • You can output inline formula using $formula$ or $$formula$

  • The usage in mathematical formula is almost the same as LaTeX

  • The mathematical formula in R Markdown is being rendered by MathJax

  • MathJax library is not embeded in HTML
    → You may not be able to have an appropriate html output unless yo are connected to internet

  • What you type in Rmd file

$Y = a + bx^2$
  • What you see in html output
    \(Y = a + bx^2\)

  • If you want to display the formula as an independent row:

  • What you type in Rmd file

$$Y = a + bx^2$$
  • What you see in html output
    \[Y = a + bx^2\]

  • When your mathematical formula is composed of multiple rows, then you use \begin{aligned}・・・\end{aligned} as follow:

  • What you type in Rmd file

$$
\begin{aligned}
(x-x')^2 + (y - y')^2 < r^2 \\
x^2 + y^2 < 0.5^2
\end{aligned}
$$
  • What you see in html output
    \[ \begin{aligned} (x-x')^2 + (y - y')^2 < r^2 \\ x^2 + y^2 < 0.5^2 \end{aligned} \]

3.11 Citation

  • If you want to cite something, then insert > at the beginning of the sentence and one space

  • What you type in Rmd file

David Easton defines "Plotics" as follows: 

> [A] Politics is “the authoritative allocation of values for the society”
  • What you see in html output

David Easton defines “Politics” as follows:

[A] Politics is “the authoritative allocation of values for the society”

3.12 Comments

  • If you have written something interesting and important, but you will not use it for a while
    → It would be better for you to save it as a comment:

  • Use <!-- and -->

  • What you type in Rmd file

I write something below, but it is not didplayed  
<!--
  As I said this sentence is not displayed  
  -->
  You cannot see the sentence above  
  • What you see in html output

I write something below, but it is not didplayed
You cannot see the sentence above

3.13 R code and chunk

  • You can make a structured document by using a simple grammar of R Markdown
  • The advantage of R Markdown lies in the fact that you can simultaneously display a document, R code, and its statistical results
  • I will introduce how to combine these three stuff

How to incorporate an R code into a document ・Type R code in a chunk
・A chunk looks like this:

```{r}

```

・Type R code into the chunk
→ You can display R code and its results

  • You can do exactly the same thing in chunk as you do in R
    ・Load a package: ex) library(tidyverse)
    ・Read a data: ex) read_csv(....)
    ・Make an object: ex) mutate(\(x = 2/x\))
    ・Data handling
    ・Visualize a data, etc…..
  • Let’s calculate the average value of the 5 numbers: 1, 2, 3, 4, 5
  • All you have to do is to type the following R code in a chunk
x <- c(1, 2, 3, 4, 5)
mean(x)
[1] 3
  • But, if you want to incorporate an R code into a document like this:
x <- c(1, 2, 3, 4, 5)  
The average value of x is 3  
  • Incorporate an R code into a document means that if you change a value of x, for example change the value 5 to 10, then the average value of x should also be updated to a new mean

  • To make this happen, you need to incorporate R code into your document

  • What you type in Rmd file

  • What you see in html output
x <- c(1, 2, 3, 4, 5)  

The average value of x is 3

  • If you change the value 5 to 10
    → You will see the following output:
x <- c(1, 2, 3, 4, 10)  

The average value of x is 4

3.14 Chunk option

  • R Makrdown is made up of R and Markdown
  • R means what lies inside a chunk
  • Markdown means what lies out of a chunk
  • You can freely customize them
  • You can customize R code and its statistical results by adding a chunk option
  • You can customize a document by setting in the YAML header
  • A chunk starts with ```{r}
  • You can add a chunk option inside of {r}
    → example: {r chunk name, option 1, option 2, ...}
Example 1: using chunk option on map:
  • You can modify the size of map by using chank option

Example 2: using chunk option on map:

3.15 Display / Non-display R code and statistical results

  • You can make this happen by assigning as a chunk option
  • You can use this option when you want to display R code only, or statistical results only
  • For instance, suppose you want to calculate the mean of x (x is composed of 1, 2, 3, 4, 5)
When you don’t assign anything in chunk option:
x <- c(1, 2, 3, 4, 5)
mean(x)
[1] 3
When you want to display R code only, and do not calculate mean of x

Type ```{r eval = FALSE} in chunk option

x <- c(1, 2, 3, 4, 5)
mean(x)
When you don’t want to display R code and want to display its statistical results only

Type ```{r echo = FALSE} in chunk option

[1] 3

3.16 Plots

  • You can output figures in R Markdown
  • You can customize the size and the resolution of figures
argument
fig.height : height of the figure, inch, default = 7
fig.width : width of the figure, inch default = 7
fig.align : figure position: left, center, or right
fig.cap : caption
dpi : figure resolution, default = 72, more than 300 is preferable if you get it published
library(tidyverse)
  • Click hr96-21.csv and download the election data in Japan’s lower house

  • Make a folder, maned data in your R Project folder

  • Put hr96-21.csv into data folder

hr <- read_csv("data/hr96-21.csv",
               na = ".")
  • Using hr, let’s make a figure with 5 inch height, 7 inch width, centered, and figure resolution is 72 dpi
  • Add "4 Candidates' election performance: 1996-2021" as caption
  • The following R code is not shown as output, but you need to type it as chunk option

{r, fig.height = 5, fig.width = 7, fig.align = "center", dpi = 72, fig.cap = "4 Candidates' election performance: 1996-2021"}

hr |> 
  select(year, voteshare, name) |> 
  filter(name == "KISHIDA, FUMIO" | name == "TAKAICHI, SANAE"| 
           name == "KONO, TARO"| name == "NODA, SEIKO") |> 
  ggplot(aes(x = year, 
             y = voteshare, 
             colour = name, 
             linetype = name, 
             shape = name)) +
  geom_point() +
       geom_line() + 
  geom_text(
    aes(y = voteshare + 0.1, 
        label = round(voteshare, digits = 1), vjust = 0)
    ) +
  theme(legend.position = c(0.85, 0.2)) +
  
  labs(x = "Election Year", 
       y = "Vote share(%)")
4 Candidates' election performance: 1996-2021

4 Candidates’ election performance: 1996-2021

3.17 Another way of writing chunk option

  • As explained in 3.1.14 Plot, you can add a chunk option inside of {r}

  • But, when you have a bunch of chuk options, it is not easy to read to add many chunks inside of {r}
    → You can assign multiple chunk options using #|

  • In 3.1.14 Plot, we assigned chunk options as follows:

```{r, fig.height = 5, fig.width = 7, fig.align = "center",
dpi = 72, fig.cap = "4 Candidates' election performance: 1996-2021"}
  • You can write these chunk options much simpler way:
```{r}
#| fig.height: 5 
#| fig.width: 7
#| fig.align: "center"
#| dpi: 72
#| fig.cap: "4 Candidates' election performance: 1996-2021"  

How to set up for entire chunk options

  • At the beginning of your Rmd file, declare as follows:
fig.align = "center" :Centering all figures
dpi = 300" :Setting figure resolution = 300

→ You type YAML header as follows:

```{r, include = FALSE}
knitr::opts_chunk$set(fig.align = "center", dpi = 300)
```
  • In this way, you can put these two options (fig.align = "center" & dpi = 300) covering all chunks in RMarkdown file

3.18 Cosmetics on R code

  • Usually R users share their R code with others
  • It is important to write easy-to-read R code
    → Add a chunk option, tidy = TRUE
    → R automatically change R code which is way easier to read
  • For instance, let’s compare the two outputs: R code without tidy = TRUE and the one with tidy = TRUE

R code you typed in a chunk

hr|>ggplot(aes(x=year,y=voteshare,colour=j_name))

Output without tidy = TRUE in your chunk option

hr|>ggplot(aes(x=year,y=voteshare,colour=j_name))

Output with tidy = TRUE in your chunk option

hr |>
    ggplot(aes(x = year, y = voteshare, colour = j_name))
  • Typing tidy = TRUE in your chunk option enables you to output way easy-to-read R code
  • To use tidy = TRUE, you need to install a package, {formatR} beforehand
  • However, contrary to other packages, you don’t have to type library(formatR) on your Rmd file

3.19 Header options

  • If you make a new R Markdown file, you will see the following header on the top
---
title: "Untitled"
author: "Masahiko Asano"
date: "2022-09-13"
output: html_document
---
4 items you can customize:

title: Title of the Rmd file
author: Your name
date: Date you make this document
output: Type of output・・・ you type html here

  • R Markdown header is written by YAML format
  • These 4 items can be modified
  • You can incorporate inline R code in them

・It would be useful for you to assign date: "2022-10-03" at date section
→ You don’t have to type today’s date every time you work on your Rmd file

  • What you type in Rmd file (out of a chunk)
---
title: "How to use RMarkdown"
author: "Masahiko Asano"
date: "`r Sys.Date()`"
output: html_document
---
  • What you see in html output

3.20 How to add contents    

  • When you make a relatively large size of Rmd file, it would be better for you to add contents
toc :Output of contents default is FALSE
toc_depth :Depth of contents default is 3
toc_float :Floating contents default is FALSE
number_sections :Serial number default is FALSE
  • # makes a chapter

  • ## makes a section

  • ### makes a sub-section

  • What you type in Rmd file (out of a chunk)

---
title: "title"
subtitle: "sub-title"
author: "Your name"
date: "`r Sys.Date()`"
output:
  html_document:  
    toc: FALSE    
    toc_depth: 3  # The depth of toc = 3
    toc_float: FALSE # No floating contents
    number_sections: FALSE # No serial numbers on contents
---
      
# 1. Making Regression Make Sense  
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  

## 1.1 Regression Fundamentals  
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy  
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy  

## 1.2 Regression and Causality  
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz  
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz  
  • What you see in html output

3.21 How to highlight R code

  • Highlighting R code enhances readability of the code
  • You can modify setting at highligh:
  • R Markdown 2.3 enables us to choose from 10 kinds highlights
  • In the following example, I choose highlight: "tango"
  • You can choose whatever you like
---
title: ""
subtitle: ""
author: ""
date: "`r Sys.Date()`"
output:
  html_document:
   highlight: "tango"  # You assign your preferable hyghlight here
---

参考文献