Announcement

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

  • Reporting multiple regression including lincom using foreach loop command

    Hi all,

    I need to report multiple regressions including their linear combinations in a single regression output table.
    I have put together a loop command for this however, there seems to be something wrong.
    If anyone is able to pick up on my mistake I would greatly appreciate the help.


    Code:
    // Define a list of dependent variables
    local depvars "Too_Cheap Too_Exp"
    
    // Split the list into separate variables
    foreach depvar of local depvars {
        
        // Run the regression
        reg `depvar' i.Soc##i.Env Attitude if mon == 1
        
        
        // Save the regression results in a table
        esttab using tableVW.docx, se append keep(1.Soc 1.Env 1.Soc#1.Env Attitude) b(2) starlevel(* 0.10 ** 0.05 *** 0.01) f noobs coeflabel(1.Soc "Social" 1.Env "Environmental" 1.Soc#1.Env "Interaction" Attitude "Attitude")
        
        // Calculate and save the first linear combination of coefficients
        lincomest _b[1.Soc] + _b[1.Soc#1.Env]
        esttab using tableVW.docx, append coeflabel((1) "Social + Interaction") f collabels(none) gaps plain nomtitles refcat((1) "\textit{Lincom}", nolabel) starlevel(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(2)) se(par fmt(2))) noobs
        
        // Calculate and save the second linear combination of coefficients
        lincomest _b[1.Env] + _b[1.Soc#1.Env]
        esttab using tableVW.docx, append coeflabel((1) "Environmental + Interaction") f collabels(none) gaps plain nomtitles starlevel(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(2)) se(par fmt(2))) stat(N ar2, labels("\hline N") fmt(a1))
    }

    and here is some sample data

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte Too_Cheap double Too_Exp float(Soc Env Soc_Env Attitude)
     5  20 0 0 0    2.752432
     5  29 0 1 0  -2.8950694
     0   9 1 0 0    .5110834
     9  40 1 1 1     .732045
     5  50 0 0 0  -3.4892976
     1  30 0 1 0    -.551881
     5  22 1 0 0   -.3218607
     5  50 1 1 1   -5.407672
    19  39 0 0 0   -2.620099
    10  30 0 1 0   -5.680028
     3  30 1 0 0   3.2253044
     1  40 1 1 1   2.1832685
     5 100 0 0 0  -.25337616
    12  35 0 1 0  -2.9394295
    10  30 1 0 0   3.2737865
     5  30 0 0 0    1.356387
    20  50 1 1 1   .53297347
    25  35 0 1 0    .6487824
    15  25 1 0 0   -5.194101
     5  35 0 0 0   .23693815
     5  30 0 1 0    .8878359
    10 100 1 0 0  -.14419919
    25 200 1 1 1     4.15253
    15  55 0 0 0     4.15253
    20  30 0 1 0    .2208825
     1  40 1 0 0   -5.680028
    10  50 1 1 1  -1.9176644
     5  50 0 0 0   1.2928877
    11  23 0 1 0    1.538296
    10  30 1 0 0  -1.4678836
    10  40 0 0 0   -4.761057
    20  75 1 1 1   -4.031064
    10 100 0 1 0    1.067579
    15  75 1 0 0   -2.513224
    12  45 0 0 0 -.007169687
    20  50 1 1 1  -2.1749706
    13  25 0 1 0   -5.680028
     5  35 1 0 0    .2177982
    10  40 0 0 0    3.112751
    10  80 0 1 0    .4424217
     9  50 1 0 0   1.6943907
    10  50 1 1 1   2.3797934
    15  35 0 0 0   1.6561188
     5  40 0 1 0   -2.427365
    15  40 1 0 0   -.1600608
    10  45 1 1 1  -4.1047564
    20  45 0 0 0    .9708589
    10  50 0 1 0   3.9095666
    10  31 1 0 0   -5.680028
    20  40 0 0 0   1.4430102
    10  50 1 1 1   -1.193782
    15  40 0 1 0  -.09726132
    20  45 1 0 0   -.7637488
    20  60 0 0 0    3.917984
    15  60 1 1 1   2.8606074
    10  50 0 1 0  -1.7110436
    10  40 1 0 0   2.1630564
    10  40 0 0 0  -1.3761293
    15  40 0 1 0   -1.489069
    19  70 1 1 1    .5454469
    15  50 1 0 0   1.4580144
    10  60 0 0 0   -.9691939
     5  40 0 1 0   -5.135316
    20  60 1 0 0  -1.2178873
    20  40 0 0 0    .9535123
    15  60 1 1 1   -4.223646
     0  51 0 1 0   .19722734
     5 100 1 0 0    .8239045
    15  50 0 0 0   -1.213591
    20  50 1 1 1  -1.9397106
    15  85 0 1 0   -1.931437
    20  39 0 0 0    .8540767
    10  40 1 0 0  -1.2935084
    20  50 1 1 1    2.165671
    29  49 0 1 0   3.2146304
    10 100 0 0 0   -3.069731
    15  50 1 0 0    .1962577
    20  50 0 1 0   -5.680028
    10  80 1 1 1    1.440369
     1 100 0 0 0   -5.680028
    25  70 1 0 0   -2.569635
    15  39 0 1 0    1.791618
     5  80 0 0 0   -2.430315
    25  80 1 0 0    .4588043
    30  70 1 1 1    1.702249
    25  50 0 1 0  -1.2351274
    10  80 0 0 0   .25786176
    25  45 1 0 0     3.15149
     5  45 1 1 1   -4.463997
    25  60 0 0 0    .4245142
    25  55 0 1 0   1.6943907
    20  45 1 0 0    .9025211
    30  80 1 1 1   .11551856
    35  36 0 0 0   2.6877315
    10  50 0 1 0   -5.463506
    20 100 1 0 0   -5.463506
    15  50 0 0 0    1.210864
    20  70 1 1 1    .7686392
    25  60 0 1 0   -.7643542
    20 100 1 0 0   1.5594715
    end
    --------



  • #2
    Code:
    ssc install estout, replace  //update allows multiple scalar additions
    estimates clear
    local c = 1
    foreach depvar in Too_Cheap Too_Exp {
        eststo e`c': qui reg `depvar' i.Soc##i.Env Attitude 
        lincom _b[1.Soc] + _b[1.Soc#1.Env]
        local r`c' = r(estimate)
        local p`c' = r(p)
        estimates restore e`c'
        lincom _b[1.Env] + _b[1.Soc#1.Env]
        local z`c' = r(estimate)
        local y`c' = r(p)
        estadd local pr "`= cond(`p`c''<0.01,"`:di %5.3f `=`r`c''''***", cond(`p`c''<0.05,"`:di %5.3f `=`r`c''''**", cond(`p`c''<0.1,"`:di %5.3f `=`r`c''''*",  "`:di %5.3f `=`r`c''''")))'"
        estadd local zr "`= cond(`y`c''<0.01,"`:di %5.3f `=`z`c''''***", cond(`y`c''<0.05,"`:di %5.3f `=`z`c''''**", cond(`y`c''<0.1,"`:di %5.3f `=`z`c''''*",  "`:di %5.3f `=`z`c''''")))'"
    local c = `c'+1
    }
    esttab e1 e2, se append keep(1.Soc 1.Env 1.Soc#1.Env Attitude) b(2)             ///
    star(* 0.10 ** 0.05 *** 0.01) stats(r2 F N pr zr)                                  ///
    coef(1.Soc "Social" 1.Env "Environmental" 1.Soc#1.Env "Interaction" Attitude "Attitude")
    HTML Code:
    https://www.statalist.org/forums/forum/general-stata-discussion/general/1514983-adding-lincom-postestimation-results-to-esttab-regression-table

    Comment


    • #3
      Hi George,

      this is fantastic! Thank you so much.

      Is it also possible to include the SE of the linear combinations below in parentheses? And to change the location of estimates to be below Attitude with a line separating theses results from thsoe of the independent variabls and r2 etc.

      Click image for larger version

Name:	Screen Shot 2023-02-22 at 08.40.19.png
Views:	1
Size:	40.9 KB
ID:	1702859


      Appreciate all the help

      Comment


      • #4
        I'm sure there's lots you can do, but I'm no expert on esttab. You can move those up, but not sure you can have them treated as coefficients. Maybe seperate esttab into groups and use append to stack a table.

        Code:
        estimates clear
        local c = 1
        foreach depvar in Too_Cheap Too_Exp {
            eststo e`c': qui reg `depvar' i.Soc##i.Env Attitude 
            lincom _b[1.Soc] + _b[1.Soc#1.Env]
            local r`c' = r(estimate)
            local s`c' = r(se)
            local p`c' = r(p)
            estimates restore e`c'
            lincom _b[1.Env] + _b[1.Soc#1.Env]
            local z`c' = r(estimate)
            local x`c' = r(se)
            local y`c' = r(p)
            estadd local pr "`= cond(`p`c''<0.01,"`:di %5.3f `=`r`c''''***", cond(`p`c''<0.05,"`:di %5.3f `=`r`c''''**", cond(`p`c''<0.1,"`:di %5.3f `=`r`c''''*",  "`:di %5.3f `=`r`c''''")))'"
            estadd local sr "(`:di %5.3f `=`s`c'''')"
            estadd local zr "`= cond(`y`c''<0.01,"`:di %5.3f `=`z`c''''***", cond(`y`c''<0.05,"`:di %5.3f `=`z`c''''**", cond(`y`c''<0.1,"`:di %5.3f `=`z`c''''*",  "`:di %5.3f `=`z`c''''")))'"
            estadd local xr "(`:di %5.3f `=`x`c'''')"
        local c = `c'+1
        }
        esttab e1 e2, se append keep(1.Soc 1.Env 1.Soc#1.Env Attitude) b(2)             ///
        star(* 0.10 ** 0.05 *** 0.01) stats( pr sr zr xr r2 F N)                                  ///
        coef(1.Soc "Social" 1.Env "Environmental" 1.Soc#1.Env "Interaction" Attitude "Attitude")

        Comment

        Working...
        X