Skip to contents

Compute the Spearman foot-rule (L1) distance between two complete rankings of the same set of items. In the context of MaxDiff models, this is for comparing item rankings of two different models.

Usage

spearman_footrule(x, y, normalize = TRUE)

Arguments

x

A vector giving the first ranking. Elements must be unique and match exactly those in y.

y

A vector giving the second ranking. Must contain the same elements as x in any order.

normalize

Logical, whether to return the normalized distance in the range [0, 1]. Defaults to TRUE.

Value

A numeric scalar with the foot-rule distance (normalized if requested).

Details

The distance is the sum of absolute rank differences between the two orderings. It equals zero when the rankings are identical and reaches its maximum when one ranking is the exact reverse of the other. If normalize = TRUE, the distance is divided by this maximum so that the result lies between 0 and 1.

Examples

spearman_footrule(c("a", "b", "c"), c("a", "b", "c"))
#> [1] 0
#> 0
spearman_footrule(c("a", "b", "c"), c("a", "c", "b"))
#> [1] 0.5
#> 0.5
# biggest distance is between reversed sequences
spearman_footrule(c("a", "b", "c"), c("c", "b", "a"))
#> [1] 1
#> 1

# Without normalization
spearman_footrule(c("a", "b", "c"), c("c", "b", "a"), normalize = FALSE)
#> [1] 4
#> 4