The KCLUS Procedure
Getting Started: KCLUS Procedure
Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 3: Shared Concepts.
This example shows how to use the KCLUS procedure to compute clusters of observations in a CAS table.
Suppose you want to group the observations in the input CAS table mycas.inpData, in which the variables are raw measures on interval scales.
The following DATA step creates the input data table, mycas.inpData, in your CAS session. This data table contains four variables: the first two variables are the input variables among which x has missing values, the third variable is the frequency variable, and the last variable is an index variable.
data mycas.inpData;
title 'Using PROC KCLUS to Analyze Data';
drop n;
id=1;
do n=1 to 1000;
x=2*rannor(12345)+20;
y=4*rannor(12345)+20;
freq = 1;
id = id + 1;
output;
end;
do n=1 to 1000;
x=3*rannor(12345)+10;
y=5*rannor(12345)+10;
freq=2;
id = id + 1;
output;
end;
do n=1 to 700;
x=10*rannor(12345);
y=10*rannor(12345);
freq=1;
id = id + 1;
output;
end;
do n=1 to 200;
x=.;
y=10*rannor(12345);
freq=1;
id = id + 1;
output;
end;
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements run PROC KCLUS and output the results to ODS tables:
proc kclus data=mycas.inpData maxclusters=3; input x y; freq freq; run;
Figure 9.1 shows that the "Number of Observations Used" is less than the "Number of Observations Read". By default, the KCLUS procedure ignores observations that have missing values, and it does not use them in the analysis. The two additional rows, "Sum of Frequencies Read" and "Sum of Frequencies Used" are displayed when the FREQ statement is specified. They provide information about the frequency values that are read and used.
Figure 9.1: Number of Observations
Figure 9.2 shows the values of the parameters that are used in clustering. Because the number of clusters is not estimated by default and MAXCLUSTERS=3, three clusters are generated. Figure 9.2 shows the number of clusters and the default values for other options.
Figure 9.2: Model Information
For each cluster, Figure 9.3 shows the number of observations; the maximum, minimum, and average distances from that cluster’s centroid to the observations in that cluster; the sum of squares error; and the standard deviation. Figure 9.3 also displays information about the nearest cluster to that cluster and the distance between their centroids.
Figure 9.3: Cluster Summary
| Cluster Summary for Interval Variables | ||||||||
|---|---|---|---|---|---|---|---|---|
| Cluster | Frequency | Distance from Cluster Centroid to Observation | SSE | Standard Deviation | Nearest Cluster | Distance to Nearest Cluster Centroid | ||
| Minimum | Maximum | Average | ||||||
| 1 | 2036 | 0.2469 | 31.8144 | 5.1894 | 78164.0 | 6.1960 | 2 | 13.8741 |
| 2 | 1172 | 0.0294 | 29.2400 | 4.4661 | 31805.1 | 5.2094 | 1 | 13.8741 |
| 3 | 492 | 0.6734 | 30.4947 | 10.7261 | 73762.5 | 12.2443 | 1 | 18.3076 |
Figure 9.4 shows the sum of squared errors (SSE) for each iteration. If the variables are interval, then the "Iteration History" table displays SSE Change and Stop Criterion columns. The SSE Change column displays the change in within-cluster distances. The Stop Criterion column displays the stopping criterion for each iteration. If the input variables are nominal, then the "Iteration History" table displays Within Distance Change and Stop Criterion columns. If the input variables are both interval and nominal, then the "Iteration History" table also displays Within Distance Change and Stop Criterion columns, but with the distance in the sense of mixed distance with both the interval and nominal parts as detailed in Clustering Both Interval and Nominal Variables.
Figure 9.4: Iteration History
| Iteration History | |||
|---|---|---|---|
| Iteration Number | SSE | SSE Change | Stop Criterion |
| 0 | 803071 | ||
| 1 | 305247 | -497824 | 18.962963 |
| 2 | 256902 | -48344 | 9.259259 |
| 3 | 223875 | -33027 | 6.740741 |
| 4 | 196483 | -27392 | 5.777778 |
| 5 | 186678 | -9805.094494 | 3.074074 |
| 6 | 184376 | -2301.721797 | 1.296296 |
| 7 | 183876 | -499.585280 | 0.555556 |
| 8 | 183735 | -141.804913 | 0.148148 |
| 9 | 183732 | -2.778097 | 0.037037 |
| 10 | 183732 | -0.301420 | 0 |
Figure 9.5 and Figure 9.6 show statistics for each variable in the INPUT statement. Figure 9.5 shows the variable statistics for all the observations in the input data table, and Figure 9.6 shows the variable statistics for the observations that belong to a specific cluster.
Figure 9.5: Descriptive Statistics
Figure 9.6: Within-Cluster Statistics