Dear list,
I would like to have a SPSS syntax which should put a crosstable from the data into a matrix. Example. My datafile looks like: Var 1 Var 2 1 1 2 2 2 3 1 3 3 3 1 1 2 2 3 3 2 1 2 2 The required output is a matrix: mat 2 0 1 1 3 1 0 0 2 The reason I want a matrix is that want to make further calculations. I also tried to make it work with OMS, but this is only a second best solution as I have to type the variables I want to keep (for example when I have answers coded 1, 2, 3 I have to type that I want to keep @1 @2 and @3) each time I want to analyse a datafile. Any help is highly appreciated! Pieter van Groenestijn University of Nijmegen The Netherlands |
Hi Pieter
PvG> I would like to have a SPSS syntax which should put a crosstable from the PvG> data into a matrix. PvG> Example. PvG> My datafile looks like: PvG> Var 1 Var 2 PvG> 1 1 PvG> 2 2 PvG> 2 3 PvG> 1 3 PvG> 3 3 PvG> 1 1 PvG> 2 2 PvG> 3 3 PvG> 2 1 PvG> 2 2 PvG> The required output is a matrix: PvG> mat PvG> 2 0 1 PvG> 1 3 1 PvG> 0 0 2 * Your sample dataset *. DATA LIST FREE/Var1 Var2 (2 F8). BEGIN DATA 1 1 2 2 2 3 1 3 3 3 1 1 2 2 3 3 2 1 2 2 END DATA. AGGREGATE /OUTFILE=* /BREAK=Var1 Var2 /n=N. SORT CASES BY Var1 Var2 . CASESTOVARS /ID = Var1 /INDEX = Var2 /GROUPBY = VARIABLE . RECODE ALL (SYSMIS=0) . LIST. -- Regards, Dr. Marta García-Granero,PhD mailto:[hidden email] Statistician --- "It is unwise to use a statistical procedure whose use one does not understand. SPSS syntax guide cannot supply this knowledge, and it is certainly no substitute for the basic understanding of statistics and statistical thinking that is essential for the wise choice of methods and the correct interpretation of their results". (Adapted from WinPepi manual - I'm sure Joe Abrahmson will not mind) |
Hi Pieter
Please, address the questions to the whole list... PvG> Probably you can help me too. I am working with matrices. As a result of PvG> some calculation I found this matrix: PvG> mat PvG> 6 PvG> 8 PvG> 6 PvG> Could you please help me to find a syntax that results in the following PvG> output scalar: PvG> res PvG> 132 PvG> 132 is the result of (6*8) + (6*6) + (8*6) in other words each element of PvG> the matrix is multiplicated with each other. It would be nice when I can PvG> also use the syntax in other examples (for example when the matrix here PvG> called mat, has 5 elements). MATRIX. COMPUTE mat={6;8;6}. COMPUTE n=NROW(mat). COMPUTE res=0. LOOP i= 1 TO n-1. - LOOP j=i+1 TO n. - COMPUTE res=res+mat(i)*mat(j). - END LOOP. END LOOP. PRINT res /FORMAT='F8' /TITLE='Result'. END MATRIX. A more general solution (assuming that that the data are a variable in a dataset: DATA LIST FREE/mat (F8). BEGIN DATA 6 8 6 END DATA. MATRIX. GET mat /VAR=mat. COMPUTE n=NROW(mat). COMPUTE res=0. LOOP i= 1 TO n-1. - LOOP j=i+1 TO n. - COMPUTE res=res+mat(i)*mat(j). - END LOOP. END LOOP. PRINT res /FORMAT='F8' /TITLE='Result'. END MATRIX. As you can see this general solution works for any number of elements (unless you have more than 40 rows of data, in that case, before MATRIX you should add "SET MXLOOPS=nnn", being nnn at least the number of rows in your variable). DATA LIST FREE/mat (F8). BEGIN DATA 6 8 6 4 2 1 3 5 7 END DATA. MATRIX. GET mat /VAR=mat. COMPUTE n=NROW(mat). COMPUTE res=0. LOOP i= 1 TO n-1. - LOOP j=i+1 TO n. - COMPUTE res=res+mat(i)*mat(j). - END LOOP. END LOOP. PRINT res /FORMAT='F8' /TITLE='Result'. END MATRIX. HTH, Marta |
Thanks Marta,
Meanwhile I found this solution, which also does the job: DATA LIST FREE/mat (F8). BEGIN DATA 6 8 6 END DATA. MATRIX. GET mat /VAR=mat. comp res=(msum(mat*t(mat))-trace(mat*t(mat)))/2. PRINT res. END MATRIX. Best Regards, Pieter van Groenestijn University of Nijmegen The Netherlands At 15:58 16-8-2006, Marta García-Granero wrote: >Hi Pieter > >Please, address the questions to the whole list... > >PvG> Probably you can help me too. I am working with matrices. As a result of >PvG> some calculation I found this matrix: >PvG> mat >PvG> 6 >PvG> 8 >PvG> 6 > >PvG> Could you please help me to find a syntax that results in the following >PvG> output scalar: >PvG> res >PvG> 132 > >PvG> 132 is the result of (6*8) + (6*6) + (8*6) in other words each element of >PvG> the matrix is multiplicated with each other. It would be nice when I can >PvG> also use the syntax in other examples (for example when the matrix here >PvG> called mat, has 5 elements). > > >MATRIX. >COMPUTE mat={6;8;6}. >COMPUTE n=NROW(mat). >COMPUTE res=0. >LOOP i= 1 TO n-1. >- LOOP j=i+1 TO n. >- COMPUTE res=res+mat(i)*mat(j). >- END LOOP. >END LOOP. >PRINT res > /FORMAT='F8' > /TITLE='Result'. >END MATRIX. > >A more general solution (assuming that that the data are a variable >in a dataset: > >DATA LIST FREE/mat (F8). >BEGIN DATA >6 8 6 >END DATA. > >MATRIX. >GET mat /VAR=mat. >COMPUTE n=NROW(mat). >COMPUTE res=0. >LOOP i= 1 TO n-1. >- LOOP j=i+1 TO n. >- COMPUTE res=res+mat(i)*mat(j). >- END LOOP. >END LOOP. >PRINT res > /FORMAT='F8' > /TITLE='Result'. >END MATRIX. > >As you can see this general solution works for any number of elements >(unless you have more than 40 rows of data, in that case, before >MATRIX you should add "SET MXLOOPS=nnn", being nnn at least the number >of rows in your variable). > >DATA LIST FREE/mat (F8). >BEGIN DATA >6 8 6 4 2 1 3 5 7 >END DATA. > >MATRIX. >GET mat /VAR=mat. >COMPUTE n=NROW(mat). >COMPUTE res=0. >LOOP i= 1 TO n-1. >- LOOP j=i+1 TO n. >- COMPUTE res=res+mat(i)*mat(j). >- END LOOP. >END LOOP. >PRINT res > /FORMAT='F8' > /TITLE='Result'. >END MATRIX. > >HTH, > >Marta |
Free forum by Nabble | Edit this page |