1
$\begingroup$

I have a question when calculating Mahalanobis Distance using R.

For the Mahalanobis distance function below:

mahalanobis(x, **center**, cov, inverted = FALSE, ...)

The center in the function above should be the mean vector of the distribution (as default). It will give me the Mahalanobis distance between each case and the mean of the distribution.

I am interested in the Mahalanobis distance between each case and the origin. My question is, can I change the center (in the function above) to 0? I feel this is not quite right but not sure. Thanks!

To make it more understandable, here is an example:

This is how I normally calculate Mahalanobis distance:

mahalanobis(x =df, center = colMeans(df), cov = cov(df))

I would like to know the Mahalanobis distance between each case and the origin. I am not sure if I can do:

mahalanobis(x = df, center = 0 , cov = cov(df))

Please let me know if it is not right! Appreciated!

$\endgroup$
0

1 Answer 1

2
$\begingroup$

From the documentation

Returns the squared Mahalanobis distance of all rows in $x$ and the vector $\mu$ = center with respect to $\Sigma$ = cov. This is (for vector $x$) defined as $$D^2 = (x - \mu)' \Sigma^{-1} (x - \mu)$$

Setting center=0 computes the squared distance from the origin.

$\endgroup$
1
  • $\begingroup$ While the question is interested in R, I think we can say the same of scipy.spatial.distance.mahalanobis where we assign v=0 instead of center=0. Here is a Python gist performing this calculation. An important implementation difference is that this Python module requires VI to be passed as the inverse of the covariance matrix, rather than passing the covariance matrix itself. $\endgroup$
    – Galen
    Oct 19, 2022 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.