if (FALSE) {
# data generating process
p = 3 #two control variables and one moderator
n = 250
x = matrix(rnorm(n*p), nrow=n)
# create targeted selection
q = -1*(x[,1]>(x[,2])) + 1*(x[,1]<(x[,2]))
# generate treatment variable
pi = pnorm(q)
z = rbinom(n,1,pi)
# tau is the true (homogeneous) treatment effect
tau = (0.5*(x[,3] > -3/4) + 0.25*(x[,3] > 0) + 0.25*(x[,3]>3/4))
# generate the response using q, tau and z
mu = (q + tau*z)
# set the noise level relative to the expected mean function of Y
sigma = diff(range(q + tau*pi))/8
# draw the response variable with additive error
y = mu + sigma*rnorm(n)
# If you didn't know pi, you would estimate it here
pihat = pnorm(q)
n_burn = 5000
n_sim = 5000
bcf_fit = bcf(y = y,
z = z,
x_control = x,
x_moderate = x,
pihat = pihat,
nburn = n_burn,
nsim = n_sim,
n_chains = 2,
update_interval = 100,
save_tree_directory = './trees')
# Predict using new data
x_pred = matrix(rnorm(n*p), nrow=n)
pred_out = predict(bcf_out=bcf_fit,
x_predict_control=x_pred,
x_predict_moderate=x_pred,
pi_pred=pihat,
z_pred=z,
save_tree_directory = './trees')
}
Run the code above in your browser using DataLab