O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a convenience function to run KNN for multiple k values

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required