]> git.donarmstrong.com Git - ape.git/blobdiff - R/mantel.test.R
some news for ape 3.0-8
[ape.git] / R / mantel.test.R
index a23bc324de8af19ff2a84dacf0478908633e5714..0de2ad9693e2218fb0caf36b25cff08d67422bb0 100644 (file)
@@ -1,8 +1,8 @@
-## mantel.test.R (2011-11-10)
+## mantel.test.R (2012-03-16)
 
 ##   Mantel Test for Similarity of Two Matrices
 
-## Copyright 2002-2006 Ben Bolker and Julien Claude
+## Copyright 2002-2011 Ben Bolker and Julien Claude
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -18,11 +18,11 @@ mant.zstat <- function(m1, m2) sum(lower.triang(m1 * m2))
 
 lower.triang <- function(m)
 {
-    if (!diff(dim(m))) print("Warning: non-square matrix")
+    if (diff(dim(m))) print("Warning: non-square matrix")
     m[col(m) <= row(m)]
 }
 
-mantel.test <- function(m1, m2, nperm = 1000, graph = FALSE,
+mantel.test <- function(m1, m2, nperm = 999, graph = FALSE,
                         alternative = "two.sided", ...)
 {
     alternative <- match.arg(alternative, c("two.sided", "less", "greater"))
@@ -30,10 +30,10 @@ mantel.test <- function(m1, m2, nperm = 1000, graph = FALSE,
     realz <- mant.zstat(m1, m2)
     nullstats <- replicate(nperm, mant.zstat(m1, perm.rowscols(m2, n)))
     pval <- switch(alternative,
-                   "two.sided" =  2 * min(sum(nullstats > realz), sum(nullstats < realz)),
-                   "less" = sum(nullstats < realz),
-                   "greater" = sum(nullstats > realz))
-    pval <- pval / nperm
+                   "two.sided" =  2 * min(sum(nullstats >= realz), sum(nullstats <= realz)),
+                   "less" = sum(nullstats <= realz),
+                   "greater" = sum(nullstats >= realz))
+    pval <- (pval + 1) / (nperm + 1) # 'realz' is included in 'nullstats'
     if (graph) {
         plot(density(nullstats), type = "l", ...)
         abline(v = realz)