Announcement

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

  • Percentage bar graph of binary variable by categorical variable

    Good evening,

    I have a binary variable (outcome) and an explanatory categorical variable (cat). I need to create a bar graph with the percentage of "1's" of <outcome> in each category of <cat>.

    I tried to use graph bar and catplot (Cox, N) without success. I always get a graph with the percentage of both the "0's" and the "1's" in each category of <cat>, using this command:

    catplot outcome cat, fraction(cat) recast(bar)

    How can I get rid of the percentage of 0's in each category of <cat>, without having to remodel the dataset with a "collapse" strategy?

    I am using Stata v12.1.


    Best regards, and many thanks in advance.
    Filipe S. Paula

  • #2
    It's the purpose of catplot (SSC; locations are asked for, not authors) to show frequencies (counts, proportions, percents) of all categories chosen. In contrast you ask for a graph of the percent of 1s. The proportions of 1s are just the mean of a binary variable, such as

    Code:
    sysuse auto, clear
    graph bar (mean) foreign, over(rep78) ytitle(proportion foreign)
    and so the graph is immediate given Stata's official commands. The mapping of proportion to percent is easily done by using axis labels:

    Code:
    graph bar (mean) foreign, over(rep78) yla(0 0.2 "20" 0.4 "40" 0.6 "60" 0.8 "80" 1 "100") ytitle(percent foreign)
    Last edited by Nick Cox; 13 Oct 2014, 18:24.

    Comment


    • #3
      Thank you for your answer, Nick.

      Best regards

      Comment


      • #4
        Just a final question:
        Is it possible to overlay that bar graph with a twoway line (for instance, a predicted variable from a logistic regression model)? As the frequency bar graph is not obtained by twoway graphs, I can't seem to combine them.

        Many thanks

        Comment


        • #5
          No, for the reason you specify. But you can do all that with twoway.

          Code:
           
          sysuse auto, clear
          egen mean = mean(foreign), by(rep78)
          logit foreign rep78
          predict predicted
          twoway bar mean rep78, barw(0.1) || mspline predicted rep78, ytitle(proportion foreign) legend(order(1 "empirical fractions" 2 "logit predictions"))

          Comment


          • #6
            Thank you once again, Nick.

            Best

            Comment

            Working...
            X