Announcement

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

  • graph bar with confidence interval

    Hi,

    I am trying to produce a nice graph bar but with confidence interval. It looks pretty tedious but I found this method here. I have 1 y-variable (info_avoiding_ba) as well as 1 dummy (T_Meat). When I type the following code everything works up to the last line where I get the message "too few variables specified" from Stata. Can anyone help? Cheers!

    collapse (mean) meaninfo_avoiding_ba= info_avoiding_ba (sd) sdinfo_avoiding_ba=info_avoiding_ba (count) n=info_avoiding_ba, by(T_Meat)
    generate hiwrite = meaninfo_avoiding_ba + invttail(n-1,0.025)*(sdinfo_avoiding_ba / sqrt(n))
    generate lowrite = meaninfo_avoiding_ba - invttail(n-1,0.025)*(sdinfo_avoiding_ba / sqrt(n))
    graph twoway (bar meaninfo_avoiding_ba) (rcap hiwrite lowrite), by(T_Meat)

    Also, if anyone knows how to add the significance value on the figure (above the bars for example)?


  • #2
    The immediate point is that twoway bar needs two variables and twoway rcap needs three.

    Here's what I think is a better way to do the sort of thing you want:

    Code:
    sysuse auto, clear
    
    statsby, by(foreign) : ci means mpg 
    
    twoway bar mean foreign, base(0) barw(0.6) bfcolor(green*0.1) || rcap ub lb foreign , xla(0 1, valuelabel) legend(off) ytitle(Miles per gallon) yla(0(5)25, ang(h))
    and here is what I think is a better way to show the data, as point symbols and bars.

    https://www.stata-journal.com/articl...article=gr0045 Section 4.

    You can add P-values and the like via titles or added text.

    Google dynamite plots and look at the first few hits to see why many statistical people advise strongly against this kind of plot.

    Comment


    • #3
      Let's push this a bit further and think about three possible representations (and there are many more)

      (1) a dynamite, detonator or plunger bar + error bar

      (2) point or marker symbol for estimate + error bar

      (3) a stripplot as from stripplot on SSC.

      (1) has a real virtue if comparisons with zero are fundamental (they usually aren't).

      (2) doesn't waste space showing zero

      (3) shows all the detail in the data too.

      In the code (3) is calculated first, as the statsby calculation produces a new resultsset.

      To show willing towards those so inclined an ornamental P-value is provided.

      Code:
      set scheme s1color 
       
      sysuse auto, clear
      
      ttest mpg, by(foreign)
      
      local show "{it:t} test {it:P}-value `: di %05.4f r(p)'" 
      
      stripplot mpg, over(foreign) height(0.6) bar cumul cumprob vertical refline yla(, ang(h)) name(G3, replace) xla(, noticks) t1title(`show') note((3))
      
      statsby, by(foreign) : ci means mpg 
      
      local opts xla(0 1, valuelabel noticks) legend(off) ytitle(Miles per gallon) xsc(r(-0.2 1.2)) t1title(`show')
      
      twoway bar mean foreign, base(0) barw(0.4) bfcolor(green*0.1) `opts' || rcap ub lb foreign ,  yla(0(5)25, ang(h)) name(G1, replace) note((1))
      
      scatter mean foreign || rcap ub lb foreign, `opts' aspect(1)  yla(15(5)30) name(G2, replace) note((2))
      Click image for larger version

Name:	ci_1.png
Views:	1
Size:	15.0 KB
ID:	1698527
      Click image for larger version

Name:	ci_2.png
Views:	1
Size:	15.5 KB
ID:	1698528
      Click image for larger version

Name:	ci_3.png
Views:	1
Size:	25.7 KB
ID:	1698529

      Comment

      Working...
      X