# Homogeneity analysis of hierarchical classifications

**Last Resort Software**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve spent more years than I care to remember analysing vegetation survey data (typically species abundances in plots) using a variety of software including my own algorithms coded in FORTRAN and C++. A recent query on the r-help list, about how to determine the number of groups to define in a hierarchical classification produced with the hclust function, prompted me to unearth one of these algorithms, homogeneity analysis^{1}, which can help to visualize how different levels of grouping partition the variability in a distance matrix.

This algorithm is extremely simple. The classification is progressively divided into groups, with all groups being defined at the same dendrogram height. At each level of grouping, the average of within-group pairwise distances is calculated. Homogeneity is then defined as:

H = 1 - Davwithin-group - Davtotal

where Dav_{total} is the average pairwise distance in the dataset as a whole.

For data were there exist well-defined clusters of values, a plot of homogeneity against number of groups will display an ‘elbow’ where the initial rapid increase in homogeneity turns to a more gradual increase. The example above shows a classification of the USArrests dataset and corresponding homogeneity plot which suggests defining 7 groups. It was generated as follows:

<br />> d <- dist(USArrests)<br />> hc <- hclust(d, method="average")<br />> h <- hmg(d, hc)<br />> plot(h, type="s", main="USArrests homogeneity")<br />> abline(v=7, lty=2)<br />> plot(hc, labels=FALSE, main="USArrests dendrogram")<br />> rect.hclust(hc, k=7)<br />

Here is the code for the hmg function:

<br />function(d, hc) {<br /><br /># R version of homogeneity analysis as described in:<br /># Bedward, Pressey and Keith. 1992. Homogeneity analysis: assessing the <br /># utility of classifications and maps of natural resources<br /># Australian Journal of Ecology 17:133-139.<br />#<br /># Arguments:<br /># d - either an object produced by dist() or a vector of<br /># pairwise dissimilarity values ordered in the manner of<br /># a dist result<br />#<br /># hc - classification produced by hclust()<br />#<br /># Value:<br /># A two column matrix of number of groups and corresponding homogeneity value<br />#<br /># This code by Michael Bedward, 2010<br /><br /> m <- hc$merge<br /><br /> # check args for consistency<br /> Nobj <- nrow(m) + 1<br /> if (length(d) != Nobj * (Nobj-1) / 2) {<br /> dname <- dparse(substitute(d))<br /> hcname <- dparse(substitute(hc))<br /> stop(paste(dname, "and", hcname, "refer to different numbers of objects"))<br /> }<br /><br /> distMean <- mean(d)<br /> grp <- matrix(c(1:Nobj, rep(0, Nobj)), ncol=2)<br /><br /> # helper function - decodes values in the merge matrix <br /> # from hclust<br /> getGrp <- function( idx ) {<br /> ifelse( idx < 0, grp[-idx], getGrp( m[idx, 1] ) )<br /> }<br /><br /> grpDistTotal <- numeric(Nobj)<br /> grpDistCount <- numeric(Nobj)<br /> hmg <- numeric(Nobj)<br /> hmg[1] <- 0<br /> hmg[Nobj] <- 1<br /><br /> distTotal <- 0<br /> distCount <- 0<br /> for (step in 1:(Nobj-2)) {<br /> mrec <- m[step, ]<br /> grp1 <- getGrp(mrec[1])<br /> grp2 <- getGrp(mrec[2])<br /><br /> distTotal <- distTotal - grpDistTotal[grp1] - grpDistTotal[grp2]<br /> distCount <- distCount - grpDistCount[grp1] - grpDistCount[grp2]<br /><br /> grpDistTotal[grp1] <- grpDistTotal[grp1] + grpDistTotal[grp2]<br /> grpDistCount[grp1] <- grpDistCount[grp1] + grpDistCount[grp2]<br /><br /> grp1Obj <- which(grp == grp1)<br /> grp2Obj <- which(grp == grp2)<br /><br /> for (i in grp1Obj) {<br /> for (j in grp2Obj) {<br /> ii <- min(i,j)<br /> jj <- max(i,j)<br /> distIdx <- Nobj*(ii-1) - ii*(ii-1)/2 + jj-ii<br /> grpDistTotal[grp1] <- grpDistTotal[grp1] + d[distIdx]<br /> grpDistCount[grp1] <- grpDistCount[grp1] + 1<br /> }<br /> }<br /><br /> # update group vector<br /> grp[grp == grp2] <- grp1<br /><br /> distTotal <- distTotal + grpDistTotal[grp1]<br /> distCount <- distCount + grpDistCount[grp1]<br /><br /> hmg[Nobj - step] <- 1 - distTotal / ( distCount * distMean )<br /> }<br /><br /> out <- matrix(c(1:Nobj, hmg), ncol=2)<br /> colnames(out) <- c("ngroups", "homogeneity")<br /> out<br />}<br />

Reference^{1}M. Bedward, D. A. Keith, R. L. Pressey. 1992. Homogeneity analysis: Assessing the utility of classifications and maps of natural resources. Australian Journal of Ecology 17(2) 133-139.

**leave a comment**for the author, please follow the link and comment on their blog:

**Last Resort Software**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.