my_knn_cv: k-nearest-neighbor function

Description Usage Arguments Value Examples

View source: R/my_knn_cv.R

Description

This function performs k nearest neighbors with k-fold cross validation.

Usage

1
my_knn_cv(train, cl, k_nn, k_cv)

Arguments

train

input data frame

cl

true class value of your training data

k_nn

integer representing the number of neighbors

k_cv

integer representing the number of folds

Value

a list with objects "class": a vector of the predicted class for all observations; "cv_err": a numeric with the cross-validation misclassification error

Examples

1
2
3
4
5
6
dat <- na.omit(my_penguins[, c("species", "bill_length_mm", "bill_depth_mm",
                               "flipper_length_mm", "body_mass_g")])
cl <- dat$species
dat <- dat[, c("bill_length_mm", "bill_depth_mm",
               "flipper_length_mm", "body_mass_g")]
my_knn_cv(dat, cl, 1, 5)

RuofengT/stat302package documentation built on Dec. 18, 2021, 11:54 a.m.