PERMANOVA and dispersion
This submodule tests whether groups of samples differ in gene content, using Jaccard distance (1 − Jaccard
similarity, which ignores shared absences). The two functions are the PAV equivalents of vegan::adonis2 and
vegan::betadisper with vegan::permutest.
PERMANOVA asks whether group centroids differ more than expected by chance. It is also sensitive to differences in spread, so a significant PERMANOVA should be read together with the dispersion test: if dispersions differ, the PERMANOVA result may reflect spread rather than location.
To show both on one figure, pass the results to pcoa.centroids:
import pavvis.plots.pcoa as pcoa
pcoa.centroids(pm, "clade", betadisper_result=bd, permanova_result=pa)
The plot reuses the results rather than re-running the tests, so the figure annotation matches the reported values.
PERMANOVA
Function
permanova
PERMANOVA on Jaccard distance for the groups of a discrete metadata column.
Equivalent to vegan::adonis2(dist ~ group, permutations=...) with Jaccard
distance (1 − Jaccard similarity, from pm.compute_jaccard()). The pseudo-F is
the ratio of between-group to within-group sums of squared distances, each
divided by its degrees of freedom. Significance comes from permuting sample
labels: p is the fraction of permutations with a pseudo-F at least as large as
observed, with the observed value counted once.
PERMANOVA is sensitive to differences in dispersion as well as location. Check
betadisper before interpreting a significant result as a shift in centroids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance. |
required |
column
|
str
|
A discrete metadata column defining the groups. |
required |
permutations
|
int
|
Number of label permutations. Defaults to 999. |
999
|
random_state
|
int | None
|
Random seed for reproducibility. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
An adonis2-style DataFrame with rows |
DataFrame
|
columns |
DataFrame
|
set on the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If column is not a discrete metadata column or has fewer than 2 groups. |
Example
from pavvis import PavMatrix
from pavvis.stats.permanova import permanova
pm = PavMatrix("path/to/pav.csv", "path/to/metadata.csv")
pa = permanova(pm, "clade", permutations=9999, random_state=42)
pa
Df SumOfSqs R2 F Pr(>F)
Model 7 20.1471 0.0859 13.8202 0.0001
Residual 1030 214.5040 0.9141 NaN NaN
Total 1037 234.6511 1.0000 NaN NaN
Interpretation
R2 is the fraction of the total variation in Jaccard distance explained by the grouping. In the example, clade
explains 8.6% of the variation in gene content. Small R² values are typical for PAV data, because much of the
variable gene content differs between individual accessions.
Dispersion (PERMDISP)
Function
betadisper
Multivariate homogeneity of group dispersions (PERMDISP) on Jaccard distance.
Follows vegan::betadisper and vegan::permutest. Samples are placed in PCoA
space using all axes, including those with negative eigenvalues (Jaccard distance
is not Euclidean). Each group's centre is computed separately in the positive and
negative axis spaces, and a sample's distance to its group centre is
z = sqrt(d₊² − d₋²), with negative values of d₊² − d₋² set to zero as in vegan.
Dispersions are compared with a one-way ANOVA F on z; significance comes from
refitting the ANOVA to permuted residuals (z minus its group mean), keeping the
group centres fixed.
Validated against vegan 2.7.5 on the Amborella PAV data: distances and F match to floating-point precision with type='centroid'. With type='median', distances differ by ~1e-5 because vegan stops its BFGS search for the spatial median earlier than the Weiszfeld iteration used here.
A significant result means the groups differ in spread. In that case a significant PERMANOVA cannot be read purely as a difference in location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance. |
required |
column
|
str
|
A discrete metadata column defining the groups. |
required |
type
|
Literal['median', 'centroid']
|
Group centre to measure distances from: 'median' (spatial median, vegan's default) or 'centroid' (mean). Defaults to 'median'. |
'median'
|
permutations
|
int
|
Number of residual permutations. Defaults to 999. |
999
|
random_state
|
int | None
|
Random seed for reproducibility. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If column is not a discrete metadata column or has fewer than 2 groups, or if type is not 'median' or 'centroid'. |
Example
from pavvis.stats.permanova import betadisper
bd = betadisper(pm, "clade", permutations=9999, random_state=42)
bd["F"], bd["p_value"]
bd["distances"].groupby(pm.metadata["clade"]).mean() # mean distance to centre per clade
Interpretation
A significant result means some groups are more variable in gene content than others. Use
pcoa.dispersion_boxplot(pm, "clade", betadisper_result=bd) to see which.