3.5 An estimation example

# create a function to estimate the treatment effects
fun_ATE  <- function(tau, N){
# @arg tau: true treatment effect
# @arg N:   sample size
# Return tau_hat: estimate of the treatment effect      
    gender  <- rbinom(N, size = 1, p = 0.5)
    race  <- rbinom(N, size = 1, p = 0.5)
    W  <- rbinom(N, size = 1, p = 0.5)    
    Y  <- 50 + tau * W  + gender * 5 + race * 10 + rnorm(n = N, mean = 5, sd = 5)

    tau_hat  <-  mean(Y[which(W == 1)]) - mean(Y[which(W == 0)]) 
    return(tau_hat)
}

# print treatment effect
print(paste("the ATE estimate is: ", fun_ATE(tau = 10, N = 2000)))
## [1] "the ATE estimate is:  10.1629106708608"
# run 2000 replications to get a distribution of tau_hats
reps  <-  2000
tau.hats  <- rep(0, reps)

for(i in 1:reps){
    tau.hats[i]  <- fun_ATE(tau = 10, N = 2000)
}

# histogram of tau hats
hist(tau.hats, breaks = 30)

# obtaining the standard error
print(paste("the mean of tau hats : ", mean(tau.hats)))
## [1] "the mean of tau hats :  10.008690687733"
print(paste("the standard error of tau hats : ", sd(tau.hats)))
## [1] "the standard error of tau hats :  0.332672608193882"