# \donttest{
n <- 1000
p <- 5
X <- matrix(rnorm(n * p), n, p) |>
as.data.frame() |>
dplyr::bind_cols(
DiscreteCovariatesToOneHot(
dplyr::tibble(
D1 = factor(
sample(1:3, n, replace = TRUE, prob = c(0.2, 0.3, 0.5)),
labels = c("first", "second", "third")
),
D2 = factor(
sample(1:2, n, replace = TRUE, prob = c(0.2, 0.8)),
labels = c("a", "b")
)
)
)
) |>
dplyr::select(
V1,
V2,
dplyr::starts_with("D1"),
V3,
V4,
dplyr::starts_with("D2"),
V5
)
expo_prob <- 1 / (1 + exp(0.4 * X[, 1] + 0.2 * X[, 2] - 0.6 * X[, 3] +
0.4 * X[, 6] + 0.6 * X[, 8] - 0.2 * X[, 9]))
W <- rbinom(n, 1, expo_prob)
event_prob <- 1 / (1 + exp(2 * (pmax(2 * X[, 1], 0) * W - X[, 2] +
X[, 6] + 3 * X[, 9])))
Y <- rbinom(n, 1, event_prob)
cf <- grf::causal_forest(X, Y, W)
cb1 <- CovariateBalance(cf)
cb2 <- CovariateBalance(
cf,
covariates = character(0),
names = c(
"medium imbalance" = "V1",
"low imbalance" = "V2",
"high imbalance" = "V3",
"no imbalance" = "V4",
"discrete 1" = "D1",
"discrete 2" = "D2"
)
)
cb3 <- CovariateBalance(
cf,
covariates = character(0),
names = c(
"medium imbalance" = "V1",
"low imbalance" = "V2",
"high imbalance" = "V3",
"no imbalance" = "V4"
),
treatment_name = "Treatment",
love_breaks = seq(0, 0.5, 0.1),
love_xlim = c(0, 0.5),
cd_nrow = 2,
cd_x_scale_width = 1,
cd_bar_width = 0.3
)
# }
Run the code above in your browser using DataLab