Announcement

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

  • Standard deviation with rolling window

    Hello,
    I'm working on a quarterly data of 244000 observations from 2000 to 2016, and I want to calculate the standard deviation of one variable using 1 year rolling windows.
    Thanks
    I'm using Stata/SE 13.0

  • #2
    Evidently you have a panel dataset as there are at most 68 quarters in that time span.

    Lacking a data example -- please do read and act on the FAQ Advice -- I used one of Stata's datasets which is quarterly and pretended that it is just a single panel to illustrate syntax.


    Code:
    . webuse turksales, clear 
    
    . gen panel = 42 
    
    . 
    . rangestat (count) n=sales (sd) sd = sales, interval(t -3 0) by(panel) 
    
    . 
    . list in 1/16, sep(4) 
    
         +-------------------------------------------+
         |      t      sales   panel   n          sd |
         |-------------------------------------------|
      1. | 1990q1        100      42   1           . |
      2. | 1990q2   97.84603      42   2    1.523086 |
      3. | 1990q3   98.84029      42   3    1.078043 |
      4. | 1990q4   100.8275      42   4   1.3069043 |
         |-------------------------------------------|
      5. | 1991q1   98.90981      42   4   1.2463517 |
      6. | 1991q2   100.9992      42   4   1.1792543 |
      7. | 1991q3   101.9653      42   4   1.2792025 |
      8. | 1991q4   104.1229      42   4   2.1645411 |
         |-------------------------------------------|
      9. | 1992q1   99.74297      42   4    1.849452 |
     10. | 1992q2   102.3116      42   4   1.7975867 |
     11. | 1992q3   104.2382      42   4    2.101481 |
     12. | 1992q4   105.8506      42   4   2.6290937 |
         |-------------------------------------------|
     13. | 1993q1   102.8379      42   4   1.5850987 |
     14. | 1993q2    104.604      42   4   1.2397534 |
     15. | 1993q3   106.5201      42   4   1.6183092 |
     16. | 1993q4   106.9322      42   4   1.8864034 |
         +-------------------------------------------+
    
    . 
    . su sales in 1/4 
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           sales |          4    99.37845    1.306904   97.84603   100.8275
    Notes:

    1. You need to install rangestat using ssc inst rangestat if you have not already done so. Searching the forum for mentions will give you other examples.

    2. I've put the sd for each window in the last quarter of the window, but tuning the interval() call allows other possibilities.

    3. Whether 4 is enough to get a good idea of variability is your concern, but keeping track of the number of values used is important, especially at the beginning (or as the case may be) at the end of eachpanel.


    English is an odd language even to people who have it as their first language. FWIW, my advice is that while "my data is" often sounds fine, "I have a data" does not sound at all idiomatic to me. "I have a dataset" is fine: I haven't yet heard that to be disconcerting to anyone. Naturally, I appreciate that English may be your second, third or even fourth language. This isn't intended to be a correction, just a detail that you and others might find helpful in writing or presentation.

    Comment


    • #3
      Thank you so much Nick for your reply

      Comment

      Working...
      X