get.NN: Function to find the nearest neighbours

Description Usage Arguments Details Value Author(s) Examples

View source: R/knn_header.R

Description

This function locates the nearest neighbours of each point in the test set in the training set. Both sets must of the same dimensions and are passed as successive rows of the same matrix P.

User can decide whether a specified number of neighbours should be sought, or whether they should be sought as some fraction of the size of the training set.

Usage

1
2
get.NN(P, k = 2, p = !k, test, train, dist.type = c("euclidean",
"absolute", "mahal"), nn.type = c("which", "dist", "max"))

Arguments

P

The matrix of data. Contains both the training and test sets.

k

The number of nearest neighbours sought.

p

The number of nearest neighbours sought, specified as a fraction of the training set.

test

The rows of the matrix P that contain the test data.

train

The rows of the matrix P that contain the training data.

dist.type

The type of distance to use when determining neighbours.

nn.type

What should be returned? Either the actual distances (dist) or their locations (rows) in P (which) or the k-th maximum distances max

Details

This function is used internally to compute the nearest neighbours; the user need not call any of these functions directly.

Value

Returns a matrix of dimensions (Number of Nearest Neighbours) x (Rows in Test Set). Each column contains the nearest neighbours of the corresponding row in the training set.

Author(s)

Mohit Dayal

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
require(MASS)
mu <- c(3,4)
Sigma <- rbind(c(1,0.2),c(0.2,1))
Y <- mvrnorm(20, mu = mu, Sigma = Sigma)
test <- 1:4
train <- 5:20
nn1a <- get.NN(Y, k = 3, test = 1:4, train = 5:20, dist.type =
'euclidean', nn.type = 'which')
nn1b <- get.NN(Y, k = 3, test = 1:4, train = 5:20, dist.type =
'euclidean', nn.type = 'dist')
nn1c <- get.NN(Y, k = 3, test = 1:4, train = 5:20, dist.type =
'euclidean', nn.type = 'max')
nn2 <- get.NN(Y, p = 0.3, test = 1:4, train = 5:20, dist.type =
'euclidean', nn.type = 'which')

MuViCP documentation built on May 1, 2019, 7:56 p.m.