
How to draw multiple Lorenz Curve and Add Gini numbers into the picture
To calculate Gini, add inequalr.ado into your stata (connect to the Internet, search inequalr in help netsearch and install the ado file).
Suppose you you have a data grouped by urban rural, you want to draw Lorenz curve for Urban and rural in one picture. You should have a variable as a basis for inequality calculation, in this example is expenditure per capita, you should have the sampling weight for each observation, in this example is weight, and you should have the categorical variable indicating the group, here is urban where urban == 1 is urban, and urban == 2 is rural.
You can easily modify this program for more than 2 categories.
Below is the do file with the comment.
or here to download the do file and its example data file.
/* Drawing Multiple Lorenz Curve and Including Gini Coefficient */
/* by Arief Anshory Yusuf (http://arief.equitablepolicy.org) */
/* Error is not responsibility of the author */
/* Suppose you want to draw Lorenz curves for Urban and Rural area
the variable is expenditure (exp), the sampling weight is 'weight'
and the group variable is 'urban'.
First, open the data */
set more off
use westjava.dta, clear
/* Create macro for the group */
local group Urban Rural
/* Create separate variable (exp and weight) for each group */
gen yUrban = exp if urban == 1
gen yRural = exp if urban == 2
gen wUrban = weight if urban == 1
gen wRural = weight if urban == 2
/* Now calculating variables for drawing the curves */
foreach i of local group {
sort y`i'
gen cumy`i' = sum(y`i' * w`i')
gen cump`i' = sum(w`i')
replace cumy`i' = cumy`i'/cumy`i'[_N]
replace cump`i' = cump`i'/cump`i'[_N]
gen equal`i' = cump`i'
label variable equal`i' "Line of Perfect Equality"
label variable cump`i' "Cum. share of population"
label variable cumy`i' "Lorenz curve `i'"
display "Inequality Indikator: `i'"
inequalr y`i' [fweight = int(w`i')]
local gini_`i' = round(r(gini),.001)
}
/* Now the drawing */
twoway scatter equalUrban cumpUrban, sort(cumyUrban) c(l l) m(i i) /// /* this is 45d line */
|| scatter cumyUrban cumpUrban, sort(cumyUrban) c(l l) m(i i) /// /* this is for Urban Lorenz C. */
|| scatter cumyRural cumpRural, sort(cumyRural) c(l l) m(i i) lp(dash) /// /* this is for Rural Lorenx C. */
/* do some labelling */ ///
yti("Cum. share of exp.") xti("Cum. share of pop.") ///
/* add Gini numbers */ ///
text(0.9 0.15 "Gini Urban = `gini_Urban'" "Gini Rural = `gini_Rural'") ///
Here is the output!
