Splitting the data as training and test set, using createDataPartition( ) function from the caret package.
KNN requires variables to be normalized or scaled. The caret package provides ways to preprocess data. Here we chose centering and scaling by settig preProcess = c("center", "scale")
Here we applied a 20-fold cross-validation as the trainControl, and trained the train data set with KNN methods. The output:
Based on the KNN output and the plot, we choose K =23, with the smallest RMSE = 0.4573 and largest R_square = 0.3596.