The CLUSTER Procedure

Example 34.3 Cluster Analysis of Fisher’s Iris Data

(View the complete code for this example.)

The iris data published by Fisher (1936) have been widely used for examples in discriminant analysis and cluster analysis. The sepal length, sepal width, petal length, and petal width are measured in millimeters on 50 iris specimens from each of three species, Iris setosa, I. versicolor, and I. virginica. Mezzich and Solomon (1980) discuss a variety of cluster analyses of the iris data. The following statements create the data set Iris:

proc format;
   value specname
      1='Setosa    '
      2='Versicolor'
      3='Virginica ';
run;

data iris;
   title 'Fisher (1936) Iris Data';
   input SepalLength SepalWidth PetalLength PetalWidth Species @@;
   format Species specname.;
   label SepalLength='Sepal Length in mm.'
         SepalWidth ='Sepal Width in mm.'
         PetalLength='Petal Length in mm.'
         PetalWidth ='Petal Width in mm.';
   datalines;
50 33 14 02 1 64 28 56 22 3 65 28 46 15 2 67 31 56 24 3
63 28 51 15 3 46 34 14 03 1 69 31 51 23 3 62 22 45 15 2
59 32 48 18 2 46 36 10 02 1 61 30 46 14 2 60 27 51 16 2
65 30 52 20 3 56 25 39 11 2 65 30 55 18 3 58 27 51 19 3
68 32 59 23 3 51 33 17 05 1 57 28 45 13 2 62 34 54 23 3
77 38 67 22 3 63 33 47 16 2 67 33 57 25 3 76 30 66 21 3
49 25 45 17 3 55 35 13 02 1 67 30 52 23 3 70 32 47 14 2

   ... more lines ...   

55 23 40 13 2 66 30 44 14 2 68 28 48 14 2 54 34 17 02 1
51 37 15 04 1 52 35 15 02 1 58 28 51 24 3 67 30 50 17 2
63 33 60 25 3 53 37 15 02 1
;

This example analyzes the iris data by using Ward’s method and two-stage density linkage and then illustrates how the FASTCLUS procedure can be used in combination with PROC CLUSTER to analyze large data sets.

The following macro, SHOW, is used in the subsequent analyses to display cluster results. It invokes the FREQ procedure to crosstabulate clusters and species. The CANDISC procedure computes canonical variables for discriminating among the clusters, and the first two canonical variables are plotted to show cluster membership. See Chapter 31: The CANDISC Procedure, for a canonical discriminant analysis of the iris species.

/*--- Define macro show ---*/
%macro show;
   proc freq;
      tables cluster*species / nopercent norow nocol plot=none;
   run;

   proc candisc noprint out=can;
      class cluster;
      var petal: sepal:;
   run;

   proc sgplot data=can;
      scatter y=can2 x=can1 / group=cluster;
   run;
%mend;

The first analysis clusters the iris data by using Ward’s method (see Output 34.3.1) and plots the CCC and pseudo F and statistics (see Output 34.3.2). The CCC has a local peak at three clusters but a higher peak at five clusters. The pseudo F statistic indicates three clusters, while the pseudo statistic suggests three or six clusters.

The TREE procedure creates an output data set containing the three-cluster partition for use by the SHOW macro. The FREQ procedure reveals 16 misclassifications. The results are shown in Output 34.3.3.

title2 'By Ward''s Method';
ods graphics on;

proc cluster data=iris method=ward print=15 ccc pseudo;
   var petal: sepal:;
   copy species;
run;

proc tree noprint ncl=3 out=out;
   copy petal: sepal: species;
run;

%show;

Output 34.3.1: Cluster Analysis of Fisher’s Iris Data: PROC CLUSTER with METHOD=WARD

Fisher (1936) Iris Data
By Ward's Method

The CLUSTER Procedure
Ward's Minimum Variance Cluster Analysis

Eigenvalues of the Covariance Matrix
 EigenvalueDifferenceProportionCumulative
1422.824171398.5570960.92460.9246
224.26707516.4461250.05310.9777
37.8209505.4374410.01710.9948
42.383509 0.00521.0000

Root-Mean-Square Total-Sample Standard Deviation10.69224

Root-Mean-Square Distance Between Observations30.24221

Cluster History
Number
of
Clusters
Clusters JoinedFreqSemipartial
R-Square
R-SquareApproximate
Expected
R-Square
Cubic
Clustering
Criterion
Pseudo F
Statistic
Pseudo
t-Squared
Tie
15CL24CL28150.0016.971.9585.933249.8 
14CL21CL5370.0019.969.9555.853295.1 
13CL18CL48150.0023.967.9535.693348.9 
12CL16CL23240.0023.965.9504.633429.6 
11CL14CL43120.0025.962.9464.673535.8 
10CL26CL20220.0027.959.9424.8136812.9 
9CL27CL17310.0031.956.9365.0238717.8 
8CL35CL15230.0031.953.9305.4441413.8 
7CL10CL47260.0058.947.9215.4343019.1 
6CL8CL13380.0060.941.9115.8146316.3 
5CL9CL19500.0105.931.8955.8248843.2 
4CL12CL11360.0172.914.8723.9951541.0 
3CL6CL7640.0301.884.8274.3355857.2 
2CL4CL31000.1110.773.6973.83503116 
1CL5CL21500.7726.000.0000.00.503 


Output 34.3.2: Criteria for the Number of Clusters with METHOD=WARD

Criteria for the Number of Clusters with METHOD=WARD


Output 34.3.3: Crosstabulation of Clusters for METHOD=WARD

Fisher (1936) Iris Data
By Ward's Method

The FREQ Procedure

Frequency
Table of CLUSTER by Species
CLUSTERSpecies
SetosaVersicolorVirginicaTotal
1
0
49
15
64
2
0
1
35
36
3
50
0
0
50
Total
50
50
50
150


Output 34.3.4: Scatter Plot of Clusters for METHOD=WARD

Scatter Plot of Clusters for METHOD=WARD


The second analysis uses two-stage density linkage. The raw data suggest two or six modes instead of three:

k

 

Modes

3  

 

12

4-6  

 

6

7  

 

4

8  

 

3

9-50  

 

2

51+

 

1

The following analysis uses K=8 to produce three clusters for comparison with other analyses. There are only six misclassifications. The results are shown in Output 34.3.5 and Output 34.3.6.

title2 'By Two-Stage Density Linkage';

proc cluster data=iris method=twostage k=8 print=15 ccc pseudo;
   var petal: sepal:;
   copy species;
run;

proc tree noprint ncl=3 out=out;
   copy petal: sepal: species;
run;

%show;

Output 34.3.5: Cluster Analysis of Fisher’s Iris Data: PROC CLUSTER with METHOD=TWOSTAGE

Fisher (1936) Iris Data
By Two-Stage Density Linkage

The CLUSTER Procedure
Two-Stage Density Linkage Clustering

Eigenvalues of the Covariance Matrix
 EigenvalueDifferenceProportionCumulative
1422.824171398.5570960.92460.9246
224.26707516.4461250.05310.9777
37.8209505.4374410.01710.9948
42.383509 0.00521.0000


K = 8

Root-Mean-Square Total-Sample Standard Deviation10.69224

Cluster History
Number
of
Clusters
 FreqSemipartial
R-Square
R-SquareApproximate
Expected
R-Square
Cubic
Clustering
Criterion
Pseudo F
Statistic
Pseudo
t-Squared
Normalized
Fusion Density
Maximum Density
in Each Cluster
Tie
Clusters JoinedLesserGreater
15CL17OB127430.0024.917.958-111073.40.39030.20663.5156 
14CL16OB137500.0023.915.955-101135.60.36370.1837100.0 
13CL15OB74440.0029.912.953-9.81193.80.35530.21303.5156 
12CL22OB49470.0036.909.950-7.71255.20.32230.17368.3678T
11CL12OB85480.0036.905.946-7.41324.80.32230.17368.3678 
10CL11OB98490.0033.902.942-6.81434.10.28790.14798.3678 
9CL13OB24450.0036.898.936-6.21554.50.28020.20053.5156 
8CL10OB25500.0019.896.930-5.21752.20.26990.13728.3678 
7CL8OB121510.0035.893.921-4.21984.00.25860.13728.3678 
6CL9OB45460.0041.888.911-3.02294.70.14120.08323.5156 
5CL6OB39470.0048.884.895-1.52765.10.1070.06053.5156 
4CL5OB21480.0048.879.8720.543534.70.09690.05413.5156 
3CL4OB90490.0046.874.8273.495114.20.07150.03703.5156 
2CL7CL31000.1017.773.6973.8350396.32.62773.51568.3678 


3 modal clusters have been formed.


Output 34.3.6: Criteria for the Number of Clusters with METHOD=TWOSTAGE

Criteria for the Number of Clusters with METHOD=TWOSTAGE


Output 34.3.7: Crosstabulation of Clusters for METHOD=TWOSTAGE

Fisher (1936) Iris Data
By Two-Stage Density Linkage

The FREQ Procedure

Frequency
Table of CLUSTER by Species
CLUSTERSpecies
SetosaVersicolorVirginicaTotal
1
50
0
0
50
2
0
48
3
51
3
0
2
47
49
Total
50
50
50
150


Output 34.3.8: Scatter Plot of Clusters for METHOD=TWOSTAGE

Scatter Plot of Clusters for METHOD=TWOSTAGE


The CLUSTER procedure is not practical for very large data sets because, with most methods, the CPU time is roughly proportional to the square or cube of the number of observations. The FASTCLUS procedure requires time proportional to the number of observations and can therefore be used with much larger data sets than PROC CLUSTER. If you want to hierarchically cluster a very large data set, you can use PROC FASTCLUS for a preliminary cluster analysis to produce a large number of clusters and then use PROC CLUSTER to hierarchically cluster the preliminary clusters.

FASTCLUS automatically creates the variables _FREQ_ and _RMSSTD_ in the MEAN= output data set. These variables are then automatically used by PROC CLUSTER in the computation of various statistics.

The following SAS code uses the iris data to illustrate the process of clustering clusters. In the preliminary analysis, PROC FASTCLUS produces ten clusters, which are then crosstabulated with species. The data set containing the preliminary clusters is sorted in preparation for later merges. The results are shown in Output 34.3.9 and Output 34.3.10.

title2 'Preliminary Analysis by FASTCLUS';
proc fastclus data=iris summary maxc=10 maxiter=99 converge=0
              mean=mean out=prelim cluster=preclus;
   var petal: sepal:;
run;

proc freq;
   tables preclus*species / nopercent norow nocol plot=none;
run;

proc sort data=prelim;
   by preclus;
run;

Output 34.3.9: Preliminary Analysis of Fisher’s Iris Data: FASTCLUS Procedure

Fisher (1936) Iris Data
Preliminary Analysis by FASTCLUS

The FASTCLUS Procedure
Replace=FULL Radius=0 Maxclusters=10 Maxiter=99 Converge=0

Convergence criterion is satisfied.

Criterion Based on Final Seeds =2.1389

Cluster Summary
ClusterFrequencyRMS Std DeviationMaximum Distance
from Seed
to Observation
Radius
Exceeded
Nearest ClusterDistance Between
Cluster Centroids
192.70678.2027 58.7362
2192.20017.7340 46.2243
3182.14966.2173 87.5049
442.52495.3268 26.2243
532.72345.8214 18.7362
672.29395.1508 29.3318
7172.02746.9576 107.9503
8182.26287.1135 37.5049
9222.26667.5029 89.0090
10332.059410.0033 77.9503

Pseudo F Statistic =370.58

Observed Over-All R-Squared =0.95971

Approximate Expected Over-All R-Squared =0.82928

Cubic Clustering Criterion =27.077


WARNING: The two values above are invalid for correlated variables.


Output 34.3.10: Crosstabulation of Species and Cluster From the FASTCLUS Procedure

Fisher (1936) Iris Data
Preliminary Analysis by FASTCLUS

The FREQ Procedure

Frequency
Table of preclus by Species
preclus(Cluster)Species
SetosaVersicolorVirginicaTotal
1
0
0
9
9
2
0
19
0
19
3
0
18
0
18
4
0
3
1
4
5
0
0
3
3
6
0
7
0
7
7
17
0
0
17
8
0
3
15
18
9
0
0
22
22
10
33
0
0
33
Total
50
50
50
150


The following macro, CLUS, clusters the preliminary clusters. There is one argument to choose the METHOD= specification to be used by PROC CLUSTER. The TREE procedure creates an output data set containing the three-cluster partition, which is sorted and merged with the OUT= data set from PROC FASTCLUS to determine which cluster each of the original 150 observations belongs to. The SHOW macro is then used to display the results. In this example, the CLUS macro is invoked using Ward’s method, which produces 16 misclassifications, and Wong’s hybrid method, which produces 22 misclassifications.

/*--- Define macro clus ---*/
%macro clus(method);
   proc cluster data=mean method=&method ccc pseudo;
      var petal: sepal:;
      copy preclus;
   run;

   proc tree noprint ncl=3 out=out;
      copy petal: sepal: preclus;
   run;

   proc sort data=out;
      by preclus;
   run;

   data clus;
      merge out prelim;
      by preclus;
   run;

   %show;
%mend;

The following statements produce Output 34.3.11 through Output 34.3.14.

title2 'Clustering Clusters by Ward''s Method';
%clus(ward);

Output 34.3.11: Clustering Clusters by Ward’s Method

Fisher (1936) Iris Data
Clustering Clusters by Ward's Method

The CLUSTER Procedure
Ward's Minimum Variance Cluster Analysis

Eigenvalues of the Covariance Matrix
 EigenvalueDifferenceProportionCumulative
1416.976349398.6664210.95010.9501
218.30992814.9529220.04170.9918
33.3570063.1269430.00760.9995
40.230063 0.00051.0000

Root-Mean-Square Total-Sample Standard Deviation10.69224

Root-Mean-Square Distance Between Observations30.24221

Cluster History
Number
of
Clusters
Clusters JoinedFreqSemipartial
R-Square
R-SquareApproximate
Expected
R-Square
Cubic
Clustering
Criterion
Pseudo F
Statistic
Pseudo
t-Squared
Tie
9OB2OB4230.0019.958.9326.264006.3 
8OB1OB5120.0025.955.9266.754345.8 
7CL9OB6300.0069.948.9186.2843819.5 
6OB3OB8360.0074.941.9076.2145926.0 
5OB7OB10500.0104.931.8926.1548542.2 
4CL8OB9340.0162.914.8704.2851939.3 
3CL7CL6660.0318.883.8244.3955259.7 
2CL4CL31000.1099.773.6953.94503113 
1CL2CL51500.7726.000.0000.00.503 


Output 34.3.12: Criteria for the Number of Clusters for Clustering Clusters from Ward’s Method

Criteria for the Number of Clusters for Clustering Clusters from Ward’s Method


Output 34.3.13: Crosstabulation for Clustering Clusters from Ward’s Method

Fisher (1936) Iris Data
Clustering Clusters by Ward's Method

The FREQ Procedure

Frequency
Table of CLUSTER by Species
CLUSTERSpecies
SetosaVersicolorVirginicaTotal
1
0
50
16
66
2
0
0
34
34
3
50
0
0
50
Total
50
50
50
150


Output 34.3.14: Scatter Plot for Clustering Clusters using Ward’s Method

Scatter Plot for Clustering Clusters using Ward’s Method


The following statements produce Output 34.3.15 through Output 34.3.17.

title2 "Clustering Clusters by Wong's Hybrid Method";
%clus(twostage hybrid);

Output 34.3.15: Clustering Clusters by Wong’s Hybrid Method

Fisher (1936) Iris Data
Clustering Clusters by Wong's Hybrid Method

The CLUSTER Procedure
Two-Stage Density Linkage Clustering

Eigenvalues of the Covariance Matrix
 EigenvalueDifferenceProportionCumulative
1416.976349398.6664210.95010.9501
218.30992814.9529220.04170.9918
33.3570063.1269430.00760.9995
40.230063 0.00051.0000


 

Root-Mean-Square Total-Sample Standard Deviation10.69224

Cluster History
Number
of
Clusters
 FreqSemipartial
R-Square
R-SquareApproximate
Expected
R-Square
Cubic
Clustering
Criterion
Pseudo F
Statistic
Pseudo
t-Squared
Normalized
Fusion Density
Maximum Density
in Each Cluster
Tie
Clusters JoinedLesserGreater
9OB10OB7500.0104.949.9323.8133042.240.2458.2179100.0 
8OB3OB8360.0074.942.9263.2232926.027.98139.451148.4350 
7OB2OB4230.0019.940.9184.243736.323.7758.967546.3026 
6CL8OB9580.0194.921.9072.1333446.320.72446.884648.4350 
5CL7OB6300.0069.914.8923.0938319.513.30317.636046.3026 
4CL6OB1670.0292.884.8701.2137241.08.413710.875848.4350 
3CL4OB5700.0138.871.8243.3349412.35.18556.289048.4350 
2CL3CL51000.0979.773.6953.9450389.519.51346.302648.4350 
1CL2CL91500.7726.000.0000.00.5031.333748.4350100.0 


3 modal clusters have been formed.


Output 34.3.16: Crosstabulation for Clustering Clusters from Wong’s Hybrid Method

Fisher (1936) Iris Data
Clustering Clusters by Wong's Hybrid Method

The FREQ Procedure

Frequency
Table of CLUSTER by Species
CLUSTERSpecies
SetosaVersicolorVirginicaTotal
1
50
0
0
50
2
0
21
49
70
3
0
29
1
30
Total
50
50
50
150


Output 34.3.17: Scatter Plot for Clustering Clusters using Wong’s Hybrid Method

Scatter Plot for Clustering Clusters using Wong’s Hybrid Method