R Simulation Code