Announcement

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

  • Generating mean for each year and the five preceding years

    Hi,

    I'm struggling to come up with the code for this thing that I'm trying to do, and I was wondering if you had any ideas.

    I need GDP growth for the years of 2014, 2017 and 2021. However, to reduce noise/ shocks, I want to take the average of five years before each of these years. For instance, for 2014, I’d associate it with the growth rate over 2009-2014; 2012-2017 for 2017 data; and 2016-2021 for 2021 data. To leave it even clearer, basically, all I need is an average of 1) the years from 2009 to 2014 (this would be the value I'd use for 2014), 2) the years from 2012 to 2017 (this would be my 2017 value), and 3) 2016 to 2021 (this would be my 2021 value).


    I initially had this but didn't work:
    egen avg_GDP = mean(ny_gdp_mktp_kd_zg) if inrange(year, year-5, year)

    I also tried:
    rolling avg_GDP = (sum(GDP) - sum(GDP[_n-6])) / 6, window(6) by(year)

    but I get an error: '' found where ':' expected

    I attached my .do file where you can actually get the data directly from the world bank using the wbopendata function

    Any thoughts?


    Thank you!
    Attached Files

  • #2
    Code:
    rangestat (mean) wanted = ny_gdp_mktp_kd_zg, interval(year -5 0)
    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    Comment


    • #3
      Clyde Schechter Thank you for this. It looks like there is something wrong because I'm some small numbers that don't make sense. For instance, for this screenshot I have, the weighted mean for the 2014 value should be approximately 5.47 (8.3+6.87+4.85+1.88+5.2+5.7/6) and not 3.05.

      Click image for larger version

Name:	Screenshot 2023-04-01 at 8.53.40 PM.png
Views:	1
Size:	125.1 KB
ID:	1708104

      Comment


      • #4
        Does it maybe has something to do with the fact that I'm not specifying that I have different groups (countries in this case)?

        Comment


        • #5
          Does it maybe has something to do with the fact that I'm not specifying that I have different groups (countries in this case)?
          Good grief, yes! You didn't mention anything about that in your post. What you are getting is the mean for all observations (all countries included) from years 2009 through 2014. To do it separately by country, just add the -by(country)- option to the -rangestat- command. (Replace country by whatever the actual variable name is.)

          Comment

          Working...
          X