Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: error bars on bar plots


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: error bars on bar plots
Date   Mon, 30 Jun 2003 14:43:13 +0100

Stephen McKay

> I'm no expert but isn't 
> 
> twoway bar mean xcatvar || rcap upper lower xcatvar
> 
> most of what you need?  Overlaying a bar plot with capped spikes.
> 

Stephen is quite correct. As I pointed out in 
a previous thread earlier in the month started by Buzz Burhans: 

> It so happens that Stata's been a bit laggard on producing  
> wrapper commands for plots showing error bars. 
> I doubt there's any policy there. 

> However, having implemented various high- 
> level commands in one way does inhibit twisting 
> them in a different direction. -graph bar- 
> for example is basically built around a -collapse- 
> of the data. Building something else major on top of 
> that, such as machinery for adding confidence 
> intervals generally, would be, I guess, rather tricky. 

My posting then focused on my criticisms of a certain 
kind of error bar plot, and Buzz's reply focused on 
his reaction to that critique. I don't think either 
of us convinced the other. 

However, what is said above for -graph bar- 
does not inhibit building something based on 
-graph twoway bar-, and you can try your own 
wrapper command. 

Here is a "no holds barred" (no bars held?) plot, 
error bars on top of thicker bars. You have 
to supply 

1. bar height variable 
2. upper error bar position variable 
3. lower error bar position variable 
4. other axis variable 

*! NJC 1.0.0  30jun2003
program baronbar
	version 8
	syntax varlist(min=4 max=4) [if] [in] [,	///
		HORizontal VERTical base(str) BARWidth(str) ///
                BSTYle(str) BColor(str) BFColor(str)    ///        
		BLSTYle(str) BLColor(str) BLWidth(str)  ///      
		BLPattern(str) plot(str asis) *         /// 
	]

	tokenize `varlist'
	args height upper lower groupvar 

	local yttl : var label `height'
	if `"`yttl'"' == "" local yttl `height'
		
	if `"`plot'"' == "" local legend legend(nodraw)

        foreach o in /// 
		base barwidth bstyle bcolor bfcolor ///
		blstyle blcolor blwidth blpattern { 
		if `"``o''"' != "" local baropts "`baropts' `o'(``o'')" 
	}	
		
	graph twoway		        	///
	(bar `height' `groupvar'	        ///
		`if' `in',			///
		ytitle(`"`yttl'"')		///
		`horizontal'                    /// 
		`vertical'                      /// 
		`baropts'    			///
	)					///
	(rcap `upper' `lower' `groupvar'	///
		`if' `in',			///
		`horizontal'                    /// 
		`vertical'                      /// 
		`legend'			///
		`options'                       /// 
	)					///
	|| `plot'				///
	// blank
end

For example, 

sysuse auto, clear
egen mean = mean(mpg), by(rep78) 
egen n = count(mpg), by(rep78) 
egen sd = sd(mpg), by(rep78) 
gen upper = mean + invttail(n-1,0.025) * sd / sqrt(n) 
gen lower = mean - invttail(n-1,0.025) * sd / sqrt(n) 

baronbar mean upper lower rep78 , barw(0.4) bstyle(ci)

Nick 
[email protected] 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index