Announcement

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

  • Multiple Lorenz curve and graph line 45

    Hello there!!!,
    Actually I am trying to compare Lorenz curves (3 different incomes) and I want to construct the line of 45 grades. The Stata code run but my problem is that the graph is wrong, because all incomes appear as straight lines I mean over y axis and those don't look as curves under line 45 that I was created. I want to compare the curves respect to first income= ym. Also if I just graph the curves of incomes is ok, but I graph the curves join with line of 45 there is a problem.

    glcurve ym [w=factor],glvar(g1) pvar(p1)
    glcurve yn_ [w=factor],glvar(g2) pvar(p2)
    glcurve yd [w=factor],glvar(g3) pvar(p3)

    gen diagline= g1
    label var diagline "45-Degree Line"
    *Graphs
    twoway ///
    (line diagline diagline, sort) (line g1 p1, sort) (line g2 p2, sort) (line g3 p3, sort) plot (line g1 g1) /// , ///
    ytitle("Cumulative proportion of income") ///
    xtitle("Cumulative proportion of the population") `options' ///
    `options' ///
    saving(P_graph, replace)

    So I am using Stata 11, I hope you can help me.

    Thanks.











  • #2
    Hi Sofia, try this code it should work.

    *Multiple Lorenz Curve

    glcurve ym [w=factor], pvar(pl) glvar(gl_m) lorenz nograph
    glcurve yn [w=factor], glvar(gl_n) lorenz nograph
    glcurve yd [w=factor], glvar(gl_d) lorenz nograph

    lab var gl_m "inc_m"
    lab var gl_n "inc_n"
    lab var gl_d "inc_d"
    lab var pl "Cumulative population share"
    sort pl

    graph twoway (line gl_m pl, yaxis(1 2)) ///
    (line gl_n pl, yaxis(1 2)) ///
    (line gl_d pl, yaxis(1 2)) ///
    (function y = x, range(0 1) yaxis(1 2)) ///
    , aspect(1) xtitle("Cumulative population share, p") ///
    title("Lorenz Curve Comparison") ///
    ytitle("Cumulative proportion of income") ///
    saving(P_graph.gph , replace) ///
    legend(label(1 "inc_m") label(2 "inc_n") label(3 "inc_d") )


    Imed.

    Comment


    • #3
      Thanks Imed!!!, but it didn't work, you know now the line of 45 is parallel to xaxis but the curves look good, I don't understand it because the line of 45 is defined when u say function y=x?. Maybe you can help me again.

      Regards

      Comment


      • #4
        This thread suffers from the lack of reproducible examples.

        Sofia: Please

        1. Post your data. Or

        2. Use a publicly available dataset people interested in the thread can all download. Or

        3. Use something like this as a sandbox:

        Code:
         
        clear 
        set obs 100
        set seed 2803
        gen y = floor(1/sqrt(runiform()))
        su y 
        glcurve y

        Comment


        • #5
          The following code illustrates what you're trying to do using a dataset that all can access:

          Code:
          sysuse auto, clear
          glcurve mpg , pvar(p1) glvar(l1) lorenz nograph
          glcurve price, pvar(p2) glvar(l2) lorenz nograph
          graph twoway (line l1 p1, sort yaxis(1 2))  ///    
                     (line l2 p2, sort yaxis(1 2)) ///    
                    (function y = x, range(0 1) yaxis(1 2)) ///    
                    , aspect(1) xtitle("Cumulative population share, p") ///    
                    title("Lorenz Curve Comparison") ///    
                    ytitle("Cumulative proportion of income") ///    
                    legend(label(1 "L1") label(2 "L2") label(3 "Equality") )
          Note the use of CODE delimiters to enhance legibility. (Please use them, folks: see the FAQ about this.) Also, please avoid statements such as "it didn't work", without providing full details. (Else how can we help you? By guessing?)

          For more examples of code, see e.g. the appendix to ‘Methods for summarizing and comparing wealth distributions’, ISER Working Paper 2005-5, https://www.iser.essex.ac.uk/publica...s/iser/2005-05 (May 2005), or 'Estimation and interpretation of measures of inequality, poverty, and social welfare using Stata', Presentation at North American Stata Users' Group Meetings 2006, Boston MA. http://econpapers.repec.org/paper/bocasug06/16.htm.
          Last edited by Stephen Jenkins; 06 Oct 2014, 10:34.

          Comment


          • #6
            Thanks Nick and Sthepen for your advices it is my first time here I am trying to do better. I have followed your code Sthepen but the result is the same. The graph shows all lorenz curve but the line of 45 is parellel to xaxis, I am not understand it why?. I have read the papers too, If I graph just the curves is ok but the problem is when plot the line. I can't find my error. I attached the graph.
            My code was:
            glcurve ym[w=factor], pvar(p1)) glvar(gl_m) lorenz nograph
            glcurve yn [w=factor], pvar (p2) glvar(gl_n) lorenz nograph
            glcurve yd [w=factor], pvar (p3) glvar(gl_d) lorenz nograph

            lab var gl_m "inc_m"
            lab var gl_n "inc_n"
            lab var gl_d "inc_d"

            graph twoway (line gl_m p1, sort yaxis(1 2)) ///
            (line gl_n p2, sort yaxis(1 2)) ///
            (line gl_d p3, sort yaxis(1 2)) ///
            (function y =x, range(0 1) yaxis(1 2)) ///
            , aspect(1) xtitle("Cumulative population share, p") ///
            title("Lorenz Curve Comparison") ///
            ytitle("Cumulative proportion of income") ///
            legend(label(1 "inc_m") label(2 "inc_n") label(3 "inc_d") label(4 "Equality") ) ///
            saving(P1_graph.gph , replace) ///
            I will appreciate your help.

            Regards


            Attached Files

            Comment


            • #7
              You have not used CODE delimiters as requested (see the FAQ). See also the FAQ advice about how to post graphs. What happens if you run the code that I posted?
              Otherwise I have no idea what is going on. Try adding a space in "=x" and see if that makes a difference (I doubt it). Otherwise you need to strip your code ride down to essentials (strip off all decorative elements) and build it up piece by piece to see the source of the problem. For example, what if you try

              Code:
              graph twoway (line gl_m p1, sort yaxis(1 2)) ///
                        (function y = x, range(0 1) yaxis(1 2)) ///
                        , aspect(1)

              Comment


              • #8
                Sofia's 45 degree line is drawn using twoway function using a function defining equality. As is documented the default range of x used by twoway function is from 0 to 1; that part is exactly right for this problem. However, twoway function therefore draws the line from (0, 0) to (1,1). This makes sense as a 45 degree line if and only if the Lorenz curves also join those points, but here they don't. As is evident by looking at the graph the curves go from (0,0) to about (1, 2500). To be a 45 degree line the line for equality must be defined to have the same vertical range as the Lorenz curves. With the given range the line from (0, 0) to (1, 1) appears essentially horizontal as it needs to be about 2500 times steeper.
                Last edited by Nick Cox; 06 Oct 2014, 17:17.

                Comment


                • #9
                  Hi Sofia, Sorry for the late reply (being at GMT+2). I presume you have tried the solutions proposed by Nick and Stephen, which I am sure work fine. For what it is worth, I have replicated your problem through the following code using hypothetical data and it works fine, you may want to try it. Kind regards, Imed.

                  *Multiple Lorenz Curve

                  set obs 1000
                  gen ym=rchi2(43)
                  gen yn=rbeta(1,2)
                  gen yd=rgamma(2,5)
                  gen factor= 1
                  glcurve ym [w=factor], pvar(pl) glvar(gl_m) lorenz nograph
                  glcurve yn [w=factor], pvar (p2) glvar(gl_n) lorenz nograph
                  glcurve yd [w=factor], pvar (p3) glvar(gl_d) lorenz nograph

                  lab var gl_m "inc_m"
                  lab var gl_n "inc_n"
                  lab var gl_d "inc_d"

                  graph twoway (line gl_m pl, sort yaxis(1 2)) ///
                  (line gl_n p2, sort yaxis(1 2)) ///
                  (line gl_d p3, sort yaxis(1 2)) ///
                  (function y = x, range(0 1) yaxis(1 2)) ///
                  , aspect(1) xtitle("Cumulative population share, p") ///
                  title("Lorenz Curve Comparison") ///
                  ytitle("Cumulative proportion of income") ///
                  legend(label(1 "inc_m") label(2 "inc_n") label(3 "inc_d") label(4 "Equality") )
                  *saving(P_graph.gph , replace)

                  Comment


                  • #10
                    Here is another way to address the problem.

                    Code:
                     
                    clear 
                    set scheme s1color 
                    set obs 100
                    set seed 2803
                    gen y = floor(1/sqrt(runiform()))
                    su y 
                    glcurve y, gl(gl) p(p) 
                    su gl, meanonly 
                    glcurve y, plot(scatteri 0 0 `r(max)' 1, recast(line) lw(vvthin) lp(dash)) yti("`: var label gl'") xtitle("`: var label p'") legend(off)
                    The twist here is to note that the line of equality required is uniquely defined by its endpoints. Thus we can fire up twoway scatteri with the coordinates of those endpoints. All are known in advance except the y coordinate of the top right corner, which we can extract (e.g.) by using summarize. However, we also use the recast() option to map those coordinates to the line between them. At the same time, we can tune its appearance. Naturally this technique can also be used with other twoway code as well.

                    Click image for larger version

Name:	lorenz.png
Views:	1
Size:	22.7 KB
ID:	295058

                    Comment


                    • #11
                      Thanks everyone; I will be following your advices, I don't understand why my graph is wrong but I will try with each code step by step that you show me, and I hope to fix my graph. Regards

                      Comment

                      Working...
                      X