I'm having issues with a problem using the data("butterfly") set. The problem is:
Problem 4: Install the package “ade4” followed by data(butterfly). Our question is: “Is genetic similarity correlated with geographic distances between butterfly colonies?” Fill in a genetic distance matrix using the distance measure we called d1. Perform a Mantel Randomization test and interpret the results. Repeat the test using the measure of distance d2. Did it make a difference? Show your for-loop code as part of your answer as well as the scatter plots and randomization plots from the Mantel Randomization tests.
Here is the function used to calculate dissimilarity. I do not understand how to
dissimilarity <- function(p1, p2){
d1 <- sum( abs(p1 - p2) / 2 )
d2_num <- sum(p1*p2)
d2_denom <- sqrt( sum(p1^2) * sum(p2^2) )
d2 <- 1 - d2_num / d2_denom
return(list(d1=d1, d2=d2))
}
Here is the code I used to set up the genetic distance matrix.
library(ade4)
data("butterfly")
butterflydat<-data.frame(butterfly)
plot(butterfly$contour[,1:2], pch=16, cex=.4)
polygon(butterfly$contour[,1:2], lty=2)
points(butterfly$xy, pch=7)
nrow(butterfly$xy)
text(butterfly$xy, labels=1:16, pos=2, cex=.8)
apply(butterfly$genet,1,sum)
(Ddist <- dist(butterfly$xy))
I am able to calculate the dissimilarity between two geographic distances (e.g.):
dissimilarity(butterfly$genet[1,],butterfly$genet[2,])$d2
but I don't understand how to apply a for-loop to fill in the matrix for all geographic distances. I also don't understand the Mantel Randomization test. The question is implying that we perform a Mantel Randomization test on each matrix, but doesn't the test compare the correlation between two matrices (i.e. d1 and d2?)
Any help would be greatly appreciated, I am new to statistics and R.
data.frame(butterfly)
throws an error, like it should since the datasetbutterfly
is not in tabular form. – Rui Barradas