knn in r

# Step 1: Load necessary libraries
library(class)

# Step 2: Prepare your data
# Assuming you have a data frame called 'data' with predictors in columns 1 to n-1 and the response variable in column n
# Replace 'data' with your actual data frame name

# Example:
# predictors <- data[, 1:(ncol(data)-1)]
# response <- data[, ncol(data)]

# Step 3: Split the data into training and testing sets
set.seed(123)  # Set seed for reproducibility
train_indices <- sample(1:nrow(data), 0.7 * nrow(data))
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]

# Step 4: Standardize the predictor variables if needed
# This step is optional and depends on the characteristics of your data
# Example:
# train_data <- scale(train_data)
# test_data <- scale(test_data)

# Step 5: Train the k-NN model
# Use the knn() function from the 'class' library
# Parameters: train, test, cl (class labels of the training data), k (number of neighbors)
k_value <- 3  # Set the desired number of neighbors
knn_model <- knn(train = train_data[, 1:(ncol(train_data)-1)], test = test_data[, 1:(ncol(test_data)-1)], cl = train_data[, ncol(train_data)], k = k_value)

# Step 6: Evaluate the model
# Compare predicted labels with actual labels
confusion_matrix <- table(knn_model, test_data[, ncol(test_data)])

# Step 7: Display the confusion matrix
print("Confusion Matrix:")
print(confusion_matrix)

# Step 8: Calculate accuracy
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
print(paste("Accuracy:", accuracy))