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

Running KNN with cross-validation in place of a validation partition

We used three partitions in the preceding code. A different approach will be to use two partitions. In this case, knn.reg will use the leave-one-out cross-validation and predict for each case of the training partition itself. To use this mode, we pass only the training partition as argument and leave the other partition as NULL. After performing steps 1 through 4 from the main recipe, do the following:

> t.idx <- createDataPartition(educ$expense, p = 0.7, list = FALSE) > trg <- educ[t.idx,] > val <- educ[-t.idx,] > res1 <- knn.reg(trg[,7:12], test = NULL, y = trg[,6], k=2, algorithm="brute") > # When run in this mode, the result object contains > # the residuals which we ...

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