Announcement

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

  • Rolling window of standard deviation in panel data, normalizing the window

    Hi everyone,
    I am new to Stata and have a specific question which hasn't been asked previously. I would like to determine the drivers of capital flow volatility (for FDI, portfolio flows etc) in a panel regression framework. I have quarterly data from 1990q1 to 2015q4 for about 30 countries. In order to obtain capital flow volatility I have to determine the rolling window standard deviation of capital flows (in % of GDP) over 16 quarters. I was able to obtain the std. dev. RW volatility values using the asrol code. However in addition I would like to normalize the capital flow size in each window to account for sudden and inflated capital flows. This has been done before in previous studies by setting the largest flow in the window in absolute terms at 100 and adjusting the rest of the flows in the window accordingly.
    However, I have no idea how to do that in Stata and whether it even is possible. Any help would be greatly appreciated, thanks!

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long country1 float time double(FDI_Y portf_Y growth cpiinf)
    1 120 .17231784760951996 -.9790787100791931 . 2313.96466339752
    1 121 .297639936208725 -.1853722333908081 . 2313.96466339752
    1 122 .5561167001724243 -.297639936208725 . 2313.96466339752
    1 123 3.7674949169158936 -1.3263252973556519 . 2313.96466339752
    1 124 .35033947229385376 .6481280326843262 8.01972920571854 171.671696468306
    1 125 .8563854098320007 .04281926900148392 12.6232550936542 171.671696468306
    1 126 1.9112964868545532 .6228257417678833 10.3181134926589 171.671696468306
    1 127 1.6290786266326904 15.704939842224121 11.0739362271975 171.671696468306
    1 128 .9140308499336243 1.9101029634475708 14.7219244954837 24.8999485988255
    1 129 .8333814740180969 .824242889881134 12.1971686945041 24.8999485988255
    1 130 1.8140782117843628 1.7009556293487549 8.71265614740751 24.8999485988255
    1 131 3.585623025894165 .8046157360076904 3.86171819523977 24.8999485988255
    1 132 .713859498500824 1.5377459526062012 5.54761931043747 10.6114940961305
    1 133 .7013773322105408 38.629981994628906 4.27610081101725 10.6114940961305
    1 134 1.4815151691436768 6.997681617736816 5.54071690729179 10.6114940961305
    1 135 1.4612315893173218 8.938626289367676 7.53490991157804 10.6114940961305
    1 136 .9901248812675476 4.15770959854126 22.4869436613209 4.17734724366985
    1 137 1.3512213230133057 -.11289051175117493 18.4146986836012 4.17734724366985
    1 138 1.1978986263275146 6.422641277313232 14.7651310963296 4.17734724366985
    1 139 1.6693300008773804 5.218678951263428 15.6788995316809 4.17734724366985

  • #2
    asrol is from SSC, as you are asked to explain (FAQ Advice #12). You don't show any of the code you used. At a guess you're doing this separately by country.

    This seems to break down into finding the maximum, scaling and then calculating the SD. No doubt asrol could help there but for variety's sake I will use a command I know better, rangestat from SSC. Here is an example with two panels, to show how and that it works. Clearly your names and numbers will differ.

    Code:
    webuse grunfeld, clear 
    
    rangestat (max) max=invest, int(year -4 0) by(company) 
    
    gen new = 100 * invest/max 
    
    rangestat (count) count=new (sd) vol=new, int(year -4 0) by(company) 
    
    list company invest new count vol in 1/30, sepby(company) 
    
         +-------------------------------------------------+
         | company   invest        new   count         vol |
         |-------------------------------------------------|
      1. |       1    317.6        100       1           . |
      2. |       1    391.8        100       2           0 |
      3. |       1    410.6        100       3           0 |
      4. |       1    257.7   62.76181       4   18.619093 |
      5. |       1    330.8   80.56503       5   16.748368 |
      6. |       1    461.2        100       5   16.748368 |
      7. |       1      512        100       5   16.748368 |
      8. |       1      448       87.5       5   15.521481 |
      9. |       1    499.6   97.57813       5   8.7142856 |
     10. |       1    547.5        100       5   5.4217849 |
     11. |       1    561.2        100       5   5.4217849 |
     12. |       1    688.1        100       5   5.4217849 |
     13. |       1    568.9   82.67694       5    7.549526 |
     14. |       1    529.2   76.90743       5   11.254671 |
     15. |       1    555.1   80.67141       5   11.102648 |
     16. |       1    642.9   93.43119       5   9.6256846 |
     17. |       1    755.9        100       5   9.6256846 |
     18. |       1    891.2        100       5   10.839822 |
     19. |       1   1304.4        100       5   8.4054833 |
     20. |       1   1486.7        100       5   2.9376609 |
         |-------------------------------------------------|
     21. |       2    209.9        100       1           . |
     22. |       2    355.3        100       2           0 |
     23. |       2    469.9        100       3           0 |
     24. |       2    262.3   55.82038       4   22.089808 |
     25. |       2    230.4   49.03171       5   26.167634 |
     26. |       2    361.6   76.95255       5   23.910048 |
     27. |       2    472.8        100       5   23.910048 |
     28. |       2    445.6   94.24704       5   22.590095 |
     29. |       2    361.6   76.48055       5   19.884192 |
     30. |       2    288.2   60.95601       5   15.591733 |
         +-------------------------------------------------+

    Comment


    • #3

      The asrol version of Nick's solution is given below for those who are interested.

      Code:
      webuse grunfeld, clear
      
       bys company: asrol invest, stat(max) wind(year 5)  
      
      gen new = 100 * invest/max  
      
      bys company: asrol new, stat(count sd) wind(year 5)
      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


      • #4
        Thank you very much Nick and Attaulllah!

        Comment

        Working...
        X