The DHS Program User Forum
Discussions regarding The DHS Program data and results
Home » Topics » Wealth Index » Gini - Income Inequality
Re: Gini - Income Inequality [message #2899 is a reply to message #2892] Mon, 08 September 2014 12:49 Go to previous messageGo to previous message
Trevor-DHS is currently offline  Trevor-DHS
Messages: 794
Registered: January 2013
Senior Member
Here is some code for calculating the Gini coefficient. To run this for subgroups, drop the cases not needed - see example:

use "BDHR61FL.DTA", clear

* Use a selection here to run this for a specific subgroup
* e.g. for urban
* drop if hv025!=1

* Summarize and get the minimum and the maximum
quietly summ hv271
local w_min = r(min)
local w_max = r(max)

* Calculating the range
local w_range = `w_max' - `w_min'
* Create 100 groups
gen w_group = int( (hv271-`w_min') / (`w_range'/(100-1)) ) + 1
* Transformed wealth score - 0 based
gen wscore_trans = hv271 - `w_min'

* Summarize by the 100 groups
collapse (sum) pop=hv012 ws=wscore_trans [pw=hv005/1000000], by(w_group)

* Accumulate population and wealth scores across groups
gen pop_accum = pop
replace pop_accum = pop_accum[_n-1] + pop if _n>1
gen wdx_accum = ws
replace wdx_accum = wdx_accum[_n-1] + ws if _n>1

* Sum total population and total wealth scores
quietly summ pop
local pop_tot = r(sum)
quietly summ ws
local wdx_tot = r(sum)

* Calculate proportion in each group for population and wealth
gen pop_prop = pop_accum / `pop_tot'
gen wdx_prop = wdx_accum / `wdx_tot'

* Calculate Gini coefficient elements
gen gini = (pop_prop - pop_prop[_n-1]) * (wdx_prop + wdx_prop[_n-1]) if _n>1

* Gini coefficient is 1 - sum of elements, multiplied by 100 to be a percentage
quietly summ gini
local gini_coeff 100 * (1 - r(sum))
di "Gini coefficient: " `gini_coeff'
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Construction
Next Topic: Error detected (Zimbabwe 2010-11)
Goto Forum:
  


Current Time: Sat May 11 14:12:09 Coordinated Universal Time 2024