[R] Gini coefficient in R
Dimitris Rizopoulos
dimitris.rizopoulos at med.kuleuven.be
Mon Jun 11 16:38:49 CEST 2007
try this version instead:
gini <- function(x, unbiased = TRUE, na.rm = FALSE){
if (!is.numeric(x)){
warning("'x' is not numeric; returning NA")
return(NA)
}
if (!na.rm && any(na.ind <- is.na(x)))
stop("'x' contain NAs")
if (na.rm)
x <- x[!na.ind]
n <- length(x)
mu <- mean(x)
N <- if (unbiased) n * (n - 1) else n * n
ox <- x[order(x)]
dsum <- drop(crossprod(2 * 1:n - n - 1, ox))
dsum / (mu * N)
}
########################
gini(c(100,0,0,0))
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "MICHELE DE MEO" <micheledemeo at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, June 11, 2007 4:13 PM
Subject: [R] Gini coefficient in R
> If I use the Ineq library and the Gini function in this way:
>
>>Gini(c(100,0,0,0))
>
> I obtain the result 0.75 instead of 1 (that is the perfect
> inequality).
>
> I think Gini's formula in Ineq is based on a formula as reported
> here:
> http://mathworld.wolfram.com/GiniCoefficient.html
>
> but in the case of perfect inequality:
>
> x_1=.......=x_n-1 =0
>
> x_n>0
>
> these formula are equal to 1 - 1/n, not to 1.
>
> ....I don't know where I'm wrong....
>
>
> --
> Michele De Meo
> http://micheledemeo.blogspot.com/
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
More information about the R-help
mailing list