st: re: combined Correlationmatrix Pearson and Spearman + LaTeX output

 From Kit Baum To statalist@hsphsun2.harvard.edu Subject st: re: combined Correlationmatrix Pearson and Spearman + LaTeX output Date Tue, 9 Dec 2008 21:53:06 -0500

```< >
René said

Is there a way to do pairwise Pearson AND Spearman Correlations in a
single matrix, such as Pearson in the lower left and Spearman in the
upper right traingle of the matrix?
Provided, that is even possible. Is there a nice way to ouput such a
correlation matrix to LaTeX?

The following requires -outtable- from SSC (ssc install outtable)

-------------------
capt prog drop corrmat
*! corrmat CFB 2008dec09
prog corrmat
version 10.1
syntax varlist(numeric) using/
qui spearman `varlist', pw
tempname c
mat `c' = r(Rho)
local k: word count `varlist'
forv i = 2/`k' {
local f: word `i' of `varlist'
forv j = 1/`=`i'-1' {
local s: word `j' of `varlist'
qui corr `f' `s'
mat `c'[`i', `j'] = r(rho)
}
}
outtable using `using', mat(`c') replace format(%9.4f)
di _n "Spearman/Pearson correlations written to `using'.tex" _n
end
--------------------

```
Stupidly, -pwcorr- returns nothing useful in its return list---merely the correlation of the last variable with itself, which is predictably unity. Thus regular -corr- is used here, invoked pairwise to avoid listwise deletion due to missing values.
```
To test,

sysuse auto
corrmat price mpg rep78 using testcorrmat
type testcorrmat.tex

Kit Baum, Boston College Economics and DIW Berlin
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```