Maximizing the Trace of a Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | *(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 (in older SPSS versions), 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. |
Related pages
...
Navigate from here