Announcement

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

  • Calculating Rolling Statistics for Panel Data With a Varying Window of Observation

    Hello Statalist,


    I would like to create some code which can calculate various statistics (moving average, moving standard deviation, perhaps others as well) for each unit in my panel data. I will use the moving average to explain the problem. It would be fairly straightforward to calculate a moving average for each unit in the panel if I could specify a fixed period of time over which the average was to be calculated (i.e if I could say for each unit, calculate a 3 month moving average for example). However, I would like this period to vary according to the characteristics of each unit's data series in such a way that the fewer the positive, non-zero observations in the series for a given unit, the longer the period over which the average is to be taken will be. I would like to do the same for the standard deviation. The two will be calculated for the same periods within a single unit. Both calculations will be trailing.

    My data is monthly. I have calculated the number of months over which the moving average should be calculated for each unit and I have generated a variable which holds this information. At the moment, I would like the number of months in the moving average to be time invariant but unit variant. Later, I may also need the period to be time variant as well so it would be extra useful if any one has a solution that is capable of integrating this additional level of flexibility.


    Thank you very much for reading this, your help will be very much appreciated!

    Charlie








  • #2
    Welcome to Statalist. One of the first things you need to learn as a forum member is that it is pretty much pointless to ask for help coding something in Stata without also showing an example of your data. The details of the code are crucial, and they often depend on fine details of the data itself. So please post back with a representative example of your data. Be sure to use the -dataex- program to do that. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      I use rangestat and rangerun for rolling statistics and they can do what you need. They are user written procedure so you need to install them by writing SSC install rangestat, SC install rangerun, then write help rangestat or rangerun to see their help files. That should get you started.

      Comment


      • #4
        Thank you Oscar! This is exactly what I needed.

        Comment

        Working...
        X