mantel_test(d1, d2, nperm)

Mantel test between two distance matrices. Returns r, p-value and permutation distribution.

statisticsspatial
Args:d1 — dist matrix 1d2 — dist matrix 2nperm=999
mantel_test <- function(d1, d2, nperm = 999) {
  if (!requireNamespace("vegan", quietly = TRUE)) install.packages("vegan")
  res <- vegan::mantel(d1, d2, permutations = nperm)
  list(r    = res$statistic,
       pval = res$signif,
       nperm = nperm)
}
# Example
d1 <- dist(matrix(rnorm(100), 10, 10))
d2 <- dist(matrix(rnorm(100), 10, 10))
mantel_test(d1, d2)
# ── Example ──────────────────────────────────────────────────────
# Morphological distance vs geographic "distance" using iris
morph_dist <- dist(iris[, 1:4])
# Simulate a second distance matrix
geo_dist   <- dist(matrix(rnorm(300), 150, 2))
mantel_test(morph_dist, geo_dist, nperm = 499)