Announcement

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

  • Centered moving average in panel data

    How do I create a centered moving average in panel data? I've consulted this document: https://www.stata.com/support/faqs/s...nd-panel-data/ -- specifically the section on panel data. But I'm not sure how / whether to use to tsset. I want to take the centered moving average of a variable over five consecutive months, across my main time variable: year_month. Thanks very much.

  • #2
    First, your time variable must be a true Stata internal format (monthly) date variable. If it isn't, your first task is to create such a variable.

    With that behind you, having panel data you will want to xtset your data. (In fact, if you try to -tsset- it you will probably get error messages as multiple observations with the same time will be detected.)

    Once you have -xtset- your data, it's just
    Code:
    tssmooth ma wanted = variable_to_be_smoothed, window(-2 1 2)

    Comment


    • #3
      The FAQ cited in #1 is flagged as for users of Stata 7.

      Essentially if you

      xtset identifier time

      or

      tsset identifier time

      then tssmooth will do the right thing.

      Another way to do it is with rangestat (SSC). This doesn't require, or check for, declaring your data as time series or panel data, but it can happily be used with such data.


      Code:
      . webuse grunfeld, clear
      
      . xtset
      
      Panel variable: company (strongly balanced)
       Time variable: year, 1935 to 1954
               Delta: 1 year
      
      . tssmooth ma mean=invest , w(2 1 2)
      The smoother applied was
           by company : (1/5)*[x(t-2) + x(t-1) + 1*x(t) + x(t+1) + x(t+2)]; x(t)= invest
      
      . rangestat (mean) mean_rs = invest, i(year -2 2) by(company)
      
      . scatter mean_rs mean
      
      . d
      
      Contains data from https://www.stata-press.com/data/r17/grunfeld.dta
       Observations:           200                  
          Variables:             8                  3 Mar 2020 20:27
      -----------------------------------------------------------------------------------------
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      -----------------------------------------------------------------------------------------
      company         byte    %9.0g                 Company
      year            int     %ty                   Year
      invest          float   %9.0g                 Investment (current year)
      mvalue          float   %9.0g                 Market value (prior year)
      kstock          float   %9.0g                 Capital stock (prior year)
      time            byte    %9.0g                 Time
      mean            float   %9.0g                 ma: x(t)= invest : window(2 1 2)
      mean_rs         double  %10.0g                mean of invest
      -----------------------------------------------------------------------------------------
      Sorted by: company  year
           Note: Dataset has changed since last saved.
      Detail: rangestat produces doubles and tssmooth floats by default. For most applications, any differences will be immaterial.

      Comment


      • #4
        Thank you both very much.

        Comment

        Working...
        X