[R] Fast R implementation of Gini mean difference
Deepayan Sarkar
deepayan at stat.wisc.edu
Thu Apr 24 06:05:45 CEST 2003
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
More information about the R-help
mailing list