1

I am trying to find the Mahalanobis Distance between the different species in the iris dataset in R. I was able to find the distance between setosa and versicolor by the following code:

library(HDMD)

#To get Mahalanobis distances between Setosa and Versicolor,
set.vers<-pairwise.mahalanobis(x=iris[1:100,1:4], grouping=iris[1:100,]$Species)
md= sqrt(set.vers$distance)

However, I am struggling to do the same for setosa and virginica. I am not sure how to select the first 50 rows and last 50 rows of the data set (i.e. not have any versicolor data)

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
daisybeats
  • 217
  • 1
  • 6

2 Answers2

2

This is a basic subsetting question. You want to subset based on Species, something along the lines of (not tested)

ss <- iris[iris$Species %in% c("Setosa", "Virginica"), ]
pairwise.mahalanobis(x = ss, grouping = ss$Species)

You can of course change the species pair you want to compare in many ways.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • When I use that, is says that ss has 0 rows. the following error appears '[1] Sepal.Length Sepal.Width Petal.Length Petal.Width Species <0 rows> (or 0-length row.names)' – daisybeats Sep 24 '20 at 10:14
  • @daisybeats The error comes from upper cases in `"Setosa"` and `"Virginica"`. Once corrected, in the second line it must be `x = ss[-5]` or `x = ss[1:4]`. And `grouping = droplevels(ss$Species)` would avoid the warning message. – Rui Barradas Sep 24 '20 at 15:08
  • 1
    @RuiBarradas you're right, calculating the distance on non-numeric Species column is a bit harder to do. – Roman Luštrik Sep 26 '20 at 16:25
1

Here is a way to get all combinations of levels in iris$Species with combn and compute the Mahalanobis distances.

library(HDMD)

inx <- sapply(levels(iris$Species), function(l) which(iris$Species == l), simplify = FALSE)
inx <- combn(inx, 2, function(x) unlist(x), simplify = FALSE)
set.vers_all <- lapply(inx, function(i) {
  pairwise.mahalanobis(x = iris[i, 1:4], grouping = droplevels(iris$Species[i]))
})
set.vers_all
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66