my_knn_cv: knn_cv

Description Usage Arguments Value Examples

View source: R/my_knn_cv.R

Description

This function takes input data, the desired number of nearest neighbors, the number of folds to be used for k-fold cross validation and performs k-fold cross validation from the given input data

Usage

1
my_knn_cv(train, cl, k_nn, k_cv)

Arguments

train

a numeric input data frame

cl

the true class value of the training data

k_nn

a numeric representing the desired number of nearest neighbors

k_cv

a numeric representing the number of folds to be used for k-fold cross validation

Value

A list of two objects where the first object are the predicted outcomes based on the input data and the second object is the average cross-validation misclassification error rate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
data("my_penguins")
penguin_data <- my_penguins[c("bill_length_mm",
                  "bill_depth_mm",
                  "flipper_length_mm",
                  "body_mass_g",
                  "species")]
penguin_data <- as.data.frame(penguin_data)
# remove rows containing NA values
data_noNA <- na.omit(penguin_data)
# split data into predictors and outcome
train <- data_noNA[, 1:4]
cl <- data_noNA[, 5]
# test function with 1-nearest neighbor and 5-fold cv
results_1nn_5cv <- my_knn_cv(train, cl, 1, 5)
# test function with 5-nearest neighbor and 5-fold cv
results_5nn_5cv <- my_knn_cv(train, cl, 5, 5)

dzeng8/STAT302PACKAGE documentation built on Dec. 20, 2021, 2:19 a.m.