Announcement

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

  • Help Needed on Plotting Rates by Categorical variable

    Dear Stata Community,


    I have three variables: an event_ind (indicating event occurs) and an education indicator and marital indicator. I want to find the rates of the event indicator over different education and marital groups. Effectively there will be 8 groups (4 education and 2 marital) and I want to plot the rates where event_ind=1 across groups.

    A sample chart would have eight bars corresponding to each of the groups showing the rates where event_ind=1.
    i
    Thanks in advance

    Code:
    input float(event_ind mother_edind marital_ind)
    1 4 1
    0 2 2
    0 2 1
    0 3 2
    1 3 1
    0 3 1
    0 2 1
    0 3 2
    1 3 1
    0 2 1
    0 3 1
    1 2 1
    0 3 1
    0 2 2
    0 2 2
    0 4 1
    1 2 1
    0 2 1
    0 2 2
    0 3 1
    end
    label values vlow_wt_ind lbw2
    label def lbw2 0 "Event:0", modify
    label def lbw2 1 "Event:1", modify
    label values mother_edind edu
    label def edu 1 "<High School", modify
    label def edu 2 "High School/GED", modify
    label def edu 3 "College", modify
    label def edu 4 "College+", modify
    label values marital_ind mar
    label def mar 1 "Married", modify
    label def mar 2 "Single", modify
    Last edited by Digvijay Singh Parmar; 17 Mar 2019, 23:42.

  • #2
    Your code fails because you changed what dataex produced after it ran, perhaps to be coy about what you are doing. With a rename from event_id to vlow_wt_ind this code works.

    I am suggesting Cleveland dot charts for your data example, if only because some bars would be of zero length and so hard to distinguish from combinations of categories not present in the dataset. There are, naturally, some personal style choices within this code. A change to graph hbar would not be difficult. I recommend against graph bar, as your categories could not be read off so easily.

    Code:
    clear
    input float(vlow_wt_ind mother_edind marital_ind)
    1 4 1
    0 2 2
    0 2 1
    0 3 2
    1 3 1
    0 3 1
    0 2 1
    0 3 2
    1 3 1
    0 2 1
    0 3 1
    1 2 1
    0 3 1
    0 2 2
    0 2 2
    0 4 1
    1 2 1
    0 2 1
    0 2 2
    0 3 1
    end
    label values vlow_wt_ind lbw2
    label def lbw2 0 "Event:0", modify
    label def lbw2 1 "Event:1", modify
    label values mother_edind edu
    label def edu 1 "<High School", modify
    label def edu 2 "High School/GED", modify
    label def edu 3 "College", modify
    label def edu 4 "College+", modify
    label values marital_ind mar
    label def mar 1 "Married", modify
    label def mar 2 "Single", modify
    
    set scheme s1color
    
    graph dot (mean) vlow_wt_ind, over(mother) over(marital) linetype(line) ///
    lines(lc(gs12) lw(vthin)) ysc(r(-0.02 .) alt titlegap(*5))              ///
    ytitle(Proportion of very low weight) yla(0 "0" 1 "1" 0.2(0.2)0.8, format(%02.1f)) name(G1)
    
    graph dot (mean) vlow_wt_ind, over(marital) over(mother) linetype(line) ///
    lines(lc(gs12) lw(vthin)) ysc(r(-0.02 .) alt titlegap(*5))              ///
    ytitle(Proportion of very low weight) yla(0 "0" 1 "1" 0.2(0.2)0.8, format(%02.1f)) name(G2)
    Click image for larger version

Name:	vlowwt1.png
Views:	1
Size:	23.4 KB
ID:	1488715

    Click image for larger version

Name:	vlowwt2.png
Views:	1
Size:	23.1 KB
ID:	1488716

    Last edited by Nick Cox; 18 Mar 2019, 02:19.

    Comment


    • #3
      Here is one option(you could reverse the order of the over() option to see if that is more to your liking):
      graph dot (percent) event_ind, over(mother_edind) over(marital_ind)

      here is another:
      graph dot (percent) event_ind, over(mother_edind) by(marital_ind)
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks a lot Nick for your helpful reply. The dataset that I have has a million rows so a graph hbar should do the trick and have enough population for each of the categories.

        Comment


        • #5
          Thank you Maarten for your help on my query.

          Comment

          Working...
          X