How to calculate mahalanobis distance in R?

The Mahalanobis distance is the relative distance between two cases and the centroid, where centroid can be thought of as an overall mean for multivariate data. We can say that the centroid is the multivariate equivalent of mean. If the mahalanobis distance is zero that means both the cases are very same and positive value of mahalanobis distance represents that the distance between the two variables is large. In R, we can use mahalanobis function to find the malanobis distance.

Example1

y1<−rpois(20,1)
y2<−rpois(20,3)
y3<−rpois(20,5)
y4<−rpois(20,8)
y5<−rpois(20,12)
y6<−rpois(20,10)
df2<−data.frame(y1,y2,y3,y4,y5,y6)
df2

Output

y1 y2 y3 y4 y5 y6
1 0 2 4 6 11 10
2 1 6 7 4 9 9
3 1 1 6 13 14 11
4 3 3 9 9 16 9
5 2 3 6 10 9 13
6 0 6 7 13 14 13
7 2 2 7 4 15 7
8 0 2 4 8 14 10
9 2 7 3 7 6 12
10 0 2 6 10 10 9
11 0 5 5 10 8 6
12 2 3 5 7 11 9
13 0 5 3 6 9 7
14 0 2 6 3 13 7
15 1 1 7 10 9 9
16 0 3 3 8 12 11
17 0 3 4 5 13 13
18 1 2 6 14 13 8
19 1 2 4 10 8 7
20 1 5 11 13 12 16

mahalanobis(df2,colMeans(df2),cov(df2))

[1] 2.588021 6.383910 4.101547 8.860628 5.248206 8.669764 6.332766
[8] 3.065049 10.556830 2.882808 6.945220 2.333995 4.171714 5.990775
[15] 5.921976 3.198976 5.971216 5.382210 4.167775 11.226611

Example3

z1<−runif(20,1,2)
z2<−runif(20,1,4)
z3<−runif(20,1,5)
z4<−runif(20,2,5)
z5<−runif(20,5,10)
df3<−data.frame(z1,z2,z3,z4,z5)
df3

Output

      z1       z2       z3       z4       z5
1 1.388613 3.591918 4.950430 3.012227 7.646999
2 1.536406 2.346386 4.009326 3.344235 6.804723
3 1.307832 2.156929 1.548907 3.719957 9.647134
4 1.452674 3.659639 4.067904 2.821600 9.042116
5 1.821635 1.581077 1.848880 2.133112 8.606968
6 1.472712 1.853850 2.757099 4.971375 8.195671
7 1.129696 1.007614 3.454963 4.500837 9.512772
8 1.084507 3.509503 3.972340 2.557956 5.070359
9 1.066166 3.487398 3.235659 2.692450 8.566473
10 1.622298 3.285975 3.214168 2.816199 6.811145
11 1.215978 2.695426 4.459403 3.883969 7.015267
12 1.748907 1.855413 1.100227 3.676822 8.668907
13 1.785502 3.365582 1.089094 2.232694 6.207582
14 1.313907 1.010318 2.040431 3.337156 6.281897
15 1.211392 2.821926 3.427129 4.835524 8.469758
16 1.127482 1.589360 4.105524 4.575452 7.425941
17 1.914011 1.015687 1.900738 2.542681 8.710688
18 1.156077 1.237109 1.667345 4.654083 6.764100
19 1.770988 3.685755 4.417545 4.637382 6.155797
20 1.594745 3.750948 1.394754 4.548843 9.902893
mahalanobis(df3,colMeans(df3),cov(df3))
[1] 3.680650 2.011037 3.520353 4.338257 5.095421 2.698317 5.394089 7.190855
[9] 6.030547 1.608436 1.705612 2.770687 7.343208 4.676116 2.461363 3.186534
[17] 6.758622 6.152332 9.599646 8.777917
Updated on: 2026-03-11T23:26:53+05:30

753 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements