Gini index and Lorenz curve with R

February 23, 2012
By

(This article was first published on tuxettechix » R, and kindly contributed to R-bloggers)

You can do anything pretty easily with R, for instance, calculate concentration indexes such as the Gini index or display the Lorenz curve (dedicated to my students).

Although I did not explain it during my lectures, calculating a Gini index or displaying the Lorenz curve can be done very easily with R. All you have to do is to figure out which of the billions packages available on CRAN (ok, only 3,629 packages to be honest) will give you the answer (and for that, Google can help you: just try to google “r cran gini” and you should be able to find by yourself a few answers).

One of the packages that can do it is ineq that you can install in R by using the command line (or by whichever alternative method you want):

?View Code RSPLUS
install.packages("ineq")

The package should be loaded in R by

?View Code RSPLUS
library(ineq)

and then, you can start to use it. I’ll show a very simple example of its use for the concepts that I have taught during the first year lectures. The example is based on the data AirPassengers that you may load by simply typing:

?View Code RSPLUS
data(AirPassengers)

(these data are the monthly totals of international airline passengers, from 1949 to 1960 and are thus relevant enough for a concentration analysis).

Gini index

The Gini index of the distribution can be calculated by:

?View Code RSPLUS
ineq(AirPassengers,type="Gini")
[1] 0.2407563

(see also help(ineq) for more advanced features)

Lorenz curve

The Lorenz curve is displayed by

?View Code RSPLUS
plot(Lc(AirPassengers))

or with

?View Code RSPLUS
plot(Lc(AirPassengers),col="darkred",lwd=2)

(if you want to change color and line width but see also help(Lc) for an advanced use). The resulting picture is given below:

To leave a comment for the author, please follow the link and comment on his blog: tuxettechix » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.