Announcement

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

  • Changing Standard Error Output using Rangestat (SSC)

    Hello Statalist,

    my question refers to the package - rangestat -
    See here: https://www.statalist.org/forums/for...s-within-range

    I run a rolling regression with rangestat which generates new variables, among them se_var, the standard errors for each coefficient.

    However, for my further analysis, I require heteroskedasiticity-consistent standard errors of White, which I would typically receive by using the "vce(robust)" option with the common regression command, which I unfortunately cannot just add to the rangestat command as an option.

    Is there a way to implement such an output for rangestat?

    I am sure that with the increasing use of rangestat (which is a powerful tool) this issue might come up more frequently in the future.

    Thank you very much for your suggestions and help.

  • #2
    The philosophy of rangestat (SSC) is that some very common, simple things are canned and (in the long run more importantly) other things can be implemented using Mata code you supply.

    It's a judgement call on how much of the rest of Stata the developers of rangestat want to rewrite to push into rangestat.

    At present this kind of robust standard errors is not supported. Robert Picard is lead developer and no doubt has a point of view on whether he wants to add them. Less importantly, they are not on any to-do list of mine.

    We'd certainly like to encourage others to work out their own example code and (subject to mutual agreement) to document any that are widely attractive in the help file.

    Most user-written (community-contributed) programs are based on what the users concerned want to do themselves!

    Comment


    • #3
      Thank you very much Nick!

      Yes you are right.
      After working a couple of days with rangestat I had to realize that this option is most likely not available and I understand, as you correctly said, that such a option would need to developed from concernced users as well.

      I am just beginning to familiarize myself with STATA (began to use it 2 months ago for a specific project), so I do not feel confident in developing such an option myself.

      So my hope was that possibly any of the experienced users of rangestat might have come across a similar issue or potentially developed a work-around solution.

      Comment


      • #4
        Much more positively, I should point to rangerun (also SSC) which has the aim of allowing people to write their own Stata programs, which can just be wrappers for a standard command call. So, from that point of view there is less need for us to add such things to rangestat.

        Comment


        • #5
          I agree with Nick that it if you want something that is not provided by the built-in rangestat functions, the easiest solution is to do it with regular Stata commands using rangerun.

          Otherwise, you can extend the functionality of rangestat by providing your own Mata function, in this case one that would perform a linear regression with robust standard errors. Since the code for rangestat is provided, you can start from its rs_reg() function and modify the code that generates the standard errors. The example below does just that using code provided by David M. Drukker in this Stata Blog post (all changes are in red). The example repeats the rolling regression in the help file.

          When you roll your own Mata routine and use it with rangestat, the variables created to hold the results of the calculations are named after the name of the Mata function and a sequence number. You will have to rename these variables manually.

          Code:
          clear all
          
          * this is a copy of the rs_reg() regression function used by rangestat,
          * modified to calculate robust standard errors
          
          mata:
          // http://blog.stata.com/2016/01/12/programming-an-estimation-command-in-stata-an-ols-command-using-mata/
          real rowvector reg_robust(real matrix Xall)
          {
              real colvector y, b, e, e2, se
              real matrix X, XpX, XpXi, V, M
              real scalar n, k, ymean, tss, mss, r2, r2a
              
              y = Xall[.,1]                // dependent var is first column of Xall
              X = Xall[.,2::cols(Xall)]    // the remaining cols are the independent variables
              n = rows(X)                  // the number of observations
              X = X,J(n,1,1)               // add a constant
              k = cols(X)                      // number of independent variables
              
              if (n > k) {    // need more than k obs to estimate model and standard errors
              
                  // compute the OLS point estimates
                  XpX  = quadcross(X, X)
                  XpXi = invsym(XpX)
              
                  // proceed only if no variable is omitted
                  if (diag0cnt(XpXi)==0) {
                  
          
                      b    = XpXi*quadcross(X, y)
                  
                      // compute robust standard errors
                      e  = y - X*b
                      e2 = e:^2
                      M    = quadcross(X, e2, X)
                      V    = (n/(n-k))*XpXi*M*XpXi
                      se   = sqrt(diagonal(V))
                  
                      // r2 and adjusted r2
                      ymean = mean(y)
                      tss   = sum((y :- ymean) :^ 2)        // total sum of squares
                      mss   = sum( (X * b :- ymean)  :^ 2)  // model sum of squares    
                      r2    = mss / tss
                      r2a   = 1 - (1 - r2) * (n - 1) / (n - k)
                  }
              }
              
              return(rows(X), r2, r2a, b', se')
          }
          end
          
          
          webuse grunfeld, clear
                          
          rangestat (reg_robust) invest mvalue kstock, interval(year -6 0) by(company)
          
          * rename variables
          rename (reg_robust1 reg_robust2 reg_robust3) (nobs r2 r2a)
          rename (reg_robust4 reg_robust5 reg_robust6) (b_mvalue b_kstock b_cons)
          rename (reg_robust7 reg_robust8 reg_robust9) (se_mvalue se_kstock se_cons)
          
          * check results for observation 15
          list in 15
          regress invest mvalue kstock if inrange(year, year[15]-6, year[15]) ///
                  & company == company[15], vce(robust)
          
          * check results for observation 40
          list in 40
          regress invest mvalue kstock if inrange(year, year[40]-6, year[40]) ///
                  & company == company[40], vce(robust)
          and the results:
          Code:
          . * check results for observation 15
          . list in 15
          
               +---------------------------------------------------------------------------------+
           15. | company | year | invest | mvalue | kstock | time | nobs |        r2 |       r2a |
               |       1 | 1949 |  555.1 | 3700.2 | 1020.1 |   15 |    7 | .62538483 | .43807724 |
               |-------------------------+-------------------------------------------------------|
               |  b_mvalue  |  b_kstock  |     b_cons  |  se_mvalue  |  se_kstock  |    se_cons  |
               | .11691239  | .15738536  | -.64808302  |  .05293358  |  .07732076  |  241.44599  |
               +---------------------------------------------------------------------------------+
          
          . regress invest mvalue kstock if inrange(year, year[15]-6, year[15]) ///
          >                 & company == company[15], vce(robust)
          
          Linear regression                               Number of obs     =          7
                                                          F(2, 4)           =       2.52
                                                          Prob > F          =     0.1960
                                                          R-squared         =     0.6254
                                                          Root MSE          =     44.498
          
          ------------------------------------------------------------------------------
                       |               Robust
                invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                mvalue |   .1169124   .0529336     2.21   0.092    -.0300548    .2638796
                kstock |   .1573854   .0773208     2.04   0.112    -.0572915    .3720622
                 _cons |   -.648083    241.446    -0.00   0.998    -671.0096    669.7135
          ------------------------------------------------------------------------------
          
          . 
          . * check results for observation 40
          . list in 40
          
               +---------------------------------------------------------------------------------+
           40. | company | year | invest | mvalue | kstock | time | nobs |        r2 |       r2a |
               |       2 | 1954 |  459.3 | 2115.5 |  669.7 |   20 |    7 | .49286716 | .23930074 |
               |-------------------------+-------------------------------------------------------|
               |  b_mvalue  |  b_kstock  |     b_cons  |  se_mvalue  |  se_kstock  |    se_cons  |
               | .27306942  | -.0499983  |  14.676042  |  .09850517  |  .33765808  |  187.30637  |
               +---------------------------------------------------------------------------------+
          
          . regress invest mvalue kstock if inrange(year, year[40]-6, year[40]) ///
          >                 & company == company[40], vce(robust)
          
          Linear regression                               Number of obs     =          7
                                                          F(2, 4)           =       4.25
                                                          Prob > F          =     0.1024
                                                          R-squared         =     0.4929
                                                          Root MSE          =     89.229
          
          ------------------------------------------------------------------------------
                       |               Robust
                invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                mvalue |   .2730694   .0985052     2.77   0.050    -.0004248    .5465636
                kstock |  -.0499983   .3376581    -0.15   0.889    -.9874874    .8874908
                 _cons |   14.67604   187.3064     0.08   0.941    -505.3698    534.7219
          ------------------------------------------------------------------------------

          Comment


          • #6
            Hello again Nick and Robert,

            thanks a lot for the input on my question!
            I will see if I can make sense of the two suggestions and come up with a solution quickly and efficiently.

            Comment

            Working...
            X