Announcement

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

  • Moving Average in Time Series

    Hi All,

    I want to calculate the backward 12-month moving average of bse_monthly_market_volatility. I have monthly data from 1st April 1996 to 2nd August 2021.

    Data example:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(cal_year month f_year bse_monthly_market_volatility)
    1996 4 1997 0
    1996 5 1997 1.0065117e-08
    1996 6 1997 6.276069e-09
    1996 7 1997 1.8687086e-08
    1996 8 1997 6.583165e-09
    1996 9 1997 8.063149e-09
    1996 10 1997 4.609707e-08
    1996 11 1997 7.338414e-09
    1996 12 1997 2.1926034e-08
    1997 1 1997 7.61335e-08
    1997 2 1997 2.1099375e-08
    1997 3 1997 6.771564e-08
    1997 4 1998 1.8926487e-08
    1997 5 1998 4.0691464e-09
    1997 6 1998 5.512927e-09
    1997 7 1998 1.364935e-08
    1997 8 1998 2.023477e-08
    1997 9 1998 1.114334e-08
    1997 10 1998 6.87759e-09
    1997 11 1998 1.778921e-08
    1997 12 1998 1.5036544e-08
    1998 1 1998 1.6176594e-08
    1998 2 1998 1.584109e-08
    1998 3 1998 4.278935e-08
    1998 4 1999 2.7780535e-08
    1998 5 1999 3.1044998e-08
    1998 6 1999 1.3318476e-07
    1998 7 1999 2.8238917e-08
    1998 8 1999 1.0505955e-08
    1998 9 1999 1.850262e-08
    1998 10 1999 4.066442e-08
    1998 11 1999 1.273221e-08
    1998 12 1999 1.5956537e-08
    1999 1 1999 2.2932744e-08
    1999 2 1999 2.8248737e-08
    1999 3 1999 2.3658366e-08
    1999 4 2000 6.469824e-08
    1999 5 2000 5.150994e-08
    1999 6 2000 1.6759108e-08
    1999 7 2000 2.5588095e-08
    1999 8 2000 2.201457e-08
    1999 9 2000 1.1783286e-08
    1999 10 2000 4.956478e-08
    1999 11 2000 3.3246504e-08
    1999 12 2000 1.908567e-08
    2000 1 2000 4.075007e-08
    2000 2 2000 5.496526e-08
    2000 3 2000 5.50728e-08
    2000 4 2001 1.2515643e-07
    2000 5 2001 2.4028049e-07
    2000 6 2001 2.5738565e-08
    2000 7 2001 3.739462e-08
    2000 8 2001 2.1040755e-08
    2000 9 2001 4.114714e-08
    2000 10 2001 5.200905e-08
    2000 11 2001 2.5289063e-08
    2000 12 2001 2.976093e-08
    2001 1 2001 2.72495e-08
    2001 2 2001 2.8541077e-08
    2001 3 2001 1.4598437e-07
    2001 4 2002 7.922798e-08
    2001 5 2002 1.437668e-08
    2001 6 2002 1.6895315e-08
    2001 7 2002 1.2099663e-08
    2001 8 2002 3.588571e-09
    2001 9 2002 7.878815e-08
    2001 10 2002 1.891207e-08
    2001 11 2002 1.2319823e-08
    2001 12 2002 2.164034e-08
    2002 1 2002 1.417156e-08
    2002 2 2002 1.9747054e-08
    2002 3 2002 1.5540722e-08
    2002 4 2003 2.093092e-08
    2002 5 2003 1.7873253e-08
    2002 6 2003 1.159552e-08
    2002 7 2003 2.1044167e-08
    2002 8 2003 4.287819e-09
    2002 9 2003 5.327618e-09
    2002 10 2003 9.99424e-09
    2002 11 2003 2.873351e-09
    2002 12 2003 1.6016847e-0

    I have used the following command: tssmooth ma bse_ma_mnthly_market_volatility = bse_monthly_market_volatility, window(12)

    But the following error is coming: window() invalid -- invalid numlist has elements outside of allowed range
    r(125);

    Kindly help me with the code.



  • #2
    Code:
    gen yearmonth= ym(cal_year, month)
    format yearmonth %tm
    tsset yearmonth

    Comment


    • #3
      Thank you, Andrew. The code worked.

      I also want to calculate the 12-month average in the case of panel data. I tried running your code, but an error, "repeated time values in sample r(451);" is coming due to panel data.

      Can you suggest what changes need to be made to your code so that tsset command works?

      Comment


      • #4
        You need to tsset using the panel identifier and time variable.

        Comment


        • #5
          Nick- I used the tsset using the panel identifier and time variable.

          In my database the panel variable is coming unbalanced and the time variable is coming with gaps. Is it a cause of concern? Will it affect my result?

          tsset ind_num2 yearmonth
          panel variable: ind_num2 (unbalanced)
          time variable: yearmonth, 1996m4 to 2021m8, but with gaps
          delta: 1 month

          Comment


          • #6
            The smoother will do what it can given the gaps.

            Comment


            • #7
              Okay,

              Thank you, Nick.

              Comment

              Working...
              X