View Zhirong Zhao's profile on LinkedIn

Blogroll

My pages

Visitors

Powered by

Header image of Hong Kong financial center courtesy of hleung on flickr.

Calculating decomposition of Gini coefficient in R

I am working on several projects on fiscal disparities. The level of inequality can be measured by summary indices such as the Gini coefficient, but I like to move a step forward and decompose the aggregate inequality value into its relevant component contributions.

Take income inequality as an example. One way of factor decomposition is to separate the effects of multiple income streams that form the total income. It has been proposed that a Gini coefficient can be separated as multiple components based on a weighted average of pseudo-Gini (Fei et al 1978; Shorrocks 1982).

How can we do this in R? Failed to find any package ("library") that can do it directly, I decided to create the function on my own by revising R codes from known packages such as ineq and reldist. It turns out to be easy and really fun!

The typical R codes for the Gini:


Gini <- function(x) {
n
<- length(x)
x <- sort(x)
G <- sum(x * 1:n)
G <- 2 * G/(n * sum(x))
G -1 - (1/n)
}

My codes for the pseudo-Gini (assuming that x1 is a component of x):

pseudo.gini <- function(x,x1) {
n <- length(x)
o <- order(x)
y <- cbind(x[o],x1[o])
G <- sum(y[,2]*1:n)
G <- 2*G/(n*sum(y[,2]))
G - 1 - (1/n)
}

The factor contribution of x1 in the inequality of x:

gini.factor <- function(x,x1) {
n <- length(x)
o <- order(x)
y <- cbind(x[o],x1[o])
G <- sum(y[,2]*1:n)
G <- 2*G/(n*sum(y[,2]))
G <- G - 1 - (1/n)
G*sum(y[,2])/sum(y[,1])
}


# --------------------------------------------
# An all-in-one upgrated version (07/31/2008)
# --------------------------------------------
dec.gini <- function(x,x1) {
        n <- length(x)
        o <- order(x)
        y <- cbind(x[o],x1[o])
        G1 <- sum(y[,1]*1:n)
        G1 <- 2*G1/(n*sum(y[,1]))
        G1 <- G1 - 1 - (1/n)
        G2 <- sum(y[,2]*1:n)
        G2 <- 2*G2/(n*sum(y[,2]))
        G2 <- G2 - 1 - (1/n)
        F1 <- G2*sum(y[,2])/sum(y[,1])
        F2 <- F1/G1
        out <- rbind(G1,G2,F1,F2)
        rownames(out) <- c("Gini","Pseudo Gini",
                        "contribution","Rel. contr.")
        out
        }
# --------------------------------------------
# An example with simulated data
# --------------------------------------------
> X <- (rnorm(100)+3)^2
> Y <- (rnorm(100))^2
> Z <- X + Y

> dec.gini(Z,Z)
                  [,1]
Gini         0.3077444
Pseudo Gini  0.3077444
contribution 0.3077444
Rel. contr.  1.0000000
> dec.gini(Z,X)
                  [,1]
Gini         0.3077444
Pseudo Gini  0.3237842
contribution 0.2964888
Rel. contr.  0.9634255
> dec.gini(Z,Y)
                   [,1]
Gini         0.30774443
Pseudo Gini  0.13351672
contribution 0.01125561
Rel. contr.  0.03657453

Comments

My web-site is created using Wix also (: Its called Jessys Party check it out! :D

These cards are simple and good. But I've observed only few cards which are genuinely extra ordinary. Others haven't amazed me in any way!

Well its not like ppl r goin to go out n meet new ppl in person like u all think no were all just gunnas go bck to myspace duhh n i dnt just go out there n meet ppl on fb i hve alot of loved ones n ppl i went to school wit that i hvent seen in for ever n if ur shuttin fb down then y u keep changing the glance of it that dnt make any since in any respect to me n im sure other ppl think the same o well i guess we all go bck to myspace now guys

That's a bummer guess I am lucky that I only have 1 Twitter handle. Thanks for stopping by and commenting. Since it's in Beta, perhaps changes will probably be made.

my dad lives across the country and i see him evry4 ages so i get it, it genuinely stinks

Jason you're correct. There is quite a bit of noise. I also think the demographics are largely skewed to women which may increase the male perception of "noise".

It seems like Facebook, no ? ;)

1 of the things I like about Pinterest is how pretty it is! Using it personally and just about to kick off an account for our restaurant group.

hey these cameras appears to be like funky i want just one!

My goal this week will be to get more on the Farmers Wife blocks done. I'm pondering 10 should be doable, maybe more as I tend to do these in big batches rather than the 2/wk that it started off out as. Excellent luck to everyone on your own goals! :)

This is something I've actually been working on for my web page. I've recently made some wholesale changes to my website to make it less complicated for others to share my information across their social platforms as well.

Hello there, You have performed a fantastic job. I'll definitely digg it and in my opinion suggest to my friends. I am sure they'll be benefited from this web site.

Good website , im new to seo and it helped alot

We wish to thank you once more for the wonderful ideas you offered Jeremy when preparing her post-graduate research and, most importantly, for providing the many ideas in one blog post. In case we had been aware of your website a year ago, we'd have been rescued from the useless measures we were taking. Thank you very much.

Content Birthday Mum....which is how we say it in England....:)

Drink at the least 2 litres of water daily, approximately 3 litres if you possibly could. Treatments for GoutNatural gout treatment methods are typical the fashion currently. Also, you must immediately stop trying most meats, particularly red meats.

Hey there :) I am a big fan of your blog posts and your blog. One could say that I started my own blog because I saw so many cool stuff on your blog :D So I just wanted to leave my link here and tell you that if you want a link on my blog I would be more then happy to write a few words abotu you and link back to you :) Love your work!

this is awesome

My Stude fix for your day! Too expensive for my budget, but I'd make a place to store it if I could get my hands on it.

This really helped me out alot, thanks for taking the time to write this!

I'm not sure exactly why but this blog is loading extremely slow for me. Is anyone else having this issue or is it a issue on my end? I'll check back later on and see if the problem still exists.

It's seems uncommon for people to react| positively to new ideas. I am highly grateful for your findings and how you broke down your ideas.

I agree completely with what you wrote. Great Stuff. Keep it going..

Is it fine if I syndicate this post on my website?

Anyone considering Postloop should do their homework first

I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again!

I would say, based for the location and placement of this indication, that it truly is obviously a slow individual crossing' sign.br /Cute shot!

Just wanted to stop by and tell you good job on your blog entry. You are not alone in your opinion as you most likely already know.

Confident, occasionally that is necessary. Though you'd be shocked how accommodating some car owners might be if you asked them to momentarily drop the hoods and kick the crybaby dolls.

Hubert H. Humphrey Institute of Public Affairs