BCompare groups · Guide test 171
ANOSIM
Compares ranks of between group and within group dissimilarities.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- ANOSIM (analysis of similarities) — Are rows more alike within groups than between them, judged by ranked dissimilarities?
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 · ANOSIM
Above 1 the later groups are more spread out (unequal covariance matrices).
Your data
Simulated with known truth3 groups on two measurements; crosses mark the group means.
If H₀ were true
- The reference: how R varies across samples when H₀ is true, R over relabelings (permutation null: 199 relabelings of these data (seed 20261107)).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .180).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- mean rank of between-group distances
- mean rank of within-group distances
- −1 to 1; near 0 when labels do not matter
The verdict
R = 0.029, p = .180
p > α = .050: compatible with H₀
R near 1 means groups separate cleanly; like PERMANOVA it is confounded by unequal dispersion.
- Statistic
- 0.029
- p-value
- 0.1800
ANOSIM on 3 groups of 12 rows on two variables: R = 0.029, p = .180. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: groups do not differ in their rank dissimilarities.
Assumptions
Independent/exchangeable units under the chosen group permutation design; dissimilarity meaningful for the data.
Formula
Parameters and notation
Rank all pairwise dissimilarities; r̄B averages between-group ranks and r̄W within-group ranks.
Structure of the method
Rank the pooled pairwise dissimilarities, compare mean between-group and within-group ranks, and scale the difference to form R. Permuting the grouping vector supplies its reference. Pairwise distances are not independent observations; the sampling units remain the original rows, which is why one permutes labels rather than distances independently.
Estimated and nuisance parameters
The empirical rank distributions and their groupwise means are summaries. No parametric location/covariance model or latent network is estimated. R is a separation index with a specific rank normalization, not a fitted correlation coefficient or explained-variance fraction.
Settings and design choices
Choose the distance, tie-ranking convention, transformation, group definition, and restricted permutation design. Set the number of permutations and seed. Testing many distances and selecting the strongest result requires multiplicity control or an independently specified selection procedure.
Fitting and adaptation
There is no model training to fine-tune. Recompute group assignments to distance ranks under allowed label permutations. If the distance comes from supervised metric learning, fit that metric separately or include its fitting in the randomization pipeline. Inspect within-group rank distributions before interpreting separation as a location effect.
Sensitivity and robustness
Group size imbalance and dispersion differences can drive results or obscure a centroid interpretation. Many identical distances create ties and reduce information. Dependence, spatial clusters, or repeated observations restrict valid exchanges. Report R with uncertainty/design context and inspect rank-distance plots; significance alone does not show that groups are well separated for prediction.
How to use and calibrate
Permute group labels within permitted restrictions; large R indicates greater between-group separation.
Example application
Compare community composition using ranked dissimilarities.
Interpretation and pitfalls
Watch out
Dispersion and group size effects can complicate interpretation. R is not an ordinary correlation coefficient despite its notation.
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
Euclidean distance, independent groups, 999 unrestricted permutations. Clustered or repeated designs require restricted permutations; location and dispersion can affect different procedures.
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(120),ncol=3);Y<-matrix(rnorm(150,mean=.3),ncol=3)
Z<-rbind(X,Y);g<-factor(rep(1:2,c(nrow(X),nrow(Y))))
print(vegan::anosim(dist(Z),g,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(120),ncol=3);Y<-matrix(rnorm(150,mean=.3),ncol=3)
Z<-rbind(X,Y);g<-factor(rep(1:2,c(nrow(X),nrow(Y))))
print(vegan::anosim(dist(Z),g,permutations=999))
Output
Call:
vegan::anosim(x = dist(Z), grouping = g, permutations = 999)
Dissimilarity: euclidean
ANOSIM statistic R: 0.1182
Significance: 0.001
Permutation: free
Number of permutations: 999Sources
- vegandevs.github.io/vegan/reference/anosim.htmlvegandevs.github.io
- anosimvegandevs.github.io
- anosimsearch.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 171, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT171.