Fast R implementation of Gini mean difference
Thank you to Deepayan Sarkar and Berwin Turlach for reminding me of the outer() function. A quick comparison shows outer() to be more than 10 times faster than the nested for() loop. The memory does blow out a bit (ran out of memory with a vector of 10000 elements), but it would be rare for me to calculate a mean difference for that many. Thanks again! Regards, Andrew C. Ward CAPE Centre Department of Chemical Engineering The University of Queensland Brisbane Qld 4072 Australia andreww at cheque.uq.edu.au Quoting Deepayan Sarkar <deepayan at stat.wisc.edu>:
You can avoid the for loops with outer, but that will use more
memory.
gmd <-
function(x, w) 0.5 * sqrt(pi) * sum(w * abs(outer(x, x, "-")))
/
((length(x)-1)*sum(w))
On Wednesday 23 April 2003 10:17 pm, Andrew C. Ward wrote:
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 at cheque.uq.edu.au
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help