[R] Fast R implementation of Gini mean difference

About this list Date view Thread view Subject view Author view Attachment view

From: Andrew C. Ward (s195404@student.uq.edu.au)
Date: Thu 24 Apr 2003 - 13:17:57 EST


Message-id: <1051154277.3ea757654bbb4@my.uq.edu.au>

I have written the following function to calculate the weighted mean
difference for univariate data (see
http://www.xycoon.com/gini_mean_difference.htm for a related
formula). Unsurprisingly, the function is slow (compared to sd or mad)
for long vectors. I wonder if there's a way to make the function
faster, short of creating an external C function. Thanks very much
for your advice.

gmd <- function(x, w) { # x=data vector, w=weights vector
   n <- length(x)
   tmp <- 0
   for (i in 1:n) {
      for (j in 1:n) {
         tmp <- tmp + w[i]*abs(x[i]-x[j])
      }
   }
   retval <- 0.5*sqrt(pi)*tmp/((n-1)*sum(w))
}

gmd(rnorm(100))

Regards,

Andrew C. Ward

CAPE Centre
Department of Chemical Engineering
The University of Queensland
Brisbane Qld 4072 Australia
andreww@cheque.uq.edu.au

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:43 EST