I am using MATRIX/ END MATRIX to calculate the determinant of a correlation matrix. In my data file I have
6 variables, each is a correlation coefficient from a sample. Each row of the data file is from a different sample created as part of a Monte Carlo simulation, so the file has about 10,000 rows. I need to select a row (6 correlations
for a sample) pass the values to MATRIX get the determinant, and then write the sample id and the value of the determinant to an external SAV file that I can then match to the original data file.
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
I put the MATRIX code into a macro loop which runs, but the SAVE command overwrites rather than appends the results.
My code so far looks like this.
GET FILE='CNTR_SIM3_12CONDS.SAV'
/KEEP= rYX1u rYX2u rYpdctu rX1X2u rX1pdctu rX2pdctu SAMPLE_.
SELECT IF SAMPLE_<=100.
DEFINE !DETS (!POS !TOKENS(1)).
!DO !I = 1 !TO !1.
TEMP.
SELECT IF SAMPLE_=!I.
MATRIX.
GET VALS.
COMPUTE R=MAKE(4,4,1).
COMPUTE R(2,1)=VALS(1).
COMPUTE R(3,1)=VALS(2).
COMPUTE R(4,1)=VALS(3).
COMPUTE R(3,2)=VALS(4).
COMPUTE R(4,2)=VALS(5).
COMPUTE R(4,3)=VALS(6).
COMPUTE R(1,2)=VALS(1).
COMPUTE R(1,3)=VALS(2).
COMPUTE R(1,4)=VALS(3).
COMPUTE R(2,3)=VALS(4).
COMPUTE R(2,4)=VALS(5).
COMPUTE R(3,4)=VALS(6).
COMPUTE SAMPLE_=VALS(7).
COMPUTE D=DET(R).
COMPUTE RST={SAMPLE_,D}.
PRINT RST.
SAVE RST/OUTFILE='DETS.SAV'
/VARIABLES=SMP DET.
END MATRIX.
!DOEND.
!ENDDEFINE.
!DETS 100.
Any help would be much appreciated.
Jason
_____________________________________________________________
Jason W. Beckstead, Ph.D. Associate Professor/Quantitative Methodologist University of South Florida College of Nursing 12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA Statistical Editor, International Journal of Nursing Studies phone: +1.813.974.7667 fax: +1.813.974.5418 personal website: http://personal.health.usf.edu/jbeckste/ International Journal of Nursing Studies http://www.elsevier.com/ijns
|
Of course. You are looping whole MATRIX sessions, that will not do.
Instead, you may put just SAVE statement inside !DO - !DOEND or (better) inside regular LOOP - END LOOP. New cases will be appended in loops to an output sav file. 21.02.2015 19:52, Beckstead, Jason
пишет:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
Assuming the cases are ordered with sample 1 being rows 1-6, sample 2 rows 7-12 etc.
Untested... may need to fiddle a bit to defray golden monkey residuals... ;-) MATRIX. GET data /FILE * /VARIABLES sample corr. LOOP I=1 TO TRUNC(NROW(data)/6). COMPUTE R=MAKE(4,4,0). COMPUTE start=(I-1)*6+1. COMPUTE R(1,2)=data(start). COMPUTE R(1,3)=data(start+1). ..... COMPUTE R(3,4)=data(start+6). COMPUTE R=R+T(R)+ MDIAG(4,1). SAVE {I,DET(R)}/OUTFILE "filepath" /VARIABLES sample Det . END LOOP. END MATRIX.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Beckstead, Jason
David and others,
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
thanks for your suggestions. I tried adapting your code but I must be missing something with the LOOP in MATRIX procedure. I keep getting an undefined error. My file structure is wide, rather than tall so all 6 vars are on the same line for each sample,
but I don't think that is the problem. I think it is with the LOOP because I can't even get the code to print 3 R mtxs. Here is the set up for the first 3 of 10,000 samples.
DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_.
BEGIN DATA.
.59 .41 .71 -.05 .67 .70 1.00
.49 .35 .62 -.06 .79 .55 2.00
.55 .38 .67 -.06 .78 .56 3.00
END DATA.
MATRIX.
GET data/FILE=*.
LOOP i=1 TO NROW(data).
COMPUTE R=MAKE(4,4,1).
COMPUTE R(2,1)=data(1).
COMPUTE R(3,1)=data(2).
COMPUTE R(4,1)=data(3).
COMPUTE R(3,2)=data(4).
COMPUTE R(4,2)=data(5).
COMPUTE R(4,3)=data(6).
COMPUTE R(1,2)=data(1).
COMPUTE R(1,3)=data(2).
COMPUTE R(1,4)=data(3).
COMPUTE R(2,3)=data(4).
COMPUTE R(2,4)=data(5).
COMPUTE R(3,4)=data(6).
PRINT R.
SAVE {I,DET(R)} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET.
END LOOP.
END MATRIX.
Any further insight would be appreciated.
Jason
_____________________________________________________________ Jason W. Beckstead, Ph.D. Associate Professor/Quantitative Methodologist University of South Florida College of Nursing 12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA Statistical Editor, International Journal of Nursing Studies phone: +1.813.974.7667 fax: +1.813.974.5418 personal website: http://personal.health.usf.edu/jbeckste/ International Journal of Nursing Studies http://www.elsevier.com/ijns
|
Administrator
|
You want data(I,1) for the first assignment and I in the others as well.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
This works.
I misled you with MDIAG. I should have used IDENT. The : method if assignment is faster than element by element. Note: also creating just the upper triangle then smashing it into its transpose and adding an Identity matrix DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. LOOP i=1 TO NROW(data). COMPUTE R=MAKE(4,4,0). COMPUTE R(1,2:4)=data(i,1:3). COMPUTE R(2,3:4)=data(i,4:5). COMPUTE R(4,3)=data(i,6). COMPUTE R=R+T(R) + IDENT(4). /*PRINT R. SAVE {I,DET(R)} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Beckstead, Jason
Did you mean to do this?
DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. LOOP i=1 TO NROW(data). comp R=MAKE(4,4,0). COMPUTE R(2,1)=data(i,1). COMPUTE R(3,1)=data(i,2). COMPUTE R(4,1)=data(i,3). COMPUTE R(3,2)=data(i,4). COMPUTE R(4,2)=data(i,5). COMPUTE R(4,3)=data(i,6). comp r= r+t(r). call setdiag(r,1). PRINT R. SAVE {I,DET(R)} /OUTFILE= * /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX. 22.02.2015 0:53, Beckstead, Jason
пишет:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
In reply to this post by David Marso
Refinement: UNTESTED.
****. DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. COMPUTE numelem=NCOL(data)-1. COMPUTE numcol=0. LOOP k=1 TO 100000. + COMPUTE numcol=numcol+k. END LOOP IF (numcol EQ numelem). LOOP i=1 TO NROW(data). + COMPUTE R=MAKE(numcol,numcol,0). + COMPUTE curitem=1. + LOOP j=1 TO numcol. + COMPUTE j1=j+1. + COMPUTE enditem=curitem + numcol - j1. + COMPUTE r(j,j1:numcol)=data(i,curitem:enditem). + END LOOP. SAVE {I,DET(R=R+T(R) + IDENT(numcol))} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX. ---------------------------------
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
David, it didn't work.
Also, an aside note, call setdiag is much better than adding ident(). Compare elapsed time: matrix. comp x= make(10000,10000,0). call setdiag(x,1). end matrix. matrix. comp x= make(10000,10000,0). comp x= x+ident(10000). end matrix. 22.02.2015 11:32, David Marso пишет:
Refinement: UNTESTED. ****. DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. COMPUTE numelem=NCOL(data)-1. COMPUTE numcol=0. LOOP k=1 TO 100000. + COMPUTE numcol=numcol+k. END LOOP IF (numcol EQ numelem). LOOP i=1 TO NROW(data). + COMPUTE R=MAKE(numcol,numcol,0). + COMPUTE curitem=1. + LOOP j=1 TO numcol. + COMPUTE j1=j+1. + COMPUTE enditem=curitem + numcol - j1. + COMPUTE r(j,j1:numcol)=data(i,curitem:enditem). + END LOOP. SAVE {I,DET(R=R+T(R) + IDENT(numcol))} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
In reply to this post by David Marso
David,
Thanks for suggestions. I figured the problem was with the LOOP structure for writing/saving out results, which I fixed. I appreciate your elegant mods to the input structure (a little matrix algebra goes a long way). Here is my final code that did the trick. *THIS WORKS -------------------------------------------------. DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. COMPUTE D=MAKE(NROW(data),2,0). LOOP i=1 TO NROW(data). COMPUTE R=MAKE(4,4,1). COMPUTE R(2,1)=data(i,1). COMPUTE R(3,1)=data(i,2). COMPUTE R(4,1)=data(i,3). COMPUTE R(3,2)=data(i,4). COMPUTE R(4,2)=data(i,5). COMPUTE R(4,3)=data(i,6). COMPUTE R(1,2)=data(i,1). COMPUTE R(1,3)=data(i,2). COMPUTE R(1,4)=data(i,3). COMPUTE R(2,3)=data(i,4). COMPUTE R(2,4)=data(i,5). COMPUTE R(3,4)=data(i,6). COMPUTE D(i,1:2)={i,DET(R)}. END LOOP. SAVE D /OUTFILE='C:\DETS.SAV' /VARIABLES=SAMPLE_ DET. END MATRIX. _____________________________________________________________ Jason W. Beckstead, Ph.D. Associate Professor/Quantitative Methodologist University of South Florida College of Nursing 12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA Statistical Editor, International Journal of Nursing Studies phone: +1.813.974.7667 fax: +1.813.974.5418 personal website: http://personal.health.usf.edu/jbeckste/ International Journal of Nursing Studies http://www.elsevier.com/ijns ________________________________________ From: SPSSX(r) Discussion [[hidden email]] on behalf of David Marso [[hidden email]] Sent: Sunday, February 22, 2015 3:32 AM To: [hidden email] Subject: Re: saving MATRIX output to SAV file Refinement: UNTESTED. ****. DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. MATRIX. GET data/FILE=*. COMPUTE numelem=NCOL(data)-1. COMPUTE numcol=0. LOOP k=1 TO 100000. + COMPUTE numcol=numcol+k. END LOOP IF (numcol EQ numelem). LOOP i=1 TO NROW(data). + COMPUTE R=MAKE(numcol,numcol,0). + COMPUTE curitem=1. + LOOP j=1 TO numcol. + COMPUTE j1=j+1. + COMPUTE enditem=curitem + numcol - j1. + COMPUTE r(j,j1:numcol)=data(i,curitem:enditem). + END LOOP. SAVE {I,DET(R=R+T(R) + IDENT(numcol))} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX. --------------------------------- David Marso wrote > This works. > I misled you with MDIAG. I should have used IDENT. > The : method if assignment is faster than element by element. > Note: also creating just the upper triangle then smashing it into its > transpose and adding an Identity matrix > DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. > BEGIN DATA. > .59 .41 .71 -.05 .67 .70 1.00 > .49 .35 .62 -.06 .79 .55 2.00 > .55 .38 .67 -.06 .78 .56 3.00 > END DATA. > MATRIX. > GET data/FILE=*. > LOOP i=1 TO NROW(data). > COMPUTE R=MAKE(4,4,0). > COMPUTE R(1,2:4)=data(i,1:3). > COMPUTE R(2,3:4)=data(i,4:5). > COMPUTE R(4,3)=data(i,6). > COMPUTE R=R+T(R) + IDENT(4). > /*PRINT R. > SAVE {I,DET(R)} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. > END LOOP. > END MATRIX. > David Marso wrote >> You want data(I,1) for the first assignment and I in the others as well. >> Beckstead, Jason wrote >>> David and others, >>> thanks for your suggestions. I tried adapting your code but I must be >>> missing something with the LOOP in MATRIX procedure. I keep getting an >>> undefined error. My file structure is wide, rather than tall so all 6 >>> vars are on the same line for each sample, but I don't think that is the >>> problem. I think it is with the LOOP because I can't even get the code >>> to print 3 R mtxs. Here is the set up for the first 3 of 10,000 samples. >>> >>> DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. >>> BEGIN DATA. >>> .59 .41 .71 -.05 .67 .70 1.00 >>> .49 .35 .62 -.06 .79 .55 2.00 >>> .55 .38 .67 -.06 .78 .56 3.00 >>> END DATA. >>> MATRIX. >>> GET data/FILE=*. >>> LOOP i=1 TO NROW(data). >>> COMPUTE R=MAKE(4,4,1). >>> COMPUTE R(2,1)=data(1). >>> COMPUTE R(3,1)=data(2). >>> COMPUTE R(4,1)=data(3). >>> COMPUTE R(3,2)=data(4). >>> COMPUTE R(4,2)=data(5). >>> COMPUTE R(4,3)=data(6). >>> COMPUTE R(1,2)=data(1). >>> COMPUTE R(1,3)=data(2). >>> COMPUTE R(1,4)=data(3). >>> COMPUTE R(2,3)=data(4). >>> COMPUTE R(2,4)=data(5). >>> COMPUTE R(3,4)=data(6). >>> PRINT R. >>> SAVE {I,DET(R)} /OUTFILE='DETS.SAV' /VARIABLES=SAMPLE_ DET. >>> END LOOP. >>> END MATRIX. >>> >>> Any further insight would be appreciated. >>> Jason >>> >>> _____________________________________________________________ >>> Jason W. Beckstead, Ph.D. >>> Associate Professor/Quantitative Methodologist >>> University of South Florida College of Nursing >>> 12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA >>> Statistical Editor, International Journal of Nursing Studies >>> phone: +1.813.974.7667 fax: +1.813.974.5418 >>> personal website: http://personal.health.usf.edu/jbeckste/ >>> International Journal of Nursing Studies http://www.elsevier.com/ijns >>> >>> >>> ===================== >>> To manage your subscription to SPSSX-L, send a message to >>> LISTSERV@.UGA >>> (not to SPSSX-L), with no body text except the >>> command. To leave the list, send the command >>> SIGNOFF SPSSX-L >>> For a list of commands to manage subscriptions, send the command >>> INFO REFCARD ----- Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/saving-MATRIX-output-to-SAV-file-tp5728761p5728770.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
In reply to this post by David Marso
FIXED and Tested.
Had a few BFs in previous ;-( -- DATA LIST FREE/rYX1 rYX2 rYpdct rX1X2 rX1pdct rX2pdct SAMPLE_. BEGIN DATA. .59 .41 .71 -.05 .67 .70 1.00 .49 .35 .62 -.06 .79 .55 2.00 .55 .38 .67 -.06 .78 .56 3.00 END DATA. DEFINE VectToUpper (!POS !CHAREND("/") / !POS !TOKENS(1) ) + COMPUTE curitem=1. + COMPUTE NC=NCOL(!2). + LOOP j=1 TO NC-1. + COMPUTE enditem=curitem + NC - (j+1). + COMPUTE !2(j,(j+1): NC) = !1(curitem:enditem). + COMPUTE curitem = enditem + 1. + END LOOP. !ENDDEFINE . MATRIX. GET data/FILE=*. COMPUTE numcol=0. LOOP k=1 TO 100. + COMPUTE numcol=numcol+k. END LOOP IF (numcol EQ NCOL(data)-1). COMPUTE k=k+1. LOOP i=1 TO NROW(data). + COMPUTE R=MAKE(k,k,0). + VectToUpper data(i,:) / R . + SAVE {I,DET(R+T(R) + IDENT(k))} /OUTFILE=* /VARIABLES=SAMPLE_ DET. END LOOP. END MATRIX.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |