################################################################################
##       Difference-in-difference estimation (Under development)
##        by Dr. Jimmy (Zhenning) Xu, 
##        follow me on Twitter https://twitter.com/MKTJimmyxu
################################################################################
#install.packages("boot")
library(boot)
setwd("C:/Users/xzhenning/Documents/R") 
load("kielmc.RData")

run_DiD <- function(my_data, indices){
  d <- my_data[indices,]
  return(
    mean(d$rprice[d$year==1981 & d$nearinc==1]) - 
      mean(d$rprice[d$year==1981 & d$nearinc==0]) - 
      (mean(d$rprice[d$year==1978 & d$nearinc==1]) - 
         mean(d$rprice[d$year==1978 & d$nearinc==0]))
  )
}

boot_est <- boot(data, run_DiD, R=1000, parallel="multicore", ncpus = 2)
#Now you should just take a look at your estimates:
  
boot_est
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = data, statistic = run_DiD, R = 1000, parallel = "multicore", 
##     ncpus = 2)
## 
## 
## Bootstrap Statistics :
##     original    bias    std. error
## t1* -11863.9 -430.5201     9019.15
quantile(boot_est$t, c(0.025, 0.975))
##       2.5%      97.5% 
## -30278.030   6005.509
boot_est$t0/sd(boot_est$t)
## [1] -1.315413
plot(density(boot_est$t))