Announcement

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

  • Moving Average

    Dear Statalisters,

    I can't find a solution to a rather simple problem. I have got balanced panel data. id is the company identifier, and I have got 100 companies (id=1,2,...,100). Date ranges from 01/01/2000 to 12/31/2012 for each id. Another important variable is ma (number of media articles) that takes values 0, 1, 2, ... , 45. Finally, Vit is a continuous variable.

    If ma is different from zero (i.e. ma > 0), then this is an event day (t=0). I want to calculate the average Vit over the non-event period t-60 to t-10 and t+10 to t+60, relative to event day t=0.

    In other words, when ma>0, I want to calculate the average value of Vit for the period t-60 to t-10 and t+10 to t+60. Of course I want to do this by id.

    I would appreciate your help.

    Thank you in advance.





  • #2
    Code:
    xtset id date
    by id, sort: gen running_total = sum(Vit)
    by id: gen running_count = sum(!missing(Vit))
    by id: gen period_total = F60.running_total - L61.running_total - (F9.running_total-L10.running_total)
    by id: gen period_count = F60.running_count - L61.running_count - (F9.running_count-L10.running_count)
    gen period_mean = period_total/period_count
    If you only want this calculated for event days, you can just add -if ma != 0- to the last command.

    Comment


    • #3
      Hi Clyde,

      Thank you very much for your reply.

      I have been trying to understand your code.

      How would you change your code if we were interested in the period t=-1, t=0 and t=+1 (instead of t-60 to t-10 and t+10 to t+60) ?

      I guess this simple 3-day window will make things more clear to me. I would change the last 3 rows of your code as follows:

      Code:
      by id: gen period_total = F1.running_total - L2.running_total
      by id: gen period_count = F1.running_count - L2.running_count
      gen event_mean = period_total/period_count if ma_date>0

      Is this correct?





      Comment


      • #4
        Exactly.

        Comment

        Working...
        X