Announcement

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

  • saving regression outputs for many regressions by group

    Hi all,

    I have stock data, so stock permno and date identifies an observation.

    There are multiple events, and for each event, there is a dummy variable for pre-event and post-event windows (i.e., pre_e1, post_e1, pre_e2, post_e2,..., pre_e5, post_e5).

    I am interested in estimating betas for each stock and for each window. For example, pseudo,

    for permno = 1 to 5 {
    reg stock_excess_return market_excess_return if permno==i&pre_e1
    reg stock_excess_return market_excess_return if permno==i&post_e1
    reg stock_excess_return market_excess_return if permno==i&pre_e2
    reg stock_excess_return market_excess_return if permno==i&post_e2
    ...
    reg stock_excess_return market_excess_return if permno==i&pre_e5
    reg stock_excess_return market_excess_return if permno==i&post_e5
    }

    and I am interested in storing regression coefficients for all the regressions.

    I did find,

    statsby, by(permno): reg stock_excess_return market_excess_return if pre_e1

    But statsby runs regressions under only one if condition. Is there a way I can run multiple regressions by permno and save regression coefficients for all of these at the same time? It feels like it ought to be much faster that way.

    I'd much appreciate any and all helps.

    Thanks so much!

    Best,


    John

  • #2
    I think the simplest way to do this is:

    Code:
    foreach v of varlist pre_e* post_e* {
        tempfile `v'
        statsby, by(permno) saving(``v''): reg stock_excess_return market_excess_return if `v'
    }
    clear
    tempfile all_results
    save `all_results', emptyok
    foreach v of varlist pre_e* post_e* {
        clear
        use ``v''
        gen event_era = "`v'"
       append using `all_results'
        save "`all_results'", replace
    }
    At the end, the data in memory, and in the temporary file `all_results' (which you may want to use and save as a permanent file) will contain the regression results from all these combinations.

    Note: In working with the code below, be careful to distinguish two single quotes in succession from a double-quote. At least as I type this, they look the same. You may want to copy/paste this code into the do-file editor and then mark it up as needed, rather than trying to re-type it.

    Comment


    • #3
      You could use the good old estimates command http://www.stata.com/help.cgi?estimates%20save or its more export-friendly wrapper eststo http://repec.org/bocode/e/estout/eststo.html

      So the only change to your code is
      Code:
      for p = 1/5 {
          forv q = 1/5 {
              reg stock_excess_return market_excess_return if permno==`p'&pre_e`q' 
              eststo reg_`p'_`q'
          }
      }
      esttab reg_1_1 reg_1_2 reg_1_3 reg_1_4 reg_1_5  // this would output your regression table on the console; esttab using to export

      Comment


      • #4
        Hi Clyde,

        Thanks so much for your code! I have been trying to run your code and understand it, but it does not run since the second for loop. I'm guessing, the problem is, you clear then run the for loop over varlist, so stata does not understand which variables I'm referring to. I'm new to tempfile, so I'm struggling to change the code to work. Could you help perhaps?

        Thank you so much for your time, and help!

        Best,


        John

        Comment


        • #5
          Ah yes, you're quite right. My mistake. You need to take :

          Code:
          clear
          tempfile all_results
          save `all_results', emptyok
          part of the file and move it way up in your do-file to before you load in the data you want to analyze. So it needs to be very early in the file.

          The -tempfile- command tells Stata to come up with a temporary name for a temporary file. But it does not tell Stata to actually create the file. So we need the -save- command to do that, and we need to make sure the file is empty when we first save it. Moving those commands early in the file will accomplish that without wiping out your data.




          Comment


          • #6
            Dear John and Clyde,

            I am facing a similar problem when I am doing the same regression for many groups (100 RICs in my data set). I would like to save and extract the estimated coefficients and their corresponding SEs for late analysis. After reading your posts, I tried the code like this :

            clear
            tempfile all_results
            save `all_results', emptyok
            use data
            set more off
            forvalues i=1(1)100 {
            tempfile `i'
            l id RIC if id==`i' & DateCode==125
            statsby, by(RIC) saving(``i''):reg DailyRminusRf MktRf SMB HML if id==`i' & estimation_window==1, noconstant
            }

            foreach i=1(1)100 {
            2. clear
            3. use ``i''
            4. gen event_era= "`i'"
            5. append using `all_results'
            6. save "`all_results'", replace
            7. }


            the regressions ran but for the last step to get the estimated coefficients, it always shows up like invalid snytax. It would be really appreciated, if you could give any help with that.

            Best regards,
            Binfang Zhu

            Comment


            • #7
              You can also use asreg that is order of magnitude faster than rolling method. It matters more in big data sets. See the following examples where we estimate regression for each company.

              Code:
              ssc install asreg // if not already installed
              
              webuse grunfeld, clear
               
              
              bys company: asreg invest mvalue kstock, se
              
              *keep only relevant variables
              keep company  _*
              
              * delete duplicate values
              bys company : keep if _n == _N
              
              list
               +----------------------------------------------------------------------------------------------------------------------+
                   | company   year   _Nobs         _R2      _adjR2      _b_cons   _b_mvalue   _b_kstock   _se_cons   _se_mv~e   _se_ks~k |
                   |----------------------------------------------------------------------------------------------------------------------|
                1. |       1   1954      20   .92135403   .91210157    -149.7824   .11928082   .37144481   105.8421   .0258342   .0370728 |
                2. |       2   1954      20   .47086233   .40861084   -49.198313   .17485601   .38964188   148.0754   .0741981   .1423669 |
                3. |       3   1954      20   .70530671   .67063691   -9.9563082   .02655119   .15169387   31.37425   .0155661   .0257041 |
                4. |       4   1954      20   .91357845   .90341121   -6.1899547   .07794781   .31571818   13.50648   .0199733   .0288132 |
                5. |       5   1954      20   .68040764   .64280854    22.707115   .16237772   .00310173   6.872076   .0570365   .0219653 |
                   |----------------------------------------------------------------------------------------------------------------------|
                6. |       6   1954      20    .9521422   .94651187   -8.6855436   .13145485   .08537425   4.545168   .0311723    .100306 |
                7. |       7   1954      20   .76350084   .73567741    -4.499533   .08752719   .12378141    11.2894   .0656259   .0170648 |
                8. |       8   1954      20    .7444461   .71438094   -.50938763   .05289412   .09240652   8.015289   .0157065    .056099 |
                9. |       9   1954      20   .66551452   .62616329   -7.7228397   .07538795   .08210356    9.35934   .0339523   .0279917 |
               10. |      10   1954      20   .64315782   .60117638    .16151866   .00457343    .4373692   2.065564   .0271608   .0795889 |
                   +----------------------------------------------------------------------------------------------------------------------+
              .
              Last edited by Attaullah Shah; 17 Apr 2018, 04:45.
              Regards
              --------------------------------------------------
              Attaullah Shah, PhD.
              Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
              FinTechProfessor.com
              https://asdocx.com
              Check out my asdoc program, which sends outputs to MS Word.
              For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

              Comment


              • #8
                I agree with Attaullah Shah (although #6 is not a rolling regression in Stata's sense, but that is not material here): there are one-line alternatives and no need for this problem to write your own loop, let alone mess around with 100 different files.

                Here's Attaullah's example done with rangestat (SSC). Note that rangestat was not written to emulate asreg, as it came first. Equally, each program does some things the other doesn't. Let a hundred flowers bloom.

                Code:
                webuse grunfeld, clear
                
                rangestat (reg) invest mvalue kstock, by(company) int(year . .)  
                
                list company reg* b_* se_* in 1
                
                     +--------------------------------------------------------------------+
                  1. | company | reg_nobs |    reg_r2 | reg_adj~2 |  b_mvalue |  b_kstock |
                     |       1 |       20 | .92135403 | .91210157 | .11928082 | .37144481 |
                     |--------------------------------------------------------------------|
                     |      b_cons    |   se_mvalue    |   se_kstock    |      se_cons    |
                     |   -149.7824    |   .02583417    |   .03707282    |    105.84211    |
                     +--------------------------------------------------------------------+
                Key posts for anyone interested: (also, you may naturally search the forum for mentions of asreg and rangestat to get examples)

                https://www.statalist.org/forums/for...s-within-range

                https://www.statalist.org/forums/for...updated-on-ssc

                https://www.statalist.org/forums/for...-rolling-betas

                In #6 I note that all regression results are for estimation_window == 1 so with any approach keeping only those observations might lead to a speed-up.

                Comment


                • #9
                  Dear Attaullah and Nick,

                  thanks a lot for the quick reply. it is working very well! I have one more question. Since I don't want the intercept term in my model. Is there any option to suppress constant term in asreg? I checked out asreg, there is no information about how to do that.

                  Best regards,
                  Binfang

                  Comment

                  Working...
                  X