Announcement

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

  • Mean over a certain period of time

    Hi everyone

    In my dataset I have the number of terrorist attacks, each year, in each country.
    What I would like to generate is a variable that would be the average number of attacks by country every 5 years (like average number of attacks between 1995 and 2000, then 2001 and 2006 and so on)
    The idea would be to run something like this:

    egen terroraverage= sum(terror), by (country 5years)

    I know this command is not correct since i dont have any variable "5 years", it is just to give you the insight.

    How can i do it please ?

    Thanks in advance,
    Killian Foubert
    Last edited by Killian Foubert; 07 Mar 2017, 06:00.

  • #2
    see
    Code:
    h rolling

    Comment


    • #3
      Your question shows some minor confusion between 5 and 6. I will guess that you want 1995-99, 2000-04, etc.

      Here is an analogue:

      Code:
      . webuse grunfeld
      
      . su year
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
              year |        200      1944.5    5.780751       1935       1954
      
      . gen quinq = 5 * floor(year/5)
      
      . egen mean = mean(invest) , by(company quinq)
      
      . tabdisp company quinq, c(mean) format(%2.1f)
      
      ------------------------------------------
                |             quinq            
        company |   1935    1940    1945    1950
      ----------+-------------------------------
              1 |  341.7   493.7   580.5  1016.2
              2 |  305.6   386.0   399.8   550.6
              3 |   49.6    79.5   129.1   151.0
              4 |   56.7    58.3    78.8   150.7
              5 |   52.2    52.4    63.7    79.0
              6 |   24.9    35.0    54.6   107.1
              7 |   27.9    45.3    48.4    68.7
              8 |   23.1    39.0    46.0    63.4
              9 |   26.1    37.9    47.3    56.3
             10 |    2.2     1.6     3.5     5.1
      ------------------------------------------
      Each quinquennium has the value of its starting year. Clearly you can get fancier by defining value labels.

      You said average, but your code was for sums. You can use other egen functions in place of mean().

      Comment

      Working...
      X