knn_Function {KMEANS.KNN}R Documentation

knn_Function

Description

This function implements a custom K-Nearest Neighbors (KNN) algorithm with data preprocessing options. It predicts the class of a new point based on the k closest neighbors in the feature space.

Usage

knn_Function(
  new_points,
  dataset,
  k = 5,
  distance_metric = "gower",
  target_variable,
  scale_data = TRUE,
  impute_data = "mean",
  weight_votes = TRUE
)

Arguments

new_points

A dataframe of new points to be classified.

dataset

A dataframe of training data.

k

The number of nearest neighbors to consider.

distance_metric

The distance metric for calculating neighbors ('gower', 'euclidean', 'manhattan').

target_variable

The name of the target variable in 'dataset'.

scale_data

A boolean to indicate whether the data should be normalized.

impute_data

The imputation method for missing values ('mean', 'median', 'mode').

weight_votes

A boolean to indicate whether votes should be weighted by the inverse of the distance.

Value

A list containing 'Predictions' with the predicted class for each new point, 'Data' with the 'new_points' dataframe and an additional column for predictions, 'Distances' with the distances of the k nearest neighbors, and 'Imputed_Values' with the imputed values for missing variables.

Examples

# Loading training data (e.g., iris)
data(iris)

# Preparing new points for prediction (e.g., two new observations)
new_points <- data.frame(Sepal.Length = c(5.1, 7.7, 1.3, 0.2, 5.1),
Sepal.Width = c(3.5, 2.6, 5, 3.7, 3.5),
Petal.Length = c(1.4 , 6.9, 4.5, 6, 3.4),
Petal.Width = c(10.1, 7.6, 5.6, 8.4, 5.2))

# Calling the custom KNN function
results <- knn_Function(new_points, dataset = iris, k = 3, target_variable = "Species")

# Displaying predictions
print(results$Predictions)

[Package KMEANS.KNN version 0.1.0 Index]