forked from tetianagorbach/po_vs_graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample4_m_bias.R
69 lines (52 loc) · 1.87 KB
/
Example4_m_bias.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
# Author: Juha Karvanen/ code Tetiana Gorbach
# Date: 2024-05-27
# This code presents an example of M-bias.
# Load required library
library(mvtnorm) # to simulate multivariate normal
library(ppcor) # to calculate partial correlations
library(bnlearn)
# Generate data -----------------------------------------------------------
# Set a seed for reproducibility
set.seed(13959077)
# Set the sample size
n <- 1000000
u1 <- rbinom(n, size = 1, prob = 0.6)
u2 <- rbinom(n, size = 1, prob = 0.4)
c <- (u1+u2) %% 2
a <- rbinom(n, size = 1, prob = 0.1 + 0.8*u1)
y1 <- numeric(n)
y0 <- numeric(n)
y1[u2 == 1] <- rbinom(sum(u2 == 1),
size = 1,
prob = 0.9)
y1[u2 == 0] <- rbinom(sum(u2 == 0),
size = 1,
prob = 0.1)
y0[u2 == 1] <- rbinom(sum( u2 == 1), size = 1,
prob = 0.1)
y0[u2 == 0] <- rbinom(sum(u2 == 0), size = 1,
prob = 0.9)
mean(y1) - mean(y0)
# Generate observed outcome
y <- ifelse(a == 1, y1, y0)
ci.test(as.numeric(y), as.numeric(a))
ci.test(y0, as.numeric(a), as.numeric(c)) # unconfoundness is not fulfilled
ci.test(y1, as.numeric(a), as.numeric(c))
ci.test(as.numeric(u1), as.numeric(c), as.numeric(u2)) # c depends on u1 and u2
ci.test(as.numeric(u2), as.numeric(c), as.numeric(u1))
ci.test(as.numeric(y1), as.numeric(u2)) # y(1) and y(0) depend on u2
ci.test(as.numeric(y0), as.numeric(u2))
# EY(1)
mean(y1)
# EY(1) using the adjustment for C
mean(y[a==1 & c==1])* mean(c) + mean(y[a==1 & c==0])*(1-mean(c))
# EY(0)
mean(y0)
# EY(0) using the adjustment for C
mean(y[a==0 & c==1])* mean(c) + mean(y[a==0 & c==0])*(1-mean(c))
# ATE
mean(y1) - mean(y0)
# ATE estimated through the adjustment
lm(y ~ a + c)
mean(y[a==1 & c==1])* mean(c) + mean(y[a==1 & c==0])*(1-mean(c)) -
mean(y[a==0 & c==1])* mean(c) - mean(y[a==0 & c==0])*(1-mean(c))