GMM Procedure
Example 14.2 Performing Cluster Analysis and Storing the Model
This example uses the Fish data set in the Sashelp library to demonstrate how to use the GMM procedure to perform cluster analysis and how to use the analytic store to save the model and use the saved model for future clustering.
The Fish data set contains 159 observations and seven variables. Among these variables, Species contains different fish species and the other six numeric variables contain statistics of the fish: height, weight, width, and three measures of length.
Because the statistics of fish in different species overlap, the clusters that PROC GMM discovers do not exactly match the fish species in the Species variable. However, PROC GMM provides a good analysis of the inhomogeneity in the data. Besides, this example shows you how to save the Gaussian mixture model in the analytic store so that you can apply this saved model to other data.
The following DATA step divides the mylib.Fish data into training and testing data tables. PROC GMM uses the first 150 observations in the mylib.Fish data for clustering and saves the model, and it uses the saved model and the last 9 observations directly for cluster score prediction.
data mylib.fish_train;
title "Using PROC GMM for Cluster Prediction";
set sashelp.fish (obs=150);
run;
data mylib.fish_test;
set sashelp.fish (firstobs=151);
run;
These statements assume that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
The following statements run PROC GMM on the training data and save the trained model in the CAS table mylib.astore:
proc gmm
data=mylib.fish_train
nThreads=32
seed=1234567890
maxClusters=100
alpha=1
inference=VB (maxVbIter=1000 covariance=DIAGONAL threshold=0.001)
clusterSumOut=mylib.clustersum
clusterCovOut=mylib.clustercov;
input _NUMERIC_;
score out=mylib.score copyvars=(_ALL_);
ods select nObs descStats modelInfo;
savestate rstore=mylib.astore(replace=yes);
run;
The following statements run the ASTORE procedure with the saved model on the testing data, and output the cluster scores in the mylib.newscore CAS table:
proc astore;
score data=mylib.fish_test out=mylib.newscore
copyvars=(_ALL_) rstore=mylib.astore;
run;
The following statements use PROC PRINT to display the predictions about the testing data in the mylib.newscore CAS table, as shown in Output 14.2.1. In this table, you can see that all nine fish that belong to the smelt species are assigned (very confidently) to the correct cluster, just as they are in the Fish training data.
proc print noobs data=mylib.newscore (obs=9);
run;
Output 14.2.1: Cluster Scores on Testing Data
| Using PROC GMM for Cluster Prediction |
| _CLUSTER_1_ | _CLUSTER_2_ | _CLUSTER_3_ | _CLUSTER_4_ | _CLUSTER_5_ | _CLUSTER_6_ | _CLUSTER_7_ | _CLUSTER_8_ | _PREDICTED_CLUSTER_ | Species | Weight | Length1 | Length2 | Length3 | Height | Width |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 8.7 | 10.8 | 11.3 | 12.6 | 1.9782 | 1.2852 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 10.0 | 11.3 | 11.8 | 13.1 | 2.2139 | 1.2838 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 9.9 | 11.3 | 11.8 | 13.1 | 2.2139 | 1.1659 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 9.8 | 11.4 | 12.0 | 13.2 | 2.2044 | 1.1484 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 12.2 | 11.5 | 12.2 | 13.4 | 2.0904 | 1.3936 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 13.4 | 11.7 | 12.4 | 13.5 | 2.4300 | 1.2690 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 5 | Smelt | 12.2 | 12.1 | 13.0 | 13.8 | 2.2770 | 1.2558 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 1.21655E-8 | 5 | Smelt | 19.7 | 13.2 | 14.3 | 15.2 | 2.8728 | 2.0672 |
| 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.1102E-12 | 1.00000 | 1.1102E-12 | 1.1102E-12 | 3.32857E-8 | 5 | Smelt | 19.9 | 13.8 | 15.0 | 16.2 | 2.9322 | 1.8792 |