[R] Help: Mahalanobis distances between 'Species' from iris

Jose Claudio Faria joseclaudio.faria at terra.com.br
Wed Jul 6 23:28:37 CEST 2005


Dear R list,

I'm trying to calculate Mahalanobis distances for 'Species' of 'iris' data
as obtained below:

Squared Distance to Species From Species:

               Setosa Versicolor Virginica
Setosa 	           0   89.86419 179.38471
Versicolor  89.86419          0  17.20107
Virginica  179.38471   17.20107         0

This distances above were obtained with proc 'CANDISC' of SAS, please,
see Output 21.1.2: Iris Data: Squared Mahalanobis Distances from
http://www.id.unizh.ch/software/unix/statmath/sas/sasdoc/stat/chap21/sect19.htm

 From this distance my intention is to make a cluster analysis as below, using
the package 'mclust':

#
# --- Begin R script ---
#
# For units compatibility of 'iris' from R dataset and 'iris' data used in
# the SAS example:
Measures = iris[,1:4]*10
Species  = iris[,5]
irisSAS  = data.frame(Measures, Species)

n   = 3
Mah = c(        0,
          89.86419,        0,
         179.38471, 17.20107, 0)

# My Question is: how to obtain 'Mah' with R from 'irisSAS' data?

D = matrix(0, n, n)

nam = c('Set', 'Ver', 'Vir')
rownames(D) = nam
colnames(D) = nam

k = 0
for (i in 1:n) {
    for (j in 1:i) {
       k      = k+1
       D[i,j] = Mah[k]
       D[j,i] = Mah[k]
    }
}

D=sqrt(D) #D2 -> D

library(mclust)
dendroS = hclust(as.dist(D), method='single')
dendroC = hclust(as.dist(D), method='complete')

win.graph(w = 3.5, h = 6)
split.screen(c(2, 1))
screen(1)
plot(dendroS, main='Single', sub='', xlab='', ylab='', col='blue')

screen(2)
plot(dendroC, main='Complete', sub='', xlab='', col='red')
#
# --- End R script ---
#

I always need of this type of analysis and I'm not founding how to make it in 
the CRAN documentation (Archives, packages: mclust, cluster, fpc and mva).

Regards,
-- 
Jose Claudio Faria
Brasil/Bahia/UESC/DCET
Estatistica Experimental/Prof. Adjunto
mails:
  joseclaudio.faria at terra.com.br




More information about the R-help mailing list