KCLUS Procedure
Example 16.4 Clustering Mixed Variables
(View the complete code for this example.)
In this example, PROC KCLUS uses the k-prototypes clustering algorithm to cluster mixed input data that contain both interval and nominal variables in the Baseball data set, which is the same data set that is used in Example 16.3. You can execute the following SAS code to load the input data table, mylib.baseball.
data mylib.baseball;
Set sashelp.baseball;
Keep CrAtBat CrHits CrRuns CrRbi CrBB Team League Division Position Div;
Run;
These statements assume that your libref is named mylib, but you can substitute any appropriately defined libref.
The following statements run the k-prototypes clustering algorithm on mixed input data. The first INPUT statement uses the LEVEL=INTERVAL option to specify the input variables CrAtBat, CrHits, CrRuns, CrRbi, and CrBB as the interval input variables; the second INPUT statement uses the LEVEL=NOMINAL option to specify the input variables Team, League, Division, Position, and Div as the nominal input variables.
The KPROTOTYPEPARAMS=USERGAMMA(VALUE=10) option specifies for the k-prototypes clustering algorithm. The NOC=ABC option uses the ABC algorithm to search for the best number of clusters in a range of 2 to 10. The PRINTALLDISTANCES option prints two additional variables, _DISTANCEINT_ and _DISTANCENOM_, in the output table
kclusout4.
proc kclus data=mylib.baseball maxiter=10 maxc=10 distancenom=relativefreq
outstat(outiter)=mylib.kclusOutstat4 printalldistances
noc=abc(B=10 minclusters=2 align=none criterion=all)
kprototypeparams=usergamma(value=10);
input CrAtBat CrHits CrRuns CrRbi CrBB / level=interval;
input Team League Division Position Div / level=nominal;
score out=mylib.kclusOut4 copyvars=
(CrAtBat CrHits CrRuns CrRbi CrBB Team League Division Position Div);
ods output FreqNom=FreqNom4;
run;
Output 16.4.1 shows the "Model Information" table. The clustering algorithm in use is the k-prototypes, and the value is 10. Since PROC KCLUS is implementing the k-prototypes algorithm on mixed input data, the distance measures for both the interval and nominal variables are displayed in the table, as Euclidean and RelativeFreq, respectively.
Output 16.4.1: Model Information for k-Prototypes Clustering Algorithm
| Model Information | |
|---|---|
| Clustering Algorithm | K-prototypes |
| Gamma Value | 10 |
| Maximum Iterations | 10 |
| Stop Criterion | Cluster Change |
| Stop Criterion Value | 0 |
| Clusters | 2 |
| Initialization | Forgy |
| Seed | 1065607171 |
| Distance for Interval Variables | Euclidean |
| Distance for Nominal Variables | RelativeFreq |
| Number of Clusters Estimation | ABC |
| Standardization | None |
| Interval Imputation | None |
| Nominal Imputation | None |
Output 16.4.2 shows the cluster summary table that is produced for the k-prototypes algorithm.
Output 16.4.2: Cluster Summary Table for k-Prototypes Clustering Algorithm
| Cluster Summary for Mixed Variables | |||||||
|---|---|---|---|---|---|---|---|
| Cluster | Frequency | Distance from Cluster Centroid to Observation | Within Cluster Distance | Nearest Cluster | Distance to Nearest Cluster Centroid | ||
| Minimum | Maximum | Average | |||||
| 1 | 94 | 16703.3 | 77673932 | 3377958 | 3.1753E8 | 2 | 21203061 |
| 2 | 228 | 4081.1 | 5148754 | 1081023 | 2.4647E8 | 1 | 21203059 |