/
example-code.Rmd
271 lines (197 loc) · 10.1 KB
/
example-code.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
---
title: "Code from examples in manuscript"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Code from examples in manuscript}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(causaloptim)
```
## confounded exposure and outcome
```{r}
b <- graph_from_literal(X -+ Y, Ur -+ X, Ur -+ Y)
V(b)$leftside <- c(0,0,0)
V(b)$latent <- c(0,0,1)
V(b)$nvals <- c(3,2,2)
E(b)$rlconnect <- E(b)$edge.monotone <- c(0, 0, 0)
obj <- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
optimize_effect_2(obj)
obj2 <- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 2) = 1} - p{Y(X = 0) = 1}")
optimize_effect_2(obj2)
obj3 <- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 2) = 1} - p{Y(X = 1) = 1}")
optimize_effect_2(obj3)
```
## multiple instruments
Not run, this takes a few minutes to compute.
```{r, eval = FALSE}
b <- graph_from_literal(Z1 -+ X, Z2 -+ X, Z2 -+ Z1, Ul -+ Z1, Ul -+ Z2,
X -+ Y, Ur -+ X, Ur -+ Y)
V(b)$leftside <- c(1, 0, 1, 1, 0, 0)
V(b)$latent <- c(0, 0, 0, 1, 0, 1)
V(b)$nvals <- c(2, 2, 2, 2, 2, 2)
E(b)$rlconnect <- c(0, 0, 0, 0, 0, 0, 0, 0)
E(b)$edge.monotone <- c(0, 0, 0, 0, 0, 0, 0, 0)
obj <- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
bounds.multi <- optimize_effect_2(obj)
b2 <- graph_from_literal(Z1 -+ X, Ul -+ Z1,
X -+ Y, Ur -+ X, Ur -+ Y)
V(b2)$leftside <- c(1, 0, 1, 0, 0)
V(b2)$latent <- c(0, 0, 1, 0, 1)
V(b2)$nvals <- c(2, 2, 2, 2, 2)
E(b2)$rlconnect <- c(0, 0, 0, 0, 0)
E(b2)$edge.monotone <- c(0, 0, 0, 0, 0)
## single instrument
obj2 <- analyze_graph(b2, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
bounds.sing <- optimize_effect_2(obj2)
b3 <- graph_from_literal(Z3 -+ X, Ul -+ Z3,
X -+ Y, Ur -+ X, Ur -+ Y)
V(b3)$leftside <- c(1, 0, 1, 0, 0)
V(b3)$latent <- c(0, 0, 1, 0, 1)
V(b3)$nvals <- c(4, 2, 2, 2, 2)
E(b3)$rlconnect <- c(0, 0, 0, 0, 0)
E(b3)$edge.monotone <- c(0, 0, 0, 0, 0)
## single instrument
obj3 <- analyze_graph(b3, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
bounds.quad <- optimize_effect_2(obj3)
joint <- function(df, alpha, pUr, pUl) {
Z1 <- df$Z1
Z2 <- df$Z2
X <- df$X
Y <- df$Y
pUr * pUl * (((pnorm(alpha[1] + alpha[2] * 1)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 1)) ^ (1 - Z1)) *
((pnorm(alpha[3] + alpha[4] * 1 + alpha[5] * Z1)) ^ Z2 *
(1 - pnorm(alpha[3] + alpha[4] * 1 + alpha[5] * Z1)) ^ (1 - Z2)) *
((pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 1)) ^ X *
(1 - pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 1)) ^ (1 - X)) *
(pnorm(alpha[10] + alpha[11] * X + alpha[12] * 1)) ^ Y *
(1 - pnorm(alpha[10] + alpha[11] * X + alpha[12] * 1)) ^ (1 - Y)) +
pUr * (1 - pUl) * (((pnorm(alpha[1] + alpha[2] * 0)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 0)) ^ (1 - Z1)) *
((pnorm(alpha[3] + alpha[4] * 0 + alpha[5] * Z1)) ^ Z2 *
(1 - pnorm(alpha[3] + alpha[4] * 0 + alpha[5] * Z1)) ^ (1 - Z2)) *
((pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 1)) ^ X *
(1 - pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 1)) ^ (1 - X)) *
(pnorm(alpha[10] + alpha[11] * X + alpha[12] * 1)) ^ Y *
(1 - pnorm(alpha[10] + alpha[11] * X + alpha[12] * 1)) ^ (1 - Y)) +
(1 - pUr) * pUl * (((pnorm(alpha[1] + alpha[2] * 1)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 1)) ^ (1 - Z1)) *
((pnorm(alpha[3] + alpha[4] * 1 + alpha[5] * Z1)) ^ Z2 *
(1 - pnorm(alpha[3] + alpha[4] * 1 + alpha[5] * Z1)) ^ (1 - Z2)) *
((pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 0)) ^ X *
(1 - pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 0)) ^ (1 - X)) *
(pnorm(alpha[10] + alpha[11] * X + alpha[12] * 0)) ^ Y *
(1 - pnorm(alpha[10] + alpha[11] * X + alpha[12] * 0)) ^ (1 - Y)) +
(1 - pUr) * (1 - pUl) * (((pnorm(alpha[1] + alpha[2] * 0)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 0)) ^ (1 - Z1)) *
((pnorm(alpha[3] + alpha[4] * 0 + alpha[5] * Z1)) ^ Z2 *
(1 - pnorm(alpha[3] + alpha[4] * 0 + alpha[5] * Z1)) ^ (1 - Z2)) *
((pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 0)) ^ X *
(1 - pnorm(alpha[6] + alpha[7] * Z1 + alpha[8] * Z2 + alpha[9] * 0)) ^ (1 - X)) *
(pnorm(alpha[10] + alpha[11] * X + alpha[12] * 0)) ^ Y *
(1 - pnorm(alpha[10] + alpha[11] * X + alpha[12] * 0)) ^ (1 - Y))
}
## get conditional probabilities
## key = XY_Z1Z2
get_cond_probs <- function(p.vals) {
z1z2.joint <- unique(p.vals[, c("Z1", "Z2")])
for(j in 1:nrow(z1z2.joint)) {
z1z2.joint$Prob.condz1z2[j] <- sum(subset(p.vals, Z1 == z1z2.joint[j, "Z1"] & Z2 == z1z2.joint[j, "Z2"])$Prob)
}
p.vals.2 <- merge(p.vals, z1z2.joint, by = c("Z1", "Z2"), sort = FALSE)
p.vals.2$Prob.cond.fin <- ifelse(p.vals.2$Prob ==0, 0.0, p.vals.2$Prob / p.vals.2$Prob.condz1z2)
res <- as.list(p.vals.2$Prob.cond.fin)
names(res) <- with(p.vals.2, paste0("p", X, Y, "_", Z1, Z2))
## conditional on Z1 only
xyz1.joint <- unique(p.vals[, c("Z1", "X", "Y")])
for(j in 1:nrow(xyz1.joint)) {
xyz1.joint$Prob.xyz1[j] <- sum(subset(p.vals, Z1 == xyz1.joint$Z1[j] &
X == xyz1.joint$X[j] & Y == xyz1.joint$Y[j])$Prob)
}
z1.marg0 <- sum(subset(xyz1.joint, Z1 == 0)$Prob.xyz1)
z1.marg1 <- sum(subset(xyz1.joint, Z1 == 1)$Prob.xyz1)
xyz1.joint$Prob.z1[xyz1.joint$Z1 == 0] <- z1.marg0
xyz1.joint$Prob.z1[xyz1.joint$Z1 == 1] <- z1.marg1
xyz1.joint$Prob.cond <- with(xyz1.joint, Prob.xyz1 / Prob.z1)
res2 <- as.list(xyz1.joint$Prob.cond)
names(res2) <- with(xyz1.joint, paste0("p", X, Y, "_", Z1))
## conditioning on Z3
z3.joint <- unique(p.vals[, c("Z3"), drop = FALSE])
for(j in 1:nrow(z3.joint)) {
z3.joint$Prob.condz3[j] <- sum(subset(p.vals, Z3 == z3.joint[j, "Z3"])$Prob)
}
p.vals.3 <- merge(p.vals, z3.joint, by = c("Z3"), sort = FALSE)
p.vals.3$Prob.cond.fin <- ifelse(p.vals.3$Prob ==0, 0.0, p.vals.3$Prob / p.vals.3$Prob.condz3)
res3 <- as.list(p.vals.3$Prob.cond.fin)
names(res3) <- with(p.vals.3, paste0("p", X, Y, "_", Z3))
list(multi = res,
sing = res2,
quad = res3)
}
## simulate and compare the two
nsim <- 50000
f.multi <- interpret_bounds(bounds.multi$bounds, obj$parameters)
f.single <- interpret_bounds(bounds.sing$bounds, obj2$parameters)
f.quad <- interpret_bounds(bounds.quad$bounds, obj3$parameters)
result <- matrix(NA, ncol = 9, nrow = nsim)
set.seed(211129)
for (i in 1:nsim) {
alpha <- rnorm(12, sd = 2)
pUr <- runif(1)
pUl <- runif(1)
p.vals.joint <- obj$p.vals
p.vals.joint$Prob <- joint(p.vals.joint, alpha, pUr, pUl)
p.vals.joint$Z3 <- with(p.vals.joint, ifelse(Z1 == 0 & Z2 == 0, 0,
ifelse(Z1 == 0 & Z2 == 1, 1,
ifelse(Z1 == 1 & Z2 == 0, 2,
3))))
if(any(p.vals.joint$Prob == 0)) next
condprobs <- get_cond_probs(p.vals.joint)
bees <- do.call(f.multi, condprobs$multi)
bees.sing <- do.call(f.single, condprobs$sing)
bees.quad <- do.call(f.quad, condprobs$quad)
result[i, ] <- unlist(c(sort(unlist(bees)), abs(bees[2] - bees[1]),
sort(unlist(bees.sing)), abs(bees.sing[2]- bees.sing[1]),
sort(unlist(bees.quad)), abs(bees.quad[2]- bees.quad[1])))
}
colnames(result) <- c("bound.lower",
"bound.upper", "width.multi",
"bound.lower.single", "bound.upper.single", "width.single",
"bound.lower.quad", "bound.upper.quad", "width.quad")
bounds.comparison <- as.data.frame(result)
#pdf("figsim.pdf", width = 8, height = 4.25, family = "serif")
par(mfrow = c(1,2))
plot(width.multi ~ width.single, data = bounds.comparison, pch = 20, cex = .3,
xlim = c(0, 1), ylim = c(0, 1), xlab= "Single IV", ylab = "Two binary IV/Single 4-level IV",
main = "Width of bounds intervals")
abline(0, 1, lty = 3)
plot(bound.lower.quad ~ bound.lower, data = bounds.comparison[1:100,], pch = 20, cex = 1,
xlim = c(-1, 1), ylim = c(-1, 1), xlab = "Two binary IV", ylab = "Single 4-level IV",
main = "Bounds values")
points(bound.upper.quad ~ bound.upper, data = bounds.comparison[1:100,], pch = 1, cex = 1)
legend("bottomright", pch = c(1, 20), legend = c("upper", "lower"))
#dev.off()
summary(bounds.comparison) # contains 467 NA's to avoid division by 0
# Verify that a single quad-level instrument yield the same bounds as two linked binary ones.
all(round(x = bounds.comparison$bound.lower, digits = 12) ==
round(x = bounds.comparison$bound.lower.quad, digits = 12) &&
round(x = bounds.comparison$bound.upper, digits = 12) ==
round(x = bounds.comparison$bound.upper.quad, digits = 12),
na.rm = TRUE)
```
## measurement error in the outcome
```{r}
b <- graph_from_literal(Ul -+ X -+ Y -+ Y2, Ur -+ Y, Ur -+ Y2)
V(b)$leftside <- c(1, 1, 0, 0, 0)
V(b)$latent <- c(1, 0, 1, 0, 1)
V(b)$nvals <- c(2, 2, 2, 2, 2)
E(b)$rlconnect <- c(0, 0, 0, 0, 0)
E(b)$edge.monotone <- c(0, 0, 0, 0, 0)
obj <- analyze_graph(b, constraints = "Y2(Y = 1) >= Y2(Y = 0)",
effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
optimize_effect_2(obj)
```