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

Steps 1 to 3 load the necessary packages and read the data file.

Step 4 creates three partitions (50 %, 25 %, and 25 %). We set the random seed to enable you to match your results with those that we display. Refer to the Creating random data partitions recipe in Chapter 2, What's in There? - Exploratory Data Analysis, for information on data partitioning.

Step 5 uses the knn function to generate predictions with k=1. It uses only the standardized values of the predictor variables and hence specifies train[,4:5] and val[,4:5].

Step 6 generates the error matrix for k=1.

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