library(causaloptim)
#> Loading required package: igraph
#> Warning: package 'igraph' was built under R version 4.1.1
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
<- graph_from_literal(X -+ Y, Ur -+ X, Ur -+ Y)
b 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)
<- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
obj optimize_effect_2(obj)
#> lower bound =
#> MAX {
#> -1 + p00_ + p11_
#> }
#> ----------------------------------------
#> upper bound =
#> MIN {
#> 1 - p10_ - p01_
#> }
<- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 2) = 1} - p{Y(X = 0) = 1}")
obj2 optimize_effect_2(obj2)
#> lower bound =
#> MAX {
#> -p10_ - p20_ - p01_ - p11_
#> }
#> ----------------------------------------
#> upper bound =
#> MIN {
#> 1 - p20_ - p01_
#> }
<- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 2) = 1} - p{Y(X = 1) = 1}")
obj3 optimize_effect_2(obj3)
#> lower bound =
#> MAX {
#> -p00_ - p20_ - p01_ - p11_
#> }
#> ----------------------------------------
#> upper bound =
#> MIN {
#> 1 - p20_ - p11_
#> }
Not run, this takes a few minutes to compute.
<- graph_from_literal(Z1 -+ X, Z2 -+ X, Z2 -+ Z1, Ul -+ Z1, Ul -+ Z2,
b -+ Y, Ur -+ X, Ur -+ Y)
X 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)
<- analyze_graph(b, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
obj
<- optimize_effect_2(obj)
bounds.multi
<- graph_from_literal(Z1 -+ X, Ul -+ Z1,
b2 -+ Y, Ur -+ X, Ur -+ Y)
X 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
<- analyze_graph(b2, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
obj2 <- optimize_effect_2(obj2)
bounds.sing
<- graph_from_literal(Z3 -+ X, Ul -+ Z3,
b3 -+ Y, Ur -+ X, Ur -+ Y)
X 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
<- analyze_graph(b3, constraints = NULL, effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
obj3 <- optimize_effect_2(obj3)
bounds.quad
<- function(df, alpha, pUr, pUl) {
joint
<- df$Z1
Z1 <- df$Z2
Z2 <- df$X
X <- df$Y
Y
* pUl * (((pnorm(alpha[1] + alpha[2] * 1)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 1)) ^ (1 - Z1)) *
pUr 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)) +
(* (1 - pUl) * (((pnorm(alpha[1] + alpha[2] * 0)) ^ Z1 * (1 - pnorm(alpha[1] + alpha[2] * 0)) ^ (1 - Z1)) *
pUr 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
<- function(p.vals) {
get_cond_probs
<- unique(p.vals[, c("Z1", "Z2")])
z1z2.joint for(j in 1:nrow(z1z2.joint)) {
$Prob.condz1z2[j] <- sum(subset(p.vals, Z1 == z1z2.joint[j, "Z1"] & Z2 == z1z2.joint[j, "Z2"])$Prob)
z1z2.joint
}
.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)
p.vals<- as.list(p.vals.2$Prob.cond.fin)
res names(res) <- with(p.vals.2, paste0("p", X, Y, "_", Z1, Z2))
## conditional on Z1 only
<- unique(p.vals[, c("Z1", "X", "Y")])
xyz1.joint for(j in 1:nrow(xyz1.joint)) {
$Prob.xyz1[j] <- sum(subset(p.vals, Z1 == xyz1.joint$Z1[j] &
xyz1.joint== xyz1.joint$X[j] & Y == xyz1.joint$Y[j])$Prob)
X
}
<- sum(subset(xyz1.joint, Z1 == 0)$Prob.xyz1)
z1.marg0 <- sum(subset(xyz1.joint, Z1 == 1)$Prob.xyz1)
z1.marg1
$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)
xyz1.joint<- as.list(xyz1.joint$Prob.cond)
res2 names(res2) <- with(xyz1.joint, paste0("p", X, Y, "_", Z1))
## conditioning on Z3
<- unique(p.vals[, c("Z3"), drop = FALSE])
z3.joint for(j in 1:nrow(z3.joint)) {
$Prob.condz3[j] <- sum(subset(p.vals, Z3 == z3.joint[j, "Z3"])$Prob)
z3.joint
}
.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)
p.vals<- as.list(p.vals.3$Prob.cond.fin)
res3 names(res3) <- with(p.vals.3, paste0("p", X, Y, "_", Z3))
list(multi = res,
sing = res2,
quad = res3)
}
## simulate and compare the two
<- 50000
nsim <- interpret_bounds(bounds.multi$bounds, obj$parameters)
f.multi <- interpret_bounds(bounds.sing$bounds, obj2$parameters)
f.single <- interpret_bounds(bounds.quad$bounds, obj3$parameters)
f.quad
<- matrix(NA, ncol = 9, nrow = nsim)
result
set.seed(211129)
for (i in 1:nsim) {
<- rnorm(12, sd = 2)
alpha <- runif(1)
pUr <- runif(1)
pUl
<- 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,
p.vals.jointifelse(Z1 == 0 & Z2 == 1, 1,
ifelse(Z1 == 1 & Z2 == 0, 2,
3))))
if(any(p.vals.joint$Prob == 0)) next
<- get_cond_probs(p.vals.joint)
condprobs
<- do.call(f.multi, condprobs$multi)
bees <- do.call(f.single, condprobs$sing)
bees.sing <- do.call(f.quad, condprobs$quad)
bees.quad
<- unlist(c(sort(unlist(bees)), abs(bees[2] - bees[1]),
result[i, ] 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")
<- as.data.frame(result)
bounds.comparison
#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)
<- graph_from_literal(Ul -+ X -+ Y -+ Y2, Ur -+ Y, Ur -+ Y2)
b 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)
<- analyze_graph(b, constraints = "Y2(Y = 1) >= Y2(Y = 0)",
obj effectt = "p{Y(X = 1) = 1} - p{Y(X = 0) = 1}")
optimize_effect_2(obj)
#> lower bound =
#> MAX {
#> -1,
#> -1 + 2p0_0 - 2p0_1
#> }
#> ----------------------------------------
#> upper bound =
#> MIN {
#> 1,
#> 1 + 2p0_0 - 2p0_1
#> }