This function performs kernel k nearest neighbors regression and classification using cross validation

KernelKnnCV(
  data,
  y,
  k = 5,
  folds = 5,
  h = 1,
  method = "euclidean",
  weights_function = NULL,
  regression = F,
  transf_categ_cols = F,
  threads = 1,
  extrema = F,
  Levels = NULL,
  seed_num = 1
)

Arguments

data

a data frame or matrix

y

a numeric vector (in classification the labels must be numeric from 1:Inf)

k

an integer specifying the k-nearest-neighbors

folds

the number of cross validation folds (must be greater than 1)

h

the bandwidth (applicable if the weights_function is not NULL, defaults to 1.0)

method

a string specifying the method. Valid methods are 'euclidean', 'manhattan', 'chebyshev', 'canberra', 'braycurtis', 'pearson_correlation', 'simple_matching_coefficient', 'minkowski' (by default the order 'p' of the minkowski parameter equals k), 'hamming', 'mahalanobis', 'jaccard_coefficient', 'Rao_coefficient'

weights_function

there are various ways of specifying the kernel function. See the details section.

regression

a boolean (TRUE,FALSE) specifying if regression or classification should be performed

transf_categ_cols

a boolean (TRUE, FALSE) specifying if the categorical columns should be converted to numeric or to dummy variables

threads

the number of cores to be used in parallel (openmp will be employed)

extrema

if TRUE then the minimum and maximum values from the k-nearest-neighbors will be removed (can be thought as outlier removal)

Levels

a numeric vector. In case of classification the unique levels of the response variable are necessary

seed_num

a numeric value specifying the seed of the random number generator

Value

a list of length 2. The first sublist is a list of predictions (the length of the list equals the number of the folds). The second sublist is a list with the indices for each fold.

Details

This function takes a number of arguments (including the number of cross-validation-folds) and it returns predicted values and indices for each fold. There are three possible ways to specify the weights function, 1st option : if the weights_function is NULL then a simple k-nearest-neighbor is performed. 2nd option : the weights_function is one of 'uniform', 'triangular', 'epanechnikov', 'biweight', 'triweight', 'tricube', 'gaussian', 'cosine', 'logistic', 'gaussianSimple', 'silverman', 'inverse', 'exponential'. The 2nd option can be extended by combining kernels from the existing ones (adding or multiplying). For instance, I can multiply the tricube with the gaussian kernel by giving 'tricube_gaussian_MULT' or I can add the previously mentioned kernels by giving 'tricube_gaussian_ADD'. 3rd option : a user defined kernel function

Author

Lampros Mouselimis

Examples


if (FALSE) {
data(ionosphere)

X = ionosphere[, -c(2, ncol(ionosphere))]
y = as.numeric(ionosphere[, ncol(ionosphere)])

out = KernelKnnCV(X, y, k = 5, folds = 3, regression = FALSE, Levels = unique(y))
}