help for clustergram Matthias Schonlau

Title

clustergram -- Graph for visualizing hierarchical and non-hierarchical cluster analyses

Syntax

clustergram varlist [if] [in] , [ options ]

cluster(varlist) Variable list with the cluster assignments

fraction(real) Increase/decrease thickness of graph elements

color(str) Specify color of graph elements

graph_options Specify additional options passed to graph, twoway

Description

clustergram draws a graph to examine how cluster members are assigned
to clusters as the number of clusters increases in a cluster analysis. This is
similar in spirit to the dendrograms (tree graphs) used for hierarchical
cluster analyses. The graph is useful for non-hierarchical
clustering algorithms, such as k-means, and for hierarchical cluster
algorithms, including when the number of observations is too large for dendrograms to be
practical.

The width of the graph boxes is proportional to the number of observations in it.
Width is defined here as vertical width, i.e. the difference between the upper and lower y-value.

varlist usually contains the variables with which the cluster algorithm was
run. These variables are only used to compute the value of the vertical axis for each
cluster. The vertical axis gives the cluster means, where the cluster means are computed over
all cluster variables and over all observations in a given cluster.

It is also possible to specify a single variable to examine the
cluster assignments w.r.t that variable. It is also possible to specify a variable
that was not among the variables that was used for the cluster analysis.

Options

cluster specifies the variables
containing cluster assignments.
The examples below show how to generate such variables using cluster.
Typically, the cluster variables will be named something like cluster1-clustermax,
where max is the maximum number of clusters identified.
If so, variable cluster1 will contain 1 for all observations to indicate the single cluster.
Variable cluster2 will contain 1's and 2's
depending on whether the observation belongs to cluster 1 or cluster 2. This option is required.

fraction specifies a fudge factor controlling the
width of graph boxes. This fudge factor is applied to all graph boxes.
This option is specified as needed to reduce visual clutter.
The value should be between 0 and 1. The default is 0.2.

color specifies the color for the box elements.
Color names are explained in colorstyle.
By default, the color is black.

graph_options are options of graph, twoway other than
symbol() and connect().

Examples

Fisher's Iris data

Plot the clustergram for up to 5 clusters for Fisher's Iris data.
We would usually standardize the Iris variables but omit this here for brevity.

. use https://www.stata-press.com/data/r17/iris, clear

. set seed 10

. local max=5

. foreach i of numlist 1/`max' {

. cluster kmeans seplen-petwid, k(`i') L2 name("cluster`i'")

. }

. clustergram seplen-petwid, cluster(cluster1-cluster`max')

(click to run)

Changing color (with 50% translucency) and specifying narrower width to reduce visual clutter:

. clustergram seplen-petwid, cluster(cluster1-cluster`max'), color(blue%50) frac(.1)

(click to run)

Instead of displaying the cluster mean over all variables, we only display the cluster mean for petal length.
This shows that the first split results into a cluster with large and a cluster with small petal length.

. clustergram petlen, cluster(cluster1-cluster`max') ytitle("Average Petal Length")

(click to run)

Women's club data

The women's club data consists of 35 indicator variables (which take values 0 and 1) about women's interests.
The purpose was to seat women with common interest at the same lunch table.
Using kmeans, we find that early on one cluster forms where all women are interested in fiction books and
later on a cluster where no women is interested in fiction books.

. use https://www.stata-press.com/data/r17/wclub.dta, clear

. set seed 10

. local max=5

. foreach i of numlist 1/`max' {

. cluster kmeans bike-fish, k(`i') L2 name("cluster`i'")

. }

. clustergram fict, cluster(cluster1-cluster`max'), ytitle(Av. Fiction Books)

(click to run)

References

Schonlau, M. The clustergram: a graph for visualizing hierarchical and non-hierarchical cluster analyses.
The Stata Journal, 2002; 2 (4):391-402.

Schonlau, M. Visualizing Hierarchical and Non-Hierarchical
Cluster Analyses with Clustergrams. Computational Statistics. 2004; 19(1): 95-111.

Author

Matthias Schonlau, University of Waterloo

schonlau at uwaterloo dot ca

www.schonlau.net

Also see

help for cluster, hammock plots (click to install from SSC)