Dear SPSS Experts,
I am trying to compute a partial correlation matrix from a correlation matrix. Equation : This is based on matrix inversion method : Partial correlation in cell (i, j) = inverse in correlation matrix (i, j) ÷ (-1 × inverse in correlation matrix (i, i) × inverse in correlation matrix (j, j)). To obtain the matrix using SPPS , i wrote the following code : GET FILE='C:\Program Files\SPSS\Employee data.sav'. CORRELATIONS /MATRIX OUT (*) /MISSING LISTWISE /VARIABLES=salary salbegin jobtime prevexp. Select if ROWTYPE_ = 'CORR'. EXECUTE . MATRIX. MGET / FILE = * / TYPE = CORR. COMPUTE INVT= INV(CR). /*** Computing Inverse of correlation matrix***/ PRINT INVT. LOOP A= 1 TO NCOL(INVT). LOOP B= 1 TO NCOL(INVT). COMPUTE PCORR = INVT(A,B) / (-1 * INVT(A,A) * INVT(B,B)). PRINT PCORR. END LOOP. END LOOP. END MATRIX . The problem with the above code lies in LOOP-END LOOP. It failed to command SPSS to calculate this equation: inverse in cell (i, j) ÷ (-1 × inverse in cell (i, i) × inverse in cell(j, j)). And it didn't create a matrix. Any help would be highly appreciated!! Thanks and Regards Ujjawal Bhandari |
Administrator
|
Does this do what you want?
MATRIX. MGET / FILE = * / TYPE = CORR. COMPUTE INVT= INV(CR). /*** Computing Inverse of correlation matrix***/ PRINT INVT. * Create the PCORR matrix before looping. COMPUTE PCORR=MAKE(NCOL(INVT),NCOL(INVT),1). LOOP A= 1 TO NCOL(INVT). LOOP B= 1 TO NCOL(INVT). COMPUTE PCORR(A,B) = INVT(A,B) / (-1 * INVT(A,A) * INVT(B,B)). END LOOP. END LOOP. * Don't PRINT PCORR until the looping is done. PRINT PCORR. END MATRIX .
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Bouncing off Bruce:
Note the denominator is a simple function of outer product of the DIAG of the INV(R). MATRIX. MGET / FILE = */ TYPE = CORR. COMPUTE INVT= INV(CR). /*** Computing Inverse of correlation matrix***/ COMPUTE PCORR=INVT &/ (1/-1 * DIAG(INVT) * T(DIAG(INVT) )). PRINT INVT. PRINT PCORR. 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?" |
Thank you Mr God :))
|
Administrator
|
In reply to this post by David Marso
Once again, David sent me to the FM. ;-) From the Arithmetic Operators section under MATRIX - END MATRIX:
&/ Elementwise division. Each element of the matrix is divided by the corresponding element of the second matrix. The matrices must have the same dimensions, or one must be a scalar.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Not only syntactically more efficient, but DRAMATIC performance differences.
**MUST BE VERY CAREFUL to ensure that denominator matrix does NOT contain 0's **. Similar performance with the other & MATRIX operators. Also MSUM vs LOOPS to SUM matrices or submatrices. -------------------------------------------------------------------- * Processor Time 00:00:05.01 * Elapsed Time 00:00:05.68 *. MATRIX. COMPUTE R1=UNIFORM(1000,1000). COMPUTE R2=UNIFORM(1000,1000). COMPUTE R3=MAKE(1000,1000,0). LOOP #=1 TO 1000. LOOP ##=1 TO 1000. COMPUTE R3(#,##)=R1(#,##)/R2(#,##). END LOOP. END LOOP. END MATRIX. * Processor Time 00:00:00.17 * Elapsed Time 00:00:00.19 *. MATRIX. COMPUTE R1=UNIFORM(1000,1000). COMPUTE R2=UNIFORM(1000,1000). COMPUTE R3=R1 &/ R2. END MATRIX. *Profile: MSUM*. *****************. ** Program 1. **. *********************************************************************. ** Calculate the sum of the values in an array using MSUM function **. *********************************************************************. MATRIX. COMPUTE V=UNIFORM(1000,1000). COMPUTE X=MSUM(V) . PRINT X / FORMAT "F10.5". END MATRIX. ********************************************************************. ************** Processor time: 00:00:00.05 **********************. ************** Elapsed time : 00:00:00.04 **********************. ***************************** END PROGRAM 1 **********************. ********************************************************************. *****************. ** Program 2. **. **********************************************************************. ** Calculate the sum of the values in an array using painfully slow **. ** and tedious element by element addition **. **********************************************************************. SET MXLOOPS 1000000. MATRIX. COMPUTE V=UNIFORM(1000,1000). COMPUTE X=0. LOOP #=1 TO NROW(V). LOOP ##=1 TO NCOL(V). COMPUTE X=X+V(#,##). PRINT X. END LOOP. END LOOP. END MATRIX. ********************************************************************. ************** Processor time: 00:00:17.93 ************************. ************** Elapsed time : 00:00:14.25 ************************. ***************************** END PROGRAM 2 **********************. ********************************************************************.
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?" |
Thank you for the detailed explanation. This is a great learning for me :)
|
Why not use /Matrix
= IN?
PARTIAL CORRPARTIAL CORR is available in the Statistics Base option. PARTIAL CORR VARIABLES= varlist [WITH varlist] BY varlist [(levels)] [/SIGNIFICANCE={TWOTAIL**}] {ONETAIL } [/STATISTICS=[NONE**] [CORR] [DESCRIPTIVES] [BADCORR] [ALL]] [/FORMAT={MATRIX** }] {SERIAL } {CONDENSED} [/MISSING=[{LISTWISE**}] [{EXCLUDE**}]] {ANALYSIS } {INCLUDE } [/MATRIX= [IN({* })] [OUT({* })]] {'savfile'|'dataset'} {'savfile'|'dataset'} Art Kendall Social Research ConsultantsOn 3/28/2013 11:09 AM, Ujjawal [via SPSSX Discussion] wrote: Thank you for the detailed explanation. This is a great learning for me :)
Art Kendall
Social Research Consultants |
Administrator
|
Art,
If you have been following recent discussions, it appears that Ujjawal is an aspiring MacroPhiliac (despite my warnings of potential irreversible brain damage and hair loss a few weeks ago). In addition a mild case of MATRIXitosis appears imminent. Keep storming up that rocky mountain Ujjawal. You know you are expected to report back in a couple months and help the fledglings sort their various issues!!! You can probably relate Art. Find new toys, beat the crap out of them! -------------------------------------------------------------------------------------
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?" |
@ Art : Thank you for your response.
Correct me if i am wrong : The problem with the PARTIAL CORR command is that it doesn't allow to create a partial correlation matrix . Lets say, you have 50 variables . You wish to calculate a correlation between every two variables controlling the other 48 variables in one run. @ David : Thank You for the trust that you have placed in me. I will be glad to be of help the fledglings sort their issues. |
You are correct PARTIAL
CORR only outputs the full correlation matrix.
It is some years since I used that procedure. Even before there were PCs, I had seen requests that PARTIAL CORR output the partial correlation matrix. Lets say, you have 50 variables . You wish to calculate a correlation between every two variables controlling the other 48 variables in one run. I think I understand what you are trying to do. I find it hard to think of why you would want to do that. You must have a huge number of cases to have correlations that would mean anything after partialling out 48 effects. I could see where you might have 50 variables and want to see a 48 * 48 controlling for 2 variables. Art Kendall Social Research ConsultantsOn 3/28/2013 2:36 PM, Ujjawal [via SPSSX Discussion] wrote: @ Art : Thank you for your response.
Art Kendall
Social Research Consultants |
Administrator
|
In reply to this post by Ujjawal
You set off some bells from my grad school days.
You might want to look at Image Factoring. However compare the keystrokes and all the other output. This gave me a chance to play with OMS! Very powerful. OTOH, I'll probably only use it when I can't roll my own with 4 lines of code ;-))) -smirk- ---------------------------------------------------------------------------------------------------- GET FILE='C:\Program Files\SPSS\Employee data.sav'. DATASET NAME emp. CORRELATIONS /MATRIX OUT (*)/MISSING LISTWISE /VARIABLES=salary salbegin jobtime prevexp. SELECT IF ROWTYPE_ = 'CORR'. MATRIX. MGET / FILE = */ TYPE = CORR. COMPUTE INVT= INV(CR). /***Inverse of correlation matrix***/. COMPUTE PCORR=INVT &/ (1/-1 * DIAG(INVT) * T(DIAG(INVT) )). PRINT PCORR / FORMAT "F10.7". END MATRIX. **OR**. DATASET DECLARE OhYeah. MATRIX. MGET / FILE = */ TYPE = CORR. COMPUTE INVT= INV(CR). /***Inverse of correlation matrix***/. SAVE (INVT &/ (1/-1 * DIAG(INVT) * T(DIAG(INVT) ))) / OUTFILE Ohyeah. END MATRIX. ********************************************************. PRESERVE. SET TVars=Names. DATASET ACTIVATE emp. DATASET DECLARE AntiImageCovariances. OMS /SELECT TABLES /IF COMMANDS=['Factor Analysis'] SUBTYPES=['Anti image Matrices'] /DESTINATION FORMAT=SAV NUMBERED=TableNumber_ OUTFILE='AntiImageCovariances'. FACTOR /VARIABLES salary salbegin jobtime prevexp /PRINT AIC /EXTRACTION IMAGE. OMSEND. RESTORE. DATASET ACTIVATE AntiImageCovariances. SELECT IF Var1 EQ 'Anti-image Covariance'. FORMATS salary salbegin jobtime prevexp (F10.7). DO REPEAT v= salary salbegin jobtime prevexp. COMPUTE V=-1*v. END REPEAT. LIST Var2 to prevexp. Run MATRIX procedure: MGET created matrix CR. The matrix has 4 rows and 4 columns. The matrix was read from the record(s) of row type CORR. PCORR -.1961231 .1767408 .0968679 -.1254512 .1767408 -.1989526 -.0910597 .1212550 .0968679 -.0910597 -.9492108 .0653126 -.1254512 .1212550 .0653126 -.9099412 ------ END MATRIX ----- ** FROM IMAGE FACTORING/OMS ** Var2 salary salbegin jobtime prevexp salary -.1961231 .1767408 .0968679 -.1254512 salbegin .1767408 -.1989526 -.0910597 .1212550 jobtime .0968679 -.0910597 -.9492108 .0653126 prevexp -.1254512 .1212550 .0653126 -.9099412 Number of cases read: 4 Number of cases listed: 4
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
|
Image Factoring is a bit difficult to get info on.
After some Googling I located one by a pioneer in FA and CFA http://link.springer.com/article/10.1007%2FBF02290173?LI=true ------- In case some of you wonder why I'm sometimes a crabby old fart, take a look at pic 2!!!
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 David Marso
Super cool :)) I can clearly see FACTOR and OMS combined has the edge over MATRIX for calculating the partial correlation matrix because they labels the matrix without defining the labels.
|
Free forum by Nabble | Edit this page |