The DISTANCE Procedure

Example 37.1 Divorce Grounds – the Jaccard Coefficient

(View the complete code for this example.)

A wide variety of distance and similarity measures are used in cluster analysis (Anderberg 1973; Sneath and Sokal 1973). If your data are in coordinate form and you want to use a non-Euclidean distance for clustering, you can compute a distance matrix by using the DISTANCE procedure.

Similarity measures must be converted to dissimilarities before being used in PROC CLUSTER. Such conversion can be done in a variety of ways, such as taking reciprocals or subtracting from a large value. The choice of conversion method depends on the application and the similarity measure. If applicable, PROC DISTANCE provides a corresponding dissimilarity measure for each similarity measure.

In the following example, the observations are states. Binary-valued variables correspond to various grounds for divorce and indicate whether the grounds for divorce apply in each of the U.S. states. A value of "1" indicates that the ground for divorce applies, and a value of "0" indicates the opposite. The 0-0 matches are treated as totally irrelevant; therefore, each variable has an asymmetric nominal level of measurement. The absence value is 0.

The DISTANCE procedure is used to compute the Jaccard coefficient (Anderberg 1973, pp. 89, 115, and 117) between each pair of states. The Jaccard coefficient is defined as the number of variables that are coded as 1 for both states divided by the number of variables that are coded as 1 for either or both states. Since dissimilarity measures are required by PROC CLUSTER, the DJACCARD coefficient is selected. Output 37.1.1 displays the distance matrix between the first 10 states.

The CENTROID method is used to perform the cluster analysis, and the resulting tree diagram from PROC CLUSTER is saved into the tree output data set. Output 37.1.2 displays the cluster history.

The TREE procedure generates nine clusters in the output data set out. After being sorted by the state, the out data set is then merged with the input data set divorce. After being sorted by the state, the merged data set is printed to display the cluster membership as shown in Output 37.1.3.

The following statements produce Output 37.1.1 through Output 37.1.3:

data divorce;
   length State $ 15;
   input State &$
         Incompatibility Cruelty Desertion Non_Support Alcohol
         Felony Impotence Insanity Separation @@;
   datalines;
Alabama          1 1 1 1 1 1 1 1 1    Alaska           1 1 1 0 1 1 1 1 0
Arizona          1 0 0 0 0 0 0 0 0    Arkansas         0 1 1 1 1 1 1 1 1
California       1 0 0 0 0 0 0 1 0    Colorado         1 0 0 0 0 0 0 0 0
Connecticut      1 1 1 1 1 1 0 1 1    Delaware         1 0 0 0 0 0 0 0 1

   ... more lines ...   

Wisconsin        1 0 0 0 0 0 0 0 1    Wyoming          1 0 0 0 0 0 0 1 1
;
title 'Grounds for Divorce';
proc distance data=divorce method=djaccard absent=0 out=distjacc;
   var anominal(Incompatibility--Separation);
   id state;
run;
proc print data=distjacc(obs=10);
   id state; var alabama--georgia;
   title2 'First 10 States';
run;
title2;
proc cluster data=distjacc method=centroid
             pseudo outtree=tree;
   id state;
   var alabama--wyoming;
run;

proc tree data=tree noprint n=9 out=out;
   id state;
run;

proc sort;
   by state;
run;

data clus;
   merge divorce out;
   by state;
run;

proc sort;
   by cluster;
run;
proc print;
   id state;
   var Incompatibility--Separation;
   by cluster;
run;

Output 37.1.1: Distance Matrix Based on the Jaccard Coefficient

Grounds for Divorce
First 10 States

StateAlabamaAlaskaArizonaArkansasCaliforniaColoradoConnecticutDelawareFloridaGeorgia
Alabama0.00000.........
Alaska0.222220.00000........
Arizona0.888890.857140.00000.......
Arkansas0.111110.333331.000000.00000......
California0.777780.714290.500000.888890.00000.....
Colorado0.888890.857140.000001.000000.500000.00000....
Connecticut0.111110.333330.875000.222220.750000.875000.00000...
Delaware0.777780.875000.500000.888890.666670.500000.750000.00000..
Florida0.777780.714290.500000.888890.000000.500000.750000.666670.00000.
Georgia0.222220.000000.857140.333330.714290.857140.333330.875000.714290


Output 37.1.2: Clustering History

Grounds for Divorce

The CLUSTER Procedure
Centroid Hierarchical Cluster Analysis

Root-Mean-Square Distance Between Observations0.694873

Cluster History
Number
of
Clusters
Clusters JoinedFreqPseudo F
Statistic
Pseudo
t-Squared
Norm
Centroid
Distance
Tie
49ArizonaColorado2..0T
48CaliforniaFlorida2..0T
47AlaskaGeorgia2..0T
46DelawareHawaii2..0T
45ConnecticutIdaho2..0T
44CL49Iowa3..0T
43CL47Kansas3..0T
42CL44Kentucky4..0T
41CL42Michigan5..0T
40CL41Minnesota6..0T
39CL43Mississippi4..0T
38CL40Missouri7..0T
37CL38Montana8..0T
36CL37Nebraska9..0T
35North DakotaOklahoma2..0T
34CL36Oregon10..0T
33MassachusettsRhode Island2..0T
32New HampshireTennessee2..0T
31CL46Washington3..0T
30CL31Wisconsin4..0T
29NevadaWyoming2..0 
28AlabamaArkansas21561.0.1599T
27CL33CL324479.0.1799T
26CL39CL356265.0.1799T
25CL45West Virginia3231.0.1799 
24MarylandPennsylvania2199.0.2399 
23CL28Utah31673.20.2468 
22CL27Ohio51365.40.2698 
21CL26Maine71118.90.2998 
20CL23CL211075.28.70.3004 
19CL25New Jersey471.86.50.3053T
18CL19Texas569.12.50.3077 
17CL20CL221548.79.90.3219 
16New YorkVirginia250.1.0.3598 
15CL18Vermont649.42.90.3797 
14CL17Illinois1647.03.20.4425 
13CL14CL152229.215.30.4722 
12CL48CL29429.5.0.4797T
11CL13CL242427.64.50.5042 
10CL11South Dakota2528.42.40.5449 
9LouisianaCL16330.33.50.5844 
8CL34CL301423.3.0.7196 
7CL8CL121819.315.00.7175 
6CL10South Carolina2621.44.20.7384 
5CL6New Mexico2724.04.70.8303 
4CL5Indiana2828.94.10.8343 
3CL4CL93131.710.90.8472 
2CL3North Carolina3255.14.11.0017 
1CL2CL750.55.11.0663 


Output 37.1.3: Cluster Membership

Grounds for Divorce

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
Arizona100000000
Colorado100000000
Iowa100000000
Kentucky100000000
Michigan100000000
Minnesota100000000
Missouri100000000
Montana100000000
Nebraska100000000
Oregon100000000

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
California100000010
Florida100000010
Nevada100000011
Wyoming100000011

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
Alabama111111111
Alaska111011110
Arkansas011111111
Connecticut111111011
Georgia111011110
Idaho111111011
Illinois011011100
Kansas111011110
Maine111110110
Maryland011001111
Massachusetts111111101
Mississippi111011110
New Hampshire111111100
New Jersey011011011
North Dakota111111110
Ohio111011101
Oklahoma111111110
Pennsylvania011001110
Rhode Island111111101
South Dakota011111000
Tennessee111111100
Texas111001011
Utah011111110
Vermont011101011
West Virginia111011011

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
Delaware100000001
Hawaii100000001
Washington100000001
Wisconsin100000001

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
Louisiana000001001
New York011001001
Virginia010001001

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
South Carolina011010001

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
New Mexico111000000

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
Indiana100001110

StateIncompatibilityCrueltyDesertionNon_SupportAlcoholFelonyImpotenceInsanitySeparation
North Carolina000000111