Announcement

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

  • Looping through multiple dependent variables with outreg2

    Dear all,

    I would like to run several regressions, looping through different dependent variables and outputting all models to an xls file with outreg2.

    There are 10 different dependent variables: x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
    A fixed set of independent variables: y1 y2 y3
    A fixed set of control variables: age i.industry i.year (note: industry and year are to be incorporated as dummies)

    The aim is to generate 10 different models (one for each DV) with the same set of IVs and controls, and output them to one xls with outreg2 (append to each other in the same tab).

    I have tried to adjust the solution suggested in this previous thread for a similar topic to my problem: https://www.statalist.org/forums/for...on-with-output

    However I have seem to have gotten something wrong - would very much appreciate if you could take a look:

    Code:
    * list dependent variables
    local dvs x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
    
    * list independent variables
    local ivs y1 y2 y3
    
    * list control variables
    local cvs age i.industry i.year
    
    foreach DV of varlist dvs {
         logit `DV' `ivs' `cvs', cluster(id)
         outreg2 using "test.xls", append excel dec(3) addtext (Year dummies, Yes, Industry dummies, Yes) addstat(Pseudo R-squared, `e(r2_p)') drop (i.year i.industry o.*)
    
       }
    
    variable dvs not found
    r(111);

    With regards to the output table in xls, I have to further questions:

    1) Omitting year / industry dummies: is it possible to omit all industry and year dummies?
    I have added "drop..." as an option (see code above), however some year/industry dummies remain in the output as they commence with a different letter, e.g. "12o.industry" instead of "o.industry"

    2) Further fitstats: So far, the output includes the Pseudo R2. I was wondering if it is possible to also add the Log Likihood, Chi2 and further R2 measures (e.g., Cragg-Uhler (Nagelkerke) R2; McKelvey & Zavoina's R2) to the outreg2 output?

    Many thanks again for your help!

  • #2
    Your loop
    Code:
    foreach DV of varlist dvs {
    should either read
    Code:
    foreach DV of varlist `dvs' {
    or
    Code:
    foreach DV of local dvs {
    or
    Code:
    foreach DV in `dvs' {
    I'll have to leave the outreg2 questions for someone with more outreg2 experience than I have.

    Comment


    • #3
      You have not shown an example data set. I have created a dummy data set according to your variables and used asdoc to make the suggested nested table. asdoc can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command. You can also watch several YouTube videos that show the use of asdoc

      * Install asdoc
      Code:
      ssc install asdoc, replace
      * Create a dummy data set
      Code:
       clear
      set obs 10
      gen id = 1
      expand 2
      replace id = 2 if _n>10
      
      forv i = 1 / 10 {
      gen x`i' = mod(_n,2)
      }
      forv y = 1 / 3 {
      gen y`y' = uniform()
      }
      expand 2
      gen year = mod(_n,3)
      gen industry = mod(_n,10)
      gen age = floor(uniform()*10)
      * Use asdoc in a loop for the required table. Please note that I am using the nest option of asdoc. There are two other options to report regression output, they are (1) detailed regressions, which is the default in asdoc (2) wide regressions, which can be invoked by using the option wide. You can see further details on these in the help file of asdoc
      Code:
      * list dependent variables
      local dvs x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
      
      * list independent variables
      local ivs y1 y2 y3
      
      * list control variables
      local cvs age  i.year
      
      foreach DV of varlist `dvs' {
           asdoc logit `DV' `ivs' `cvs', cluster(id) nest dec(3) add(Year dummies, Yes, Industry dummies, Yes) stat(r2_p) drop(i.year) save(myresults)
        
         }
      Click image for larger version

Name:	reg.png
Views:	1
Size:	27.7 KB
ID:	1481131


      Last edited by Attaullah Shah; 29 Jan 2019, 21:07.
      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

      Working...
      X