Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Save stored matrix as dataset

    I have a dataset that looks like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(pct_total113300 pct_total115100 pct_total115200 pct_total211100 pct_total212100)
     100  100  100   100  100
     2.9 2.24 4.64 10.07 3.41
     .44  .24  .32   .61  .32
    1.88  .87 1.82  3.85  1.6
       .    .    .     .    .
       .  .02    .     .    .
       .    .    .   .22    .
       .  .12  .24    .2    .
       .  .01    .   .13    .
       .  .01    .   .29  .06
       .    .    .   .15    .
     .14  .11  .28  1.36  .27
       .  .04    .   .39  .17
     .08  .09    .   .28  .14
       .    .    .   .13  .17
       .  .06    .   .13   .1
     .17  .56 1.11     .    .
       .    .    .     .    .
       .    .    .     .    .
       .    .    .     .    .
    end
    (In the actual dataset, there are 294 variables, each having 734 observations - which is too big to display here.)

    I want to get the correlation between every pair of variables and put that into a matrix. In this matrix, the correlation between variable i and j is in entry i,j. I've tried typing in -pwcorr- and according to Stata's rcorrelate.pdf, the pairwise correlation matrix is stored in r(C). How do I put r(C) into a dataset with each row and column labelled by the right variable?

  • #2
    Originally posted by Xiaoyang Li View Post
    How do I put r(C) into a dataset with each row and column labelled by the right variable?
    The easiest way is like the following (using Stata's auto dataset for illustration).
    Code:
    sysuse auto
    quietly pwcorr _all
    matrix define C = r(C)
    drop _all
    svmat double C, names(col)
    matrix drop C
    This retains the names of the variables. I'm not sure what you mean by "a dataset with each row . . . labeled by the right variable". If you want to insert a new string variable whose rows contain the variable names, then you can do that with a loop, but I'm not sure that it's worth it—a dataset is not really a (correlation) matrix.
    Code:
    quietly generate str varnames = ""
    quietly ds
    forvalues i = 1/`=_N' {
        local varname : word `i' of `r(varlist)'
        quietly replace varnames = "`varname'" in `i'
    }
    order varnames

    Comment


    • #3
      Dear Joseph,
      Thank you! This works really well.

      Comment

      Working...
      X