Python implementation of the Mantel test, a significance test of the correlation between two distance matrices
Project description
mantel
Python implementation of the Mantel test (Mantel, 1967), which is a significance test of the correlation between two distance matrices.
Installation
The mantel package is available on PyPI and can be installed using pip:
$ pip install mantel
Once installed, the package can be imported in the normal way:
import mantel
Usage
The mantel package contains one main function, test() with the following call signature:
def test(X: array_like, Y: array_like, perms: int = 10000, method: str = "pearson",
tail: str = "two-tail", ignore_nans: bool = False) -> MantelResult:
Xarray_like: First distance matrix (condensed or redundant).Yarray_like: Second distance matrix (condensed or redundant), where the order of elements corresponds to the order of elements in X.permsint, optional: The number of permutations to perform (default:10000). A larger number gives more consistent results but takes longer to run. If the number of possible permutations of the distance matrix is smaller thanperms, all possible permutations will be enumerated leading to a deterministic result (this can be forced by settingpermsto0).methodstr, optional: Type of correlation coefficient to use; eitherpearsonorspearman(default:pearson).tailstr, optional: Which tail to test in the calculation of the empirical p-value; eitherupper,lower, ortwo-tail(default:two-tail).ignore_nansbool, optional: Ignorenp.nanvalues in the Y matrix (default:False). This can be useful if you have missing values in one of the matrices.
The function returns a MantelResult object with the following properties:
MantelResult.rfloat: Veridical correlationMantelResult.pfloat: Empirical p-valueMantelResult.zfloat: Standard score (z-score)MantelResult.stochastic_testbool:Trueif the test was performed by randomly sampling possible permutations;Falseif the test was performed by enumerating all possible permutations.MantelResult.correlationsarray: Correlation coefficients produced by the testMantelResult.meanfloat: Mean ofcorrelationsMantelResult.stdfloat: Sample standard deviation ofcorrelations(ifstochastic_test==True) or population standard deviation ofcorrelations(ifstochastic_test==False)
Worked Example
Let’s say we have a set of four objects and we want to correlate X (the distances between the four objects using one measure) with Y (the corresponding distances between the four objects using another measure). For example, your “objects” might be species of animal, and your two measures might be genetic distance and geographical distance (the hypothesis being that species that live far away from each other will tend to be more genetically different).
For four objects, there are six pairwise distances. First you should compute the pairwise distances for each measure and store the distances in two condensed or redundant distance matrices (the test() function will accept either format). No distance functions are included in this package, since the metrics you use will be specific to your particular data.
Let’s say our data looks like this:
# E.g. species A through D
# A~B A~C A~D B~C B~D C~D
dists1 = [0.2, 0.4, 0.3, 0.6, 0.9, 0.4] # E.g. genetic distances
dists2 = [0.3, 0.3, 0.2, 0.7, 0.8, 0.3] # E.g. geographical distances
We pass the data to the test() function and optionally specify the number of permutations to test against, a correlation method to use (either ‘pearson’ or ‘spearman’), and which tail to test (either ‘upper’, ‘lower’, or ‘two-tail’). In this case, we’ll use the Pearson correlation and test the upper tail, since we’re expecting to find a positive correlation.
result = mantel.test(dists1, dists2, perms=10000, method='pearson', tail='upper')
This will measure the veridical Pearson correlation between the two sets of pairwise distances. It then repeatedly measures the correlation again and again under permutations of one of the distance matrices to produce a distribution of correlations under the null hypothesis. In the example above, we requested 10,000 permutations (the default). However, for four objects there are only 4! = 24 possible permutations of the matrix. In such a scenario, where the number of requested permutations is greater than the number of possible permutations, mantel.test() simply tests against all possible permutations of the matrix, yielding a deterministic result.
Printing the result shows the veridical correlation, empirical p-value, and z-score:
print(result)
# MantelResult(0.9148936170212766, 0.041666666666666664, 2.040402492261023)
Individual results can be accessed by the relevant property name. For example, to check if the veridical correlation is significant, we could do:
print(result.p < 0.05)
# True
Since the p-value is less than 0.05, we can conclude that there is a significant correlation between the two sets of distances. This suggests that the species that live closer together tend to be more genetically related, while those that live further apart tend to be less genetically related.
Plotting
The mantel package also provides a plot() function, which plots the distribution of sample correlations against the veridical. For example, here we generate two random distance matrices and plot the results:
dists1 = np.random.random(351)
dists2 = np.random.random(351)
result = mantel.test(dists1, dists2)
fig, axis = mantel.plot(result)
fig.savefig('example.svg')
The plot() function has several other arguments for customization:
alphafloat: Significance level for rejecting the null hypothesis (default: 5%)hist_colorstr: Color used for the histogram bars (default: 'lightgray').gaussian_colorstr: Color used for the normal distribution curve and the confidence interval limits (default: 'black').acceptance_colorstr: Color used for drawing the vertical line and the label of the veridical correlation if the null hypothesis is rejected according to the significance level value (default: 'black').rejection_colorstr: Color used for drawing the vertical line and the label of the veridical correlation if the null hypothesis cannot be rejected according to the significance level value (default: 'black').
Contributing
This project is in the stable/maintenance phase and is not under active development. I am only aiming to fix bugs and keep it compatible with current versions of Python so that it remains useful to the community. With that in mind, contributions are very welcome, but please read the CONTRIBUTING file before submitting a pull request.
License
This package is licensed under the terms of the MIT License.
References and Links
Mantel, N. (1967). The detection of disease clustering and a generalized regression approach. Cancer Research, 27(2), 209–220.
Mantel Test on Wikipedia: https://en.wikipedia.org/wiki/Mantel_test
A guide to the Mantel test for linguists: https://joncarr.net/s/a-guide-to-the-mantel-test-for-linguists.html
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mantel-2.2.3.tar.gz.
File metadata
- Download URL: mantel-2.2.3.tar.gz
- Upload date:
- Size: 20.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6f795a2dcab66d30560fad404f458dade7853ad67f2c1e4c249107897497003
|
|
| MD5 |
2ff21cb6c9eab05440509e2b3e1df3e8
|
|
| BLAKE2b-256 |
5722c0670e4990f95ee5fc3b90774c322b337d827c7ff2ec306f576b94b29822
|
File details
Details for the file mantel-2.2.3-py3-none-any.whl.
File metadata
- Download URL: mantel-2.2.3-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eca16ea757a65b77481fadd9b97d3bcc35f990d4105b61674827159a57b9c040
|
|
| MD5 |
1564daad8cba6b8dbd39588ea7024ac1
|
|
| BLAKE2b-256 |
cae42c943885e4c6b9a20a6b574e2bec0d37c16f7b19f9eb39a88ad092f5d18d
|