Learn R Programming

evolqg (version 0.3-4)

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)

Value

returns a matrix of species-species distances.

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.

Author

Diogo Melo

References

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

See Also

Examples

Run this code
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)

if (FALSE) {
#Multiple threads can be used with some foreach backend library, like doMC or doParallel
library(doMC)
registerDoMC(cores = 2)
MultiMahalanobis(mean.list, RandomMatrix(10), parallel = TRUE)
}

Run the code above in your browser using DataLab