# NOT RUN {
if(require("ranger") && require("ggplot2")) {
## generate data
set.seed(1)
n <- 150 # number of observations
p <- 5 # number of covariates
D <- rbinom(n, 1, 0.5) # random treatment assignment
Z <- matrix(runif(n*p), n, p) # design matrix
Y0 <- as.numeric(Z %*% rexp(p) + rnorm(n)) # potential outcome without treatment
Y1 <- 2 + Y0 # potential outcome under treatment
Y <- ifelse(D == 1, Y1, Y0) # observed outcome
## name the columns of Z
colnames(Z) <- paste0("V", 1:p)
## specify learners
learners <- c("random_forest")
## perform generic ML inference
# small number of splits to keep computation time low
x <- GenericML(Z, D, Y, learners,
num_splits = 2,
parallel = FALSE)
## the plot we wish to replicate
plot(x = x, type = "GATES")
## get the data to plot the GATES estimates
data <- setup_plot(x = x, type = "GATES")
## define variables to appease the R CMD check
group <- estimate <- ci_lower <- ci_upper <- NULL
## replicate the plot(x, type = "GATES")
# for simplicity, we skip aligning the colors
ggplot(mapping = aes(x = group,
y = estimate), data = data$data_plot) +
geom_hline(aes(yintercept = 0),
color = "black", linetype = "dotted") +
geom_hline(aes(yintercept = data$data_BLP["beta.1", "estimate"],
color = "ATE"),
linetype = "dashed") +
geom_hline(aes(yintercept = data$data_BLP["beta.1", "ci_lower"],
color = paste0(100*data$confidence_level, "% CI (ATE)")),
linetype = "dashed") +
geom_hline(yintercept = data$data_BLP["beta.1", "ci_upper"],
linetype = "dashed", color = "red") +
geom_point(aes(color = paste0("GATES with ", 100*data$confidence_level, "% CI")), size = 3) +
geom_errorbar(mapping = aes(ymin = ci_lower,
ymax = ci_upper))
}
# }
Run the code above in your browser using DataLab