Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

MultiMahalanobis

Calculate Mahalonabis distance for many vectors


Description

Calculates the Mahalanobis distance between a list of species mean, using a global covariance matrix

Usage

MultiMahalanobis(means, cov.matrix, parallel = FALSE)

Arguments

means

list or array of species means being compared. array must have means in the rows.

cov.matrix

a single covariance matrix defining the scale (or metric tensor) to be used in the distance calculation.

parallel

if TRUE computations are done in parallel. Some foreach backend must be registered, like doParallel or doMC.

Value

returns a matrix of species-species distances.

Author(s)

Diogo Melo

References

http://en.wikipedia.org/wiki/Mahalanobis_distance

See Also

Examples

mean.1 <- colMeans(matrix(rnorm(30*10), 30, 10))
mean.2 <- colMeans(matrix(rnorm(30*10), 30, 10))
mean.3 <- colMeans(matrix(rnorm(30*10), 30, 10))
mean.list <- list(mean.1, mean.2, mean.3)

# If cov.matrix is the identity, calculated distance is euclidian:
euclidian <- MultiMahalanobis(mean.list, diag(10))
# Using a matrix with half the variance will give twice the distance between each mean:
half.euclidian  <- MultiMahalanobis(mean.list, diag(10)/2) 

# Other covariance matrices will give different distances, measured in the scale of the matrix
non.euclidian <- MultiMahalanobis(mean.list, RandomMatrix(10))

#Input can be an array with means in each row
mean.array = array(1:36, c(9, 4))
mat = RandomMatrix(4)
MultiMahalanobis(mean.array, mat)

#Multiple threads can be used with some foreach backend library, like doMC or doParallel
#library(doParallel)
##Windows:
#cl <- makeCluster(2)
#registerDoParallel(cl)
##Mac and Linux:
#registerDoParallel(cores = 2)
#MultiMahalanobis(mean.list, RandomMatrix(10), parallel = TRUE)

evolqg

Tools for Evolutionary Quantitative Genetics

v0.2-8
MIT + file LICENSE
Authors
Ana Paula Assis, Diogo Melo, Edgar Zanella, Fabio Andrade Machado, Guilherme Garcia
Initial release
2020-11-14

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.