Suppose I want to fit a k-nearest-neighbour using caret package in R:

index       <- createDataPartition(iris$Species, p=0.75, list=FALSE)
iris_train <- iris[ index, ]
iris_test  <- iris[-index, ]

fitControl <- trainControl(method = "cv",
                       number = 4, 
                       savePred = TRUE, 
                       classProb = TRUE)

iris_knn <- train(Species ~ ., 
                  data = iris_train, 
                  method = "knn", 
                  trControl = fitControl)

As far as I understand k-nn, this algorithm defines the class of an observation according to an election: the k closest points to the observation are considered the most frequent class is defined as the correct class for the observation.

Many sources I checked say that Euclidean distance is the most commonly used distance, but suppose I need another distance because of reasons. How can I define another distance using caret?

For example, suppose I have evidence to say that Manhattan distance is better than Euclidean to my data set. How can I say this to R using caret package?


closed as off-topic by user20160, mdewey, kjetil b halvorsen, Michael Chernick, Peter Flom Oct 1 '18 at 10:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – user20160, mdewey, kjetil b halvorsen, Michael Chernick, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Why do you stick so much to the caret package? Do you want to discuss the use of alternative measures of distance or is this basically a question about software, how to code/program something? $\endgroup$ – Martijn Weterings Sep 30 '18 at 12:56
  • $\begingroup$ I stick with caret because I am more familiar with it. I want to discuss the use of alternative measures of distance in this particular software. $\endgroup$ – Marcus Nunes Sep 30 '18 at 13:24
  • $\begingroup$ That is not very clear to me what you want. Do you look for a software solution (which would be off-topic) or something else? The caret package links to knn::class which explicitly states to be about Euclidean distance. Do you want to hack this somehow? Do you have a statistics question or a programming question? I do not see how your question is about the use of alternative measures, instead of how to code/program it. What is the statistical question? $\endgroup$ – Martijn Weterings Sep 30 '18 at 13:52
  • $\begingroup$ Questions about how to do something in a particular software are off topic here. $\endgroup$ – Peter Flom Oct 1 '18 at 10:42

I think this is an interesting question, as I havent seen KNN be used with a different distance metric than Euclidean.

The method "knn" does not seem to allow choosing other distance metrics, as it applies the knn() function from base R. The method "kknn" however performs k-nearest-neighbour as well using the kknn library and seems to use the Minkowski distance which should be the Manhattan distance using the parameter distance=1.

Here you can find the available models for caret (This is where I found "kknn"): http://topepo.github.io/caret/available-models.html


Not the answer you're looking for? Browse other questions tagged or ask your own question.