Announcement

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

  • How to do moving sum?

    Hi,

    I am trying to do a 3 month moving sum for variables, my data is weekly data, can anyone help me? Thank you.

    Best,
    Xixi Lin

  • #2
    Since you do not describe your data, I will assume that you have a variable, week, identifying the week, and you have a variable, x, that you want the rolling sum of.

    Code:
    tsset week
    sort week
    gen cum_sum_x = sum(x)
    gen moving_sum = cum_sum_x - L13.cum_sum_x
    Notes:
    1. The 13th lag is used because 3 months = 13 weeks.
    2. This is the moving sum of the past 13 weeks (including the current week). If you want a moving sum centered around the given week it would be F6._cum_sum_x - L7.cum_sum_x. Similar modifications could be made to get a moving sum of the coming 13 weeks.

    Comment


    • #3
      If you have no missings, you could just do a moving average and multiply by 13. See help tssmooth.
      Last edited by Nick Cox; 20 Feb 2015, 11:41.

      Comment


      • #4
        Thank you so much.

        Best,
        Xixi Lin

        Comment

        Working...
        X