spear_dist {MSmix}R 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 the rows of a full ranking matrix.

Usage

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

Arguments

rankings

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

rho

An optional full ranking whose Spearman distance from each row of 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.

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 squared Euclidian distance between full rankings; otherwise, it recalls the compute_rank_distance routine of the BayesMallows package, which implements several metrics for rankings.

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 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, compute_rank_distance

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.
borda <- rank(data_description(rankings = ranks_antifragility)$mean_rank)
spear_dist(rankings = ranks_antifragility, rho = borda)

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


[Package MSmix version 1.0.1 Index]