To build KNN models for regressions, perform the following steps:
- Load the dummies, FNN, scales, and caret packages as follows:
> library(dummies) > library(FNN) > library(scales) > library(caret)
- Read the data:
> educ <- read.csv("education.csv")
- Generate dummies for the categorical variable region, and add them to educ as follows:
> dums <- dummy(educ$region, sep="_") > educ <- cbind(educ, dums)
- Because KNN performs distance computations, we should either rescale or standardize the predictors. In the present example, we have three numeric predictors and a categorical predictor in the form of three dummy variables. Standardizing dummy variables is tricky, and hence, we will scale the numeric ones to [0, 1] and leave ...