Announcement

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

  • Including confidence intervalls in bar graph with two variables

    Dear all,

    I'm using Stata (version 17.0) for the first time to get through the statistics for my master thesis. It went well but now I got stuck with including two variables with confidence intervalls into my graph. It would be greatly appreciated, if anyone could help me with this.

    What's the issue: I'm trying to create a graph similar to the one below with red and blue bars, but with 95%-confidence intervalls included. Achieving 1-3 at the same time has so far been impossible for me.
    1. The graph needs to include 2 variables CFCindex (CFCindivtotaldayexpenditure) and extv (external_values), which are both of monetary valus.
    2. I need confidence intervalls.
    3. The variables are supposed to be grouped by location (BI03bWardname), so that values are directly comparable.

    This is the structure that I want to produce, but I'm struggeling at doing this with confidence intervalls.
    Code:
    graph bar CFCindvex extv, over(BI03bWardname)
    Click image for larger version

Name:	Graph bar.png
Views:	1
Size:	84.1 KB
ID:	1767823




    I've tried about any option I was able to find and understand. These are the results I've produced so far, maybe there's something in it which only needs small adaption:

    Using cibar, I did get the confidence intervalls but not for 2 variables
    Code:
    cibar CFCindvex, over (BI03bWardname)
    Click image for larger version

Name:	Graph cibar.png
Views:	1
Size:	67.4 KB
ID:	1767824


    Using ciplot, I'm quite close to the result I'd like to have, but without the bars.
    Code:
    ciplot CFCindvex extv, by(ward_numeric) vertical
    Click image for larger version

Name:	Graph ciplot.png
Views:	1
Size:	79.1 KB
ID:	1767825


    Using coefplot (sorry for the messed up labelling) bars and confidence intervalls are there, but I didn't find a way to do the sorting as needed.
    Code:
    estimates clear
    local list
    foreach v of varlist CFCindvex external_values {
         qui: mean `v', over(ward_numeric)
         estimates store e_`v'
    }
    
    coefplot e_CFCindvex e_external_values, vertical recast(bar) citop fint(*.95) graphr(c(white)) nooffset
    Click image for larger version

Name:	graph coefplot.png
Views:	1
Size:	96.3 KB
ID:	1767826


    Please let me know if you need more context or information and thanks a lot for your help with this!
    Lina

  • #2
    I would use cisets from SSC, which certainly supersedes my ciplot (SSC) without doing the same thing.

    For more on cisets, see the announcement at https://www.statalist.org/forums/for...-interval-sets

    The point to cisets is not to draw graphs, but to produce datasets containing the data you need to draw graphs.

    I don't know very much about the other two commands, both also from SSC, and may be missing something simple.

    Here is a problem with -- so far as I can tell -- similar structure. You don't give a data example or use a standard dataset, so I use a different example.

    Please see FAQ Advice #12 for how to explain community-contributed commands you are using and how to give a data example.


    Code:
    webuse citytemp, clear 
    
    cisets mean cooldd, over(region) saving(ci1, replace)
    
    cisets mean heatdd, over(region) saving(ci2, replace)
    
    use ci1 
    
    append using ci2 
    
    list 
    
    gen x = cond(varname == "cooldd", group - 0.2, group + 0.2)
    
    forval j = 1/4 {
        local which : label (group) `j'
        local call `call' `j' "`which'"
    }
    
    twoway bar point x if varname == "cooldd", barw(0.4) lcolor(blue) fcolor(blue*0.2) ///
    || rcap ub lb x if varname == "cooldd", lcolor(blue) ///
    || bar point x if varname == "heatdd", barw(0.4) lcolor(red) fcolor(red*0.2) ///
    || rcap ub lb x if varname == "heatdd", lcolor(red) legend(order(1 "Cooling" 3 "Heating") pos(12) row(1)) ///
    xla(`call', noticks) /// 
    xtitle("") ytitle(Degree days (days {&degree}F)) name(G1, replace)
    
    scatter point x if varname == "cooldd", mcolor(blue) ///
    || rcap ub lb x if varname == "cooldd", lcolor(blue) ///
    || scatter point x if varname == "heatdd", mcolor(red) ///
    || rcap ub lb x if varname == "heatdd", lcolor(red) legend(order(1 "Cooling" 3 "Heating") pos(12) row(1)) ///
    xla(`call', noticks) /// 
    xtitle("") ytitle(Degree days (days {&degree}F)) name(G2, replace)

    You appear to want the widely condemned bar plus error bar plot, pejoratively known as dynamite plot, detonator plot, or plunger plot. See e.g.

    https://simplystatistics.org/posts/2...lots-must-die/

    http://users.stat.umn.edu/~rend0020/...4-dynamite.pdf

    for why very competent people think it's a bad idea.

    That's why ciplot doesn't support such a style.

    A bare minimum for such plots is to use a pale infill colour so that the error bar remains visible.

    In this particular example, large samples mean short intervals, but the point is the technique not the results.

    Click image for larger version

Name:	ci_G1.png
Views:	1
Size:	41.1 KB
ID:	1767831
    Click image for larger version

Name:	ci_G2.png
Views:	1
Size:	37.7 KB
ID:	1767832

    Comment


    • #3
      Hello Nick,

      thanks a lot for your support! This was exactly what I was looking for, you helped me out a lot. Also thank you for the heads up on the overall problem with those bars. For my purpose it should do the trick, but I'll keep it in mind for the discussion and interpretation of the data.

      Sorry for not including the data, I'll be aware of this if I need to ask anything in the future.

      All the best
      Lina

      Comment


      • #4
        Although I dislike the style, I should point out that

        Code:
        webuse citytemp, clear 
        
        ciplot cooldd heatdd, by(region) recast(bar) name(G0, replace) fcolor(none ..)
        is an ad hoc stab at what you want. You may need base(0) for your data.

        Comment

        Working...
        X