# [R] Mahalanobis Distance

David Cross d.cross at tcu.edu
Mon Sep 26 21:55:12 CEST 2011

```When I first saw your question, I thought the problem might have something to do with inverting the variance-covariance matrix, S, but that is not the case, I think:

S for s.1 and s.2:

> S
[,1]         [,2]
[1,] 1.835044e+01 8.392485e-04
[2,] 8.392485e-04 4.093558e-07

inverse(S):

> solve(S)
[,1]         [,2]
[1,]    0.06013287    -123.2825
[2,] -123.28254430 2695612.8008

So, I am not sure what the difficulty is with your calculations.  However, I wonder how much value there is to computing the Mahalanobis distance with two variables that are measured on such different scales?

> summary(s.1)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-5.282  -3.204  -1.225   0.000   2.111   9.453

> summary(s.2)
Min.    1st Qu.     Median       Mean    3rd Qu.       Max.
-1.000e-03 -4.999e-04 -2.203e-06  0.000e+00  4.967e-04  1.008e-03

How would you interpret such a distance?

David Cross
d.cross at tcu.edu
www.davidcross.us

On Sep 26, 2011, at 2:05 PM, jorgeA wrote:

> Hello R helpers,
>
> I'm trying to use Mahalanobis distance to calculate distance of two time
> series, to make some comparations with euclidean distance, DTW, etc, but I'm
> having some dificults.
>
> I have, for example, two objects:
>
> s.1 <- c( 5.6324702,  1.3994353, -3.2572327, -3.8311846, -1.2248719,
> 0.9894694, -2.2835332, -5.1969285, -5.2823988, -3.1499400, -1.7307950,
> 2.8221209,  0.7005370,  4.9601216,  9.4527303)
>
> s.2 <- c(-1.000489e-03, -8.577807e-04, -7.150633e-04, -5.716564e-04,
> -4.280622e-04, -2.860101e-04, -1.451796e-04, -2.202688e-06,  1.441569e-04,
> 2.891237e-04,  4.280430e-04,  5.652797e-04,  7.100960e-04,  8.619236e-04,
> 1.007821e-03)
>
> when I try to calculate distance with *dist *function and *proxy *package
> like this:
>
> library(proxy)
> dist(rbind(s.1, s.2), method="mahalanobis")
>
> I have the following error:
> system is computationally singular: reciprocal condition number =
> 3.84863e-020
>
> if I try with de* mahalanobis() *function I have the same problem
> test <- rbind(s.1, s.2)
> mahalanobis(test, center=colMeans(test), cov=var(test))
>
> And trying with diferent series I have the following error:
> "Lapack routine dgesv: system is exactly singular"
>
> I found some similar errors on the mailing list, but couldn't find some
> useful help for my case.
>
>
> Am I doing something wrong? Isn't it possible to use mahalanobis distance
> with that kind of data?
>
> Thank you very much for your help.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Mahalanobis-Distance-tp3844960p3844960.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

```