Gini index and Lorenz curve with R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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):
install.packages("ineq") |
The package should be loaded in R by
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:
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:
ineq(AirPassengers,type="Gini") [1] 0.2407563 |
(see also help(ineq)
for more advanced features)
Lorenz curve
The Lorenz curve is displayed by
plot(Lc(AirPassengers)) |
or with
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:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.