Skip to content

construct a nearest neighbor contingency table

5 messages · Tom Elliott, Sarah Goslee, Jari Oksanen +1 more

#
Hello-

I have plot data of  mapped male and female cottonwoods, collected for
my master's thesis. The data are multiple rows in 3 columns:
x-coordinate, y-coordinate, and a code for male, female, or unknown. I
want to test it for spatial segregation of the sexes using methods by
Phillip Dixon (1994, 2002).

 Is there a way in R to build a nearest neighbor contingency table for
the data? I would like the format to be 3 rows (male, female,
unknown), with columns for counts of the nearest neighbors in each
class.

Thanks,
Tom

Biology Department
Central Washington University
#
What about using dist() on the xy coordinates, then finding the minimum
for each row? That would get you nearest neighbor for each site.

Sarah
On Tue, Jun 3, 2008 at 7:28 PM, Tom Elliott <tnelliott at gmail.com> wrote:

  
    
#
Quoting Sarah Goslee <sarah.goslee at gmail.com>:
This suggests the following code:
nn <- apply(m, 1, which.min)
gender    female male unknown
   female      18    8       2
   male         9    2       5
   unknown      2    4       0

Which surely is doable and even straightforward, and supports R's  
reputation as an environment where you can do anything, but not easily.

An alternative is to install spdep package (spatial dependence) which  
has a fucntion called knearneigh:
gender    female male unknown
   female      18    8       2
   male         9    2       5
   unknown      2    4       0

The drawback is spdep is very heavy and requires many other packages.  
On the other hand, you get many new things if you want to have spatial  
analysis.

cheers, jari oksanen
#
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
#
Hello,

You can use the function "dixon2002" in the R package "ecespa". It 
only requires  a data.frame with x, y and "type"  columns as input.

Please, tell me how does it work for you.

Regards,


Marcelino
At 01:28 04/06/2008, Tom Elliott wrote:
***************************************
Marcelino de la Cruz Rot
Depto. Biologia Vegetal
EUIT Agricola
Universidad Politecnica de Madrid
28040-Madrid
SPAIN