Skip to content

Instantly share code, notes, and snippets.

@halfak
Last active August 29, 2015 14:12
Show Gist options
  • Save halfak/9204ffe666e85bd7ad63 to your computer and use it in GitHub Desktop.
Save halfak/9204ffe666e85bd7ad63 to your computer and use it in GitHub Desktop.
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(rpareto(1000, 5, 4))
[1] 0.5422903
> gini(rpareto(1000, 5, 5))
[1] 0.5621999
> conc(rpareto(1000, 5, 4))
[1] 0.002452469
> conc(rpareto(1000, 5, 5))
[1] 0.002726071
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment