Announcement

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

  • rangestat/runby

    Dear All, I wonder if the following question can be solved by (ssc install) rangestat/runby? The data set is:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long stkcd int year double sale
    2 2008 40991779215
    2 2009 48881013143
    2 2010 50713851443
    2 2011 71782749801
    2 2012 1.03116e+11
    2 2013 1.35419e+11
    2 2014 1.46388e+11
    2 2015 1.95549e+11
    2 2016 2.40477e+11
    2 2017 2.42897e+11
    4 2008 43314824.87
    4 2009 60080600.35
    4 2010 131331494.7
    4 2011 74503718.53
    4 2012 97363301.61
    4 2013 72784567.16
    4 2014    80608820
    4 2015 120454422.5
    4 2016 287670026.6
    4 2017   138605842
    5 2008 108720264.8
    5 2009 43192476.48
    5 2010 137234232.9
    5 2011 63534839.01
    5 2012 99421227.75
    5 2013 52061702.69
    5 2014 52776994.29
    5 2015 84125970.69
    5 2016 481863433.3
    5 2017 530922230
    For each `stkcd', I'd like to perform a rolling regression using the recent 5-year data (including the current year) by regressing sale on a constant and trend term (1,2,3,4,5). Then, collect five residuals and calculate its standard deviation. Any suggestions? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I would do this with rangerun (also SSC).

    Your years in each window of 5 are to be translated to 1, 2, 3, 4, 5 (presumably to ensure intercepts that can be interpreted easily; the slope estimate is unaffected by change of time origin). (I wouldn't call that a trend term; the trend is a summary of the response, not a version of the time.)

    The program here does what it can with incomplete windows, always regarded as starting at year 1. You are at liberty to ignore results for incomplete windows.

    Here's code, with a check through direct regression that I got it right.

    Contradictory impulses here: I used doubles to show that answers are identical, but it is hard for me to believe that economists or business people really think in terms of 10 or 11 significant figures, so I changed the units of sales to billions of whatever the currency is.

    Standard deviation of residuals I've interpreted as RMSE from regression. Note the different divisor, which bites with samples this small.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long stkcd int year double sale
    2 2008 40991779215
    2 2009 48881013143
    2 2010 50713851443
    2 2011 71782749801
    2 2012 1.03116e+11
    2 2013 1.35419e+11
    2 2014 1.46388e+11
    2 2015 1.95549e+11
    2 2016 2.40477e+11
    2 2017 2.42897e+11
    4 2008 43314824.87
    4 2009 60080600.35
    4 2010 131331494.7
    4 2011 74503718.53
    4 2012 97363301.61
    4 2013 72784567.16
    4 2014    80608820
    4 2015 120454422.5
    4 2016 287670026.6
    4 2017   138605842
    5 2008 108720264.8
    5 2009 43192476.48
    5 2010 137234232.9
    5 2011 63534839.01
    5 2012 99421227.75
    5 2013 52061702.69
    5 2014 52776994.29
    5 2015 84125970.69
    5 2016 481863433.3
    5 2017 530922230
    end 
    
    gen double SALE = sale/1e9 
    
    program myreg 
        su year, meanonly 
        gen ytrans = year - r(min) + 1 
        reg SALE ytrans 
        gen double b_cons = _b[_cons] 
        gen double b_year = _b[ytrans] 
        gen double rmse = e(rmse) 
        gen double nobs = e(N) 
    end 
    
    rangerun myreg, use(SALE year) by(stkcd) int(year -4 0) 
    
    * check some results 
    gen ytrans2 = . 
    gen double b_cons_reg = . 
    gen double rmse_reg = . 
    
    quietly forval j = 1/6 {  
        local J = `j' + 4 
        replace ytrans2 =  cond(inrange(_n, `j', `J'), (_n - `j' + 1), .) 
        regress SALE ytrans2 in `j'/`J' 
        replace b_cons_reg = _b[_cons] in `J' 
        replace rmse_reg = e(rmse) in `J' 
    }
    
    list year nobs b*  rmse* in 1/10  
    
         +-------------------------------------------------------------------------+
         | year   nobs      b_cons      b_year   b_cons_~g        rmse    rmse_reg |
         |-------------------------------------------------------------------------|
      1. | 2008      .           .           .           .           .           . |
      2. | 2009      2   33.102545   7.8892339           .           0           . |
      3. | 2010      3   37.140142   4.8610361           .   2.4725132           . |
      4. | 2011      4   29.540911    9.420575           .   6.1404928           . |
      5. | 2012      5   18.952025   14.715018   18.952025   10.889177   10.889177 |
         |-------------------------------------------------------------------------|
      6. | 2013      5   14.339086   22.547812   14.339086   11.482127   11.482127 |
      7. | 2014      5   24.988556   25.498455   24.988556   6.5492284   6.5492284 |
      8. | 2015      5     43.2096    29.08045     43.2096   9.1078475   9.1078475 |
      9. | 2016      5     63.7342     33.4852     63.7342   12.452874   12.452874 |
     10. | 2017      5     99.4325     30.9045     99.4325   15.099623   15.099623 |
         +-------------------------------------------------------------------------+

    Comment


    • #3
      Dear Nick, Thanks a lot. Is it possible to replace
      Code:
      su year, meanonly
      gen ytrans = year - r(min) + 1
      with
      Code:
      gen ytrans = _n
      ?
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Why not try that, to see if it works?

        Comment


        • #5
          Dear Nick, Indeed, I tried and found the same results. Thanks.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            It would be riskier with gaps in the data, or so I guess.

            Comment


            • #7
              Dear Nick, Thanks for this concern. I created some missing values (gaps) but continued to find the results identical.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment

              Working...
              X