find_nn: Find Nearest Neighbors

View source: R/find_nn.R

find_nnR Documentation

Find Nearest Neighbors

Description

find_nn finds the nearest neighbor for a given vector. Neighbors can be found via pearson correlation, cosine similarity, Euclidean distance, or Manhattan distance.

Usage

find_nn(vec, neighbors, n = 5, method = "cor")

Arguments

vec

The vector for which neighbors are to be found. This is either a vector saved as object or a row of the neighbors data frame or matrix. If a row of a data frame or matrix is specified, this row is excluded as competitor for the nearest neighbor (as a row will be closest to itself).

neighbors

The potential neighbors, usually given as data frame or matrix.

n

The number of neighbors wanted as output. Defaults to 5.

method

The measure used to determine the nearest neighbors. Either "cor", "cosim", "euclid" (default), or "manhat".

Author(s)

D. Schmitz

Examples


### a vector saved as object & a matrix of neighbors

vector <- runif(50, 0, 10)
data("gdsm_mat")

find_nn(vec = vector, neighbors = gdsm_mat, 3, "cor")

find_nn(vec = vector, neighbors = gdsm_mat, 3, "cosim")

find_nn(vec = vector, neighbors = gdsm_mat, 3, "euclid")

find_nn(vec = vector, neighbors = gdsm_mat, 3, "manhat")


### a vector specified by its name & its matrix of neighbors

data("gdsm_mat")

find_nn(vec = "var12", neighbors = gdsm_mat, 3, "cor")

find_nn(vec = "var12", neighbors = gdsm_mat, 3, "cosim")

find_nn(vec = "var12", neighbors = gdsm_mat, 3, "euclid")

find_nn(vec = "var12", neighbors = gdsm_mat, 3, "manhat")


dosc91/gdsm documentation built on Aug. 21, 2022, 4:16 a.m.