Announcement

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

  • Display coefficients from multiple regressions in one column?


    Basically I want to regress one dependent variable A "individually" on several independent variables B,C,D (i.e. A on B, A on C and A on D). But the problem is that I want to use outreg2 to produce a table combining coefficients from these three different regressions into the same column. Like this:
    A
    coefficient of B 5.2
    coefficient of C 4.8
    coefficient of D 2.0
    I'm stuck on this because I can only generate table like this:
    A A A
    Coefficient of B 5.2
    Coefficient of C 4.8
    Coefficient of D 2.0
    Thank you!
    Last edited by Michael Pei; 07 Nov 2016, 16:56.

  • #2
    Hi Michael,

    You could loop over the models and store the coefficients in a new variable, like this example:
    Code:
    * GENERATE EXAMPLE DATA
    clear
    
    set obs 10
    foreach new_var in A B C D {
        gen `new_var' = rnormal(0, 1)
    }
    
    * CODE TO LOOP OVER MODELS
    gen var_name = "."
    gen coefficients = .
    local i = 1
    
    qui foreach var of varlist B-D {
        qui reg A `var'
        mat E = e(b)
        replace coefficients = E[1,1] in `i'
        replace var_name = "Coefficient of `var'" in `i'
        local `i++'
    }
    
    * COEFFICIENT TABLE
    table var_name, contents(sum coefficients) cw
    The first part of the code is just to generate some example data. The result looks like this:
    Code:
    . table var_name, contents(sum coefficients) cw
    
    --------------------------------
            var_name | sum(coeffi~s)
    -----------------+--------------
    Coefficient of B |       .274651
    Coefficient of C |      .0675308
    Coefficient of D |      .2300407
    --------------------------------
    Last edited by Mathias Pedersen Heinze; 07 Nov 2016, 20:27.

    Comment


    • #3
      Thank you so much! The codes are really helpful.

      A follow-up question: how can I export the table? I tried to use outreg2 but it didn't give me the table I want. Thanks.

      Comment


      • #4
        There are several tools for exporting Stata output out there. One simple way, is to use the user-written command logout. Install it with the following command.
        Code:
        ssc install logout
        It supports various export formats, i.e. xlsx, docx, dta, tex. Assuming you want a Word table, use the following code
        Code:
        logout, save("mytable.docx") word : table var_name, contents(sum coefficients) cw
        Remember to specify the full path.

        Comment


        • #5
          I was searching for a code to run regression based on industry (SIC) and year, and save residuals and coefficients of these regression as variables to be used in next level analysis.
          Plus I want the regression to run if at least 20 observations are there per each SIC-year
          I found a useful code from the statalist,quoted below
          HTML Code:
          levelsof SIC, local(codes)  
          foreach code in `codes' {          
          count if SIC == `code'        
          if r(N) >= 20 {                  
          reg DV IV1 if SIC == `code'        
          predict res_`code' if SIC == `code', residuals          
          }    }
          But it generated residuals for each SIC. But I wanted coefficients and residuals for each SIC-year (2000-2014).
          I would be really grateful if someone could help me with that, I have around 50 SICs spanning over 10 years (Each SIC will have many firms), approximately need around 50*10 sic-year regressions and generate residual and coefficients as separate variables.

          Comment

          Working...
          X