# Gini Coefficient, Lorenz Curve, Lorenz Asymmetry Coefficient を計算する関数 lorenz <- function(x){ # http://www2.dmu.dk/1_om_dmu/2_afdelinger/3_teri/4_personlig/5_cfd/Download/ec00.pdf stopifnot(x >= 0) # check sample n <- length(x <- sort(x)) # number of sample and sort x <- x / sum(x) # convert to relative abundance l <- cumsum(x) # cumulative abundance u <- mean(x) # mean species size m <- sum(u>x) # number of species with size less than u if (a <- sum(u==x)) { # species with size equal to mean size exist? int.1 <- m/n + l[m]/l[n] int.2 <- (m+a)/n + l[m+a]/l[n] asym <- c(int.1, int.2) # Lorenz asymmetry coefficient names(asym) <- c("min","max") } else { d <- (u-x[m]) / (x[m+1]-x[m]) fu <- (m+d) / n lu <- (l[m]+d*x[m+1]) / l[n] asym <- sum(fu, lu) # Lorenz asymmetry coefficient } axis.x <- seq(0, 1, length=n+1) axis.y <- c(0, l) gini <- (2*sum(axis.x-axis.y) / n) # gini.coefficient u.gini <- gini * n / (n-1) # unbiased estimator curve <- data.frame(axis.x, axis.y) # lorenz curve list(gini.coefficient=gini, unbiased.gini=u.gini, asymmetry.coefficient=asym, lorenz.curve=curve) } # 使用方法 dat1 <- c(rep(5.55556, 9), 50) dat2 <- c(rep(2,5), rep(18,5)) dat3 <- c(1,2,3,3,4,5) lorenz(dat1) # dat1とdat2で Gini Coefficient は同じであるものの lorenz(dat2) # Lorenz Asymmetry Coefficient は異なる lorenz(dat3) # 平均値と同じ値がある場合は、Lorenz Asymmetry Coefficient は点ではなく範囲で出力される