# create a function to estimate the treatment effectsfun_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 effectprint(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_hatsreps <-2000tau.hats <-rep(0, reps)for(i in1:reps){ tau.hats[i] <-fun_ATE(tau =10, N =2000)}# histogram of tau hatshist(tau.hats, breaks =30)
# obtaining the standard errorprint(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"