forked from scunning1975/mixtape_learnr
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDirected_Acyclical_Graphs.Rmd
121 lines (87 loc) · 2.81 KB
/
Directed_Acyclical_Graphs.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
---
title: "Causal Inference: <br> *The Mixtape*"
subtitle: "<it>Directed Acyclical Graphs</it>"
output:
learnr::tutorial:
css: css/style.css
highlight: "kate"
runtime: shiny_prerendered
---
## Welcome
This is material for the **Directed Acyclical Graphs** chapter in Scott Cunningham's book, [Causal Inference: The Mixtape.](https://mixtape.scunning.com/)
### Packages needed
The first thing you need to do is install a few packages to make sure everything runs:
```{r, eval = FALSE}
install.packages("tidyverse")
install.packages("cli")
install.packages("haven")
install.packages("rmarkdown")
install.packages("learnr")
install.packages("estimatr")
install.packages("haven")
```
### Load
```{r load, warning=FALSE, message=FALSE}
library(learnr)
library(haven)
library(tidyverse)
library(estimatr)
library(stargazer)
# 10 minute code time limit
options(tutorial.exercise.timelimit = 600)
# read_data function
read_data <- function(df) {
full_path <- paste0("https://raw.github.com/scunning1975/mixtape/master/", df)
return(haven::read_dta(full_path))
}
```
## Collider - Discrimination
```{r collider_discrimination, exercise=TRUE, echo=FALSE}
tb <- tibble(
female = ifelse(runif(10000)>=0.5,1,0),
ability = rnorm(10000),
discrimination = female,
occupation = 1 + 2*ability + 0*female - 2*discrimination + rnorm(10000),
wage = 1 - 1*discrimination + 1*occupation + 2*ability + rnorm(10000)
)
lm_1 <- lm(wage ~ female, tb)
lm_2 <- lm(wage ~ female + occupation, tb)
lm_3 <- lm(wage ~ female + occupation + ability, tb)
stargazer(lm_1,lm_2,lm_3,
type = "text",
column.labels = c("Biased Unconditional", "Biased", "Unbiased Conditional")
)
```
#### QUESTIONS
- What is the true direct effect of discrimination on wages?
- Explain the channels by which discrimination impacts wages.
- What makes occupation a collider?
- What controls are necessary to eliminate this collider bias?
## Movie Star
```{r movie_star, exercise=TRUE, echo=FALSE}
set.seed(3444)
star_is_born <- tibble(
beauty = rnorm(2500),
talent = rnorm(2500),
score = beauty + talent,
c85 = quantile(score, .85),
star = ifelse(score>=c85,1,0)
)
star_is_born %>%
lm(beauty ~ talent, .) %>%
ggplot(aes(x = talent, y = beauty)) +
geom_point(size = 0.5, shape=23) + xlim(-4, 4) + ylim(-4, 4)
star_is_born %>%
filter(star == 1) %>%
lm(beauty ~ talent, .) %>%
ggplot(aes(x = talent, y = beauty)) +
geom_point(size = 0.5, shape=23) + xlim(-4, 4) + ylim(-4, 4)
star_is_born %>%
filter(star == 0) %>%
lm(beauty ~ talent, .) %>%
ggplot(aes(x = talent, y = beauty)) +
geom_point(size = 0.5, shape=23) + xlim(-4, 4) + ylim(-4, 4)
```
#### QUESTIONS
- What is the correlation between talent and beauty among stars? Non-stars?
- But what is the correlation between talent and beauty in the population?