I need to cluster some points into two groups OF THE SAME SIZE, or +- 1 of course if the input data set has an odd number of observations. I have no idea how to do this.
Let's take this example:
input name $ height weight sex $ Hair $;
Bob 76 175 M Brown
Joe 45 160 M Brown
Jim 70 200 M Black
Paul 72 160 M Brown
Mel 56 130 F Blond
Jill 60 125 F Brown
Proc fastclus data=test out=test_out maxclusters=2 noprint;
var height weight;
proc print data = test_out;
So I want to force 3 observations into cluster 1, and 3 into cluster 2. I am really not picky about the clustering technique/method or even the PROC. It just needs to split data points evenly.
Thanks a bunch!
This will add a group variable assigning alternating records to one or the other group with values of 1 or 0:
data paired; set test; group = mod(_n_, 2); run;
If the are no criteria for grouping or differentiation. This is extensible to other size groups as well.
Thank you everyone for the responses. I should have been a bit more clear I what i am trying to do. I am not simply trying to split the data into two groups, I am trying to create two groups, same number of observations, which minimizes the optimal path for the sum of the two groups. So instead of height and weight, imagine latitude and longitude. Later on in my program I am going to construct the optimum route calculator. I want to split the points in such a way that when I perform a 'travelling salemen' analysis on the two groups separately, it minimizes the total distance. Clustering seems like a natural way to split the groups, I just can't figure out how to force the new groups to be the same size.
You might take a look at the HPSPLIT procedure (part of SAS/STAT 14.1, which is in SAS 9.4m3). It has several options, such as INTERVALBINS and MAXBRANCH, that allow you to apply limits to how the algorithm splits the data.
It also creates a pretty cool decision-tree style of output -- in addition to the regular tabular output. You might even be able to adapt an example to your particular problem.
Even though the procedure is named "HPSPLIT", you don't need access to a high-performance environment to use it.
Given your explanation of what you are trying to solve (the TSP in two regions), you might want to rethink your requirement that you split the locations into two clusters with equal number of points. The following example shows two clusters, one with 4 points, the other with 6. It seems to me that if you are trying to minimize the total distance, you'd do much better with two unequally spaced clusters and one route that connects to two clusters.If you try to put one of the points in the lower group into the top cluster, I think you'll increase the total distance.
data A; input x y @@; datalines; -0.1 2 0 2 0.1 2 0 2.1 -0.1 -3 0 -3 0.1 -3 -0.1 -3.1 0 -3.1 0.1 -3.1 ; proc sgplot data=A; scatter x=x y=y; xaxis grid min=-2 max=2; yaxis grid; run;
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.