Announcement

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

  • Graph bar multiple dichotomous variables by another dichotomous variable

    Hey. I have ten dichotomous variables (yes/no) to describe specific terms of an event. The dependent variable is also a dichotomous variable (yes/no). I have to generate a bar chart which shows in the best way the percentage of each variable according to the two categories of the dependent variable. I've been trying to find the proper command without success.

  • #2
    The (conditional) mean of a binary variable carries the information you need. Hence consider some variation on this for technique.

    Code:
    clear 
    set obs 100 
    
    * 10 (0,1) predictors 
    forval j = 1/10 { 
        gen x`j' = _n >= 9 * `j' 
    } 
    
    * 1 (0,1) outcome 
    gen y = mod(_n, 2) 
    
    graph bar (mean) x*, over(y)

    Comment


    • #3
      Thanks a lot for your assistance! I appreciate your recommendation, but what if I want to generate a bar chart which the order sets all independent variables in the x axis one next to the other (e.g. apples(yes), apples(no), pears (yes), pears (no), and so on). I want the dependent variable be shown by the color of the bars instead of differentiating 0 to the left and 1 to the right side of the x axis.

      Comment


      • #4
        Code:
        clear 
        set obs 100 
        
        * 10 (0,1) predictors 
        forval j = 1/10 { 
            gen x`j' = _n >= 9 * `j' 
        } 
        
        * 1 (0,1) outcome 
        gen y = mod(_n, 2) 
        
        preserve 
        
        gen id = _n 
        reshape long x, i(id y) j(which)
        separate x, by(y) 
        
        graph hbar x?, over(y) over(which) legend(order(1 "y = 0" 2 "y = 1"))
        
        restore

        Comment


        • #5
          Thanks for your code, Nick. And I am sorry for getting back to you again. The bar chart format looks exactly as I wanted. I didn't made any changes, how may I put in the percentages for each bar manually to represent my data?

          Comment


          • #6
            That just calls for blabel(bar) -- do consult

            Code:
            help graph hbar
            -- to see the means of (0,1) variables, except that if you wish to see percentages, you need to multiply by 100 first. Display format is at your choice.

            Code:
            clear 
            set obs 100 
            
            * 10 (0,1) predictors 
            forval j = 1/10 { 
                gen x`j' = _n >= 9 * `j' 
            } 
            
            * 1 (0,1) outcome 
            gen y = mod(_n, 2) 
            
            preserve 
            
            gen id = _n 
            reshape long x, i(id y) j(which)
            replace x = x * 100 
            separate x, by(y) 
            
            graph hbar x?, over(y) over(which) legend(order(1 "y = 0" 2 "y = 1")) blabel(bar, format(%2.1f)) 
            
            restore

            Comment


            • #7
              framed_bar from SSC is another way to do this.

              https://www.statalist.org/forums/for...dable-from-ssc

              Comment

              Working...
              X