# kNN randomization test in R?

I am trying to determine significant differences between groups of data using a k-nearest neighbor randomization test in R. This test basically looks for similarity amongst homogenous groups and separates them out using random clustering. In the literature, this test is called a "K-nearest neighbor (kNN) randomization test," however, I'm not certain if it called by other names elsewhere.

For my specific data, I have isotopic ratios given for various prey items. I have already grouped together ecologically similar prey item types, and now want to see if those groups differ from one another in their isotopic signature. This is where the kNN test would come in.

-
kmeans() (you can find it by typping ?? folowed by the name of what you want as in ??kmean) –  user603 Sep 21 '10 at 17:36
Another useful way to find functions is the apropos function. –  Shane Sep 21 '10 at 17:38
@kwak: why not post your comment as an answer? –  Shane Sep 21 '10 at 17:38
@all:> i propose to change the title of the post, so as to make it easier to find for someone looking for the R implementation of other procedures (beside k-means). –  user603 Sep 21 '10 at 21:11
@kwak Yes, I think it's better to change the title because k-means vs. kNN seems ambiguous. –  chl Sep 21 '10 at 22:07
show 1 more comment

kmeans() (you can find it by typing ?? followed by the name of what you want as in ??kmean). In general, a good trick is to first look it up on a R specific search engine

-
You can also do RSiteSearch("k-means") –  nico Sep 21 '10 at 20:42
Thanks: i did know of this feature ;) –  user603 Sep 21 '10 at 21:09

I am not sure to understand your question since you talk about k-means, which is basically an unsupervised method (i.e. where classes are not known a priori), while at the same time you are saying that you already identified groups of individuals. So I would suggest to look at classification methods, or other supervised methods where class membership is known and the objective is to find a weighted combination of your variables that minimize your classification error rate (this is just an example). For instance, LDA does a good job (see the CRAN task view on Multivariate Statistics), but look also at the machine learning community (widely represented on the stats.stackexchange) for other methods.

Now since you also talked of k-nearest neighbor, I wonder if you are not introducing a confusion between k-means and kNN. In this case, the corresponding R function is knn() in the class package (it includes cross-validation), or see the kknn package.

-

You appear to have confused "cluster analysis" with "classification". The former is where you don't know the groupings and wish to determine them from the training data to hand. Classification is where you know the groups and want to predict them.

There are a few packages in R that do this. For example, look at the results of this R Site Search for suitable packages.

Alternatively, perhaps you are looking for a multivariate analysis of variance? In which case lm() and aov() would be worth looking at for a start. This presumes you want to "model" your data as a function of the group variable?

-
I believe the k nearest-neighbors test is a multivariate analysis. Perhaps I have confused cluster analysis with classification, but I don't think either apply in this situation. I already have categorized the variables into groups, and now want to see if these groups statistically differ from one another. This test is different that an ANOVA or linear model. Any suggestions? Thanks. –  Sharon Sep 21 '10 at 23:03
@Sharon I'm a little bit puzzled about the term 'randomization test' (the only paper I found using this keyword, j.mp/9OCB9I, seems rather to focus on power considerations) -- I rest on my idea that you could use cross-validation to assess the classification accuracy in your data, e.g. leave-one-out or k-fold CV for this purpose (available in kknn and class packages). Now, I don't know on which basis your classes were defined, but it is generally meaningless to use ANOVA to test if the groups differ on the variables that were used to define class membership. –  chl Sep 22 '10 at 7:30
@Sharon; can you explain your data a bit better (I'm a (sort of) ecologist and use isotopes all the time so you can be quite technical if it helps)? –  Gavin Simpson Sep 22 '10 at 11:54