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 loads the required packages, and step 2 reads the data.

Since KNN requires all the predictors to be numeric, step 3 uses the dummy function from the dummies package to generate dummies for the categorical variable region, and then adds the resulting dummy variables to the educ data frame.

Step 4 scales the numeric predictor variables to the [01] range using the rescale function from the scales package. Standardizing the numerical predictors will be another option, but standardizing dummy variables will be tricky. Some analysts standardize numerical predictors and leave the dummy variables as they are. However, for consistency, we choose to have all of our predictors in the [01] range. Since the dummies are already ...

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