Announcement

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

  • estimating lagged standard deviation of past three years

    I have a panel data setting. I want to create another variable i.e. the standard deviation of investment from t-3 to t-1. In other words a new variable must compute the standard deviation of investment in past three years. please help.

    My data looks as follows:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int unitid str18 firmid int time float Investment
    1 "0876711D LN Equity" 2003           .
    1 "0876711D LN Equity" 2004           .
    1 "0876711D LN Equity" 2005           .
    1 "0876711D LN Equity" 2006           .
    1 "0876711D LN Equity" 2007           .
    1 "0876711D LN Equity" 2008           .
    1 "0876711D LN Equity" 2009   .04298013
    1 "0876711D LN Equity" 2010 -.016369443
    1 "0876711D LN Equity" 2011 -.012404328
    1 "0876711D LN Equity" 2012  -.05723596
    1 "0876711D LN Equity" 2013           .
    1 "0876711D LN Equity" 2014           .
    1 "0876711D LN Equity" 2015           .
    1 "0876711D LN Equity" 2016           .
    1 "0876711D LN Equity" 2017           .
    1 "0876711D LN Equity" 2018           .
    2 "1218069D LN Equity" 2003           .
    2 "1218069D LN Equity" 2004    .4360955
    2 "1218069D LN Equity" 2005   .26635048
    2 "1218069D LN Equity" 2006   .23388498
    2 "1218069D LN Equity" 2007   .21420796
    2 "1218069D LN Equity" 2008    .2275067
    2 "1218069D LN Equity" 2009   .16295853
    2 "1218069D LN Equity" 2010   .11736838
    2 "1218069D LN Equity" 2011    .0952867
    2 "1218069D LN Equity" 2012           .
    2 "1218069D LN Equity" 2013           .
    2 "1218069D LN Equity" 2014           .
    2 "1218069D LN Equity" 2015           .
    2 "1218069D LN Equity" 2016           .
    2 "1218069D LN Equity" 2017           .
    2 "1218069D LN Equity" 2018           .
    3 "1334987D LN Equity" 2003           .
    3 "1334987D LN Equity" 2004  .005009345
    3 "1334987D LN Equity" 2005  .016525032
    3 "1334987D LN Equity" 2006  .009007737
    3 "1334987D LN Equity" 2007 .0027638024
    3 "1334987D LN Equity" 2008  .025354864
    3 "1334987D LN Equity" 2009  .007856831
    3 "1334987D LN Equity" 2010   .01920615
    3 "1334987D LN Equity" 2011           .
    3 "1334987D LN Equity" 2012           .
    3 "1334987D LN Equity" 2013           .
    3 "1334987D LN Equity" 2014           .
    3 "1334987D LN Equity" 2015           .
    3 "1334987D LN Equity" 2016           .
    3 "1334987D LN Equity" 2017           .
    3 "1334987D LN Equity" 2018           .
    4 "1561649D LN Equity" 2003           .
    4 "1561649D LN Equity" 2004    .2383473
    4 "1561649D LN Equity" 2005   .23322147
    4 "1561649D LN Equity" 2006    .2747023
    4 "1561649D LN Equity" 2007           .
    4 "1561649D LN Equity" 2008           .
    4 "1561649D LN Equity" 2009   .16537003
    4 "1561649D LN Equity" 2010    .4768854
    4 "1561649D LN Equity" 2011   .14333837
    4 "1561649D LN Equity" 2012   .13682278
    4 "1561649D LN Equity" 2013   .11177154
    4 "1561649D LN Equity" 2014    .2387057
    4 "1561649D LN Equity" 2015           .
    4 "1561649D LN Equity" 2016           .
    4 "1561649D LN Equity" 2017           .
    4 "1561649D LN Equity" 2018           .
    5 "1638414D LN Equity" 2003           .
    5 "1638414D LN Equity" 2004   .12095035
    5 "1638414D LN Equity" 2005    .1120581
    5 "1638414D LN Equity" 2006   .10006714
    5 "1638414D LN Equity" 2007   .11736046
    5 "1638414D LN Equity" 2008   -.1074682
    5 "1638414D LN Equity" 2009   .10434808
    5 "1638414D LN Equity" 2010    .0954181
    5 "1638414D LN Equity" 2011   .14149284
    5 "1638414D LN Equity" 2012   .28253892
    5 "1638414D LN Equity" 2013   .15810315
    5 "1638414D LN Equity" 2014   .03957232
    5 "1638414D LN Equity" 2015           .
    5 "1638414D LN Equity" 2016           .
    5 "1638414D LN Equity" 2017           .
    5 "1638414D LN Equity" 2018           .
    6 "1655637D LN Equity" 2003           .
    6 "1655637D LN Equity" 2004  .002285661
    6 "1655637D LN Equity" 2005 .0007066662
    6 "1655637D LN Equity" 2006 .0010206974
    6 "1655637D LN Equity" 2007  .001707869
    6 "1655637D LN Equity" 2008  .010133424
    6 "1655637D LN Equity" 2009   .05300895
    6 "1655637D LN Equity" 2010  .003342496
    6 "1655637D LN Equity" 2011 .0040739654
    6 "1655637D LN Equity" 2012 .0010755167
    6 "1655637D LN Equity" 2013  .002353301
    6 "1655637D LN Equity" 2014 .0014642834
    6 "1655637D LN Equity" 2015  .003463925
    6 "1655637D LN Equity" 2016   -.1074682
    6 "1655637D LN Equity" 2017           .
    6 "1655637D LN Equity" 2018           .
    8 "3572335Q LN Equity" 2003           .
    8 "3572335Q LN Equity" 2004   .09133787
    8 "3572335Q LN Equity" 2005    .1582893
    8 "3572335Q LN Equity" 2006   .18906696
    end

  • #2
    You can use asrol or rangestat for rolling window statistics. Both the programs are available from SSC. An example is provided below using asrol:

    Code:
    ssc install asrol
    
    bys unitid : asrol Investment, stat(sd) window( time 4) xf(focal) gen(SD3)
    bys unitid : asrol Investment, stat(count) window( time 4) xf(focal)
    I am using a window of four years and then delete the current observation with option xf(focal) thereby making the calculations from year t-3 to t-1. I have also calculated the number of observations on which the calculated SD is based. This additional variable can be used to see how many observations were used in the calculations. If you want to put a minimum observation criterion, then you can use option min(#), e.g. minimum 3 observations, then
    Code:
    bys unitid : asrol Investment, stat(sd) window( time 4) xf(focal) gen(SD3) min(3)
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      thank you for the suggestion. I have attempted to apply the code but I am getting the following error:
      <istmt>: 3499 asrolw() not found
      r(3499);

      Any suggestions on that? Many thanks.

      Comment


      • #4
        Here is a corresponding example for rangestat (SSC).

        Code:
        . webuse grunfeld, clear
        
        . rangestat (count) invest (sd) invest, int(year -3 -1) by(company)
        
        . tab invest_count, missing
        
           count of |
             invest |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |         10        5.00        5.00
                  2 |         10        5.00       10.00
                  3 |        170       85.00       95.00
                  . |         10        5.00      100.00
        ------------+-----------------------------------
              Total |        200      100.00
        Wherever the count is missing or 1, the SD is missing any way, and you are liberty to set the SD to missing if the count is 2.

        Comment


        • #5
          Seems your installation was not successful. Try re-installing asrol
          Code:
          ssc install asrol, replace
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            I am trying to calculate the rolling standard deviations for my sample of monthly stock returns. I could successfully use Nick Cox's suggestion of rangestat however I also receive the same error message: "<istmt>: 3499 asrolw() not found" when trying to use asrol. I already tried reinstalling but that doesn't solve the issue.
            I have a question about rangestat. Do we have a similar option in rangestat to set the min or max number of observations we want to be using when calculating the standard errors? The reason I want to have this option is that the estimates I calculate when using windows of (month -12 -1) are not reflective of the volatility of the stock when I have less than 3-4 observations used in the calculation. I was thinking of using (month -12 6) with a max(12) option to get better predictions for months with lower number of previous return values.
            Can you help me with this option and also comment on the technique I am trying to use if you think it's not ideal?

            Regards,
            Hamidreza

            Comment

            Working...
            X