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 2, 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 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 1: Number of Observations

Using PROC KCLUS to Analyze Data

The KCLUS Procedure

Number of Observations Read2900
Number of Observations Used2700
Sum of Frequencies Read3900
Sum of Frequencies Used3700


Figure 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 2 shows the number of clusters and the default values for other options.

Figure 2: Model Information

Model Information
Clustering AlgorithmK-means
Maximum Iterations10
Stop CriterionCluster Change
Stop Criterion Value0
Clusters3
InitializationForgy
Seed193615979
Distance for Interval VariablesEuclidean
StandardizationNone
Interval ImputationNone


For each cluster, Figure 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 3 also displays information about the nearest cluster to that cluster and the distance between their centroids.

Figure 3: Cluster Summary

Cluster Summary for Interval Variables
ClusterFrequencyDistance from Cluster Centroid
to Observation
SSEStandard
Deviation
Nearest
Cluster
Distance
to
Nearest
Cluster
Centroid
MinimumMaximumAverage
14870.565230.444710.729873156.712.2564218.3687
220380.228931.79705.201978608.46.2106313.8882
311750.020229.23044.474131968.15.2160213.8882


Figure 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 4: Iteration History

Iteration History
Iteration
Number
SSESSE ChangeStop
Criterion
0468559  
1245325-22323418.925926
2196784-485418.037037
3186678-101063.222222
4184520-2157.9537951.185185
5184064-456.1679230.666667
6183881-182.6024260.518519
7183793-88.3052240.555556
8183740-52.9273860.111111
9183733-6.7516840
1018373300


Figure 5 and Figure 6 show statistics for each variable in the INPUT statement. Figure 5 shows the variable statistics for all the observations in the input data table, and Figure 6 shows the variable statistics for the observations that belong to a specific cluster.

Figure 5: Descriptive Statistics

Descriptive Statistics
VariableMeanStandard
Deviation
x11.0206488.189686
y10.2465469.472050


Figure 6: Within-Cluster Statistics

Within Cluster Statistics
VariableClusterMeanStandard
Deviation
x1-2.11309.0921
 29.562610.5049
 318.99306.0294
y1-4.79548.3923
 29.385211.0225
 319.58087.9047


Last updated: September 17, 2021