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 it works...

Step 1 reads the data file.

In step 2, we define two functions to perform k-fold cross-validation. Rows 1-5 define the first function, and rows 6-13 define the second function.

The first function rdacb.kfold.crossval.reg sets up the k-folds and uses the second one to build the model and compute the errors for each fold.

Line 2 creates the folds by randomly sampling from 1 to k. Thus, if a data frame has 1,000 elements, this line will generate 1,000 random integers from 1 to k. The idea is that if the ith random number is, say, 3, then the ith case of the data frame belongs to the third fold.

Line 3 invokes the second function to compute the errors for each fold.

Line 4 creates a list with the raw values of the mean squared ...

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