Statalist The Stata Listserver


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

Re: st: Storing a correlation matrix


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: Storing a correlation matrix
Date   Tue, 18 Apr 2006 12:17:54 -0500

Marcello Pagano <[email protected]> wrote, 

> Bill Gould in 1999 answered the question of how to store pairwise 
> correlations of a varlist in a matrix--basically calculate the 
> covariance matrix and then convert it into a correlation matrix.  The 
> problem with that answer is that it does not use all the data--basically 
> the difference between -corr-and -pwcorr-.  Does anyone have a better 
> answer, one that uses all the data, like -pwcorr- does?

One solution would be the following Mata function, 

==============================================================================
real matrix pwcorr(string scalar vars, |string scalar touse)
{
        real scalar        n
        real matrix        X
        real matrix        P
        string matrix        V

        V = tokens(vars)
        n = cols(V)
        P = J(n, n, 1)

        for (i=1; i<=n; i++) {
                for (j=1; j<i; j++) {
                        if (i!=j) {
                                st_view(X, ., (V[i], V[j]), touse)
                                P[i,j] = P[j,i] = corr(variance(X,1))[2,1]
                        }
                }
        }
        return(P)
}
==============================================================================

The frunction takes two arguments.  The first is a list of variables (not a
varlist, i.e., "m1 m2 m3", not "m*").  The second is argument is optional.
It is the name of a single variable that has zero and nonzero values.
Observations with nonzero values will be used.

Here's the function in action:

        . sysuse auto, clear
        (1978 Automobile Data)

        . replace mpg = . in 1/20
        (20 real changes made, 20 to missing)

        . replace weight = . in 21/30
        (10 real changes made, 10 to missing)

        . mata: pwcorr("mpg weight displ")
        [symmetric]
                          1              2              3
            +----------------------------------------------+
          1 |             1                                |
          2 |  -.7304624386              1                 |
          3 |  -.7407515152    .8711929724              1  |
            +----------------------------------------------+

If you want to save the matrix in Mata matrix, code 

        : whatever = pwcorr("mpg weight displ")

in Mata.

If you want to save the matrix in a Stata matrix, code 

        . mata: st_matrix("whatever", pwcorr("mpg weight displ"))

-- Bill
[email protected]
*
*   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/



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