Mahalanobis Distance – Mahalanobis Distance Question in R

mahalanobisrregression

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!

Best Answer

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.