Announcement

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

  • Rolling Standard Deviation

    Hi Stata Experts,
    even though I have noticed multiple threads on this issue, I am afraid I dont fully understand the instructions and, hence, am not able to reproduce them with my dataset.
    My dataset consists of 1000+ Banks, with information on their return on assets (RoA) over a range of 16 quarters.
    I want to calculate the 3-quarter rolling standard deviation of RoA for each bank in each quarter. How do I implement that?
    Also, I do not have observations in every quarter for some banks, would I have to account for that, or does stata simply skip those banks where there is less than 3 quarters of data?

    Help is much appreciated! Thank you in advance,
    Chris

  • #2
    If you provide an example data, you shall get more direct help. Anyhow, you can use asrol for rolling standard deviation, with minimum three observation. I assume that you named your time variable as quarter
    Code:
    ssc install asrol
    
    bys bank: asrol RoA, stat(sd) window(quarter 3) min(3)
    Please note, if your data has missing values, asrol takes care of that. If you do not want a minimum of three observations for calcualtion of the standard deviation, then remove the option min(3), so your code will look like.

    Code:
    bys bank: asrol RoA, stat(sd) window(quarter 3)
    . For more details of asrol, type:
    Code:
    help asrol
    Last edited by Attaullah Shah; 18 Apr 2018, 07:31.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Dear Attaullah, please excuse the missing data example, I am new to statalist.
      Anyhow, your solution worked perfectly! Thank you a lot!
      Best,
      Chris

      Comment


      • #4
        Here's another way to do it . The style is similar -- but also different. With rangestat (SSC) you can specify that you want several statistics. Here I keep track of the count of the number of observations used. I didn't see a precise specification of how your three quarters are to be set-up. With rangestat you can have them centred on each observation (one past, one now, one future), backwards, forwards,etc.

        The example dataset here isn't panel data, but I pretend that it is and show you the syntax.

        Code:
        . webuse turksales, clear  
        
        . gen whatever = 42 
        
        . rangestat (count) sales (sd) sales, interval(t -2 0) by(whatever) 
        
        . list in 1/12, sep(4)
        
             +-----------------------------------------------------+
             |      t      sales   whatever   sales_~t    sales_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          3   1.5180421 |
             |-----------------------------------------------------|
          5. | 1991q1   98.90981         42          3   1.1277854 |
          6. | 1991q2   100.9992         42          3   1.1599403 |
          7. | 1991q3   101.9653         42          3   1.5617701 |
          8. | 1991q4   104.1229         42          3   1.5992399 |
             |-----------------------------------------------------|
          9. | 1992q1   99.74297         42          3   2.1900249 |
         10. | 1992q2   102.3116         42          3   2.2008343 |
         11. | 1992q3   104.2382         42          3    2.255268 |
         12. | 1992q4   105.8506         42          3   1.7718276 |
             +-----------------------------------------------------+

        Comment


        • #5
          Hi Nick, great! I also tried the way you suggested and it worked out as well.
          Thank you!

          Comment

          Working...
          X