daviddupont
daviddupont

Reputation: 175

Stata bar chart multiple models

I am running the same regression for two variables, once with robust standard errors and one without them. Thus, in total I have 4 regression results. This is a replication material:

sysuse auto, clear      

tempfile abcd
   save abcd, empty replace

preserve
reg price mpg
regsave mpg
gen Y=1
gen X="Normal SEs"
save abcd, replace
restore

preserve
reg price rep78
regsave rep78
gen Y=1
gen X="Normal SEs"
append using abcd
save abcd, replace
restore

            
forvalues i=1/2{
if `i'==1 local sub="mpg"
if `i'==2 local sub="rep78"
            preserve
            reg price `sub', robust
            regsave `sub'
            gen Y=1
            gen X="Robust SEs"
            append using abcd
            save abcd, replace
            restore
            }
            
use abcd, clear

I want to create a horizontal bar chart comparing the size of the standard errors for the mpg and the rep78 regressions when using robust and when not using robust. Ideally it should have on the axis "mpg" and then two bars of different colours (for robust and normal SEs), and the same for "rep78". Ideally it would look something like this ( https://www.stata.com/support/faqs/graphics/gph/graphdocs/horizontal-bar-chart-with-multiple-bars-graphed-over-another-variable/index.html). However, the chart in the link takes the mean instead of the individual regressions in each category (in my actual dataset I have 6 regressions)

I tried the following:

eclplot stderr stderr stderr Y, horizontal supby(X) rplottype(bar)   

But it only has one column and the bars are overlayed instead of next to each other. I was wondering if someone could help me create this chart?

Upvotes: 1

Views: 183

Answers (1)

Nick Cox
Nick Cox

Reputation: 37358

regsave and eclplot are community-contributed, as you are asked to explain (Stata tag wiki).

For the problem stated, you don't need a choreography of preserve restore save append and what not. You can just loop over regressions and save what you want to other variables in the dataset.

Note: The results variables are not aligned with the existing dataset.

Note: The tacit assumption is that you won't need more results than there are observations in the dataset.

For a more serious program, I would just code this up using postfile.

sysuse auto, clear 

gen predictor = "" 
gen robust = mod(_n, 2) == 0  
label def robust 0 "not robust" 1 "robust"
label val robust robust 

gen se = . 

local i = 1 

foreach x in mpg rep78 { 
    
reg price `x'
replace se = _se[`x'] in `i'
replace predictor = "`x'" in `i'

local ++i 
reg price `x', robust 
replace se = _se[`x'] in `i'
replace predictor = "`x'" in `i'

local ++i 

} 

graph hbar (asis) se, over(robust) over(predictor) scheme(s1color) asyvars

Upvotes: 1

Related Questions