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
construct a nearest neighbor contingency table
5 messages · Tom Elliott, Sarah Goslee, Jari Oksanen +1 more
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:
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
Sarah Goslee http://www.functionaldiversity.org
Quoting Sarah Goslee <sarah.goslee at gmail.com>:
What about using dist() on the xy coordinates, then finding the minimum for each row? That would get you nearest neighbor for each site.
This suggests the following code:
gender <- sample(c("female","male","unknown"), 50, repla=T,
prob=c(0.5, 0.4,0.1))
coord <- matrix(runif(100), nrow=50)
m <- as.matrix(dist(coord))
diag(m) <- NA>
nn <- apply(m, 1, which.min)
table(gender, gender[nn])
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:
table(gender, gender[knearneigh(coord, k=1)$nn])
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
On Tue, Jun 3, 2008 at 7:28 PM, Tom Elliott <tnelliott at gmail.com> wrote:
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
-- Sarah Goslee http://www.functionaldiversity.org
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Quoting Sarah Goslee <sarah.goslee at gmail.com>:
What about using dist() on the xy coordinates, then finding the minimum for each row? That would get you nearest neighbor for each site.
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
On Tue, Jun 3, 2008 at 7:28 PM, Tom Elliott <tnelliott at gmail.com> wrote:
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
-- Sarah Goslee http://www.functionaldiversity.org
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
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:
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
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
*************************************** Marcelino de la Cruz Rot Depto. Biologia Vegetal EUIT Agricola Universidad Politecnica de Madrid 28040-Madrid SPAIN