Announcement

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

  • How can i save the output of pwcorr and correlate into dta?Thanks a lot!

    I want to compute the correlation of x between different stocks in each half year. There are 20+ half years and 2000+ stocks. (Table 1). Therefore, I reshape the data firstly yh by yh(Table 2) ,and compute the correlation of x (Table 3).

    I want to ask two questions:
    1.Is there any concise solutions?
    2.How can i save the output of pwcorr and correlate into dta?
    Thanks for your reply!

    Table 1
    stkcd tradedate yh x
    1 16806 92 0.078212
    1 16807 92 0.128066
    1 16811 92 0.597885
    1 16812 92 0.012668
    1 16813 92 0.2803
    ... ... ... ...
    2027 16806 92 0.507343
    2027 16807 92 0.299147
    2027 16811 92 0.369548
    2027 16812 92 0.216952
    2027 16813 92 0.525851
      ... ... ...
    1 17147 93 0.143762
    1 17148 93 0.694211
    1 17149 93 0.529129
    1 17150 93 0.516332
    1 17156 93 0.042779
    ... ... ... ...
    2027 17147 93 0.82723
    2027 17148 93 0.973761
    2027 17149 93 0.122705
    2027 17150 93 0.184536
    2027 17156 93 0.250978
      ... ... ...
    1 17280 94 0.354138
    1 17281 94 0.918449
    1 17282 94 0.315837
    1 17295 94 0.112138
    1 17296 94 0.47002
    ... ... ... ...
    2027 17280 94 0.731127
    2027 17281 94 0.637081
    2027 17282 94 0.404287
    2027 17295 94 0.923884
    2027 17296 94 0.53458
      ... ... ...
    Table 2
    tradedate x1 x2 ... x2027 yh
    16806 0.078212 ... ... 0.507343 92
    16807 0.128066 ... ... 0.299147 92
    16811 0.597885 ... ... 0.369548 92
    16812 0.012668 ... ... 0.216952 92
    16813 0.2803 ... ... 0.525851 92
    ... ... ... ... ... ...
    Table 3
    x1 x2 ... x2027
    x1 1 ... ...
    x2 0.152024 ... ...
    ... ... ... ... ...
    x2027 0.001852 ... ... 1
    Last edited by Jacky Wei; 19 Dec 2020, 02:42.

  • #2
    I am not sure what you are going to do with so many numbers, and I am pretty sure that saving correlation matrices as dta files does not make much sense. But doing it is mechanically easy:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . correlate price-weight
    (obs=69)
    
                 |    price      mpg    rep78 headroom    trunk   weight
    -------------+------------------------------------------------------
           price |   1.0000
             mpg |  -0.4559   1.0000
           rep78 |   0.0066   0.4023   1.0000
        headroom |   0.1112  -0.3996  -0.1480   1.0000
           trunk |   0.3232  -0.5798  -0.1572   0.6608   1.0000
          weight |   0.5478  -0.8055  -0.4003   0.4795   0.6691   1.0000
    
    
    . mat C = r(C)
    
    . clear
    
    . svmat C
    number of observations will be reset to 6
    Press any key to continue, or Break to abort
    number of observations (_N) was 0, now 6
    
    . list, sep(0)
    
         +-----------------------------------------------------------------------+
         |        C1          C2          C3          C4          C5          C6 |
         |-----------------------------------------------------------------------|
      1. |         1   -.4559489    .0065533    .1112435    .3232065    .5478396 |
      2. | -.4559489           1    .4023404   -.3995853   -.5798199   -.8055198 |
      3. |  .0065533    .4023404           1   -.1479982   -.1572433   -.4003441 |
      4. |  .1112435   -.3995853   -.1479982           1    .6607842    .4794664 |
      5. |  .3232065   -.5798199   -.1572433    .6607842           1     .669138 |
      6. |  .5478396   -.8055198   -.4003441    .4794664     .669138           1 |
         +-----------------------------------------------------------------------+
    
    .

    Comment


    • #3
      Originally posted by Joro Kolev View Post
      I am not sure what you are going to do with so many numbers, and I am pretty sure that saving correlation matrices as dta files does not make much sense. But doing it is mechanically easy:

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . correlate price-weight
      (obs=69)
      
      | price mpg rep78 headroom trunk weight
      -------------+------------------------------------------------------
      price | 1.0000
      mpg | -0.4559 1.0000
      rep78 | 0.0066 0.4023 1.0000
      headroom | 0.1112 -0.3996 -0.1480 1.0000
      trunk | 0.3232 -0.5798 -0.1572 0.6608 1.0000
      weight | 0.5478 -0.8055 -0.4003 0.4795 0.6691 1.0000
      
      
      . mat C = r(C)
      
      . clear
      
      . svmat C
      number of observations will be reset to 6
      Press any key to continue, or Break to abort
      number of observations (_N) was 0, now 6
      
      . list, sep(0)
      
      +-----------------------------------------------------------------------+
      | C1 C2 C3 C4 C5 C6 |
      |-----------------------------------------------------------------------|
      1. | 1 -.4559489 .0065533 .1112435 .3232065 .5478396 |
      2. | -.4559489 1 .4023404 -.3995853 -.5798199 -.8055198 |
      3. | .0065533 .4023404 1 -.1479982 -.1572433 -.4003441 |
      4. | .1112435 -.3995853 -.1479982 1 .6607842 .4794664 |
      5. | .3232065 -.5798199 -.1572433 .6607842 1 .669138 |
      6. | .5478396 -.8055198 -.4003441 .4794664 .669138 1 |
      +-----------------------------------------------------------------------+
      
      .
      Thank for your answer. Because one correlation matrix to one half year, there are 20+ correlation matrixs in all. These data need to be saved to a dta file. Your answer is the solution to one correlation matrix, not the solution to me. Thanks again.

      Comment


      • #4
        There is a corr() function in egenmore fromSSC that may help here.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          There is a corr() function in egenmore fromSSC that may help here.
          Thank you very much.If the data can be saved as the dta file, it's so much better.

          Comment

          Working...
          X