Mirrors the C# KNearestNeighbors class: predicts from the k training rows closest to each
new observation – an inverse-squared-distance weighted average of their responses for
regression, or their most common response for classification.
Usage
ml_knn(
x,
y,
newdata,
k,
regression = TRUE,
what = c("prediction", "neighbors", "bootstrap", "intervals"),
seed = NULL,
realizations = 1000,
alpha = 0.1
)Arguments
- x
a numeric matrix or data frame of training predictors, one row per observation.
- y
the training response, one value per row of
x.- newdata
predictors to predict for, with the same number of columns as
x.- k
the number of neighbors.
- regression
TRUE(the default) for a weighted average;FALSEfor the modal class.- what
which result to return:
"prediction"(the default),"neighbors"(the indices of each new observation's neighbors),"bootstrap"(a prediction from one bootstrap resample of the training data), or"intervals"(bootstrapped prediction intervals).- seed
integer PRNG seed, used by
"bootstrap"and"intervals";NULLuses the clock.- realizations
the number of bootstrap resamples for
"intervals". Default 1000.- alpha
the interval level for
"intervals": 0.1 (the default) gives a 90% interval.
Value
for "prediction" and "bootstrap", a numeric vector; for "neighbors", a matrix of
0-BASED training-row indices with one row per row of newdata and k columns; for
"intervals", a matrix with columns lower, median, upper, mean. Note "intervals"
always reports percentiles, even for a classifier – upstream has no classification branch
there.