Announcement

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

  • Getting average of lagged variables

    I generate lagged variable for t-1 to t-20 by using the following code:

    forvalues i = 1/20 {
    by fips (year), sort: gen Damage`i' = L`i'.Damage
    replace Damage`i' = 0 if Damage`i' == .
    }

    Now I want to get the average Damage for the past 3, 6 and 10 years respectively.

    by fips (year), sort: egen Damage_13 = mean(Damage1 Damage2 Damage3)

    by fips (year), sort: egen Damage_49 = mean(Damage4 Damage5 Damage6 Damage7 Damage8 Damage9)

    by fips (year), sort: egen Damage_1020 = mean(Damage10 Damage11 Damage12 Damage13 Damage14 Damage15 Damage16 Damage17 Damage18 Damage19 Damage20)

    But the above code does not work. Ideally, I want

    Damage13 = (Damage_t-1 + Damage_t-2 + Damage_t-3)/3

  • #2
    See rangestat from SSC,

    Comment


    • #3
      Can you please give some pointers as to what I should be looking for under help rangestat?

      Comment


      • #4
        You do not need to create the lagged variables. As Nick Cox suggested, you should use -rangestat- for this. For example,
        Code:
        rangestsat (mean) damage_1_3 = Damage, by(fips) interval(year -3 -1)
        and analogous code for the others.

        -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer, and is available from SSC.

        Comment


        • #5
          Naturally I agree with Clyde Schechter's advice. It is characteristic of rangestat that it ignores missing values to the extent possible. That may or may not be what you want. It's prudent therefore to keep track of the count of non-missing values as well as the mean.

          Here is a simple example: Note that as should be expected a complete window of 3 previous values is impossible until we get to year 4. Gaps (absent values) or missing values within a panel would also reduce completeness.


          Code:
          webuse grunfeld, clear 
          
          rangestat (count) count_1_3 = invest (mean) invest_1_3 = invest, by(company) interval(year -3 -1)
          
          list invest *1_3 in 1/10 
          
          
          
               +-------------------------------+
               | invest   count_~3   invest_~3 |
               |-------------------------------|
            1. |  317.6          .           . |
            2. |  391.8          1   317.60001 |
            3. |  410.6          2       354.7 |
            4. |  257.7          3   373.33333 |
            5. |  330.8          3   353.36667 |
               |-------------------------------|
            6. |  461.2          3   333.03334 |
            7. |    512          3       349.9 |
            8. |    448          3   434.66667 |
            9. |  499.6          3   473.73334 |
           10. |  547.5          3   486.53334 |
               +-------------------------------+
          Backing up, why in #1 did an egen call like

          Code:
          mean(Damage1 Damage2 Damage3)
          not work? The answer is clear in the help for egen. mean() takes an expression, but a varlist with two or more variable names is not an expression. rowmean() would work, but the larger point -- as made by Clyde -- is that generating a bundle of lagged variables is not needed at all.

          Comment

          Working...
          X