Learn R Programming

abdiv (version 0.2.0)

manhattan: Manhattan and related distances

Description

The Manhattan or city block distance is the sum of absolute differences between the elements of two vectors. The mean character difference is a closely related measure.

Usage

manhattan(x, y)

mean_character_difference(x, y)

modified_mean_character_difference(x, y)

Arguments

x, y

Numeric vectors

Value

The distance between x and y. The modified mean character difference is undefined if all elements in x and y are zero, in which case we return NaN.

Details

For vectors x and y, the Manhattan distance is given by $$d(x, y) = \sum_i |x_i - y_i|.$$ Relation of manhattan() to other definitions:

  • Equivalent to R's built-in dist() function with method = "manhattan".

  • Equivalent to vegdist() with method = "manhattan".

  • Equivalent to the cityblock() function in scipy.spatial.distance.

  • Equivalent to the manhattan calculator in Mothur.

  • Equivalent to \(D_7\) in Legendre & Legendre.

  • Whittaker's index of association (\(D_9\) in Legendre & Legendre) is the Manhattan distance computed after transforming to proportions and dividing by 2.

The mean character difference is the Manhattan distance divided by the length of the vectors. It was proposed by Cain and Harrison in 1958. Relation of mean_character_difference() to other definitions:

  • Equivalent to \(D_8\) in Legendre & Legendre.

  • For binary data, equivalent to \(1 - S_1\) in Legendre & Legendre, where \(S_1\) is the simple matching coefficient.

The modified mean character difference is the Manhattan distance divided by the number elements where either x or y (or both) are nonzero. Relation of modified_mean_character_difference() to other definitions:

  • Equivalent to \(D_{19}\) in Legendre & Legendre.

  • Equivalent to vegdist() with method = "altGower".

  • For binary data, it is equivalent to the Jaccard distance.

References

Cain AJ, Harrison GA. An analysis of the taxonomist's judgment of affinity. Proceedings of the Zoological Society of London 1958;131:85-98.

Examples

Run this code
# NOT RUN {
x <- c(15, 6, 4, 0, 3, 0)
y <- c(10, 2, 0, 1, 1, 0)
manhattan(x, y)
# Whittaker's index of association
manhattan(x / sum(x), y / sum(y)) / 2

mean_character_difference(x, y)
# Simple matching coefficient for presence/absence data
# Should be 2 / 6
mean_character_difference(x > 0, y > 0)

modified_mean_character_difference(x, y)
# Jaccard distance for presence/absence data
modified_mean_character_difference(x > 0, y > 0)
jaccard(x, y)
# }

Run the code above in your browser using DataLab