Announcement

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

  • Can I make a bivariate histogram for two indicator variables?

    Let's say I have two dummy variables like this.

    Code:
    set obs 100
    gen A=runiformint(0,1)
    gen B=runiformint(0,1)
    I want to graphically see how many are A=1, B=1 & how many are A=1,B=0 & how many are A=0,B=1 & how many are A=0, B=0.

    So basically I need a bivariate histogram. Is it available on Stata? I googled a bit and it seems R has it.

  • #2
    I got most of the way towards something of the kind with this code

    Code:
    clear 
    set obs 100
    set seed 2803 
    gen A=runiformint(0,1)
    gen B=runiformint(0,1)
    graph bar (count), over(A) by(B, note("") l1title(A, orient(horizontal) size(vlarge)) b1title(B) col(1)) blabel(bar) subtitle(, pos(9) nobox nobexpand fcolor(none)) 
    and then made some tweaks with the graph editor.


    Click image for larger version

Name:	biv_histogram.png
Views:	1
Size:	15.0 KB
ID:	1578922

    Comment


    • #3
      Hi James Park. You might find this old thread helpful. I found a DO file with some examples that were (apparently) inspired by that thread. If you really want a clustered bar-chart (which would be my guess), then something like this might do the trick. (I use the same dataset Nick generated in #2.)

      Code:
      * Make a clustered bar-chart with A (0 vs 1)
      * on the X-axis, and B as the cluster variable.
      clear
      set obs 100
      set seed 2803
      gen A=runiformint(0,1)
      gen B=runiformint(0,1)
      separate B, by(B)
      label define Alabs 0 "A=0" 1 "A=1"
      label values A Alabs
      graph bar (count) B0 B1, over(A) blabel(bar)
      Click image for larger version

Name:	AxB.png
Views:	1
Size:	16.8 KB
ID:	1578936

      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        I agree with Bruce Weaver that a simple bar chart conveys exactly the same information and indeed may be preferable.

        Comment


        • #5
          Here is a way to implement Nick's matrix design in #2 without resorting to the graph editor.

          Code:
          clear
          set obs 100
          set seed 2803
          gen A=runiformint(0,1)
          gen B=runiformint(0,1)
          egen group= group(A B), label
          gen gr= cond(inlist(group, 1, 3), 0, 1)
          graph bar (count), over(group, ///
          relabel(1 " " 2 "0 " 3 " " 4 "1")  ///
          label(labsize(medium)) gap(*0.01) ) ///
          by(gr, note("") l1title(A, orient(horizontal) ///
          size(vlarge)) b1title(B, orient(horizontal) ///
          size(medium)) col(1)) blab(total) ///
          subtitle(, pos(9) nobox nobexpand fcolor(none)) ///
          nofill scheme(s1color)

          Comment


          • #6
            Thank you so much everyone. It works beautifully.

            Comment

            Working...
            X