forked from luebby/PsyPag-CausalSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CausalSim-Code.R
79 lines (62 loc) · 1.95 KB
/
CausalSim-Code.R
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
### Preliminaries
# install.packages("mosaic")
library(mosaic)
### First Simulation
set.seed(1896) # Reproducibility
n <- 1000 # Sample Size
learning <- rnorm(n) # X
knowing <- 5 * learning + rnorm(n) # Z
understanding <- 3 * knowing + rnorm(n) # Y
Sim_chain <- data.frame(learning, knowing, understanding)
res1_S1 <- lm(understanding ~
learning,
data = Sim_chain)
coef(res1_S1)
res2_S1 <- lm(understanding ~
learning + knowing,
data = Sim_chain)
coef(res2_S1)
### Second Simulation
set.seed(1896) # Reproducibility
n <- 1000 # Sample Size
intelligence <- rnorm(n, mean = 100, sd = 15) # Z
learning.time <- 200 - intelligence + rnorm(n) # X
### Excercise
# test.score <-
# Putting it all together
Sim_fork <- data.frame(learning.time, intelligence, test.score)
res1_S2 <- lm(test.score ~
learning.time,
data = Sim_fork)
coef(res1_S2)
res2_S2 <- lm(test.score ~
learning.time + intelligence,
data = Sim_fork)
coef(res2_S2)
### Third Simulation
set.seed(1896) # Reproducibility
n <- 1000 # Sample Size
kind <- rnorm(n) # X
look <- rnorm(n) # Y
dating <- ((kind > 1) | (look > 1)) # ~Z
luck <- rbinom(n, size = 1, prob = 0.05) # U_Z
dating <- (1 - luck) * dating + luck * (1 - dating) # Z
Sim_collider <- data.frame(look,
dating,
kind) %>%
mutate(dating = ifelse(dating,
"date",
"no date"))
res1_S3 <- lm(kind ~
look,
data = Sim_collider)
coef(res1_S3)
res2_S3 <- lm(kind ~
look + dating,
data = Sim_collider)
coef(res2_S3)
gf_point(kind ~ look,
color = ~ dating,
data = Sim_collider) %>%
gf_lm()
### Excercise: Simulation Gender Pay Gap