Announcement

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

  • Calculate the rolling standard deviation for only even years

    Dear all,

    We are stuck with a problem regarding the calculation of rolling averages only for even firm years over a period of 5 years, using the current and previous years. We used the rangestat command but we were not able to restrict this calculation to only run for the even years.

    this is the code we ran;

    ssc install rangestat
    rangestat (sd) two_rev, interval (year -8 0) by (Company)

    Company-year-expenses-volatility(wanted)
    1-2003-2-.
    1-2004-3-*
    1-2005-4-.
    2-2002-5-
    *
    2-2003-5-.
    2-2004-6-*
    3-2000-2-*
    3-2001-4-.
    3-2002-5-
    *
    3-2003-6-.
    3-2004-5-
    *
    3-2005-8-.
    3-2006-5-
    *
    3-2007-8-.
    3-2008-12-value
    3-2009-3
    3-2010-6-value
    3-2011-6
    3-2012-5-value

    *=missing value

    For some reason rangestat gives for the years 2008, 2010 and 2012 the same value. It seems like the whole interval is not taken into account.

    We hope someone can help us with this problem!

    Thanks in advance,

    Jonathan

  • #2
    The reason you are getting the same result for company 3 in years 2012 and 2010 is that, although the standard deviations are based on different sets of observations, the actual values in those two sets are the same. In years 2012 and 2011, expenses = 5 and 6, respectively. These are not considered when doing the calculation for year 2010, but in their place we include the observations for 2002 and 2003 which are, it turns out, 5 and 6, respectively.

    I cannot reproduce your claim that the value is the same in 2008: I get a different value applying your code to your data. I don't know what you are doing differently from what you show, but what you show does not bear out your claim.

    As for being interested in this only for even numbered years, probably the simplest solution is to generate them for all years and then replace them with missing values in the odd numbered years:

    Code:
    replace volatility = . if mod(year, 2) == 1
    Yes, there is a way to force -rangestat- to only calculate them in the even numbered years, but it is a bit cumbersome and I don't think it will offer any advantage that makes it worth the extra trouble.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X