Mahalanobis Distance in R #2

Tags:

Continuing from the previous thread,

Let’s compute mahalanobis distance using the equation of it instead of using mahalanobis function:

> mx = data.matrix(x)
> mx
     x  y z
[1,] 1 12 5
[2,] 0 14 7
[3,] 8  0 6
[4,] 2  2 3
[5,] 5  1 8
> d = mx[1,] – mx[2,]
> d
 x  y  z
 1 -2 -2
> t(d)
     x  y  z
[1,] 1 -2 -2
> t(d) %*% solve(cov(mx)) %*% d
         [,1]
[1,] 1.750912

We can compute the distance between two means of two groups using pooled covariance matrix in mahalanobis distance as well.