Skip to content

Canberra dist and double zeros

1 message · Jari Oksanen

#
Canberra distance is defined in function `dist' (standard library `mva') as

sum(|x_i - y_i| / |x_i + y_i|)

Obviously this is undefined for cases where both x_i and y_i are zeros.  Since 
double zeros are common in many data sets, this is a nuisance.  In our field 
(from which the distance is coming), it is customary to remove double zeros: 
contribution to distance is zero when both x_i and y_i are zero.  Could it be 
possible to have this kind of feature in R as well?

It seems that this would do the trick without breaking applications where 
double zeros do not occur:

--- R-1.2.2/src/appl/distance.c	Sun Oct 15 18:13:25 2000
+++ R-work/src/appl/distance.c	Mon Mar  5 10:16:53 2001
@@ -93,5 +93,5 @@
 double R_canberra(double *x, int nr, int nc, int i1, int i2)
 {
-    double dist;
+    double dist, sum;
     int count, j;
 
@@ -100,5 +100,7 @@
     for(j=0 ; j<nc ; j++) {
 	if(R_FINITE(x[i1]) && R_FINITE(x[i2])) {
-	    dist += fabs(x[i1] - x[i2])/fabs(x[i1] + x[i2]);
+	    sum = fabs(x[i1] + x[i2]);
+	    if (sum > 0.0)
+		dist += fabs(x[i1] - x[i2])/sum;
 	    count++;
 	}



Best wishes, Jari Oksanen