Announcement

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

  • calculate beta using rangestat

    Hi everyone,
    I want to calculate the beta of the stock market using the CRSP data. The RANGESTAT is, for an interval of 5, regressing on 1to5, 2 to6 and so on. however, I want to run a regression on observations like 1 to 5 then 6 to 10 and so on. I looked up the help of this command and didn't find anything for that. I appreciate if you can help me.

  • #2
    That's not what -rangestat- is really for, although you can use it that way. You don't post any example data, which makes it harder to help you. In the future, when asking for help with code, use the -dataex- command to show an example of your data. If you are running version 15.1 or a fully updated version 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    That said, here's an example using one of StataCorp's data sets

    Code:
    webuse grunfeld, clear
    
    //    CREATE FIVE YEAR INTERVALS FOR YEAR
    summ year, meanonly
    gen period= floor((year-`r(min)')/5)
    tabstat year, by(period) statistics(min max)
    
    rangestat (reg) mvalue kstock, by(period) interval(year . .)



    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      That's not what -rangestat- is really for, although you can use it that way. You don't post any example data, which makes it harder to help you. In the future, when asking for help with code, use the -dataex- command to show an example of your data. If you are running version 15.1 or a fully updated version 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.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      That said, here's an example using one of StataCorp's data sets

      Code:
      webuse grunfeld, clear
      
      // CREATE FIVE YEAR INTERVALS FOR YEAR
      summ year, meanonly
      gen period= floor((year-`r(min)')/5)
      tabstat year, by(period) statistics(min max)
      
      rangestat (reg) mvalue kstock, by(period) interval(year . .)


      input double permno long date double siccd str8(ncusip ticker) str36 comnam str8 cusip double(acperm prc shrout vwretd) float(ydate ret rindex) double(nobs rs_mktrf rs_cons)
      10000 9502 . "" "" "" "68391610" . . . -.0001381478 1986 . . . . .
      10000 9503 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.5625 3680 .01380894 1986 . -100.95773 . . .
      10000 9504 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.5 3680 -.02074435 1986 -.024390243 -2.502241 1 . .
      10000 9505 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.5 3680 -.01121904 1986 0 -.4591761 2 . .
      10000 9506 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.5 3680 .00008337963999999999 1986 0 -1.007432 3 . .
      10000 9509 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.625 3680 .00274908 1986 .05 31.97064 4 . .
      10000 9510 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.75 3680 .0003662401 1986 .04761905 -.8667772 5 . .
      10000 9511 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -2.875 3680 .008205866000000001 1986 .04545455 21.4057 6 . .
      10000 9512 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -3 3680 .004701549 1986 .04347826 -.4270502 7 . .
      10000 9513 3990 "68391610" "OMFGA" "OPTIMUM MANUFACTURING INC" "68391610" . -3 3680 -.0017411750000000002 1986 0 -1.3703407 8 . .
      end
      format %d date
      format %ty ydate
      [/CODE]
      ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      HI Mr.Schechter,
      you right thanks,

      this is the code which i found in statalist:


      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


      sort permno date ydate
      rangestat (myreg) ret rindex, by(permno ) interval(date -251 0) casewise
      rename (myreg1 myreg2 myreg3) (nobs rs_mktrf rs_cons)
      isid permno date
      by permno: replace rs_mktrf = . if _n < 252 | nobs < 50
      by permno: replace rs_cons = . if _n < 252 | nobs < 50
      save "rangestat_results.dta", replace

      However, for each year, I need to have a beta coefficient per the permno. what should I do ?!

      Comment


      • #4
        So there are a number of Stata community commands built to do this: Stata's built-in rolling command and rollreg, asreg, asrol (all from SSC; SSC install rollreg)

        Statalist post that that might be helpful Calculating Stock's Beta with rollreg and Calculate market betas for firm stocks

        See also here, here, here, and here

        Comment


        • #5
          You don't have to go through all that rigmarole to use -rangestat- for this. The current version of -rangestat- has a built in (reg) operator and does not require you to write a -myreg- program. All you need, for your data, is this:

          Code:
          //    CREATE FIVE YEAR INTERVALS FOR YEAR
          summ ydate, meanonly
          gen period= floor((ydate-`r(min)')/5)
          
          rangestat (reg) ret rindex, by(period) interval(ydate . .)
          But make sure you have the latest version of -rangestat-. So, run -adoupdate rangestat- to be sure.

          Comment


          • #6
            Thank you, thank you so much, Mr. Benson and Mr. Schechter, it worked.

            Comment

            Working...
            X