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,
p = k
)
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.
a data frame or matrix
a numeric vector (in classification the labels must be numeric from 1:Inf)
an integer specifying the k-nearest-neighbors
the number of cross validation folds (must be greater than 1)
the bandwidth (applicable if the weights_function is not NULL, defaults to 1.0)
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'
there are various ways of specifying the kernel function. See the details section.
a boolean (TRUE,FALSE) specifying if regression or classification should be performed
a boolean (TRUE, FALSE) specifying if the categorical columns should be converted to numeric or to dummy variables
the number of cores to be used in parallel (openmp will be employed)
if TRUE then the minimum and maximum values from the k-nearest-neighbors will be removed (can be thought as outlier removal)
a numeric vector. In case of classification the unique levels of the response variable are necessary
a numeric value specifying the seed of the random number generator
a numeric value specifying the 'minkowski' order, i.e. if 'method' is set to 'minkowski'. This parameter defaults to 'k'
Lampros Mouselimis
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
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))
}
Run the code above in your browser using DataLab