Announcement

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

  • Bar Graph with SE bars - 2 continuous variables

    Hi all,

    I need to add standard error bars to the bar chart below.
    My question is whether there is a way to add SE bars when plotting more than one variable?
    My survey data contains a categorical variable determing which treatment the subject was allocated to (Treatment group).
    Within each treatment subjects had to answer two questions "Too_Cheap" and "Cheap".

    This is the code I used to plot the graph below

    Code:
    collapse (mean) meanToo_Cheap = Too_Cheap meanCheap = Cheap (sd) sdToo_Cheap = Too_Cheap sdCheap = Cheap (count) nToo_Cheap= Too_Cheap nCheap= Cheap, by(Treatment_Group)
    generate hiToo_Cheap = meanToo_Cheap + invttail(nToo_Cheap-1,0.025)*(sdToo_Cheap / sqrt(nToo_Cheap))
    generate loToo_Cheap = meanToo_Cheap - invttail(nToo_Cheap-1,0.025)*(sdToo_Cheap / sqrt(nToo_Cheap))
    generate hiCheap = meanCheap + invttail(nCheap-1,0.025)*(sdCheap / sqrt(nCheap))
    generate loCheap = meanCheap - invttail(nCheap-1,0.025)*(sdCheap / sqrt(nCheap))
    
    graph bar meanToo_Cheap meanCheap, over(Treatment_Group)
    graph twoway (bar meanToo_Cheap meanCheap Treatment_Group) (rcap hiToo_Cheap loToo_Cheap hiCheap loCheap Treatment_Group)
    Click image for larger version

Name:	Screen Shot 2023-01-21 at 16.55.32.png
Views:	1
Size:	41.1 KB
ID:	1698111


    When I try use the rcast command as follows I get the message "rcap requires 3 variables: hiToo_Cheap loToo_Cheap hiCheap loCheap Treatment_Group"

    Here is also some data

    Would be super grateful for any help,




    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte Treatment_Group int Cheap double Too_Cheap
    2  1    0
    1  8    5
    2 12    5
    3 12   11
    2 17   15
    3 16   13
    3 10    5
    3  5    1
    2 20   10
    2 10    3
    3 30   20
    1 10    5
    3 20    5
    2 20   10
    3 15   10
    2 16   10
    3 17   12
    1 12    5
    2 25    5
    3 20   25
    1 20   15
    1 25   35
    3 20   15
    1  9   19
    1 29   20
    1 20   10
    1 30   20
    4 10    9
    3 30   10
    2 20   15
    3 10    5
    1 18   10
    1 25   20
    2 25   10
    3 15    5
    3 25   15
    4 15    1
    2 10    1
    1 20   10
    2 30   10
    3 20   15
    4 20   10
    2 25   20
    1 25   12
    2 30   25
    1 25   20
    2 30   20
    4 10    5
    3 19   29
    2 45   15
    3 35   20
    2 20    9
    4 30   20
    1 30   15
    1 20    5
    3 30   25
    2 25   15
    1 20   10
    3 30   20
    4 25   10
    4 20   10
    1 25 9.99
    3 30   10
    1 20    3
    3 40   40
    1 20   15
    3 25   10
    4 30   20
    3 25   15
    3 20   10
    3 18   10
    4 20   10
    1 30   10
    4 30   20
    4 30   20
    3 10   10
    4 12    5
    1 15    5
    4 40   25
    4 60   20
    2 25   15
    3 15    0
    2 39   30
    1 40   35
    1 22   15
    1 25   15
    3 25   25
    3 25   18
    1 20   10
    2 29   15
    1 20   10
    2 35   20
    2 15    5
    3 45   20
    1 40   18
    3 40   30
    3 35   20
    1 40   10
    3 30   25
    1 30   20
    end
    label values Treatment_Group Treatment_labels
    label def Treatment_labels 1 "1. Control", modify
    label def Treatment_labels 2 "2. SOC", modify
    label def Treatment_labels 3 "3. ENV", modify
    label def Treatment_labels 4 "4. SOC*ENV", modify


  • #2
    I find it easier (less error-prone) to work with margins and marginsplot in situations like these. Here's an example using combomarginsplot (ssc)

    Code:
    reg Too_Cheap i.Treatment_Group
    margins Treatment_Group, saving(m1, replace)
    
    reg Cheap i.Treatment_Group
    margins Treatment_Group, saving(m2, replace)
    
    // ssc install combomarginsplot
    combomarginsplot m1 m2, legend(order (1 "Too Cheap" 2 "Cheap")) xtitle("Treatment Group")
    
    erase m1.dta
    erase m2.dta
    Last edited by Justin Niakamal; 21 Jan 2023, 10:15.

    Comment


    • #3
      Hi Justin,

      thank you very much for your response.

      I am working with Stata 13 and unfortunately get an error message that the combomarginsplot requires Stata16.

      Would you happen to have another suggestion?

      Thanks,
      Carmen

      Comment


      • #4
        Try this

        Code:
        reg Too_Cheap i.Treatment_Group
        margins Treatment_Group, saving(m1, replace)
        
        reg Cheap i.Treatment_Group
        margins Treatment_Group, saving(m2, replace)
        
        use m1, clear
        append using m2, gen(m2)
        
        tw rcap _ci_ub _ci_lb _m1  if m2, lcolor(blue) || connected _margin _m1  if m2, lcolor(blue) mcolor(blue) ||    /// 
           rcap _ci_ub _ci_lb _m1  if !m2, lcolor(red) || connected _margin _m1 if !m2, lcolor(red) mcolor(red)         ///
           xlab(, valuelabel labsize(small)) legend(order(2 "Cheap" 4 "Too Cheap")) xtitle("Treatment Group")             ///
           scheme(s1color) ylab(, angle(h)) leg(region(lcolor(white))) graphregion(margin(r+5))
           
          
          
        erase m1.dta
        erase m2.dta
        Last edited by Justin Niakamal; 21 Jan 2023, 13:40.

        Comment


        • #5
          This gives me a graph as below

          Click image for larger version

Name:	Screen Shot 2023-01-22 at 09.51.08.png
Views:	1
Size:	47.9 KB
ID:	1698175


          I would need the bar graph (from initial post) with the SE bars on top (sorry, maybe my message was not very clear on this).
          Perhaps I could overlay this one to the bar graph?

          I have managed to create the graph for both variables separately. Another idea is to combine these? Is this possible?

          Code:
          collapse (mean) meanToo_Cheap = Too_Cheap meanCheap = Cheap (sd) sdToo_Cheap = Too_Cheap sdCheap = Cheap (count) nToo_Cheap= Too_Cheap nCheap= Cheap, by(Treatment_Group)
          generate hiToo_Cheap = meanToo_Cheap + invttail(nToo_Cheap-1,0.025)*(sdToo_Cheap / sqrt(nToo_Cheap))
          generate loToo_Cheap = meanToo_Cheap - invttail(nToo_Cheap-1,0.025)*(sdToo_Cheap / sqrt(nToo_Cheap))
          generate hiCheap = meanCheap + invttail(nCheap-1,0.025)*(sdCheap / sqrt(nCheap))
          generate loCheap = meanCheap - invttail(nCheap-1,0.025)*(sdCheap / sqrt(nCheap))
          
          graph twoway (bar meanToo_Cheap Treatment_Group) (rcap hiToo_Cheap loToo_Cheap Treatment_Group)
          
          graph twoway (bar meanCheap Treatment_Group) (rcap hiCheap loCheap Treatment_Group)

          Comment


          • #6
            See the top results of Googling for "dynamite plot" for why many people, including myself, strongly recommend graphs like that above over so-called dynamite, plunger, or detonator plots.

            Comment

            Working...
            X