#install.packages("dplyr")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#install.packages("kknn")
library(kknn)
#load the mtcars data
data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#normalize all numeric variables in the mtcars dataset
mtcars_norm <- mtcars %>%
  mutate_at(vars(-c("mpg", "cyl", "vs", "am", "gear", "carb")), scale)
head(mtcars_norm)
##                    mpg cyl        disp         hp       drat           wt
## Mazda RX4         21.0   6 -0.57061982 -0.5350928  0.5675137 -0.610399567
## Mazda RX4 Wag     21.0   6 -0.57061982 -0.5350928  0.5675137 -0.349785269
## Datsun 710        22.8   4 -0.99018209 -0.7830405  0.4739996 -0.917004624
## Hornet 4 Drive    21.4   6  0.22009369 -0.5350928 -0.9661175 -0.002299538
## Hornet Sportabout 18.7   8  1.04308123  0.4129422 -0.8351978  0.227654255
## Valiant           18.1   6 -0.04616698 -0.6080186 -1.5646078  0.248094592
##                         qsec vs am gear carb
## Mazda RX4         -0.7771651  0  1    4    4
## Mazda RX4 Wag     -0.4637808  0  1    4    4
## Datsun 710         0.4260068  1  1    4    1
## Hornet 4 Drive     0.8904872  1  0    3    1
## Hornet Sportabout -0.4637808  0  0    3    2
## Valiant            1.3269868  1  0    3    1
#split the normalized data into training set 70% and testing set 30%
set.seed(123)
train_index <- sample(1:nrow(mtcars_norm), 0.7*nrow(mtcars_norm))
train_set <- mtcars_norm[train_index,]
test_set <- mtcars_norm[-train_index,]
#fit the knn model on the training set for k=1
knn_model <- kknn(mpg ~ ., train_set, test_set, k=1)
#calculate the mean squared error
mse <- mean((test_set$mpg - knn_model$fit)^2)
mse
## [1] 6.758

The mean squared error for k=1 is 6.75. This means that the average squared difference between the actual and predicted mpg values is 6.75.

#fit the knn model on the training set for k=3
knn_model <- kknn(mpg ~ ., train_set, test_set, k=3)
#calculate the mean squared error
mse <- mean((test_set$mpg - knn_model$fit)^2)
mse
## [1] 3.030023

The mean squared error for k=3 is 3.030. This means that the average squared difference between the actual and predicted mpg values is 3.030.

#fit the knn model on the training set for k=5
knn_model <- kknn(mpg ~ ., train_set, test_set, k=5)
#calculate the mean squared error
mse <- mean((test_set$mpg - knn_model$fit)^2)
mse
## [1] 3.320798

The mean squared error for k=5 is 3.321. This means that the average squared difference between the actual and predicted mpg values is 3.321.

#fit the knn model on the training set for k=7
knn_model <- kknn(mpg ~ ., train_set, test_set, k=7)
#calculate the mean squared error
mse <- mean((test_set$mpg - knn_model$fit)^2)
mse
## [1] 4.452252

The mean squared error for k=7 is 4.452 This means that the average squared difference between the actual and predicted mpg values is 4.452. Based of the mean squared error values, the best k value is 3 because it has the lowest mean squared error value of 3.030. This means that the k=3 model has the smallest average squared difference between the actual and predicted mpg values compared to the other k values.

#install.packages("ggplot2")
library(ggplot2)
#plot the mean squared error values for different k values
mse_values <- c(6.75, 3.030, 3.321, 4.452)
k_values <- c(1, 3, 5, 7)
mse_df <- data.frame(k_values, mse_values)
ggplot(mse_df, aes(x=k_values, y=mse_values)) +
  geom_point() +
  geom_line() +
  xlab("k values") +
  ylab("Mean Squared Error") +
  ggtitle("Mean Squared Error vs. k values")

The plot shows that the mean squared error decreases as the k value increases from 1 to 3, and then increases as the k value increases from 3 to 7. This indicates that the k=3 model has the lowest mean squared error value, making it the best model among the k values tested. The choice of k value can have a significant impact on the performance of the k-nearest neighbors algorithm. A small k value may result in overfitting, where the model is too sensitive to noise in the data and may not generalize well to new data. On the other hand, a large k value may result in underfitting, where the model is too simple and may not capture the underlying patterns in the data. It is important to choose an appropriate k value based on the specific dataset and problem at hand to achieve the best performance.