Announcement

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

  • outreg2

    Hello,

    I have a question regarding exporting the Stata table using outreg2. The following is my code:

    foreach var in s109c2 s109c1a s109c1b s109c1c s109c1d s109c1e s109c1f s765t s765ba s765bb s765bc s765bd s765be s765bf diff_gr {


    reg `var' rv treatment treat_rv if rv<35 & rv>=-35, r
    outreg2 using "Table1.xls", replace nocons bdec(3) se ctitle("Linear RD, 1984-1989") keep (treatment) label

    reg `var' rv treatment treat_rv if rv<47 & rv>=-48, r
    outreg2 using "Table1.xls", append nocons bdec(3) se ctitle("Linear RD, 1983-1990") keep (treatment) label

    reg `var' rv treatment treat_rv if rv<59 & rv>=-60, r
    outreg2 using "Table1.xls", append nocons bdec(3) se ctitle("Linear RD, 1982-1991") keep (treatment) label

    reg `var' rv treatment treat_rv if rv<83 & rv>=-84, r
    outreg2 using "Table1.xls", append nocons bdec(3) se ctitle("Linear RD, 1980-1993") keep (treatment) label


    }

    But I do not get RDD coefficient for each variable in a different row after this. Just one variable. My question is, how can I export table with a row lable of each variable reporting the coefficient of treatment only, without mentioning treatment in the table? In general, do you have any suggestions for exporting this code to Excel? I'm stuck here. Thanks in advance.

  • #2
    outreg2 is from SSC (FAQ Advice #12). It is not clear what you want. You have 15 variables \(\times\) 4 regressions = 60 coefficients. Do you want 15 columns with 4 rows each or something else?

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      outreg2 is from SSC (FAQ Advice #12). It is not clear what you want. You have 15 variables \(\times\) 4 regressions = 60 coefficients. Do you want 15 columns with 4 rows each or something else?
      I want 15 rows with 4 columns(columns would specify the type of RD and chosen years), all in one table.

      Comment


      • #4
        Install estout from SSC. The code below creates a dataset similar to yours. You can make the code exact by creating a 4-level categorical variable called "which" corresponding to your -if- conditions. See

        Code:
        help estout
        for options to label the columns and coefficients. You can export as CSV format which can be opened in Excel.

        Code:
        clear
        set obs 100
        set seed 02142022
        estimates clear
        local vars "s109c2 s109c1a s109c1b s109c1c s109c1d s109c1e s109c1f s765t s765ba s765bb s765bc s765bd s765be s765bf diff_gr"
        
        foreach var of local vars{
            gen `var'= runiform()
        }
        gen treatment= runiformint(0,1)
        gen which = runiformint(1,4)
        
        local i 1
        local models
        forval w= 1/4{
            foreach var of local vars{
                eststo m`i': reg `var' treatment if which==`w'
                local models "`models' m`i'"
                local ++i
            }
        }
        
        *PROGRAM TO APPEND MODELS
        capt prog drop appendmodels
        *! version 1.0.0  14aug2007  Ben Jann
        program appendmodels, eclass
            // using first equation of model
            version 8
            syntax namelist
            tempname b V tmp
            foreach name of local namelist {
                qui est restore `name'
                mat `tmp' = e(b)
                local eq1: coleq `tmp'
                gettoken eq1 : eq1
                mat `tmp' = `tmp'[1,"`eq1':"]
                local cons = colnumb(`tmp',"_cons")
                if `cons'<. & `cons'>1 {
                    mat `tmp' = `tmp'[1,1..`cons'-1]
                }
                mat `b' = nullmat(`b') , `tmp'
                mat `tmp' = e(V)
                mat `tmp' = `tmp'["`eq1':","`eq1':"]
                if `cons'<. & `cons'>1 {
                    mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
                }
                capt confirm matrix `V'
                if _rc {
                    mat `V' = `tmp'
                }
                else {
                    mat `V' = ///
                    ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
                    ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
                }
            }
            local names: colfullnames `b'
            mat coln `V' = `names'
            mat rown `V' = `names'
            eret post `b' `V'
            eret local cmd "whatever"
        end
        
        
        eststo M1: appendmodels  `=ustrregexra("`models'", "^(.*m15)(.*)$", "$1")'
        eststo M2: appendmodels `=ustrregexra("`models'", "^.*m15(.*m30)(.*)$", "$1")'
        eststo M3: appendmodels `=ustrregexra("`models'", "^.*m30(.*m45)(.*)$", "$1")'
        eststo M4: appendmodels `=ustrregexra("`models'", "^.*m45(.*m60)(.*)$", "$1")'
        esttab M1 M2 M3 M4, noobs
        Run this in a do file. The local `models' contains all saved results.

        Code:
        
        . di "`models'"
         m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19 m20 m21 m22 m23 m24 m25 m26 m27 m28 m29 m30 m
        > 31 m32 m33 m34 m35 m36 m37 m38 m39 m40 m41 m42 m43 m44 m45 m46 m47 m48 m49 m50 m51 m52 m53 m54 m55 m56 m57 m58
        > m59 m60
        So what the last lines of code are doing is to pick sets of 15 stored estimates: m1-m15, m16-m30, and so on.

        Code:
        . di ustrregexra("`models'", "^(.*m15)(.*)$", "$1")
         m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15
        
         di ustrregexra("`models'", "^.*m15(.*m30)(.*)$", "$1")
         m16 m17 m18 m19 m20 m21 m22 m23 m24 m25 m26 m27 m28 m29 m30
        Res.:

        Code:
        . esttab M1 M2 M3 M4, noobs
        
        ----------------------------------------------------------------------------
                              (1)             (2)             (3)             (4)  
                                                                                    
        ----------------------------------------------------------------------------
        treatment          0.0891          0.0405          -0.109          -0.117  
                           (0.84)          (0.34)         (-1.02)         (-0.98)  
        
        treatment          -0.167          -0.130          0.0531         -0.0435  
                          (-1.41)         (-1.18)          (0.46)         (-0.30)  
        
        treatment           0.107         -0.0212          0.0234          0.0181  
                           (0.85)         (-0.19)          (0.22)          (0.17)  
        
        treatment           0.124         -0.0646          -0.156         0.00331  
                           (1.19)         (-0.59)         (-1.41)          (0.03)  
        
        treatment         -0.0687          -0.273*         0.0150         -0.0545  
                          (-0.75)         (-2.42)          (0.21)         (-0.38)  
        
        treatment         -0.0416           0.201          0.0958          -0.109  
                          (-0.33)          (1.62)          (0.93)         (-0.96)  
        
        treatment          0.0302          0.0528           0.128          0.0562  
                           (0.24)          (0.39)          (1.32)          (0.46)  
        
        treatment         -0.0302         -0.0640           0.155          -0.135  
                          (-0.29)         (-0.49)          (1.38)         (-1.17)  
        
        treatment           0.164          0.0580         0.00767          0.0394  
                           (1.31)          (0.61)          (0.07)          (0.33)  
        
        treatment           0.134         -0.0851          -0.177           0.154  
                           (1.07)         (-0.78)         (-1.67)          (1.25)  
        
        treatment           0.129           0.127         -0.0652         -0.0777  
                           (1.02)          (1.12)         (-0.57)         (-0.60)  
        
        treatment           0.118           0.143         0.00328         -0.0589  
                           (1.20)          (1.26)          (0.03)         (-0.65)  
        
        treatment         0.00590          -0.200          0.0882         -0.0634  
                           (0.05)         (-1.76)          (0.99)         (-0.58)  
        
        treatment         -0.0120          -0.306***       -0.112         -0.0680  
                          (-0.13)         (-3.47)         (-1.14)         (-0.50)  
        
        treatment          -0.201        -0.00968          -0.117          -0.140  
                          (-1.54)         (-0.09)         (-1.09)         (-1.15)  
        ----------------------------------------------------------------------------
        t statistics in parentheses
        * p<0.05, ** p<0.01, *** p<0.001
        Last edited by Andrew Musau; 14 Feb 2022, 09:24.

        Comment

        Working...
        X