NOTE: This page has been delinked.  It is no longer being maintained, and information on this page may be out of date. Because this page has been delinked, we cannot not answer questions regarding this page.

How can I perform a Mantel test in R?

A Mantel test measures the correlation between two matrices typically containing measures of distance.  A Mantel test is one way of testing for spatial autocorrelation.  Using functions in the ade4 library, we can perform a Mantel test in R.  To download and load this library, enter install.packages("ade4") and then library(ade4).  There are other Mantel test functions available in other R libraries and our choice of this library's should not be seen as an endorsement in any way.


Let's look at an example. Our dataset, ozone, contains ozone measurements from thirty-two locations in the Los Angeles area aggregated over one month. The dataset includes the station number (Station), the latitude and longitude of the station (Lat and Lon), and the average of the highest eight hour daily averages (Av8top). We will be interested in testing if the differences in ozone measurements are smaller for stations that are closer together than for stations that are far apart.  This data, and other spatial datasets, can be downloaded from the University of Illinois's Spatial Analysis Lab. We can look at a summary of our location variables to see the range of locations under consideration.

ozone<-read.table("", sep=",", header=T)
head(ozone, n=10)

   Station   Av8top      Lat       Lon
1       60 7.225806 34.13583 -117.9236
2       69 5.899194 34.17611 -118.3153
3       72 4.052885 33.82361 -118.1875
4       74 7.181452 34.19944 -118.5347
5       75 6.076613 34.06694 -117.7514
6       84 3.157258 33.92917 -118.2097
7       85 5.201613 34.01500 -118.0597
8       87 4.717742 34.06722 -118.2264
9       88 6.532258 34.08333 -118.1069
10      89 7.540323 34.38750 -118.5347

To run a Mantel test, we will need to generate two distance matrices: one containing spatial distances and one containing distances between measured outcomes at the given points.  In the spatial distance matrix, entries for pairs of points that are close together are lower than for pairs of points that are far apart.  In the measured outcome matrix, entries for pairs of locations with similar outcomes are lower than for pairs of points with dissimilar outcomes.  We do this using the dist function.  The Mantel test function will require objects of this "distance" class. 

station.dists <- dist(cbind(ozone$Lon, ozone$Lat))
ozone.dists <- dist(ozone$Av8top)

as.matrix(station.dists)[1:5, 1:5]

          1         2         3         4         5
1 0.0000000 0.3937326 0.4088031 0.6144127 0.1854888
2 0.3937326 0.0000000 0.3749446 0.2206810 0.5743590
3 0.4088031 0.3749446 0.0000000 0.5116772 0.4994034
4 0.6144127 0.2206810 0.5116772 0.0000000 0.7944601
5 0.1854888 0.5743590 0.4994034 0.7944601 0.0000000

as.matrix(ozone.dists)[1:5, 1:5]

         1        2        3        4        5
1 0.000000 1.326612 3.172921 0.044354 1.149193
2 1.326612 0.000000 1.846309 1.282258 0.177419
3 3.172921 1.846309 0.000000 3.128567 2.023728
4 0.044354 1.282258 3.128567 0.000000 1.104839
5 1.149193 0.177419 2.023728 1.104839 0.000000

These are the two matrices which the function will be testing for a correlation.  The test consists of calculating the correlation of the entries in the matrices, then permuting the matrices and calculating the same test statistic under each permutation and comparing the original test statistic to the distribution of test statistics from the permutations to generate a p-value.  The number of permutations defines the precision with which the p-value can be calculated.  The function to perform the Mantel test is mantel.rtest and the required arguments are the two distance matrices.  The number of permutations can also be specified by the user, but is by default 99.

mantel.rtest(station.dists, ozone.dists, nrepet = 9999)

Monte-Carlo test
Observation: 0.1636308 
Call: mantel.rtest(m1 = station.dists, m2 = ozone.dists, nrepet = 9999)
Based on 9999 replicates
Simulated p-value: 0.0312 

Based on these results, we can reject the null hypothesis that these two matrices, spatial distance and ozone distance, are unrelated with alpha = .05.  The observed correlation, r = 0.1636308, suggests that the matrix entries are positively associated.  So smaller differences in ozone are generally seen among pairs of stations that are close to each other than far from each other.   Note that since this test is based on random permutations, the same code will always arrive at the same observed correlation but rarely the same p-value. 

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.