-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathcausality-tidy.Rmd
633 lines (486 loc) · 14 KB
/
causality-tidy.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
---
title: 'Code for QSS tidyverse Chapter 2: Causality'
author: "Kosuke Imai and Nora Webb Williams"
date: "First Printing"
output:
pdf_document: default
always_allow_html: true
---
# Causality
```{r setup2, include = FALSE, purl=FALSE, eval = T}
library(knitr)
library(kableExtra)
library(tidyverse)
opts_chunk$set(tidy = FALSE,
fig.align = "center",
background = "#EEEEEE",
fig.width = 7,
fig.height = 7,
out.width = ".7\\linewidth",
out.height = ".7\\linewidth",
cache = TRUE)
options(width = 60)
set.seed(12345)
```
## Racial Discrimination in the Labor Market
```{r, message=FALSE}
## Load required packages
library(tidyverse)
library(qss)
## Load in the data from QSS package
data(resume, package = "qss")
```
```{r}
## Or read in a saved CSV
resume <- read_csv("resume.csv")
```
```{r, message=FALSE}
resume <- read_csv("resume.csv",
col_types = cols(
firstname = col_character(),
sex = col_character(),
race = col_character(),
call = col_number()))
```
```{r}
dim(resume)
```
```{r}
head(resume)
tail(resume)
glimpse(resume)
```
```{r}
summary(resume)
```
```{r, message=FALSE}
race.call.summary <- resume %>%
group_by(race, call) %>% # create for each race and callback status
count()
race.call.summary
```
```{r, message=FALSE}
race.call.tab <- race.call.summary %>%
pivot_wider(names_from = call, # reshape the data
values_from = n)
race.call.tab
```
```{r}
race.call.tab.names <- race.call.tab %>%
rename(no_callback = "0",
callback = "1")
race.call.tab.names
```
```{r}
race.call.tab.names <- race.call.tab.names %>%
mutate(total_resumes = no_callback + callback,
callback_prop = callback / total_resumes)
race.call.tab.names
```
```{r}
overall_callback <- resume %>%
summarize(total_callback_rate = sum(call) / n())
overall_callback
```
```{r}
overall_callback <- resume %>%
summarize(total_callback_rate = mean(call))
overall_callback
```
```{r, message=FALSE}
callback_by_race <- resume %>%
group_by(race) %>%
summarize(callback_rate = mean(call))
callback_by_race
```
## Subsetting Data in R
### Logical Values and Operators
```{r}
class(TRUE)
```
```{r}
as.integer(TRUE)
as.integer(FALSE)
```
```{r}
x <- c(TRUE, FALSE, TRUE) # a vector with logical values
mean(x) # proportion of TRUEs
sum(x) # number of TRUEs
```
```{r}
FALSE & TRUE
TRUE & TRUE
```
```{r}
TRUE | FALSE
FALSE | FALSE
```
```{r}
TRUE & FALSE & TRUE
```
```{r}
(TRUE | FALSE) & FALSE # the parentheses evaluate to TRUE
TRUE | (FALSE & FALSE) # the parentheses evaluate to FALSE
```
```{r}
TF1 <- c(TRUE, FALSE, FALSE)
TF2 <- c(TRUE, FALSE, TRUE)
TF1 | TF2
TF1 & TF2
```
### Relational Operators
```{r}
4 > 3
"Hello" == "hello" # R is case-sensitive
"Hello" != "hello"
```
```{r}
x <- c(3, 2, 1, -2, -1)
x >= 2
x != 1
```
```{r}
## logical conjunction of two vectors with logical values
(x > 0) & (x <= 2)
## logical disjunction of two vectors with logical values
(x > 2) | (x <= -1)
```
```{r}
x.int <- (x > 0) & (x <= 2) # logical vector
x.int
mean(x.int) # proportion of TRUEs
sum(x.int) # number of TRUEs
```
### Subsetting
```{r}
## callback rate for black-sounding names
resume %>%
filter(race == "black") %>% # only keep observations where "race" is black
summarize(mean(call)) # take the average of call
```
```{r}
## Subset the data with black names
resumeB <- filter(resume, race == "black")
## Calculate the mean callback rate
summarize(resumeB, mean(call))
## with $ operator to run mean() on the call column
mean(resumeB$call)
```
```{r}
resumeBf <- filter(resume, race == "black" & sex == "female")
```
```{r}
## callback rate for black female names
Bf_callback <- filter(resume, race == "black" & sex == "female") %>%
summarize(callback_rate = mean(call)) %>%
pull()
## print the value to the console
print(Bf_callback)
## callback rate for black male names
Bm_callback <- filter(resume, race == "black" & sex == "male") %>%
summarize(callback_rate = mean(call)) %>%
pull()
## print the value to the console
print(Bm_callback)
## callback rate for white female names
Wf_callback <- filter(resume, race == "white" & sex == "female") %>%
summarize(callback_rate = mean(call)) %>%
pull()
## print the value to the console
print(Wf_callback)
## callback rate for white male names
Wm_callback <- filter(resume, race == "white" & sex == "male") %>%
summarize(callback_rate = mean(call)) %>%
pull()
## print the value to the console
print(Wm_callback)
## difference between white women and black women
Wf_callback - Bf_callback
## difference between white men and black men
Wm_callback - Bm_callback
```
```{r, message=FALSE}
racial_gaps_by_sex <- resume %>%
group_by(race, sex) %>% # using two variables to group the data
summarize(callback = mean(call)) %>% # the callback rate for each group
pivot_wider(names_from = race, # reshaping the data
values_from = callback) %>%
mutate(race_gap = white - black)
print(racial_gaps_by_sex)
```
```{r, message=FALSE}
## what happens in this portion of the code?
resume %>%
group_by(race, sex) %>%
summarize(callback = mean(call))
## What happens after we add the pivot_wider()?
resume %>%
group_by(race, sex) %>%
summarize(callback = mean(call)) %>%
pivot_wider(names_from = race,
values_from = callback)
## And so on
```
### Simple Conditional Statements
```{r}
resume <- resume %>%
## create a new variable that is 1 if the resume is black and female,
## 0 otherwise
mutate(BlackFemale = if_else(race == "black" &
sex == "female", 1, 0))
```
```{r}
# Rows in the resumeBf data
nrow(resumeBf)
# Is that equal to sum of BlackFemale?
nrow(resumeBf) == resume %>% summarize(bf = sum(BlackFemale))
```
### Factor Variables
```{r}
resume_fact <- resume %>%
mutate(type = if_else(race == "black" & sex == "female", "BlackFemale", ""),
type = if_else(race == "black" & sex == "male", "BlackMale", type),
type = if_else(race == "white" & sex == "female", "WhiteFemale", type),
type = if_else(race == "white" & sex == "male", "WhiteMemale", type))
head(resume_fact)
```
```{r}
resume <- resume %>%
## add a categorical variable for race/gender type
mutate(type = case_when(race == "black" & sex == "female" ~ "BlackFemale",
race == "white" & sex == "female" ~ "WhiteFemale",
race == "black" & sex == "male" ~ "BlackMale",
race == "white" & sex == "male" ~ "WhiteMale",
TRUE ~ "other"
))
head(resume)
## Did any observations receive the "other" value for type?
filter(resume, type == "other")
```
```{r}
## check object class
class(resume$type)
## coerce the character variable into a factor variable
resume <- resume %>%
mutate(type = as.factor(type))
## look at the levels of the factor
levels(resume$type)
```
```{r, message=FALSE}
firstname_callback <- resume %>%
group_by(firstname) %>%
select(firstname, call) %>%
summarize(callback = mean(call))
head(firstname_callback)
```
## Causal Effects and the Counterfactual
```{r}
slice(resume, 1)
```
## Randomized Controlled Trials
### The Role of Randomization
### Social Pressure and Voter Turnout
```{r}
## Load in the data from QSS package
data(resume, package = "qss")
## Or from a csv
social <- read_csv("social.csv",
col_types = cols(sex = col_character(),
yearofbirth = col_double(),
primary2004 = col_double(),
messages = col_character(),
primary2006 = col_double(),
hhsize = col_double()))
summary(social) # summarize the data
```
```{r, message=FALSE}
## Average turnout by treatment message
turnout_by_message <- social %>%
group_by(messages) %>%
summarize(turnout = mean(primary2006))
turnout_by_message
## Differences between treatment(s) and control means
turnout_diffs <- turnout_by_message %>%
pivot_wider(names_from = messages,
values_from = turnout) %>%
mutate(diff_Civic_Duty = `Civic Duty` - Control,
diff_Hawthorne = Hawthorne - Control,
diff_Neighbors = Neighbors - Control) %>%
select(diff_Civic_Duty, diff_Hawthorne, diff_Neighbors)
turnout_diffs
```
```{r, message=FALSE}
social %>%
mutate(age = 2006 - yearofbirth) %>%
group_by(messages) %>%
summarize(age_avg = mean(age),
primary2004_avg = mean(primary2004),
hhsize_avg = mean(hhsize))
```
## Observational Studies
### Minimum Wage and Unemployment
```{r, message=FALSE}
minwage <- read_csv("minwage.csv") # load the data
## or
data(minwage, package = "qss")
dim(minwage) # dimension of data
glimpse(minwage)
summary(minwage) # summary of data
```
```{r, message=FALSE}
## Add a 'state' variable
minwage <- minwage %>%
mutate(state = if_else(location == "PA", "PA", "NJ"))
## Create the 'new_wage' object
new_wage <- 5.05
## Calculate the proportions above and below the new wage by state
state_props <- minwage %>%
mutate(above_min_before = if_else(wageBefore >= new_wage, 1, 0),
above_min_after = if_else(wageAfter >= new_wage, 1, 0)) %>%
group_by(state) %>%
summarize(prop_before = mean(above_min_before),
prop_after = mean(above_min_after))
state_props
```
```{r, message=FALSE}
## First create new variables to calculate the
## proportion of full-time employees
minwage <- minwage %>%
mutate(totalAfter = fullAfter + partAfter,
fullPropAfter = fullAfter / totalAfter)
## Then calculate the average proportion of full-time workers by state
full_prop_by_state <- minwage %>%
group_by(state) %>%
summarize(fullPropAfter = mean(fullPropAfter))
## To calculate the difference between states, we use pivot_wider()
## and mutate()
pivot_wider(full_prop_by_state,
names_from = state, values_from = fullPropAfter) %>%
mutate(diff = NJ - PA)
```
### Confounding Bias
```{r, message = FALSE}
chains_by_state <- minwage %>%
group_by(state) %>%
count(chain) %>%
mutate(prop = n / sum(n)) %>%
pivot_wider(-n, # this drops the 'n' variable prior to pivoting
names_from = state,
values_from = prop)
chains_by_state
```
```{r message=FALSE, warning=FALSE}
full_prop_by_state_chain <- minwage %>%
group_by(state, chain) %>%
summarize(fullPropAfter = mean(fullPropAfter)) %>%
pivot_wider(names_from = state,
values_from = fullPropAfter) %>%
mutate(diff = NJ - PA)
full_prop_by_state_chain
```
```{r, message = FALSE}
prop_by_state_chain_location_subset <- minwage %>%
filter(!location %in% c("shoreNJ", "centralNJ")) %>%
group_by(state, chain) %>%
summarize(fullPropAfter = mean(fullPropAfter)) %>%
pivot_wider(names_from = state,
values_from = fullPropAfter) %>%
mutate(diff = NJ - PA)
prop_by_state_chain_location_subset
```
### Before-and-After and Difference-in-Differences Designs
```{r}
## First, create a variable for the full-time
## proportion prior to the change
minwage <- minwage %>%
mutate(totalBefore = fullBefore + partBefore,
fullPropBefore = fullBefore / totalBefore)
## Then look at the differences in average proportion of full-time
## before and after (in NJ only)
minwage %>%
filter(state == "NJ") %>%
summarize(diff = mean(fullPropAfter) - mean(fullPropBefore))
```
```{r, message=FALSE}
## DiD estimate
minwage %>%
group_by(state) %>%
## difference before and after
summarize(diff = mean(fullPropAfter) - mean(fullPropBefore)) %>%
pivot_wider(names_from = state, values_from = diff) %>%
## difference in difference between states
mutate(diff_in_diff = NJ - PA)
```
## Descriptive Statistics for a Single Variable
### Quantiles
```{r, message=FALSE}
## median difference-in-differences
minwage %>%
group_by(state) %>%
summarize(diff = median(fullPropAfter) - median(fullPropBefore)) %>%
pivot_wider(names_from = state, values_from = diff) %>%
mutate(diff_in_diff = NJ - PA)
```
```{r}
## summary() shows quartiles for the two wages variables
## as well as minimum, maximum, and mean
minwage %>%
filter(state == "NJ") %>% # just look at NJ
select(wageBefore, wageAfter) %>%
summary()
## The interquartile range
minwage %>%
filter(state == "NJ") %>%
select(wageBefore, wageAfter) %>%
summarize(wageBeforeIQR = IQR(wageBefore),
wageAfterIQR = IQR(wageAfter))
```
```{r}
## Create an object for the quantiles we want (deciles)
decile_probs <- seq(from = 0, to = 1, by = 0.1)
## Save deciles as characters
decile_names <- as.character(decile_probs)
## Generate the deciles for wage before and after
minwage %>%
filter(state == "NJ") %>%
select(wageBefore, wageAfter) %>%
summarize(wageBeforeDecile = quantile(wageBefore, probs = decile_probs),
wageAfterDecile = quantile(wageAfter, probs = decile_probs),
decile = decile_names)
```
### Standard Deviation
```{r}
## Calculate the RMS of the change in full-time
## employment proportion in NJ
minwageNJ %>%
mutate(fullPropChange = fullPropAfter - fullPropBefore,
sqfullPropChange = fullPropChange^2) %>%
summarize(rms = sqrt(mean(sqfullPropChange)))
## Compare to the mean
minwageNJ %>%
mutate(fullPropChange = fullPropAfter - fullPropBefore) %>%
summarize(mean = mean(fullPropChange))
```
```{r, include=FALSE}
## Original, before corrections
minwage %>%
group_by(state) %>%
summarize_at(vars(wageAfter, wageBefore),
.funs = list(sd, var))
minwage %>%
group_by(state) %>%
summarize_at(vars(wageAfter, wageBefore),
.funs = list(stdv = sd,
variance = var))
```
```{r}
minwage %>%
group_by(state) %>%
summarize_at(vars(fullPropBefore, fullPropAfter),
.funs = list(sd, var))
minwage %>%
group_by(state) %>%
summarize_at(vars(fullPropBefore, fullPropAfter),
.funs = list(stdv = sd,
variance = var))
```