September 2017
Beginner to intermediate
560 pages
25h 18m
English
Running knn for several values of k to choose the best one, involves repetitively executing similar lines of code several times. We can automate the process with the following convenience function that runs knn for multiple values of k, reports the RMS error for each, and also produces a scree plot of the RMS errors:
rdacb.knn.reg.multi <- function (trg_predictors, val_predictors, trg_target, val_target, start_k, end_k)
{
rms_errors <- vector()
for (k in start_k:end_k) {
rms_error <- rdacb.knn.reg(trg_predictors, val_predictors,
trg_target, val_target, k)
rms_errors <- c(rms_errors, rms_error)
}
plot(rms_errors, type = "o", xlab = "k", ylab = "RMSE")
}
With the preceding function, ...
Read now
Unlock full access