nearest.neighbours | R Documentation |
Find the nearest neighbours of a term vector in a DSM, given either as a scored cooccurrence matrix or a pre-computed distance matrix. The target term can be selected by name (in which case the cooccurrence or distance matrix must be labelled appropriately) or specified as a vector (if the DSM is given as a matrix).
nearest.neighbours(M, term, n = 10, M2 = NULL, byrow = TRUE, drop = TRUE, skip.missing = FALSE, dist.matrix = FALSE, ..., batchsize=50e6, verbose=FALSE)
M |
either a dense or sparse matrix representing a scored DSM (or an object of class |
term |
either a character vector specifying one or more target terms for which nearest neighbours will be found, or a matrix specifying the target vectors directly. A plain vector is interpreted as a single-row matrix. |
n |
an integer giving the number of nearest neighbours to be returned for each target term |
M2 |
an optional dense or sparse matrix (or object of class |
byrow |
whether target terms are looked up in rows (default) or columns ( |
drop |
if |
skip.missing |
if |
dist.matrix |
if |
... |
additional arguments are passed to |
batchsize |
if |
verbose |
if |
In most cases, the target term itself is automatically excluded from the list of neighbours. There are two exceptions:
The target term is given as a vector rather than by name.
Nearest neighbours are determined in a cross-distance setting. This is the case if (i) M2
is specified or (ii) M
is a pre-computed distance matrix and not marked to be symmetric.
With dist.matrix=TRUE
, the returned distance matrix always includes the target term.
M
can also be a pre-computed distance or similarity matrix from an external source, which must be marked with as.distmat
. If M
is a sparse similarity matrix, only non-zero cells will be considered when looking for the nearest neighbours. Keep in mind that dist.matrix=TRUE
is only valid if M
is a symmetric matrix and marked as such.
A list with one entry for each target term
found in M
, giving
dist.matrix=FALSE
(default): the nearest neighbours as a numeric vector of distances or similarities labelled with the corresponding terms and ordered by distance
dist.matrix=TRUE
: a full distance or similarity matrix for the target term and its nearest neighbours (as an object of class dist.matrix
). An additional attribute selected
contains a logical vector indicating the position of the target term in the matrix.
If drop=TRUE
, a list containing only a single target term will be simplified to a plain vector or distance matrix.
Stephanie Evert (https://purl.org/stephanie.evert)
dist.matrix
for more information on available distance metrics and similarity measures
nearest.neighbours(DSM_Vectors, c("apple_N", "walk_V"), n=10) nearest.neighbours(DSM_Vectors, "apple_N", n=10, method="maximum") as.dist(nearest.neighbours(DSM_Vectors, "apple_N", n=10, dist.matrix=TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.