*(Q) I am running several cluster analyses and I want to compare some of the solutions to some of the other solutions using Cohen's Kappa. *The problem I'm having is that I must recode one of the categorical variables so that it maximizes the overlap with the other before I can compute the Kappa. The variables range between 2 and 7 categories, but each variable will have the same number of categories. * For instance, This is what I have after performing a Crosstabs: A1 A2 A3 B1 5 5 55 B2 25 5 10 B3 5 30 10 And this is what I need: A1 A2 A3 B1 25 5 10 B2 5 30 10 B3 5 5 55. *(A) * Maximizing the trace of a matrix by changing the row order. * This works for any matrix square matrix of dimension 7 or less. * An 8*8 matrix would required the testing of more than 40,320 diagonals since the macros need one variable per diagonal, and SPSS is limited to about 32,000 variables, the algorithm would have to be changed to accomodate matrix larger than 7*7. * Raynald Levesque rlevesque@videotron.ca Posted to SPSSX-L on 2002/02/26. *//////////////. DEFINE !mxtrace(nb=!TOKENS(1)) PRESERVE. !IF (!nb>'5') !THEN SET PRINTBACK=none. !IFEND SET MITERATE=1000000. /* Part 1: Find all permutations */ /* First create a file containing all combinations (with replacements)*/. INPUT PROGRAM. !DO !cnt=1 !TO !nb LOOP !CONCAT('cnt',!cnt) =1 TO !nb. LEAVE !CONCAT('cnt',!cnt). !DOEND END CASE. !DO !cnt=1 !TO !nb. END LOOP. !DOEND END FILE. END INPUT PROGRAM. * Count if some numbers appear more than once. NUMERIC c1 TO !CONCAT('c',!nb). !DO !cnt=1 !TO !nb COUNT !CONCAT('c',!cnt) =cnt1 TO !CONCAT('cnt',!nb,'(',!cnt,')'). !DOEND /* Keep only true permutations (without replacements)*/. EXECUTE. SELECT IF MAX(c1 TO !CONCAT('c',!nb))=1. * Compute number of permutations and define a macro producing that value *. COMPUTE nobreak=1. RANK VARIABLES=c1 BY nobreak /N INTO npermut. DO IF $CASENUM=1. WRITE OUTFILE 'c:\\temp\\define npermut.sps' /"DEFINE !npermut()"npermut"!ENDDEFINE.". END IF. STRING newname(A8). COMPUTE newname=CONCAT('p',LTRIM(STRING($CASENUM,F8.0))). FLIP cnt1 TO !CONCAT('cnt',!nb) /NEWNAMES=newname. COMPUTE casen=$CASENUM. SAVE OUTFILE='c:\\temp\\all permutations.sav' /DROP=case_lbl. GET FILE='c:\\temp\\original data.sav'. COMPUTE casen=$CASENUM. MATCH FILES FILE=* /FILE='c:\\temp\\all permutations.sav' /BY=casen. SAVE OUTFILE='c:\\temp\\data and all permutations.sav' /DROP casen. INCLUDE 'c:\\temp\\define npermut.sps'. !part2 nb=!nb. !ENDDEFINE. ***************. * Part 2: Determine the permutation which maximizes the trace *. ***************. DEFINE !part2(nb=!TOKENS(1)) !LET !trlast=!CONCAT('tr',!nb) NUMERIC tr1 TO !trlast. * For each permutation *. !DO !idx = 1 !TO !EVAL(!npermut) COMPUTE permut=!idx. SORT CASES BY !CONCAT('p',!idx). * Get then save the diagonal of that permutation *. !DO !casenum = 1 !TO !nb COMPUTE !CONCAT('tr',!casenum)=0. IF $CASENUM=!casenum !CONCAT('tr',!casenum)=!CONCAT('a',!casenum). !DOEND !LET !filen=!QUOTE(!CONCAT('c:\\temp\\temp',!idx,'.sav')) SAVE OUTFILE=!filen /KEEP=permut tr1 TO !trlast. !DOEND /* Merge all the diagonal files */ GET FILE='c:\\temp\\temp1.sav'. !DO !idx = 2 !TO !EVAL(!npermut) ADD FILES /FILE=* /FILE=!QUOTE(!CONCAT('c:\\temp\\temp',!idx,'.sav')). !DOEND /* Next file is saved for verification purposes */. SAVE OUTFILE='c:\\temp\\all diagonals.sav'. /* Calculate the sum of each diagonal (the trace) */. AGGREGATE /OUTFILE=* /BREAK=permut /tr1 TO !trlast = SUM(tr1 TO !trlast). COMPUTE tr=SUM(tr1 TO !trlast). /* Get the permutation with the largest trace */. SORT CASES BY tr(D). N OF CASES 1. FORMATS permut (F8.0). * The first case contains the permutation number with the highest trace value. WRITE OUTFILE='c:\\temp\\best permut.sps' /'DEFINE !bestp()'permut'!ENDDEFINE.'. EXECUTE. INCLUDE 'c:\\temp\\best permut.sps'. !part3 nb=!nb. !ENDDEFINE. *///////////////////////. DEFINE !part3(nb=!TOKENS(1)) GET FILE='c:\\temp\\data and all permutations.sav'. SORT CASES BY !CONCAT('p',!EVAL(!bestp)). MATCH FILES FILE=* /KEEP=a1 TO !CONCAT('a',!nb). * Erase temporary files *. !DO !idx = 1 !TO !EVAL(!npermut) !LET !filen=!QUOTE(!CONCAT('c:\\temp\\temp',!idx,'.sav')) ERASE FILE=!filen. !DOEND RESTORE. !ENDDEFINE. *//////////////. * Notes: The macros assume the data file is be called "original data.sav" and must is in c:\\temp\\. * The parametrer nb is the dimension of the matrix (this could be automated). ************. *EXAMPLE 1. ************. DATA LIST LIST /a1 a2 a3. BEGIN DATA 5 5 55 25 5 10 5 30 10 END DATA. LIST. SAVE OUTFILE='c:\\temp\\original data.sav'. SET MPRINT=yes. !mxtrace nb=3. EXECUTE. ************. *EXAMPLE 2. ************. DATA LIST LIST /a1 a2 a3 a4. BEGIN DATA 5 5 55 7 25 5 10 20 5 30 10 15 18 32 25 18 END DATA. LIST. SAVE OUTFILE='c:\\temp\\original data.sav'. !mxtrace nb=4. EXECUTE. ************. *EXAMPLE 3. ************. DATA LIST LIST /a1 a2 a3 a4 a5. BEGIN DATA 5 5 55 7 15 25 5 10 20 26 5 30 10 15 16 18 32 25 18 26 15 16 17 18 19 END DATA. LIST. SAVE OUTFILE='c:\\temp\\original data.sav'. !mxtrace nb=5. EXECUTE. ************. *EXAMPLE 4. ************. DATA LIST LIST /a1 a2 a3 a4 a5 a6. BEGIN DATA 5 5 55 7 15 19 25 5 10 20 26 26 5 30 10 15 16 32 18 32 25 18 26 12 15 16 17 18 19 23 12 65 23 15 14 2 END DATA. LIST. SAVE OUTFILE='c:\\temp\\original data.sav'. !mxtrace nb=6. EXECUTE.