The HPCLUS Procedure

Example 5.1 Cluster Analysis

This example uses the iris data set as input to demonstrate how to use PROC HPCLUS to perform cluster analysis. 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.

In this example, the HPLUS procedure is used to find three clusters. When the input data set resides on the client and no PERFORMANCE statement is specified, as in the following example, the client performs all computations:


proc hpclus data=sashelp.iris maxclusters=3 outiter outstat=hpclusOutstat1;
   score out=hpclusOut1;
   input SepalLength SepalWidth PetalLength PetalWidth;
   id SepalLength SepalWidth PetalLength PetalWidth Species;
run;


Figure 7 shows the results from the cluster analysis.

Figure 7: PROC HPCLUS with Three Clusters

Using PROC HPCLUS to Analyze Data

The HPCLUS Procedure

Performance Information
Execution ModeSingle-Machine
Number of Threads16

Data Access Information
DataEngineRolePath
SASHELP.IRISV9InputOn Client
WORK.HPCLUSOUT1V9OutputOn Client
WORK.HPCLUSOUTSTAT1V9OutputOn Client

Model Information
Maximum Iteration10
Stop CriterionCluster Change
Stop Criterion Value0
Clusters3
Seed Initialization12345
DistanceEuclidean

Number of Observations Read150
Number of Observations Used150

Cluster Summary
ClusterFrequencyDistance from Cluster Centroid
to Observation
SSEStandard
Deviation
Nearest
Cluster
Distance
to
Nearest
Cluster
Centroid
MaximumMinimumAverage
16216.60642.19947.38153982.18.0142317.9718
25012.48030.66184.81711515.15.5047133.5693
33815.29712.59587.19842387.97.9272117.9718

Iteration Statistics
Iteration
Number
SSE
075216
119644
29352.340043
38148.103759
47919.714264
57885.144143
67885.144143

Descriptive Statistics
VariableMeanStandard
Deviation
SepalLength58.4333338.280661
SepalWidth30.5733334.358663
PetalLength37.58000017.652982
PetalWidth11.9933337.622377

Within Cluster Statistics
VariableClusterMeanStandard
Deviation
SepalLength159.01614.6641
 250.06003.5249
 368.50004.9416
SepalWidth127.48392.9628
 234.28003.7906
 330.73682.9009
PetalLength143.93555.0889
 214.62001.7366
 357.42114.8859
PetalWidth114.33872.9750
 22.46001.0539
 320.71052.7987


In this example, PROC HPCLUS generates the data set hpclusOut1, which contains the cluster membership information for each observation in the input data set. For each observation, the hpclusOut1 data set includes the variables that are specified in the ID statement and two new variables, _CLUSTER_ID_ and _DISTANCE_, which are the ID of the closest cluster and the distance between the observation and the centroid of the closest cluster, respectively. This example uses the variables in both the INPUT statement and the ID statement in order to transfer these variables to the output data set to do further analysis. The following statements extract the first 10 observations from the output data set; they are shown in Figure 8.


proc print noobs data=hpclusOut1(obs=10);
   title1 'First 10 Observations in Output Data Set from PROC HPCLUS';
run;
title1;


Figure 8: First 10 Observations in the Output Data Set

First 10 Observations in Output Data Set from PROC HPCLUS

SepalLengthSepalWidthPetalLengthPetalWidthSpecies_CLUSTER_ID__DISTANCE_
5033142Setosa21.49599
4634143Setosa24.15187
4636102Setosa26.40297
5133175Setosa23.82596
5535132Setosa25.26859
4831162Setosa24.13739
5234142Setosa22.10666
4936141Setosa22.56866
4432132Setosa26.69014
5035166Setosa23.86756


The following statements run PROC FREQ with the output data set hpclusOut1 to compare the clusters with the species classification. Figure 9 shows the results.

proc freq data=hpclusOut1;
   tables _CLUSTER_ID_*Species;
run;

Figure 9: Comparison of Clusters Using PROC FREQ

The FREQ Procedure

Frequency
Percent
Row Pct
Col Pct
Table of _CLUSTER_ID_ by Species
_CLUSTER_ID_(Cluster
ID)
Species(Iris Species)
SetosaVersicolorVirginicaTotal
1
0
0.00
0.00
0.00
48
32.00
77.42
96.00
14
9.33
22.58
28.00
62
41.33
 
 
2
50
33.33
100.00
100.00
0
0.00
0.00
0.00
0
0.00
0.00
0.00
50
33.33
 
 
3
0
0.00
0.00
0.00
2
1.33
5.26
4.00
36
24.00
94.74
72.00
38
25.33
 
 
Total
50
33.33
50
33.33
50
33.33
150
100.00


PROC HPCLUS creates the output statistics data set, which contains the cluster centroids. This data set includes the iteration number as _ITERATION_, the cluster ID as _CLUSTER_ID_, and the cluster centroids, which consist of the variables that are specified in the INPUT statement. Because the OUTITER option is specified, cluster centroids for each iteration are added to the hpclusOutStat1 data set. The following statements extract the centroids before the first iteration and after the last iteration. Figure 10 and Figure 11 show the results.



proc print noobs data=hpclusOutstat1(firstobs=1 obs=3);
   title2 'Cluster centroids before the first iteration';
run;
title2;


proc print noobs data=hpclusOutstat1(firstobs=13 obs=15);
   title2 'Cluster centroids after the last iteration';
run;
title2;


Figure 10: Cluster Centroids before the First Iteration

Cluster centroids before the first iteration

_ITERATION__CLUSTER_ID_SepalLengthSepalWidthPetalLengthPetalWidth
0163254915
0261284712
0363336025


Figure 11: Cluster Centroids after the Last Iteration

Cluster centroids after the last iteration

_ITERATION__CLUSTER_ID_SepalLengthSepalWidthPetalLengthPetalWidth
4159.323127.553844.292314.3846
4250.060034.280014.62002.4600
4368.742930.885757.914321.1714


To plot the clusters, you can use the CANDISC procedure to compute canonical variables and then use the SGPLOT procedure to plot the results as follows:

proc candisc data=hpclusOut1 anova out=can;
   class _CLUSTER_ID_;
   var SepalLength SepalWidth PetalLength PetalWidth;
   title2 'Canonical Discriminant Analysis of Iris Clusters';
run;


proc sgplot data=Can;
   scatter y=Can2 x=Can1 / group=_CLUSTER_ID_ ;
   title3 'Plot of Canonical Variables Identified by Cluster';
run;

Output 5.1.1 shows the plot for clustering.

Output 5.1.1: Graphs for Cluster Analysis

Graphs for Cluster Analysis


Last updated: May 25, 2022