R code for chapter 2 of MSE book