spear_dist: Spearman distance

View source: R/MSmix_functions_package.R

spear_distR Documentation

Spearman distance

Description

Compute either the Spearman distance between each row of a full ranking matrix and a reference complete ranking, or the Spearman distance matrix between all pairs of full rankings.

Usage

spear_dist(
  rankings,
  rho = NULL,
  subset = NULL,
  diag = FALSE,
  upper = FALSE,
  plot_dist_mat = FALSE
)

Arguments

rankings

Integer N\timesn matrix or data frame with full rankings in each row.

rho

An optional full ranking whose Spearman distance from each row in rankings must be computed. Defaults to NULL, meaning that the Spearman distance matrix between all pairs of rows in rankings must be computed.

subset

Optional logical or integer vector specifying the subset of observations, i.e. rows of the rankings, to be kept. Missing values are taken as FALSE. Defaults to NULL meaning that all the rows are considered.

diag

Logical: whether the diagonal of the Spearman distance matrix must be returned. Used when rho = NULL. Defaults to FALSE.

upper

Logical: whether the upper triangle of the Spearman distance matrix must be printed. Used when rho = NULL. Defaults to FALSE.

plot_dist_mat

Logical: whether the Spearman distance matrix must be plotted. Used when rho = NULL. Defaults to FALSE.

Details

When rho = NULL, spear_dist recalls the dist function from the base package to compute the Spearman metric as squared Euclidian distance between all pairs of rows in rankings; otherwise, it recalls the compute_rank_distance routine of the BayesMallows package for the computation of the Spearman distance between each row in rankings and the full ranking rho.

Value

When rho = NULL, an object of class "dist" corresponding to the Spearman distance matrix; otherwise, a vector with the Spearman distances between each row in rankings and the full ranking rho.

References

Sørensen Ø, Crispino M, Liu Q and Vitelli V (2020). BayesMallows: An R Package for the Bayesian Mallows Model. The R Journal, 12(1), pages 324–342, DOI: 10.32614/RJ-2020-026.

See Also

plot.dist

Examples


## Example 1. Spearman distance between two full rankings.
spear_dist(rankings = c(4, 8, 6, 9, 2, 11, 3, 5, 1, 12, 7, 10), rho = 1:12)

## Example 2. Spearman distance between the Antifragility ranking dataset and the Borda ranking.
r_antifrag <- ranks_antifragility[, 1:7]
borda <- rank(data_description(rankings = r_antifrag)$mean_rank)
spear_dist(rankings = r_antifrag, rho = borda)

## Example 3. Spearman distance matrix of the Sports ranking dataset.
r_sports <- ranks_sports[, 1:8]
dist_mat <- spear_dist(rankings = r_sports)
dist_mat
# Spearman distance matrix for the subsample of females.
dist_f <- spear_dist(rankings = r_sports, subset = (ranks_sports$Gender == "Female"))
dist_f


MSmix documentation built on April 3, 2025, 9:29 p.m.