Announcement

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

  • generating correlation variable for every 5 obs

    hello stata community

    I need to create a code that would allow me to get a correlation coefficient for every 5 observations
    so it`ll be a bit lagged. but I don`t know how to get this code started
    any help, please??

  • #2
    This could mean several things. Here is one, using rangestat from SSC.

    Code:
    . webuse grunfeld, clear
    
    . rangestat (corr) invest mvalue, int(year -4 0) by(company)
    
    . l invest mvalue year corr_* in 1/20
    
         +-----------------------------------------------+
         | invest   mvalue   year   corr_n~s      corr_x |
         |-----------------------------------------------|
      1. |  317.6   3078.5   1935          1           . |
      2. |  391.8   4661.7   1936          2           1 |
      3. |  410.6   5387.1   1937          3   .99281671 |
      4. |  257.7   2792.2   1938          4   .95894557 |
      5. |  330.8   4313.2   1939          5   .93163887 |
         |-----------------------------------------------|
      6. |  461.2   4643.9   1940          5   .83720993 |
      7. |    512   4551.2   1941          5   .71138893 |
      8. |    448   3244.1   1942          5   .61736255 |
      9. |  499.6   4053.7   1943          5   .04940336 |
     10. |  547.5   4379.3   1944          5   .49006492 |
         |-----------------------------------------------|
     11. |  561.2   4840.9   1945          5   .92462252 |
     12. |  688.1   4900.9   1946          5   .86023499 |
     13. |  568.9   3526.5   1947          5   .51363863 |
     14. |  529.2   3254.7   1948          5   .59707088 |
     15. |  555.1   3700.2   1949          5    .6908442 |
         |-----------------------------------------------|
     16. |  642.9   3755.6   1950          5     .880105 |
     17. |  755.9     4833   1951          5   .95136503 |
     18. |  891.2   4924.9   1952          5   .94160264 |
     19. | 1304.4   6241.7   1953          5   .97052548 |
     20. | 1486.7   5593.6   1954          5   .85586191 |
         +-----------------------------------------------+

    Comment


    • #3
      Nick Cox thank you it works perfect, but I need to modify it
      I would like to get 2 correlation variables. but the code you gave me let me create only one correlation because the correlation nobs have been used already
      also I would like to get the correlation of only the last week of each month or the last 5 days of the month.
      is it possible to do that?
      TIA

      Comment


      • #4
        Your next application of rangestat can and should specify new variable names for new results. See the syntax diagram right at the top of the help for that command, which shows that you can specify new variable names. New names are needed when you don't like rangestat's defaults or those defaults have already been used.

        Wanting the last five days of the month is just a matter of identifying them with an if condition. As you have not given a data example or mentioned any of your variable names, I suggest that you do that if you want detailed advice. See https://www.statalist.org/forums/help#stata for how to give a data example.

        While you're visiting please swing by https://www.statalist.org/forums/help#realnames

        Comment

        Working...
        X