1
votes

I have a set of regressions that are exported using the community-contributed Stata command estout in the following format:

foreach yvar of varlist yvar1 yvar2 {
    eststo: quietly regress `yvar' xvar1 xvar2, robust
    estadd ysumm
}

esttab using mydoc.csv, se stats(N ymean r2_a) replace

However, I found that the community-contributed command outreg2 can create better regression tables, so I would like to use that instead.

I would like to include the mean of dependent variable (ymean) when using outreg2 but I cannot call e(ymean) as the relevant scalars are not created.

Below is my code:

local first_loop yes

foreach yvar of varlist yvar1 yvar2 {
    quietly regress `yvar' xvar1 xvar2, robust
    if "`first_loop'"=="yes"{
        outreg2 using outreg2_test.xls, replace ctitle(`yvar') ///
         addstat(Mean, e(ymean)) label
        local first_loop no
    }
    else {
        outreg2 using outreg2_test.xls, append ctitle(`yvar') ///
         addstat(Mean, e(ymean)) label
    }
}

My code above does not work because outreg2 does not recognize e(ymean).

My questions:

  • Is this scalar specific to estadd?

  • Is there a way I can include mean of dependent variable in the regression output table when using outreg2?

2

2 Answers

2
votes

It appears that e(ymean) is not standard regression output for regress.

The simplest method is probably to specify that you wish to report the dependent variable as well (depvar option), and that you wish to report the mean as well as the coefficient and standard errors.

For example:

outreg2 using outreg2_test.xls, replace ctitle(`yvar') depvar stats(coef se mean) label

Note that this method will also report the means of your independent variables.

Alternatively, to report only the ymean at the bottom of the column, you can summarize your dependent variable and add the mean manually:

local first_loop yes

foreach yvar of varlist yvar1 yvar2 {
  quietly regress `yvar' xvar1 xvar2, robust
  quietly summ `yvar'   
  if "`first_loop'"=="yes"{
    outreg2 using outreg2_test.xls, replace ctitle(`yvar') addstat(Mean, r(mean)) label
    local first_loop no
  }
  else {
    outreg2 using outreg2_test.xls, append ctitle(`yvar') addstat(Mean, r(mean)) label
  }
}

You can also add the ymean to a local macro and use it in your addstat() option if preferred, and -depending on your regression analysis- you may want to add if e(sample) to your summarize command.

1
votes

The command estadd ysumm can also be used before calling outreg2:

local first_loop yes

foreach yvar of varlist yvar1 yvar2 { 
    quietly regress `yvar' xvar1 xvar2, robust
    estadd ysumm
    if "`first_loop'"=="yes"{
        outreg2 using outreg2_test.xls, replace ctitle(`yvar') ///
           addstat(Mean of dependent variable, e(ymean)) label

        local first_loop no
    }
    else {
        outreg2 using outreg2_test.xls, append ctitle(`yvar') ///
           addstat(Mean of dependent variable, e(ymean)) label
    }
}