Performing leave-one-out-cross-validation to limit overfitting

We provide the framework of the code to perform leave-one-out-cross-validation for linear regression. You should be able to easily adapt this code for any other regression technique. The rationale and explanation presented under the previous recipe Performing k-fold cross-validation apply to this one as well.

How to do it...

To perform leave-one-out-cross-validation (LOOCV) to limit overfitting, follow the steps below:

  1. Read the data:
    > bh <- read.csv("BostonHousing.csv")
  2. Create the two functions shown as follows; we show line numbers for discussion:
    1 rdacb.loocv.reg <- function(df) { 2 mean.sqr.errs <- sapply(1:nrow(df), rdacb.loocv.reg.iter, df) 3 list("mean_sqr_errs"= mean.sqr.errs, "overall_mean_sqr_err" ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.