Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Joint -pwcorr- and -spearman-

From   "Rodrigo A. Alfaro" <>
To   <>
Subject   Re: st: Joint -pwcorr- and -spearman-
Date   Wed, 23 Aug 2006 16:54:35 -0400


Typing: -type C:\Stata8\ado\updates\s\spearman.ado-
will show you the code for spearman. It is simple and
full of comments.

This line "syntax varlist(min=2 max=2) [if] [in]" tells
you that the command was created for 2 variables only.

The following lines:
                egen `r1' = rank(`1') if `touse'
                egen `r2' = rank(`2') if `touse'
                corr `r1' `r2'
tell you that spearman is a simple correlation of the
"ranks" of the variables. Therefore, generate the
variables and use the same code that you already have.

sysuse auto, clear
egen price_r=rank(price)
egen mpg_r=rank(mpg)
matrix accum S= price_r mpg_r, nocons dev
matrix S=corr(S)
matrix list S


----- Original Message ----- 
From: "Herve STOLOWY" <>
To: <>
Sent: Wednesday, August 23, 2006 8:28 AM
Subject: Re: st: Joint -pwcorr- and -spearman-

Dear Nick:

Thank you for your help. I tried what you suggest but I face difficulties at 
the first two steps (given my insufficient knowledge of matrices).

Before contacting you, I tried to solve the issue with the Stata manual.

1. Get pairwise correlation matrix, say -pearson-.

I did the following:

matrix accum R =  var1 var2,nocons dev
matrix R = corr(R)
matrix list R

I checked that I get the correct coefficients (comparing with -pwcorr-).

Is this method correct?
With -pwcorr-, I can get the p-value. Is there a way to also include the 
p-value in the matrix?

2. Get pairwise Spearman correlation matrix, say -spearman-.

I did not find a solution. I guess there must be something equivalent to 
corr(R) for Spearman.

Best regards

Hervé Stolowy

Coordinateur du Département/Head of Department
HEC Paris
Département Comptabilité Contrôle de gestion / Dept of Accounting and 
Management Control
1, rue de la Liberation
78351 - Jouy-en-Josas
Tel: +33 1 39 67 94 42 - Fax: +33 1 39 67 70 86 (replace # by @)
>>> 08/21/06 10:00 PM >>>
Despite your question, doing this synchronously
does not seem especially important to what you
want here. I suppose you could get two
Statas working more or less in unison, but they
would still need to talk to each other.

A recipe for the hybrid matrix is

1. Get pairwise correlation matrix, say -pearson-.

2. Get pairwise Spearman correlation matrix, say -spearman-.

3. Loop over rows and columns one side of the principal

matrix both = pearson
local I = rowsof(pearson)
forval i = 1/`I' {
local j1 = `i' + 1
forval j = `j1'/`I' {
matrix both[`i',`j'] = spearman[`i',`j']


matrix list both

As each matrix is square, either the number of rows or
the number of columns suffices to indicate the matrix size.

There are no doubt cuter ways to do it, especially in Mata, but this works.

I guess you want this as a compact display, not for further analysis.



Do you know if there is a possibility to run -pwcorr- and -spearman-
jointly (i.e., on the same variables at the same time) in order to get
the results in only one matrix: with the -pwcorr- results in the lower
left triangle and the -spearman- results in the upper right triangle (or
the reverse presentation)?
*   For searches and help try:

*   For searches and help try:

*   For searches and help try:

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