CAssociation · Guide test 254
Mantel permutation test
Tests association between two distance or dissimilarity matrices using joint row and column permutations.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Mantel test (and partial Mantel, correlogram) — Are two sets of dissimilarities between the same rows related — for example, community and environment, or genes and geography?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Mantel test
The trait rises by this much across the map, so nearby objects are alike; 0 is no relation.
Your data
Simulated with a known trait gradientLeft: objects, darker for a higher trait. Right: every pair. Mantel's r is the correlation in the right panel; its null shuffles which object sits where.
If H₀ were true
- The reference: how r varies across samples when H₀ is true, 999 relabelings of the objects (permutation null on the shared stream).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .124).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the two distance matrices: here geographic distance and trait difference
- r after relabelling the objects (rows and columns of A together)
The verdict
r = 0.13, p = .124
p > α = .050: compatible with H₀
Pairs are not independent, which is why the null comes from relabelling objects, not from the usual correlation test. It tests a linear relation between distances only.
- Statistic
- 0.125
- p-value
- 0.1240
Mantel test on 20 objects with locations and a trait: r = 0.13, p = .124. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: two dissimilarity matrices are unrelated under exchangeable object labels.
Assumptions
Both matrices describe the same objects; the proposed label permutations must be valid, possibly restricted by design.
Formula
Parameters and notation
A,B are distance/dissimilarity matrices; Pearson or rank correlation must be specified.
Structure of the method
Compute a correlation between corresponding off-diagonal entries of two object-distance matrices, then jointly permute rows and columns of one matrix according to valid object relabelings. The inferential units are objects, not the much larger number of pairwise distances, whose values share endpoints and are dependent.
Estimated and nuisance parameters
Estimate the two dissimilarity matrices and their Pearson or rank correlation; no fitted regression slope across independent distance pairs is implied. If distances depend on estimated transformations or selected features, that upstream fitting becomes part of the analysis and may require repetition under the selected null.
Settings and design choices
Choose distance metrics, variable scaling, correlation type, permutation strata/blocks, number of draws, and tail convention. In vegan, the supplied Mantel call performs the package’s upper-tail association test; a negative statistic is not automatically given a two-sided significance interpretation. Matrix rows must represent the same objects in the same order.
Fitting and adaptation
Adapt by defining a scientifically valid object-level exchangeability scheme, then permute matching rows/columns together. Restricted permutations can reflect a design but do not automatically solve arbitrary spatial autocorrelation. If data-driven feature or distance selection occurred, separate exploration from testing or include it in a justified full procedure.
Sensitivity and robustness
Inspect matching IDs, missing distances, shared spatial gradients, correlated measurement errors, and sensitivity to distance/scaling choices. Ordinary Pearson correlation p values on flattened distances are invalid. A Mantel association describes correspondence of dissimilarities; it is not an individual-level causal relationship or a general substitute for spatial regression.
How to use and calibrate
Permute rows and matching columns of one matrix together, recompute the matrix correlation, and compare the observed statistic.
Example application
Assess association between two sets of between-object dissimilarities.
Interpretation and pitfalls
Watch out
Pairwise distances are dependent, so ordinary correlation p values are wrong. Spatially autocorrelated matrices and partial Mantel tests can have invalid exchangeability.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Both distance matrices describe the same 20 objects in identical order. Permute object labels, not the individual pairwise distances.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: vegan
# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
X<-matrix(rnorm(60),20,3);Y<-matrix(rnorm(60),20,3);print(vegan::mantel(dist(X),dist(Y),method="pearson",permutations=999))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
text=True, capture_output=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
result.check_returncode()
Validated — Executed successfully in the validation environment · needs vegan
set.seed(2026)
X<-matrix(rnorm(60),20,3);Y<-matrix(rnorm(60),20,3);print(vegan::mantel(dist(X),dist(Y),method="pearson",permutations=999))
Output
Mantel statistic based on Pearson's product-moment correlation
Call:
vegan::mantel(xdis = dist(X), ydis = dist(Y), method = "pearson", permutations = 999)
Mantel statistic r: -0.06235
Significance: 0.743
Upper quantiles of permutations (null model):
90% 95% 97.5% 99%
0.129 0.165 0.188 0.227
Permutation: free
Number of permutations: 999Sources
- vegan — vegan · mantelAuthor documentation/source reviewed: vegan 2.7-6.search.r-project.org
- mantelsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 254, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT254.