my_knn_cv: Cross Validation for K-Nearest Neighbors

Description Usage Arguments Value Examples

View source: R/my_knn_cv.R

Description

Run k-fold cross validations for k-nearest neighbors.

Usage

1
my_knn_cv(data, cl, k_nn, k_cv)

Arguments

data

The input data frame used for prediction. Note this data should not include NAs.

cl

The true class values of the data, which are compared against the KNN prediction. Note these classification should not include NAs.

k_nn

An integer that represents the number of neighbors KNN should use.

k_cv

The number of folds that the function will use for cross validation.

Value

A list containing the following items. The vector class, which contains the predicted classes when KNN train on the entire provided data set. The number cv_err, which is between 0 and 1 and shows the cross-validation misclassification rate.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Load and set up the penguins data
data(my_penguins)
my_penguins <- na.omit(my_penguins)

penguins_no_NA <- na.omit(my_penguins)
my_data <- data.frame("bill_length" = penguins_no_NA$bill_length_mm,
    "bill_depth" = penguins_no_NA$bill_depth_mm,
    "flipper_length" = penguins_no_NA$flipper_length_mm,
    "body_mass" = penguins_no_NA$body_mass_g)
my_cl <- penguins_no_NA$species

my_knn_cv(data = my_data, cl = my_cl, k_nn = 2, k_cv = 5)
my_knn_cv(data = my_data, cl = my_cl, k_nn = 7, k_cv = 3)

thomson3uw/project3part1package documentation built on Dec. 23, 2021, 9:58 a.m.