What’s the fastest implementation of the Mantel test in R?

I’ve been helping a friend with some spatial analyses, which include a Mantel test to evaluate spatial autocorrelation. The original vector data is aggregated into increasingly small cells, and spatial autocorrelation is tested for each grid size (to assess the effect of scale), to the point where the smallest scale will simply crash the program. So for the sake of my short patience code optimization, I have been looking at different implementations of the test in R.

So far I have found three implementations: mantel.rtest on the ade4 package, mantel on the ecodist package, and mantel on the vegan package.

Curious to see if there was any difference in processing speed,  I timed each implementation, with the same data:

> length(dst.bu04800) # first distance vector
[1] 405450

> length(dsg.bu04800) # second distance vector
[1] 405450

> # using ecodist
> system.time(man.bu.04800 <- mantel(dst.bu04800 ~ dsg.bu04800, nperm = 1000))
user  system elapsed
17.66    0.03   18.18

> # using ade4
> system.time(man.bu.04800 <- mantel.rtest(dst.bu04800,dsg.bu04800, nrepet = 1000))
user  system elapsed
39.33    5.99   45.71

> # using vegan
> system.time(man.bu.04800 <- mantel(dst.bu04800,dsg.bu04800, permutations = 1000))
user  system elapsed
53.07    4.42   57.69

For perspective, the machine I am using is a Dell Precision T7500 with Dual 2.13 Ghz Quad Core Xeon E5506 processors , and 6GB of RAM. I’m running R 2.13.0 64-bit, on Windows 7 Ultimate 64.

I guess that ecodist it is, at least for me.

But, just so you know, distance in ecodist is way slower than using dist in the base package:

> system.time(dst.bu04800 <- distance(bu04800$TOT_BU_AR))
user  system elapsed
13.49    0.02   13.51

> system.time(dst.bu04800 <- dist(bu04800$TOT_BU_AR))
user  system elapsed
0.01    0.00    0.02

If you think, for any reason, that I’m doing something wrong and there’s a faster way, let me know!

Advertisements

About thiagosilva

Ecologist and "Remote Senser", Postdoctoral Fellow at INPE, Brazil.
This entry was posted in News and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s