Announcement

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

  • Looping through regressions and saving regression coefficients

    Dear all,

    I currently have panel data in the following form:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double DealNumber float(date ret ret_rf mkt_rf smb hml rf)
    128631020 690 .003883495 -.012116506 .0071 -.42 0 .016
    128631020 691 .01934236 .0033423584 -.0007 .32 -.06 .016
    128631020 692 .0037950664 -.012204934 .0108 -.6 -.28 .016
    128631020 693 .02079395 .00479395 .0017 .04 -.03 .016
    128631020 694 .002814815 -.013185186 .0035 -.04 .19 .016
    128631020 695 -.01019353 -.02619353 -.001 .62 .05 .016
    128631020 696 0 -.017 -.0049 .07 .1 .017
    128631020 697 .0009701492 -.016029852 -.0032 .52 -.02 .017
    128631020 699 -.004696936 -.021696936 .0003 -.24 .12 .017
    128631020 700 .0037453184 -.013254683 -.0002 .43 -.21 .017
    128631020 701 .02052239 .003522387 .0046 .07 -.36 .017
    128631020 702 -.003656307 -.02065631 -.0093 .62 .08 .017

    I want to compute (and add to my data) regression coefficients on each of the Fama-French factors by regressing ret_rf on mkt_rf smb and hml for each DealNumber (approximately 10,000, with 180 observations each). The coefficients computed should be saved as a new variable for each corresponding DealNumber. Is there a way to do this?

    Best,
    Stijn

  • #2
    Code:
    gen betamkt_rf=.
    gen betasmb=.
    gen betahml=.
    
    levelsof DealNumber, local(numberlist)
    foreach n in `numberlist'{
    reg ret_rf mkt_rf smb hml if `n'==DealNumber
    replace betamkt_rf = _b[mkt_rf] if `n'==DealNumber
    replace betasmb = _b[smb] if `n'==DealNumber
    replace betahml = _b[hml] if `n'==DealNumber
    }

    Comment


    • #3
      Thank you!

      Comment


      • #4
        Dear all,

        For some reason, after running several regressions properly (approximately 35), I get a "no observations (r)2000" error, which does not make sense since every observation has values for each variable. Does anyone have a clue how to fix this?

        Best,
        Stijn

        Comment


        • #5
          Can you try this: in your do file, add the line:
          Code:
          set trace on
          let it run, and scroll up in the result window to see at what DealNumber the problem occurs, then check what data that number has.

          Comment


          • #6
            The problem occurs at a variable with only four observations, I used the following code to exclude DealNumbers with less than 15 observations:

            bysort DealNumber: gen nobs = _N
            set trace on
            quietly levelsof DealNumber, local(numberlist)
            foreach n in `numberlist'{
            reg ret_rf mkt_rf smb hml if `n'==DealNumber & nobs>15
            replace betamkt_rf = _b[mkt_rf] if `n'==DealNumber
            replace betasmb = _b[smb] if `n'==DealNumber
            replace betahml = _b[hml] if `n'==DealNumber
            }

            Am I making a mistake in omitting such deals?

            Comment


            • #7
              Code:
              quietly levelsof DealNumber if nobs>15, local(numberlist)
              foreach n in `numberlist'{
              reg ret_rf mkt_rf smb hml if `n'==DealNumber
              replace betamkt_rf = _b[mkt_rf] if `n'==DealNumber
              replace betasmb = _b[smb] if `n'==DealNumber
              replace betahml = _b[hml] if `n'==DealNumber
              }
              This way only numbers with obs>15 get entered into the list of values Stata loops over.

              Your way includes all dealnumbers.
              Stata gets to deal number X, with obs<15, and you ask it to regress, but only if obs>15


              comapre e.g.:
              Code:
              sysuse auto
              reg price headroom if foreign>0
              reg price headroom if foreign>1

              Comment


              • #8
                This should also work (rangestat is from SSC; search this forum for other mentions)

                Code:
                rangestat (reg)  ret_rf mkt_rf smb hml  , int(DealNumber 0 0)
                This dopey example shows the kinds of results that rangestat leaves in its wake. (groups is from the Stata Journal.) Note that you can ignore results using your own definition of unacceptably small, given that reg_nobs is returned as a variable.

                Code:
                . sysuse auto, clear
                (1978 Automobile Data)
                
                . rangestat (reg) price weight , int(foreign 0 0)
                
                
                . groups foreign reg_nobs reg_r2 reg_adj_r2 b_weight b_cons se_weight se_cons , show(none)
                
                  +----------------------------------------------------------------------+
                  |  foreign | reg_nobs |    reg_r2 | reg_adj~2 |  b_weight |     b_cons |
                  | Domestic |       52 | .45211821 | .44116058 | 2.9948135 |  -3861.719 |
                  |----------------------------------------------------------------------|
                  |            se_weight             |               se_cons             |
                  |            .46623215             |             1579.5218             |
                  +----------------------------------------------------------------------+
                
                  +----------------------------------------------------------------------+
                  |  foreign | reg_nobs |    reg_r2 | reg_adj~2 |  b_weight |     b_cons |
                  |  Foreign |       22 | .78416166 | .77336974 | 5.3620402 | -6033.3158 |
                  |----------------------------------------------------------------------|
                  |            se_weight             |               se_cons             |
                  |            .62903753             |             1480.8998             |
                  +----------------------------------------------------------------------+
                
                .

                Comment


                • #9
                  It worked, thanks so much.

                  Comment

                  Working...
                  X