I need to calculate the mahalanobis distance for a numerical dataset of 500 independent observations grouped in 12 groups (species). I know how to compare two matrices , but I do not understand how to calculate mahalanobis distance from my dataset i.e. between the 12 species. R documentation gives
mahalanobis(x, center, cov, inverted = FALSE, ...)
x is the matrix, cov is covariance matrix (cov(x)
)
but I do not understand how I can calculate the metric for the 12 groups
I found this question on mahalanobis but it does not answer really my question
Getting the distances is straigtforward if you organize your data in a 500 by 12
data.frame
ormatrix
. To show you, first we create adata.frame
with some toy data:where you have the 12 species called A-L. Organized in this way, you simply run the following line:
Remember, the function returns the square of the distances!
I hope this helps.