R code for sims