Skip to content
Prev 194 / 7419 Next

construct a nearest neighbor contingency table

Quoting Sarah Goslee <sarah.goslee at gmail.com>:
One rule in R mailing list is that you should never hurry with your  
answers. Somebody else may have answered already, or there is a more  
obvious way. I have been submerged in spdep lately and came out with a  
spdep package way of finding k nearest neighbours. You do not need  
that, but the standard R package class has function knn.cv just for  
this purpose (class is in the VR bundle of Venables & Ripley and  
should be installed with any standard distribution of R). Usually knn  
methods need train and test data set, but knn.cv does simple cross  
validation. With the examle of the previous post (gender = gender  
name, coord = spatial coordinates) you get

table(gender, knn.cv(coord, cl=gender))

coord need not be spatial coordinates (in the examples they are not),  
but they can be.

cheers, jari oksanen