Building KNN models for regression
The FNN
package provides the necessary functions to apply the KNN technique for regression. In this recipe, we look at the use of the knn.reg
function to build the model and then the process of predicting with the model as well. We also show some additional convenience mechanisms to make the process easier.
Getting ready
Install the FNN
, dummies
, caret
, and scales
packages if you do not already have them installed. If you have not already downloaded the data files for this chapter, do so now and ensure that the education.csv
file is in the R working directory. The file has data about several school districts in the US. The following table describes the variables:
Variable | Meaning |
state | US state code |
region | Region of the country (1 = NE, ...) |
urban | Number of residents per thousand residing in urban areas in 1970 |
income | Per capita personal income in 1973 |
under 18 | Number of residents per thousand under 18 years of age in 1974 |
expense | Per capita expenditure on public education... |