mantel_test: Mantel test

View source: R/mantel.R

mantel_testR Documentation

Mantel test

Description

This version of the Mantel test supports asymmetric matrices and multiple partial-Mantel controls, a combination of features that is not possible using other widely available Mantel functions such as mantel.partial.

Usage

mantel_test(
  x,
  y,
  z = NULL,
  nperm = 999,
  method = "pearson",
  alternative = c("two.sided", "less", "greater")
)

Arguments

x, y

Square matrices (can be asymmetric).

z

Optional list of one or more square control matrices if partial Mantel is desired.

nperm

Number of random permutations to perform (positive integer).

method

Correlation method (see cor for details).

alternative

A character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less".

Value

A list with the following components:

  • 'stat': The correlation coefficient between x and y, measured on the data provided. This will be a partial correlation if z is specified.

  • 'quantile': The quantile of the observed stat in the null distribution.

  • 'p.value': Significance, derived from the combination of quantile and alternative.

  • 'perm': A vector of null correlations based on permuted data.

Examples

# A convenience function to generate symmetric matrices:
sym_matrix <- function(x){
   n <- which(cumsum(1:100) == length(x)) + 1
   m <- matrix(0, n, n)
   m[lower.tri(m)] <- x
   m[upper.tri(m)] <- t(m)[upper.tri(t(m))]
   m
}

# Simulate some example data:
g <- runif(45)
w <- runif(45)
d <- runif(45)
e <- sample(0:1, 45, T)
G <- sym_matrix(g) # e.g. a genetic matrix
W <- sym_matrix(w) # e.g. a wind matrix
D <- sym_matrix(d) # e.g. a distance matrix
E <- sym_matrix(e) # e.g. an environment matrix

# Basic function usage:
mantel_test(G, W)                      # simple first-order Mantel
mantel_test(G, W, list(D, E))          # partial Mantel
mantel_test(G, W, method = "kendall")  # use of alternative test statistic

# Demonstrate that the function matches vegan::mantel.partial output:
# (using only symmetric matrices, and just one control variable, since
# vegan does not work with asymmetric matrices or multiple controls)
a <- mantel_test(G, W, list(D), nperm = 99999)
b <- vegan::mantel.partial(G, W, D, permutations = 99999)
c(a$stat, b$statistic)
c(1 - a$quantile, b$signif) # will differ slightly due to randomization

matthewkling/windscape documentation built on Sept. 26, 2024, 4:22 p.m.