R code for estimation & simulation