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 to any other regression technique. The rationale and explanation presented under the previous recipe Performing k-fold cross-validation apply to this one as well.
Performing leave-one-out cross-validation to limit overfitting
How to do it...
To perform leave-one-out cross-validation (LOOCV) to limit overfitting, perform the following steps:
- Read the data:
> bh <- read.csv("BostonHousing.csv")
- 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), ...