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 |
| Performance Information | |
|---|---|
| Execution Mode | Single-Machine |
| Number of Threads | 16 |
| Data Access Information | |||
|---|---|---|---|
| Data | Engine | Role | Path |
| SASHELP.IRIS | V9 | Input | On Client |
| WORK.HPCLUSOUT1 | V9 | Output | On Client |
| WORK.HPCLUSOUTSTAT1 | V9 | Output | On Client |
| Model Information | |
|---|---|
| Maximum Iteration | 10 |
| Stop Criterion | Cluster Change |
| Stop Criterion Value | 0 |
| Clusters | 3 |
| Seed Initialization | 12345 |
| Distance | Euclidean |
| Number of Observations Read | 150 |
|---|---|
| Number of Observations Used | 150 |
| Cluster Summary | ||||||||
|---|---|---|---|---|---|---|---|---|
| Cluster | Frequency | Distance from Cluster Centroid to Observation | SSE | Standard Deviation | Nearest Cluster | Distance to Nearest Cluster Centroid | ||
| Maximum | Minimum | Average | ||||||
| 1 | 62 | 16.6064 | 2.1994 | 7.3815 | 3982.1 | 8.0142 | 3 | 17.9718 |
| 2 | 50 | 12.4803 | 0.6618 | 4.8171 | 1515.1 | 5.5047 | 1 | 33.5693 |
| 3 | 38 | 15.2971 | 2.5958 | 7.1984 | 2387.9 | 7.9272 | 1 | 17.9718 |
| Iteration Statistics | |
|---|---|
| Iteration Number | SSE |
| 0 | 75216 |
| 1 | 19644 |
| 2 | 9352.340043 |
| 3 | 8148.103759 |
| 4 | 7919.714264 |
| 5 | 7885.144143 |
| 6 | 7885.144143 |
| Descriptive Statistics | ||
|---|---|---|
| Variable | Mean | Standard Deviation |
| SepalLength | 58.433333 | 8.280661 |
| SepalWidth | 30.573333 | 4.358663 |
| PetalLength | 37.580000 | 17.652982 |
| PetalWidth | 11.993333 | 7.622377 |
| Within Cluster Statistics | |||
|---|---|---|---|
| Variable | Cluster | Mean | Standard Deviation |
| SepalLength | 1 | 59.0161 | 4.6641 |
| 2 | 50.0600 | 3.5249 | |
| 3 | 68.5000 | 4.9416 | |
| SepalWidth | 1 | 27.4839 | 2.9628 |
| 2 | 34.2800 | 3.7906 | |
| 3 | 30.7368 | 2.9009 | |
| PetalLength | 1 | 43.9355 | 5.0889 |
| 2 | 14.6200 | 1.7366 | |
| 3 | 57.4211 | 4.8859 | |
| PetalWidth | 1 | 14.3387 | 2.9750 |
| 2 | 2.4600 | 1.0539 | |
| 3 | 20.7105 | 2.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 |
| SepalLength | SepalWidth | PetalLength | PetalWidth | Species | _CLUSTER_ID_ | _DISTANCE_ |
|---|---|---|---|---|---|---|
| 50 | 33 | 14 | 2 | Setosa | 2 | 1.49599 |
| 46 | 34 | 14 | 3 | Setosa | 2 | 4.15187 |
| 46 | 36 | 10 | 2 | Setosa | 2 | 6.40297 |
| 51 | 33 | 17 | 5 | Setosa | 2 | 3.82596 |
| 55 | 35 | 13 | 2 | Setosa | 2 | 5.26859 |
| 48 | 31 | 16 | 2 | Setosa | 2 | 4.13739 |
| 52 | 34 | 14 | 2 | Setosa | 2 | 2.10666 |
| 49 | 36 | 14 | 1 | Setosa | 2 | 2.56866 |
| 44 | 32 | 13 | 2 | Setosa | 2 | 6.69014 |
| 50 | 35 | 16 | 6 | Setosa | 2 | 3.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
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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_ | SepalLength | SepalWidth | PetalLength | PetalWidth |
|---|---|---|---|---|---|
| 0 | 1 | 63 | 25 | 49 | 15 |
| 0 | 2 | 61 | 28 | 47 | 12 |
| 0 | 3 | 63 | 33 | 60 | 25 |
Figure 11: Cluster Centroids after the Last Iteration
| Cluster centroids after the last iteration |
| _ITERATION_ | _CLUSTER_ID_ | SepalLength | SepalWidth | PetalLength | PetalWidth |
|---|---|---|---|---|---|
| 4 | 1 | 59.3231 | 27.5538 | 44.2923 | 14.3846 |
| 4 | 2 | 50.0600 | 34.2800 | 14.6200 | 2.4600 |
| 4 | 3 | 68.7429 | 30.8857 | 57.9143 | 21.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
