O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

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

  1. Read the data:
> bh <- read.csv("BostonHousing.csv") 
  1. 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" = mean(mean.sqr.errs), 
              "std_dev_mean_sqr_err" = sd(mean.sqr.errs)) 
4 } 
 
5 rdacb.loocv.reg.iter <- function(k, df) { 
6   mod <- lm(MEDV ~ ., data = df[-k, ] ) 
7   pred <- predict(mod, df[k,]) 
8   sqr.err <- (pred - df[k, "MEDV"])^2 
9 } 
  1. With the preceding two functions in place, you can run LOOCV as follows (this runs 506 linear regression ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required