The HPCLUS Procedure
Example 5.3 Clustering Nominal Variables
In this example, PROC HPCLUS clusters nominal variables in the Soybean data set, which is from the UCI Machine Learning Repository at http://archive.ics.uci.edu/ml/datasets/Soybean+%28Small%29.[2]The Soybean data set includes 47 observations, each of which has 35 variables. Each observation is labeled as one of four diseases: ,
,
, and
. Diseases
,
, and
have 10 observations each, and disease
has 17 observations. For more detailed data information, see Lichman (2013). All the variables in this data set are used in this example.
You can execute the following SAS code to generate the SAS data set:
%let base = http://archive.ics.uci.edu/ml/machine-learning-databases/soybean;
%let _INPUTNOMVAR = date plantstand precip temp hail
crophist areadamaged severity seedtmt
germination plantgrowth leaves
leafspotshalo leafspotsmarg leafspotsize
leafshread leafmalf leafmild stem
lodgings stemcankers cankerlesion
fruitingbodies externaldecay mycelium
intdiscolor sclerotia fruitpods fruitspots
seed moldgrowth seeddiscolor seedsize
shriveling roots;
data soybean;
length soybeanID 8;
infile "&base/soybean-small.data" device=url delimiter=',';
input &_INPUTNOMVAR. classID $ ;
soybeanID = _N_;
run;
The following statements run the k-modes clustering algorithm with a frequency-based distance measure (DISTANCENOM=RELATIVEFREQ) and check whether the clusters that the procedure obtains match the labels of the observations in the data set:
proc hpclus data=soybean maxiter=10 maxc=4 DISTANCENOM= RELATIVEFREQ
outstat=outstat outiter ;
input &_INPUTNOMVAR. / level=nominal;
id classID soybeanID ;
score out=out_hpclus1;
run;
proc sort data=out_hpclus1;
by _cluster_id_;
run;
title " ";
proc freq data=out_hpclus1;
by _CLUSTER_ID_;
table classID ;
run;
Output 5.3.1 shows the cluster summary table that is produced for four clusters.
Output 5.3.1: Cluster Summary Table for Four Clusters
| Cluster Summary for Nominal Variables | |||||||
|---|---|---|---|---|---|---|---|
| Cluster | Frequency | Distance from Cluster Centroid to Observation | Sum of Within Cluster Distance | Nearest Cluster | Distance to Nearest Cluster Centroid | ||
| Maximum | Minimum | Average | |||||
| 1 | 10 | 9.2000 | 4.8000 | 6.0100 | 60.1000 | 2 | 11.4706 |
| 2 | 17 | 8.7059 | 5.8235 | 7.2076 | 122.5 | 1 | 11.6000 |
| 3 | 10 | 7.3000 | 5.1000 | 5.9500 | 59.5000 | 4 | 13.8000 |
| 4 | 10 | 6.9000 | 4.1000 | 5.7400 | 57.4000 | 1 | 11.9000 |
Output 5.3.2 shows some of the entries from the descriptive statistics table that is produced for each nominal input variable. This table shows the frequencies of levels for the input nominal variables in the data set.
Output 5.3.2: Descriptive Statistics for Nominal Variables
| Descriptive Statistics for Nominal Variables | ||
|---|---|---|
| Variable | Level | Frequency |
| date | 0 | 10 |
| 1 | 5 | |
| 2 | 7 | |
| 3 | 9 | |
| 4 | 5 | |
| 5 | 5 | |
| 6 | 6 | |
| plantstand | 0 | 22 |
| 1 | 25 | |
| precip | 0 | 10 |
| 1 | 4 | |
| 2 | 33 | |
| . | . | . |
| . | . | |
| . | . | |
| . | . | |
Output 5.3.3 shows some of the entries from the within-cluster frequencies table that is produced for each cluster. This table shows frequencies of levels for the input nominal variables in each cluster. The table shows some information about how the levels of variables are distributed in each cluster, which is important for intracluster similarity.
Output 5.3.3: Within-Cluster Frequencies for Nominal Variables
| classID | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| D3 | 10 | 100.00 | 10 | 100.00 |
Output 5.3.4 through Output 5.3.7 show whether the observations are classified correctly with respect to the specified labels of observations (,
,
, and
). Output 5.3.4 shows that all observations that belong to class
are in cluster 1. Similarly, all observations that belong to classes
,
, and
are in clusters 2, 3, and 4, respectively. Thus, you can conclude that the clustering algorithm classified the observations accurately for this example without using the labels in an unsupervised approach.
Output 5.3.4: Labels of Observations for Cluster 1
| classID | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| D2 | 10 | 100.00 | 10 | 100.00 |
Output 5.3.5: Labels of Observations for Cluster 2
| classID | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| D1 | 10 | 100.00 | 10 | 100.00 |
Output 5.3.6: Labels of Observations for Cluster 3
Output 5.3.7: Labels of Observations for Cluster 4
[2] Disclaimer: SAS may reference other websites or content or resources for use at Customer's sole discretion. SAS has no control over any websites or resources that are provided by companies or persons other than SAS. Customer acknowledges and agrees that SAS is not responsible for the availability or use of any such external sites or resources, and does not endorse any advertising, products, or other materials on or available from such websites or resources. Customer acknowledges and agrees that SAS is not liable for any loss or damage that may be incurred by Customer or its end users as a result of the availability or use of those external sites or resources, or as a result of any reliance placed by Customer or its end users on the completeness, accuracy, or existence of any advertising, products, or other materials on, or available from, such websites or resources.