I wish to carry out a mantel test between a matrix of geographical distances in kilometers (matrix 1) and a corresponding matrix of Bray-curtis distances in which I leave out those values with zero geographical distance. (A distance of 0 km represent neighbouring samples that are not identical and should not be pooled, but are not wholly independent of one another either). Therefore, I wish to keep them in my analysis.
The simple mantel test is easy: I read in the geographical matrix using the basic
my_geodist_matrix <-read.table("my_geodist_matrix.txt",header=TRUE,row.names=1)
The matrix looks like this:
1top 1bottom 2top 2bottom 3top 3bottom
1top 0 0 20.72 20.72 127.5 127.5
1bottom 0 0 20.72 20.72 127.5 127.5
2top 20.72 20.72 0 0 148.137 148.137
2bottom 20.72 20.72 0 0 148.137 148.137
3top 127.5 127.5 148.137 148.137 0 0
3bottom 127.5 127.5 148.137 148.137 0 0
Then, I calculate the BC distances (using the vegan package) thus:
my_otu_table.dist<-vegdist(my_otu_table,method="bray")
(looks like this):
1top 1bottom 2top 2bottom 3top
1bottom 0.8341957
2top 0.9948253 0.9908943
2bottom 0.9919492 0.9853757 0.4667466
3top 0.9482715 0.6923454 0.9926684 0.9882600 3bottom 0.9463127 0.6581957 0.9901074 0.9843602 0.1683397
and do the simple mantel test
mantel(my_geodist_matrix ,my_otu_table.dist)
So far, no trouble at all. However, when I try to leave out the pairs with 0 in the geographical matrix and the corresponding Bray-Curtis values, it goes wrong.
my_geodist_matrix <- lapply(my_geodist_matrix, function(x){replace(x, x == 0, NA)})
and
my_ otu_table.dist[is.na(my_geodist_matrix)] <- NA
When I do the mantel test now, R gives the following error messages:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""dist"" to a data.frame
or
Error in cor(as.vector(xdis), ydis, method = method, use = use) :
missing observations in cov/cor
How do I do the desired comparison the best way (and correctly!)?