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

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 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

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


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
ClusterFrequencyDistance from Cluster Centroid
to Observation
SSEStandard
Deviation
Nearest
Cluster
Distance
to
Nearest
Cluster
Centroid
MinimumMaximumAverage
120360.246931.81445.189478164.06.1960213.8741
211720.029429.24004.466131805.15.2094113.8741
34920.673430.494710.726173762.512.2443118.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
SSESSE ChangeStop
Criterion
0803071  
1305247-49782418.962963
2256902-483449.259259
3223875-330276.740741
4196483-273925.777778
5186678-9805.0944943.074074
6184376-2301.7217971.296296
7183876-499.5852800.555556
8183735-141.8049130.148148
9183732-2.7780970.037037
10183732-0.3014200


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

Descriptive Statistics
VariableMeanStandard
Deviation
x11.0206488.189686
y10.2465469.472050


Figure 9.6: Within-Cluster Statistics

Within Cluster Statistics
VariableClusterMeanStandard
Deviation
x19.575510.5230
 219.00065.9973
 3-2.00809.1157
y19.416711.0228
 219.59807.8904
 3-4.76048.3576


Last updated: December 21, 2018