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: upper D Baseline 1, upper D Baseline 2, upper D Baseline 3, and upper D Baseline 4. Diseases upper D Baseline 1, upper D Baseline 2, and upper D Baseline 3 have 10 observations each, and disease upper D Baseline 4 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
ClusterFrequencyDistance from Cluster Centroid
to Observation
Sum of
Within
Cluster
Distance
Nearest
Cluster
Distance
to
Nearest
Cluster
Centroid
MaximumMinimumAverage
1109.20004.80006.010060.1000211.4706
2178.70595.82357.2076122.5111.6000
3107.30005.10005.950059.5000413.8000
4106.90004.10005.740057.4000111.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
VariableLevelFrequency
date010
 15
 27
 39
 45
 55
 66
plantstand022
 125
precip010
 14
 233
...
 ..
 ..
 ..


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

 

The FREQ Procedure

classIDFrequency PercentCumulative
Frequency
Cumulative
Percent
D310100.0010100.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 (upper D Baseline 1, upper D Baseline 2, upper D Baseline 3, and upper D Baseline 4). Output 5.3.4 shows that all observations that belong to class upper D Baseline 3 are in cluster 1. Similarly, all observations that belong to classes upper D Baseline 1, upper D Baseline 2, and upper D Baseline 4 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

 

The FREQ Procedure

classIDFrequency PercentCumulative
Frequency
Cumulative
Percent
D210100.0010100.00


Output 5.3.5: Labels of Observations for Cluster 2

 

The FREQ Procedure

classIDFrequency PercentCumulative
Frequency
Cumulative
Percent
D110100.0010100.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.

Last updated: May 25, 2022