Announcement

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

  • How to do a Pareto Chart

    Hi,
    I need to do a Pareto chart in his original form, not in the modified version implemented with pdplot. Is there any ado file or any other way to do it?
    Is there any way to do it in stata? I'm trying to combine bar or histogram and line, but no way to do it. One of the problem in that my variable of interest is categorical and since I have to do a loop for about 100 graph, I cannot recode variable in descending order every time.

    I could succeed in by combine

    graph bar (count), over(Categoria, sort(1) descending) ------------------ the sort option allow me to order items according to freq

    and

    line cum Categoria, yaxis(2) sort

    but there is no way to do it


    On the other hand with twoway graph, that allows me to combine bar and line, I'm not abre to use the sort option


    Many thanks

    Last edited by Lucilla Rava; 11 Oct 2021, 14:20.

  • #2
    Index Analysis using Stata PCA and MCA command

    Comment


    • #3
      Lucilla Rava
      Hello Lucilla,

      You can do this with `twoway bar` and `twoway connected`. The key is to "sort the frequency table first", then create a new variable containing the plotting positions 1, 2, 3, and so on. Both plots use those positions, with the original category names attached as axis labels. There is no need to recode your original category variable manually.

      The attached `pareto_basic.ado` is a small wrapper using built-in Stata commands. Place it in your working directory and run:

      Code:
      run pareto_basic.ado
      pareto_basic Categoria
      It counts observations in each category, sorts by decreasing frequency, and calculates the cumulative percentage "after sorting". The bars show frequencies on the left axis; the line shows cumulative percentages on the right. String categories and numeric categories with value labels are both supported, and your original data are restored after plotting.

      For one graph per group, where `group_id` is your numeric grouping variable:

      Code:
      levelsof group_id, local(groups)
      foreach g of local groups {
          pareto_basic Categoria if group_id == `g', name(pareto)
          graph export "pareto_`g'.png", name(pareto) replace
      }
      Each call determines its own category order, so the same loop works for 100 groups. Missing categories are excluded from the counts and denominator; frequency ties are ordered by category value. This example assumes one observation per item, rather than an already aggregated frequency table.

      The accompanying `pareto_examples.do` includes a complete example and a loop with different category rankings across groups. Both files were tested in Stata 16.1. The underlying commands are documented in contract, twoway bar, and axis label options.
      Click image for larger version

Name:	pareto_example.png
Views:	1
Size:	125.2 KB
ID:	1787571

      Attached Files
      Last edited by Choonjoo Lee; Today, 04:13.

      Comment


      • #4
        @Lucida Rava posted this one post #1 on 11 October 2021 and hasn't been seen since. I must have thought at the time, so what's wrong with pdplot then? And the question still stands, at least rhetorically.

        The original announcement of
        pdplot in https://www.stata.com/statalist/arch.../msg00515.html didn't include graphical examples. As some of you know, the forum in its present form goes back to 2014.

        From 1994 to 2014, the forum was email-based list, hence the name, without graphical additions.

        I will post some examples shortly.

        On a different level: The Pareto chart was first used by Joseph Juran. https://taproot.com/who-drew-the-first-pareto-chart/

        Last edited by Nick Cox; Today, 09:45.

        Comment


        • #5
          pdplot is on SSC. It was a response in 2006 to the late Leland (Lee) Wilkinson's paper in The American Statistician earlier that year.

          The example in the help file is not much use. Here I will steal @Choonjoo Lee's data example from #3.

          The acceptance intervals are just for a null model in which categories are equally probable. In practice, frequencies of each category will always depart from equality a little, or even a lot, and you're seeing 95% intervals from simulation. More in the paper and in the help file.

          In #3 Choonjoo Lee surely had enough space to show category labels horizontally rather than at an angle. With pdplot there is a horizontal option if you need it or want it. because labels are long or otherwise.



          Code:
          clear 
          
          input str12 Categoria int frequency
          "Damaged" 45
          "Delayed" 30
          "Wrong item" 15
          "Other" 10
          end
          
          pdplot Categoria [fw=freq] , aiopts(blcolor(stc1) bfcolor(stc1*0.2)) xsc(r(0.8 4.2)) name(G1) 
          
          pdplot Categoria [fw=freq] , aiopts(blcolor(stc1) bfcolor(stc1*0.2)) xsc(r(0.8 4.2)) horizontal name(G2)


          Click image for larger version

Name:	pdplot1.png
Views:	1
Size:	30.7 KB
ID:	1787615
          Click image for larger version

Name:	pdplot2.png
Views:	1
Size:	31.5 KB
ID:	1787616

          Comment


          • #6
            Thank you, Nick, for bringing the earlier discussion and pdplot examples into this thread. They make the purpose of the acceptance intervals much clearer.

            My small wrapper was intended to illustrate the traditional bars-plus-cumulative-line display requested in #1, and to automate its ordering across groups. It uses standard Stata graphing techniques and makes no claim to a new method.

            For an introduction to this traditional format, readers may find ASQ's Pareto chart guide helpful. It explains descending bars and the optional cumulative-percentage line, with matching frequency and percentage scales. This wrapper implements the frequency-based version of that display.

            Your point about the labels is well taken. I have made horizontal labels the default and added a horizontal option and support for frequency weights, so the aggregated example can be used directly:

            Code:
            pareto_basic Categoria [fw=frequency]
            pareto_basic Categoria [fw=frequency], horizontal
            Click image for larger version

Name:	pareto_horizontal.png
Views:	1
Size:	121.1 KB
ID:	1787618

            I will also point readers to pdplot as the existing implementation of Wilkinson's Pareto dot plot, with its equal-probability reference intervals.
            Attached Files

            Comment


            • #7
              Choonjoo Lee You're writing some very nice code to produce some very nice graphs!

              Speaking for myself only: although for other purposes I support cumulative distributions and support quantile curves very strongly, the addition of cumulative curves never struck me as valuable for the usual applications of Pareto charts. It always seemed to me that the individual bars carried almost all of the useful information.

              When Leland Wilkinson brought out his paper it seemed to me that he had taken the more valuable part of the Pareto chart and made it even more informative if you wanted the additional acceptance intervals as a null or reference model.

              I just looked in my files and discovered that I have already made the desired small tweaks (better example; better default bar colouring) in pdplot in 2024. I just never sent the files to SSC. So I will now ask Kit Baum to post updates on SSC.

              Comment


              • #8
                Nick Cox
                Thank you, Nick, for your kind words and for explaining the reasoning behind pdplot. I appreciate the distinction you make between the information in the individual frequencies and the additional reference intervals.

                In reviewing the older SSC version, I noted a few small robustness improvements concerning validation of nreps() and level(), and double-precision storage of frequency totals. I would be happy to share the review code and reproducible examples for your consideration.

                Thank you for arranging the SSC update. I look forward to the updated release.

                Comment

                Working...
                X