my_knn_cv: K-nearest neighbor cross-validation

Description Usage Arguments Value Examples

View source: R/my_knn_cv.R

Description

This function uses k-nearest neighbor analysis to generate predicted classifications for an input dataset based on input covariates. Also uses cross-validation to compute average misclassification rate in order to assist in the selection of an optimal value of k.

Usage

1
my_knn_cv(train, cl, k_nn, k_cv)

Arguments

train

A data frame of training set cases. Include only covariates of interest to be used in predicting class.

cl

A vector including the true class values of the training data

k_nn

An integer representing the number of neighbors considered.

k_cv

An integer representing the number of folds used in cross-validation.

Value

A list with the following elements: "class": a vector of the predicted class for all observations, and "cv_err": a numeric with the cross-validation misclassification error.

Examples

1
2
penguins <- tidyr::drop_na(my_penguins)
my_knn_cv(penguins[, 3:6], penguins$species, 1, 5)

adammcbroom/STAT302package documentation built on Dec. 18, 2021, 10:25 p.m.