-
Notifications
You must be signed in to change notification settings - Fork 0
/
r-coding.Rmd
172 lines (121 loc) · 3.19 KB
/
r-coding.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: "Coding Notes"
description: |
Coding Notes and Resources
author:
- name: Alex Stephenson
output:
distill::distill_article:
toc: true
toc_depth: 3
site: distill::distill_website
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(scipen = 999)
```
```{r, message = F, warning = F}
library(broom)
library(data.table)
library(dplyr)
library(fixest)
library(ggplot2)
library(marginaleffects)
library(purrr)
```
## Basic R Operations
There are a voluminous amount of resources on R online. Here is a very minimal crash course.
The most important underlying concept in R is that (almost) everything is an object.
### Data Types
R has six basic data types. The four most common types we work with are vectors, lists, matrices, and data frames.
#### Vectors
```{r}
## Three ways to create a vector
v = c(1,2,3,4,5)
v2 = seq(from=1,to=5, by = 1)
## Since the sequence is counting with no breaks we can also do this
v3 = 1:5
```
Vectors can also be characters (strings in other languages)
```{r}
w = c("A", "B", "C")
w2 = c("1","2","3")
## We can convert to other types by the as.* series
w3 = as.character(v)
w4 = as.numeric(w2)
```
#### Lists
```{r}
l = list(v, w)
l
```
#### Matrices
```{r}
m = matrix(seq(1,16,1), nrow = 4, byrow = T)
m
```
For much more about linear algebra in R, consult the [Matrix Algebra in R](matrix.html) section.
#### Data Frames
```{r}
df = data.frame(
var1 = letters[1:5],
var2 = c(1,2,3,4,5)
)
df
```
### Subsetting
### Control Operations
### Functions
## Graphing in R
### The Basics
R has default plotting, but for the purpose of these notes I exclusively use `ggplot2`. A main distinction between base plotting and `ggplot2` is that `ggplot2` requires a data frame and presumes that your data is tidy.
```{r, echo = TRUE, message = F, warning = F}
## Using the built-in dataset diamonds
df = diamonds
## Data
df |>
## Mapping
ggplot(aes(x = carat, y = price)) +
## geom
geom_point() +
## we can have multiple geoms
geom_smooth(method = "lm", se = F)+
## Labels
labs(x = "Carat",
y = "Price",
title = "Example Chart")+
## theme
theme_minimal()
```
For more extensive discussion of graphing capabilities in R, check out [Data Visualization: A Practical Introduction by Healy](https://socviz.co/).
### Useful Plots for Political Science
#### Coefficient Plot
First let's make a model using the gapminder dataset.
```{r}
## Make a model with robust standard errors using the gapminder
## dataset
m1 = fixest::feols(lifeExp ~ gdpPercap + pop + continent, data = gapminder::gapminder, vcov = "HC1") |>
tidy(conf.int = TRUE)
head(m1)
```
```{r}
m1 |>
ggplot(aes(x = term, y = estimate,
ymin = conf.low,
ymax = conf.high))+
geom_point() +
geom_pointrange() +
coord_flip() +
theme_minimal() +
labs(x = "",
y = "Coefficient Estimates",
title = "Coefficient Plot")
```
### Added Variable Plots
### Marginal Effects Plots
```{r}
marginModel = fixest::feglm(lifeExp ~ gdpPercap + pop + continent, data = gapminder::gapminder, vcov = "HC1") |>
marginaleffects::slopes(by = TRUE)
```
## Simulations in R
### The Birthday Problem