Matrix Distance/Similarity Computation
dist.RdThese functions compute and return the auto-distance/similarity matrix between either rows or columns of a matrix/data frame, or a list, as well as the cross-distance matrix between two matrices/data frames/lists.
Usage
dist(x, y = NULL, method = NULL, ..., diag = FALSE, upper = FALSE,
pairwise = FALSE, by_rows = TRUE, convert_similarities = TRUE,
auto_convert_data_frames = TRUE)
simil(x, y = NULL, method = NULL, ..., diag = FALSE, upper = FALSE,
pairwise = FALSE, by_rows = TRUE, convert_distances = TRUE,
auto_convert_data_frames = TRUE)
pr_dist2simil(x)
pr_simil2dist(x)
as.dist(x, FUN = NULL)
as.simil(x, FUN = NULL)
# S3 method for class 'dist'
as.matrix(x, diag = 0, ...)
# S3 method for class 'simil'
as.matrix(x, diag = NA, ...)Arguments
- x
For
distandsimil, a numeric matrix object, a data frame, or a list. A vector will be converted into a column matrix. Foras.similandas.dist, an object of classdistandsimil, respectively, or a numeric matrix. Forpr_dist2similandpr_simil2dist, any numeric vector.- y
NULL, or a similar object thanx- method
a function, a registry entry, or a mnemonic string referencing the proximity measure. A list of all available measures can be obtained using
pr_DB(see examples). The default fordistis"Euclidean", and forsimil"correlation".- diag
logical value indicating whether the diagonal of the distance/similarity matrix should be printed by
print.dist/print.simil. Note that the diagonal values are never stored indistobjects.In the context of
as.matrixthe value to use on the diagonal representing self-proximities. In case of similarities, this defaults toNAsince a priori there are no upper bounds, so the maximum similarity needs to be specified by the user.- upper
logical value indicating whether the upper triangle of the distance/similarity matrix should be printed by
print.dist/print.simil- pairwise
logical value indicating whether distances should be computed for the pairs of
xandyonly.- by_rows
logical indicating whether proximities between rows, or columns should be computed.
- convert_similarities, convert_distances
logical indicating whether distances should be automatically converted into similarities (and the other way round) if needed.
- auto_convert_data_frames
logical indicating whether data frames should be converted to matrices if all variables are numeric, or all are logical, or all are complex.
- FUN
optional function to be used by
as.distandas.simil. IfNULL, it is looked up in the method registry. If there is none specified there,FUNdefaults topr_simil2distandpr_dist2simil, respectively.- ...
further arguments passed to the proximity function.
Details
The interface is fashioned after dist, but can
also compute cross-distances, and allows user extensions by means of
registry of all proximity measures (see pr_DB).
Missing values are allowed but are excluded from all computations
involving the rows within which they occur. If some columns are
excluded in calculating a Euclidean, Manhattan, Canberra or
Minkowski distance, the sum is scaled up proportionally to the
number of columns used (compare dist in
package stats).
Data frames are silently coerced to matrix if all columns are of
(same) mode numeric or logical.
Distance measures can be used with simil, and similarity
measures with dist. In these cases, the result is transformed
accordingly using the specified coercion functions (default:
\(\mathrm{pr\_simil2dist}(x) = 1 - \mathrm{abs}(x)\) and \(\mathrm{pr\_dist2simil}(x) = 1 / (1 + x)\)).
Objects of class simil and dist can be converted one in
another using as.dist and as.simil, respectively.
Distance and similarity objects can conveniently be subset (see examples). Note that duplicate indexes are silently ignored.
Value
Auto distances/similarities are returned as an object of class dist/simil and
cross-distances/similarities as an object of class crossdist/crosssimil.
References
Anderberg, M.R. (1973), Cluster analysis for applications, 359 pp., Academic Press, New York, NY, USA.
Cox, M.F. and Cox, M.A.A. (2001), Multidimensional Scaling, Chapman and Hall.
Sokol, R.S. and Sneath P.H.A (1963), Principles of Numerical Taxonomy, W. H. Freeman and Co., San Francisco.
Author
David Meyer [email protected] and Christian Buchta [email protected]
Examples
### show available proximities
summary(pr_DB)
#> * Similarity measures:
#> Braun-Blanquet, Chi-squared, Cramer, Dice, Fager, Faith, Gower, Hamman,
#> Jaccard, Kulczynski1, Kulczynski2, Michael, Mountford, Mozley, Ochiai,
#> Pearson, Phi, Phi-squared, Russel, Simpson, Stiles, Tanimoto,
#> Tschuprow, Yule, Yule2, angular, correlation, cosine, eDice, eJaccard,
#> simple matching
#>
#> * Distance measures:
#> Bhjattacharyya, Bray, Canberra, Chord, Euclidean, Geodesic, Hellinger,
#> Kullback, Levenshtein, Mahalanobis, Manhattan, Minkowski, Podani,
#> Soergel, Wave, Whittaker, divergence, fJaccard, supremum
#>
### get more information about a particular one
pr_DB$get_entry("Jaccard")
#> names Jaccard, binary, Reyssac, Roux
#> FUN R_bjaccard
#> distance FALSE
#> PREFUN pr_Jaccard_prefun
#> POSTFUN NA
#> convert pr_simil2dist
#> type binary
#> loop FALSE
#> C_FUN TRUE
#> PACKAGE proxy
#> abcd FALSE
#> formula a / (a + b + c)
#> reference Jaccard, P. (1908). Nouvelles recherches sur la
#> distribution florale. Bull. Soc. Vaud. Sci. Nat., 44, pp.
#> 223--270.
#> description The Jaccard Similarity (C implementation) for binary data.
#> It is the proportion of (TRUE, TRUE) pairs, but not
#> considering (FALSE, FALSE) pairs. So it compares the
#> intersection with the union of object sets.
### binary data
x <- matrix(sample(c(FALSE, TRUE), 8, replace = TRUE), ncol = 2)
dist(x, method = "Jaccard")
#> 1 2 3
#> 2 1.0
#> 3 1.0 0.0
#> 4 0.5 1.0 1.0
### for real-valued data
dist(x, method = "eJaccard")
#> 1 2 3
#> 2 1.0
#> 3 1.0 0.0
#> 4 0.5 1.0 1.0
### for positive real-valued data
dist(x, method = "fJaccard")
#> 1 2 3
#> 2 1.0
#> 3 1.0 0.0
#> 4 0.5 1.0 1.0
### cross distances
dist(x, x, method = "Jaccard")
#> [,1] [,2] [,3] [,4]
#> [1,] 0.0 1.0 1.0 0.5
#> [2,] 1.0 0.0 0.0 1.0
#> [3,] 1.0 0.0 0.0 1.0
#> [4,] 0.5 1.0 1.0 0.0
### pairwise (diagonal)
dist(x, x, method = "Jaccard",
pairwise = TRUE)
#> [1] 0 0 0 0
### this is the same but less efficient
as.matrix(stats::dist(x, method = "binary"))
#> 1 2 3 4
#> 1 0.0 1 1 0.5
#> 2 1.0 0 0 1.0
#> 3 1.0 0 0 1.0
#> 4 0.5 1 1 0.0
### numeric data
x <- matrix(rnorm(16), ncol = 4)
## test inheritance of names
rownames(x) <- LETTERS[1:4]
colnames(x) <- letters[1:4]
dist(x)
#> A B C
#> B 3.720843
#> C 1.890827 3.412261
#> D 2.992026 3.228353 2.402304
dist(x, x)
#> A B C D
#> A 0.000000 3.720843 1.890827 2.992026
#> B 3.720843 0.000000 3.412261 3.228353
#> C 1.890827 3.412261 0.000000 2.402304
#> D 2.992026 3.228353 2.402304 0.000000
## custom distance function
f <- function(x, y) sum(x * y)
dist(x, f)
#> A B C
#> B -2.79998828
#> C 2.62478618 -1.34868195
#> D -0.03746439 -0.71180300 1.90384746
## working with lists
z <- unlist(apply(x, 1, list), recursive = FALSE)
(d <- dist(z))
#> A B C
#> B 3.720843
#> C 1.890827 3.412261
#> D 2.992026 3.228353 2.402304
dist(z, z)
#> A B C D
#> A 0.000000 3.720843 1.890827 2.992026
#> B 3.720843 0.000000 3.412261 3.228353
#> C 1.890827 3.412261 0.000000 2.402304
#> D 2.992026 3.228353 2.402304 0.000000
## subsetting
d[[1:2]]
#> A
#> B 3.720843
subset(d, c(1,3,4))
#> A C
#> C 1.890827
#> D 2.992026 2.402304
d[[c(1,2,2)]] # duplicate index gets ignored
#> A
#> B 3.720843
## transformations and self-proximities
as.matrix(as.simil(d, function(x) exp(-x)), diag = 1)
#> A B C D
#> A 1.00000000 0.02421355 0.15094698 0.05018565
#> B 0.02421355 1.00000000 0.03296658 0.03962271
#> C 0.15094698 0.03296658 1.00000000 0.09050917
#> D 0.05018565 0.03962271 0.09050917 1.00000000
## row and column indexes
row.dist(d)
#> [1] 2 3 4 3 4 4
col.dist(d)
#> [1] 1 1 1 2 2 3