# Re: [R] Fast R implementation of Gini mean difference

From: Andrew C. Ward (s195404@student.uq.edu.au)
Date: Thu 24 Apr 2003 - 14:23:51 EST

Message-id: <1051158231.3ea766d7ce7d7@my.uq.edu.au>

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@cheque.uq.edu.au

Quoting Deepayan Sarkar <deepayan@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
> > 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
> >
> >
> > 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

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

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