Announcement

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

  • #16
    Dear Nick,

    you are right, sorry.
    Now I realized that you overcame the problem I mentioned above by giving unique names to the variables, which are generated in rolling regressions in the line (adding `v') :
    rolling `v'_i=_b[_cons]... It then allows unique variable names in rolling_.dta files and successful merge.
    I was not attentive enough, sorry and thank you!

    Comment


    • #17
      Thanks for reporting back!

      Comment


      • #18
        Dear Nick,

        is there a way to add an "if condition" for the minimum number of observations for a rolling regression?
        Here a similar issue was discussed, but I cannot really see how it was solved: http://www.statalist.org/forums/foru...f-observations

        I tried to add the condition into already merged table of the results, saying that replace by empty if `var'_N <30, but rolling regression takes very long, and there are many of them red-crossed (not enough observations). Thus, I am looking for a way to implement the conditions before the rolling regressions.

        Thanks a lot.

        Comment


        • #19
          You could move to rangestat (SSC). Search the forum to see mentions of its use in regression.

          Comment


          • #20
            This is the most recent post on using rangestat for regressions on a rolling window.

            Comment


            • #21
              Dear all,

              I have a mess with this suggested rangestat. I installed it via "ssc install rangestat".
              Even when I try to run the code taken from the link you indicate, I receive several errors:
              Code:
              mata:
              ------------------------------------------------- mata (type end to exit) -----------------------------------------------------------
              :
              : mata clear
              
              :
              : mata set matastrict on
              
              :
              : real rowvector myreg(real matrix Xall)
              >
              > {
              >
              >     real colvector y, b, Xy
              >
              >     real matrix X, XX
              >
              >
              >
              >     y = Xall[.,1]                // dependent var is first column of Xall
              invalid expression
              -------------------------------------------------------------------------------------------------------------------------------------
              r(3000);
              
              .
              .     X = Xall[.,2::cols(Xall)]    // the remaining cols are the independent variables
              unrecognized command:  X
              r(199);
              
              .
              .     X = X,J(rows(X),1,1)         // add a constant
              unrecognized command:  X
              r(199);
              
              .
              .    
              .
              .     XX = quadcross(X, X)        // linear regression, see help mata cross(), example 2
              unrecognized command:  XX
              r(199);
              
              .
              .     Xy = quadcross(X, y)
              unrecognized command:  Xy
              r(199);
              
              .
              .     b  = invsym(XX) * Xy
              unrecognized command:  b
              r(199);
              
              .
              .    
              .
              .     return(rows(X), b')
              invalid syntax
              r(198);
              
              .
              . }
              unrecognized command:  } invalid command name
              r(199);
              
              .
              . end
              unrecognized command:  end
              r(199);

              My final goal is to change this into smth faster using -rangestat-:

              Code:
              gen t = ym(1979, 12) + _n
              tsset t
              foreach v of varlist CCZ CDA CDB CDC CDD CDE CDF CDG CDH CDI CDJ CDK CDL HHV {
              display `v'
                  rolling `v'_i=_b[_cons] `v'_N=e(N), window(36) saving(rolling_results_`v') reject(e(N) < 30): regress `v' mkt hml smb mom
              }     
              
              use rolling_results_CCZ.dta, clear
              local vlist CDA CDB CDC CDD CDE CDF CDG CDH CDI CDJ CDK CDL HHV
              foreach var of local vlist{
              merge 1:1  start end using rolling_results_`var'
              }
              drop _merge
              What am I doing wrong with rangestat and how can I switch to it?

              Thanks a lot.
              Last edited by Ekaterina Shpanenberg; 01 Mar 2017, 13:21.

              Comment


              • #22
                Don't copy and then paste the solution to Stata's Command window. Paste it in a new do-file and then run the do-file.

                Comment


                • #23
                  Dear all,

                  now my -rangestat- is working as a code, but it does not generate the right results (regression constants are not the same as they were in -rolling- and not the same when I check in Excel). I still cannot understand why..

                  This is a part of my dataset. Number of observations is different for each stock.

                  stock ymdate excess mkt smb hml mom
                  100001 330 .0385 -.0063 .0071 .0267
                  100001 331 .0287317 .0352 -.0075 -.0093 -.0087
                  100001 332 -.0397046 -.0259 .0053 .0028 .0071

                  I would like to make a rolling regressions with 36-month window and more than 30 observations and I run this code:

                  Code:
                  mata:
                  mata clear
                  mata set matastrict on
                  real rowvector myreg(real matrix Xall)
                  {
                      real colvector y, b, Xy
                      real matrix X, XX
                  
                      y = Xall[.,1]                // dependent var is first column of Xall
                      X = Xall[.,2::cols(Xall)]    // the remaining cols are the independent variables
                      X = X,J(rows(X),1,1)         // add a constant
                      
                      XX = quadcross(X, X)        // linear regression, see help mata cross(), example 2
                      Xy = quadcross(X, y)
                      b  = invsym(XX) * Xy
                      
                      return(rows(X), b')
                  }
                  end
                  
                  * regressions with a constant over a rolling window of 36 periods by stock
                  rangestat (myreg) excess mkt smb hml mom, by (stock) interval (ymdate -35 -1) casewise
                  
                  * the Mata function returns first the number of observations and then as many
                  * variables as there are independent variables (plus the constant) for the betas
                  rename (myreg1 myreg2 myreg3 myreg4 myreg5 myreg6) (nobs rs_cons beta_mkt beta_smb beta_hml beta_mom)
                  
                  * reject results if the window is less than 6 or if the number of obs < 4
                  by stock:replace rs_cons = . if _n < 36 | nobs < 30
                  save "rangestat_results.dta", replace
                  I receive constants and beta coefficients several times higher than when using -rolling. The error is excatly in the regression itself, because even if I take the same columns and do a regression separately, the results do not match.

                  Could you please help what is wrong?

                  Thanks a lot.
                  Last edited by Ekaterina Shpanenberg; 02 Mar 2017, 04:31.

                  Comment


                  • #24
                    Dear all,

                    I realized what is going on. I wrongly renamed the variables, it should be:
                    Code:
                    rename (myreg1 myreg2 myreg3 myreg4 myreg5 myreg6) (nobs beta_mom beta_hml beta_smb beta_mkt rs_cons)
                    instead of:
                    Code:
                      
                     rename (myreg1 myreg2 myreg3 myreg4 myreg5 myreg6) (nobs rs_cons beta_mkt beta_smb beta_hml beta_mom)

                    Comment


                    • #25
                      Note that with rangestat you are asking for windows of 35, not 36.

                      Comment


                      • #26
                        Dear all,

                        how can I modify the code so that I have a column with t-statistics for each intercept?
                        Code:
                        mata:
                        mata clear
                        mata set matastrict on
                        real rowvector myreg(real matrix Xall)
                        {
                            real colvector y, b, Xy
                            real matrix X, XX
                        
                            y = Xall[.,1]                // dependent var is first column of Xall
                            X = Xall[.,2::cols(Xall)]    // the remaining cols are the independent variables
                            X = X,J(rows(X),1,1)         // add a constant
                            
                            XX = quadcross(X, X)        // linear regression, see help mata cross(), example 2
                            Xy = quadcross(X, y)
                            b  = invsym(XX) * Xy
                            
                            return(rows(X), b')
                        }
                        end
                        
                        * regressions with a constant over a rolling window of 36 periods by wficn
                        rangestat (myreg) stock mkt smb hml, by (id) interval (ymdate -36 -1) casewise
                        
                        * variables as there are independent variables (plus the constant) for the betas
                        rename (myreg1 myreg2 myreg3 myreg4 myreg5) (nobs beta_hml beta_smb beta_mkt rs_cons)
                        
                        * reject results if the window is less than 36 or if the number of obs is less than 30
                        by id:replace rs_cons = . if _n < 36 | nobs < 30
                        by id:replace beta_hml = . if _n < 36 | nobs < 30
                        by id:replace beta_smb = . if _n < 36 | nobs < 30
                        by id:replace beta_mkt = . if _n < 36 | nobs < 30
                        save "alphas.dta", replace
                        Thanks a lot.

                        Comment


                        • #27
                          Here's a previous post of mine that includes a more elaborate mata function that calculates standard errors. From there, you can get your t-statistics. Here's a complete example with a few spot checks.

                          Code:
                          webuse grunfeld, clear
                          sort company year
                          
                          * define a linear regression with intercept in Mata. Follows the sample code found at
                          * http://blog.stata.com/2016/01/05/programming-an-estimation-command-in-stata-computing-ols-objects-in-mata/
                          mata:
                          mata clear
                          mata set matastrict on
                          real rowvector myreg(real matrix Xall)
                          {
                              real colvector y, b, Xy, e, e2, se
                              real matrix X, XpX, XpXi, V
                              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
                              
                              // compute the OLS point estimates
                              XpX  = quadcross(X, X)
                              XpXi = invsym(XpX)
                              b    = XpXi*quadcross(X, y)
                              
                              // compute the standard errors
                              e  = y - X*b
                              e2 = e:^2
                              k  = cols(X)
                              V  = (quadsum(e2)/(n-k))*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
                          
                          rangestat (myreg) invest mvalue kstock, interval(year -4 0) casewise by(company)
                          rename myreg* (nobs myr2 myr2a b_mvalue b_kstock b_constant se_mvalue se_kstock se_constant)
                          
                          gen t_mvalue = b_mvalue / se_mvalue
                          gen t_kstock = b_kstock / se_kstock
                          gen t_constant = b_constant / se_constant
                          
                          * make a few spot checks
                          local i 10
                          reg invest mvalue kstock if company == company[`i'] & inrange(year,year[`i']-4,year[`i'])
                          list in `i'
                          
                          local i 100
                          reg invest mvalue kstock if company == company[`i'] & inrange(year,year[`i']-4,year[`i'])
                          list in `i'

                          Comment

                          Working...
                          X