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
>