Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

RE: st: Pearson/Spearman Correlation Matrix

From   Red Owl <>
To   <>
Subject   RE: st: Pearson/Spearman Correlation Matrix
Date   Sat, 11 Jan 2014 00:09:46 -0500


I'm not sure why you would want to do this, but the following
provides some example code that does what you asked.  This builds
a correlation matrix with Pearson r's below the diagonal and
Spearman rho's above the diagonal.

***** BEGIN STATA CODE *****
sysuse auto, clear
local varlist mpg price weight length
* Get Pearson Matrix in Stata
corr `varlist'
matrix R = r(C)
* Get row and col names
local rnames : rownames R
local cnames : colnames R
* Get Spearman Matrix in Stata
spearman `varlist', matrix
matrix S = r(Rho)
* Convert Stata Pearson Matrix to Mata Matrix
mata: mataR = st_matrix("R")
* Convert Stata Spearman Matrix to Mata Matrix
mata: mataS = st_matrix("S")
* Clone Mata Pearson Matrix for Combined mataRS Mata Matrix
* Pearson and Spearman in Mata
mata: mataRS = mataR
* Replace Pearson r with Spearman rho
* in Top Half of Combined mataRS Mata Matrix
mata: mataRS[1,2] = mataS[2,1]
mata: mataRS[1,3] = mataS[3,1]
mata: mataRS[1,4] = mataS[4,1]
mata: mataRS[2,3] = mataS[3,2]
mata: mataRS[2,4] = mataS[4,2]
mata: mataRS[3,4] = mataS[4,3]
* Display Pearson, Spearman, and Combined Matrices in Mata
mata: mataR
mata: mataS
mata: mataRS
* Convert Combined mataRS Mata Matrix to Stata Matrix RS
mata: st_matrix("RS",mataRS)
matrix rownames RS = `rnames'
matrix colnames RS = `cnames'
* Display Combined Stata Matrix RS in Stata
matlist RS, format(%8.3f)
***** END STATA CODE *****

Red Owl
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index