[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   "Nick Cox" <>
To   <>
Subject   st: RE: re: combined Correlationmatrix Pearson and Spearman + LaTeX output
Date   Wed, 10 Dec 2008 13:55:28 -0000

-corrci- returns a matrix of Pearson correlations. Rank-transform everything beforehand, and you could use it to get a matrix of Spearman correlations. 

. search corrci

SJ-8-3  pr0041  .  Speaking Stata: Corr. with confidence, Fisher's z revisited
        (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
        Q3/08   SJ 8(3):413--439
        reviews Fisher's z transformation and its inverse, the
        hyperbolic tangent, and reviews their use in inference
        with correlations


Kit Baum

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

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

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index