/
README.Rmd
145 lines (116 loc) · 3.05 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 400,
out.width = "100%"
)
```
# fwlplot
<!-- badges: start -->
<!-- badges: end -->
This is a super simple package to help make scatter plots of two variables after residualizing by covariates. This package uses `fixest` so things are super fast. This is meant to (as much as possible) be a drop in replacement for `fixest::feols`. You should be able to replace `feols` with `fwl_plot` and get a plot.
## Installation
You can install the development version of fwlplot like so:
``` r
devtools::install_github("kylebutts/fwlplot")
```
## Example
Here's a simple example with fixed effects removed by `fixest`.
```{r}
library(fwlplot)
library(fixest)
flights <- data.table::fread("https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv")
flights$long_distance = (flights$distance > 2000)
# Sample 10000 rows
sample = flights[sample(nrow(flights), 10000), ]
```
```{r}
#| fig.width: 8
#| fig.height: 4
# Without covariates = scatterplot
fwl_plot(dep_delay ~ air_time, data = sample)
```
```{r}
#| fig.width: 8
#| fig.height: 4
# With covaraites = FWL'd scatterplot
fwl_plot(
dep_delay ~ air_time | origin + dest,
data = sample, vcov = "hc1"
)
```
### Plot random sample
If you have a large dataset, we can plot a sample of points with the `n_sample` argument. This determines the number of points *per plot* (see multiple estimation below).
```{r}
#| fig.width: 8
#| fig.height: 4
fwl_plot(
dep_delay ~ air_time | origin + dest,
# Full dataset for estimation, 1000 obs. for plotting
data = flights, n_sample = 1000
)
```
### Full `feols` compatability
This is meant to be a 1:1 drop-in replacement with fixest, so everything should work by just replacing `feols` with
```{r}
#| fig.width: 8
#| fig.height: 4
feols(
dep_delay ~ air_time | origin + dest,
data = sample, subset = ~long_distance, cluster = ~origin
)
```
```{r}
#| fig.width: 8
#| fig.height: 4
fwl_plot(
dep_delay ~ air_time | origin + dest,
data = sample, subset = ~long_distance, cluster = ~origin
)
```
### Multiple estimation
```{r}
#| fig.width: 8
#| fig.height: 4
# Multiple y variables
fwl_plot(
c(dep_delay, arr_delay) ~ air_time | origin + dest,
data = sample
)
```
```{r}
#| fig.width: 8
#| fig.height: 6
# `split` sample
fwl_plot(
c(dep_delay, arr_delay) ~ air_time | origin + dest,
data = sample, split = ~long_distance, n_sample = 1000
)
```
```{r}
#| fig.width: 8
#| fig.height: 10
# `fsplit` = `split` sample and Full sample
fwl_plot(
c(dep_delay, arr_delay) ~ air_time | origin + dest,
data = sample, fsplit = ~long_distance, n_sample = 1000
)
```
### ggplot2
```{r}
#| fig.width: 8
#| fig.height: 10
library(ggplot2)
theme_set(theme_grey(base_size = 16))
fwl_plot(
c(dep_delay, arr_delay) ~ air_time | origin + dest,
data = sample, fsplit = ~long_distance,
n_sample = 1000, ggplot = TRUE
)
```