k-NN bagging
The k-NN classifier introduced a classification model in the previous section. We can make this robust using the bootstrap method. The broader algorithm remains the same. As with the typical bootstrap method, we can always write a program consisting of the loop and depending on the number of required bootstrap samples, or bags, the control can be specified easily. However, here we will use a function from the FNN
R package. The ownn
function is useful for carrying out the bagging method on the k-NN classifier.
The ownn
function requires all variables in the dataset to be numeric. However, we do have many variables that are factor variables. Consequently, we need to tweak the data so that we can use the ownn
function. The covariate data from the training and test dataset are first put together using the rbind
function. Using the model.matrix
function with the formula ~.-1
, we convert all factor variables into numeric variables. The important question here is how does the model...