Announcement

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

  • Rolling Windows

    Hi everyone,

    I need to create a 50 day rolling average and standard deviation for variable low, where I'm expected to lose the first 50 observations (that is, the rolling average/st. deviation variable only starts at observation 51).

    I've tried a number of commands, namely:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    tsegen average = rowmean(L(0/49).low)
    tssmooth ma average  = low, window(50)
    mvsumm low, generate(average) stat(mean) window(50)
    movavg average = low, lags(50)
    rangestat (mean) low (sd) low, interval(date -49 0) 
    end
    But all of them just start at observation 1. What are the rolling windows at observation 1? I'm confused because observation 1 doesn't have 50 past observations? Are any of these codes still legitimate to use? Any advice on a rolling standard deviation too?

    I'm using Stata 14.1 on Mac.

    Thankful for any help!

  • #2
    What you are getting is the moving average of all of the lags from first through fiftieth, however many exist. So in the first observation -tssmooth- will give you missing value, in the second observation you will get the first value, in the third observation you will get the average of the first two values, etc. When you finally reach observation 51, you will get a moving average based on all of the 50 first lags, and that will continue to the end of your data.

    By the way, your -tsegen- and -rangestat- commands will give you not the first 50 lags, but the current observation and the first 49 lags. The -tssmooth- command will work with the first 50 lags and excludes the current observation. I don't know about the other commands because I am not familiar with them.

    Anyway, if you need to discard values of the moving average that are not based on a full set of 50 lags, you can write some code to replace those results with missing values. For example, using -rangestat- the approach is illustrated by the following code in the grunfeld on-line data set (since you didn't show example data from your own) for a 5 year lagging moving average:

    Code:
    webuse grunfeld, clear
    
    rangestat (mean) mvalue (count) cmv = mvalue, by(company) interval(year -5 -1)
    replace mvalue_mean = . if cmv < 5

    Comment


    • #3
      You can use the min() option of asrol. to specify the minimum number of observation for calculating the required statistics. The new beta version of asrol
      accepts a flexible window. The new version can be installed from my site. Copy and paste the following line in Stata and press enter.

      Code:
      net install asrol, from(http://fintechprofessor.com) replace
      Using the Clyde's example, here is the asrol code:
      Code:
      bys company : asrol mvalue, stat(mean) window(year -6 -1) min(5)
           +--------------------------------------------------------------------+
           | company   year   invest   mvalue   kstock   time   cmv   mean_6_~e |
           |--------------------------------------------------------------------|
        1. |       1   1935    317.6   3078.5      2.8      1     .           . |
        2. |       1   1936    391.8   4661.7     52.6      2     1           . |
        3. |       1   1937    410.6   5387.1    156.9      3     2           . |
        4. |       1   1938    257.7   2792.2    209.2      4     3           . |
        5. |       1   1939    330.8   4313.2    203.4      5     4           . |
           |--------------------------------------------------------------------|
        6. |       1   1940    461.2   4643.9    207.2      6     5   4046.5401 |
        7. |       1   1941      512   4551.2    255.2      7     5   4359.6201 |
        8. |       1   1942      448   3244.1    303.7      8     5   4337.5201 |
        9. |       1   1943    499.6   4053.7    264.1      9     5   3908.9201 |
       10. |       1   1944    547.5   4379.3    201.6     10     5   4161.2201 |
           +--------------------------------------------------------------------+
      You can find more about asrol here https://fintechprofessor.com/asrol-f...tics-in-stata/
      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 so much to both. This has been very helpful

        Comment

        Working...
        X