df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
iv.est
iv.est
p
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
iv.est
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
iv.est
p
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
frontdoor.est
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
iv.est
p
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
iv.est
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
frontdoor.est
iv.est
### b) front-door is true, iv is not
# i1 is violated (there is a backdoor path from Z to Y)
# so when beta = 0, *the identified iv functional is not zero*
p_values_null_fdoor_correct_i1_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i1_violated <- c(p_values_null_fdoor_correct_i1_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i1_violated <= 0.05)/length(p_values_null_fdoor_correct_i1_violated)
power <- typeI
size <- power
size
p_values_null_fdoor_correct_i1_violated
size
rm(list=ls())
library(mgcv)
sapply(list.files(pattern=".R", path="./R", full.names = TRUE), source)
# simulation times
N <- 500
# sample size
n <- 1000
############################## Front-door and IV ###############################
# simulate data where both frontdoor and IV are correct and they should agree
df <- dgp_frontdoor_iv_both_correct(n)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Marginal tests for backdoor and frontdoor
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
# Evidence factor
both.est <- c(frontdoor.est, iv.est)
both.eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(both.est, both.eif)
p
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
iv.est
p
df <- dgp_frontdoor_iv_fdoor_correct_i2_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
frontdoor.est
iv.est
df <- dgp_frontdoor_iv_fdoor_correct_i3_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
iv.est
p
frontdoor.est
df <- dgp_frontdoor_iv_fdoor_correct_i3_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
iv.est
p
df <- dgp_frontdoor_iv_fdoor_correct_i4_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
iv.est
frontdoor.est
rm(list=ls())
library(mgcv)
sapply(list.files(pattern=".R", path="./R", full.names = TRUE), source)
# simulation times
N <- 500
# sample size
n <- 1000
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
frontdoor.est
iv.est
print(pnorm(sqrt(n) * abs(frontdoor.est) / sd(frontdoor.eif), lower.tail = FALSE) * 2)
print(pnorm(sqrt(n) * abs(iv.est) / sd(iv.eif), lower.tail = FALSE) * 2)
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
iv.est
p
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
frontdoor.est
rm(list=ls())
library(mgcv)
sapply(list.files(pattern=".R", path="./R", full.names = TRUE), source)
# simulation times
N <- 500
# sample size
n <- 1000
### a) both models are correct
p_values_null_both_correct <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_both_correct(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_both_correct <- c(p_values_null_both_correct, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_both_correct <= 0.05)/length(p_values_null_both_correct)
power <- typeI
size <- power
# [1] 0
### b) front-door is true, iv is not
# i1 is violated (there is a backdoor path from Z to Y)
# so when beta = 0, *the identified iv functional is not zero*
p_values_null_fdoor_correct_i1_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i1_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i1_violated <- c(p_values_null_fdoor_correct_i1_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i1_violated <= 0.05)/length(p_values_null_fdoor_correct_i1_violated)
power <- typeI
size <- power
# [1] 0.032
# i2 is violated (there is a direct effect from Z to Y),
# so when beta = 0, *the identified iv functional is not zero*
p_values_null_fdoor_correct_i2_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i2_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i2_violated <- c(p_values_null_fdoor_correct_i2_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i2_violated <= 0.05)/length(p_values_null_fdoor_correct_i2_violated)
power <- typeI
size <- power
# [1] 0.044
# i3 is violated
# there is no backdoor path/direct effect from Z to Y, and the front door
# is true (M fully mediate and beta = 0),  *so the identified iv functional is zero*
p_values_null_fdoor_correct_i3_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i3_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i3_violated <- c(p_values_null_fdoor_correct_i3_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i3_violated <= 0.05)/length(p_values_null_fdoor_correct_i3_violated)
power <- typeI
size <- power
# [1] 0
# i4 is violated
# no association between Z and A, and no direct effect from Z to Y,
# *so the identified iv functional is zero*
p_values_null_fdoor_correct_i4_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i4_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i4_violated <- c(p_values_null_fdoor_correct_i4_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i4_violated <= 0.05)/length(p_values_null_fdoor_correct_i4_violated)
power <- typeI
size <- power
typeI <- sum(p_values_null_both_correct <= 0.05)/length(p_values_null_both_correct)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i1_violated <= 0.05)/length(p_values_null_fdoor_correct_i1_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i2_violated <= 0.05)/length(p_values_null_fdoor_correct_i2_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i3_violated <= 0.05)/length(p_values_null_fdoor_correct_i3_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i4_violated <= 0.05)/length(p_values_null_fdoor_correct_i4_violated)
power <- typeI
size <- power
size
size
size
typeI <- sum(p_values_null_fdoor_correct_i3_violated <= 0.05)/length(p_values_null_fdoor_correct_i3_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i2_violated <= 0.05)/length(p_values_null_fdoor_correct_i2_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i1_violated <= 0.05)/length(p_values_null_fdoor_correct_i1_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_both_correct <= 0.05)/length(p_values_null_both_correct)
power <- typeI
size <- power
size
dgp_frontdoor_iv_both_correct
dgp_frontdoor_iv_fdoor_correct_i1_violated
dgp_frontdoor_iv_fdoor_correct_i2_violated
dgp_frontdoor_iv_fdoor_correct_i1_violated
dgp_frontdoor_iv_both_correct
dgp_frontdoor_iv_fdoor_correct_i2_violated
dgp_frontdoor_iv_fdoor_correct_i3_violated
dgp_frontdoor_iv_fdoor_correct_i4_violated
rm(list=ls())
library(mgcv)
sapply(list.files(pattern=".R", path="./R", full.names = TRUE), source)
# simulation times
N <- 500
# sample size
n <- 1000
############################## Front-door and IV ###############################
# simulate data where both frontdoor and IV are correct and they should agree
df <- dgp_frontdoor_iv_both_correct(n)
dgp_frontdoor_iv_fdoor_correct_i3_violated
p_values_null_fdoor_correct_i3_violated <- c()
for (i in 1:N) {
df <- dgp_frontdoor_iv_fdoor_correct_i3_violated(n = n, beta = 0)
data <- df$df
# estimate using APIPW (front door IF)
frontdoor <- estimate_frontdoor(data)
frontdoor.est <- frontdoor$frontdoor.est
frontdoor.eif <- frontdoor$frontdoor.eif
# estimate using UIV (IV IF)
iv <- estimate_uiv(data)
iv.est <- iv$iv.est
iv.eif <- iv$iv.eif
# Evidence factor
est <- c(frontdoor.est, iv.est)
eif <- cbind(frontdoor.eif, iv.eif)
p <- evidence_factor(est = est, eif = eif)
p_values_null_fdoor_correct_i3_violated <- c(p_values_null_fdoor_correct_i3_violated, p)
print(paste0(i,"th simulation done"))
}
typeI <- sum(p_values_null_fdoor_correct_i3_violated <= 0.05)/length(p_values_null_fdoor_correct_i3_violated)
power <- typeI
size <- power
size
typeI <- sum(p_values_null_fdoor_correct_i3_violated <= 0.05)/length(p_values_null_fdoor_correct_i3_violated)
power <- typeI
size <- power
size
