# Re: st: St: pwcorr in matrix

 From May Boggess To statalist@hsphsun2.harvard.edu Subject Re: st: St: pwcorr in matrix Date 19 May 2004 18:05:18 -0500

```On Wednesday Richard asked:

> The FAQ
> http://www.stata.com/support/faqs/stat/rho.html explains how to obtain
> the correlation matrix as a STATA matrix.  How does one obtain the same
> matrix with the coefficient of significance, as one obtains on the
> screen by using the command pwcorr, sig?

There was a similar Statalist question some time ago:

http://www.stata.com/statalist/archive/2003-10/msg00845.html

but that code did not save the significance. The code below does.

--May

mmb@stata.com

* this puts the pairwise correlations into a matrix

clear
sysuse auto

* want correlations of mpg price and rep78
local varnum=3

* change variable names
* use preserve to get my dataset when finished

preserve

rename mpg var1
rename price var2
rename rep78 var3

* CREATE THE FOUR MATRICES THAT WILL CONTAIN THE RESULTS

matrix C = I(`varnum')
matrix N = I(`varnum')
matrix T= I(`varnum')
matrix P= I(`varnum')

forvalues i=1(1)`varnum' {
forvalues j=1(1)`i'{
capture  corr var`i' var`j'
* GET THE CORRELATION AND RESULTS, IF THERE HAS NOT BEEN ERROR
if _rc==0{
* RHO IS THE CORRELATION
matrix C[`i',`j']=r(rho)
matrix C[`j',`i']=r(rho)
* N IS THE NUBMER OF OBSERVATIONS
matrix N[`i',`j']=r(N)
matrix N[`j',`i']=r(N)
* P IS THE P-VALUES
matrix P[`i',`j']=2*ttail(r(N)-2,/*
*/ abs(r(rho))*sqrt(r(N)-2)/sqrt(1-r(rho)^2))
matrix P[`j',`i']=P[`i',`j']
* T IS THE T-STATISTICS
matrix T[`i',`j']= abs(r(rho))*sqrt(r(N)-2)/sqrt(1-r(rho)^2)
matrix T[`j',`i']=T[`i',`j']
}
else{
* IF 	-COR- HAD AN ERROR, PUT RESULTS TO MISSING
matrix C[`i',`j']=.
matrix C[`j',`i']=.
matrix N[`i',`j']=.
matrix N[`j',`i']=.
matrix P[`i',`j']=.
matrix P[`j',`i']=.
matrix T[`i',`j']=.
matrix T[`j',`i']=.
}
* PUT APPROPRIATE VALUES ON THE DIAGONAL
matrix C[`i',`i']=1
matrix N[`i',`i']=r(N)
matrix P[`i',`i']=.
matrix T[`i',`i']=.
}
}

*PRINT RESULTS
display as text "sample correlations"
display as text "number of observations"

display as text "t-statistics"

display as text "p-values"

* COMPARE RESULTS WITH PWCORR

pwcorr var1 var2 var3 , sig

* GET ORIGINAL DATA SET BACK
restore

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```