## ---------------------------
## KLASIFIKASI TRANSMISI MOBIL DENGAN KNN
## Dataset: mtcars (32 mobil)
## Variabel Target: am (0=automatic, 1=manual)
## ---------------------------

# 1. Memuat Paket dan Data
library(class)     # Untuk algoritma KNN
## Warning: package 'class' was built under R version 4.4.3
library(ggplot2)   # Untuk visualisasi
library(gridExtra) # Untuk menyusun plot
## Warning: package 'gridExtra' was built under R version 4.4.3
# Memuat dataset mtcars lengkap (32 observasi)
data(mtcars)
cat("Jumlah mobil dalam dataset:", nrow(mtcars), "\n")
## Jumlah mobil dalam dataset: 32
# 2. Menampilkan Seluruh Data
cat("\n## Seluruh Data Mobil ##\n")
## 
## ## Seluruh Data Mobil ##
print(mtcars)
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
# 3. Persiapan Data
# Mengubah target variable menjadi factor
mtcars$am <- factor(mtcars$am, levels = c(0, 1), labels = c("Automatic", "Manual"))

# Normalisasi fitur (min-max scaling)
normalize <- function(x) {
  (x - min(x)) / (max(x) - min(x))
}

# Normalisasi fitur yang dipilih
features <- c("mpg", "hp", "wt")
mtcars_norm <- as.data.frame(lapply(mtcars[, features], normalize))
mtcars_norm$am <- mtcars$am
mtcars_norm$car_name <- row.names(mtcars) # Menyimpan nama mobil

# 4. Membagi Data (80% training, 20% testing)
set.seed(123)
train_idx <- sample(1:nrow(mtcars_norm), 0.8 * nrow(mtcars_norm))
train_data <- mtcars_norm[train_idx, ]
test_data <- mtcars_norm[-train_idx, ]

cat("\n## Mobil dalam Training Set ##\n")
## 
## ## Mobil dalam Training Set ##
print(train_data[, c("car_name", "am")])
##              car_name        am
## 31      Maserati Bora    Manual
## 15 Cadillac Fleetwood Automatic
## 19        Honda Civic    Manual
## 14        Merc 450SLC Automatic
## 3          Datsun 710    Manual
## 10           Merc 280 Automatic
## 18           Fiat 128    Manual
## 22   Dodge Challenger Automatic
## 11          Merc 280C Automatic
## 5   Hornet Sportabout Automatic
## 20     Toyota Corolla    Manual
## 29     Ford Pantera L    Manual
## 23        AMC Javelin Automatic
## 30       Ferrari Dino    Manual
## 9            Merc 230 Automatic
## 28       Lotus Europa    Manual
## 8           Merc 240D Automatic
## 27      Porsche 914-2    Manual
## 7          Duster 360 Automatic
## 32         Volvo 142E    Manual
## 26          Fiat X1-9    Manual
## 17  Chrysler Imperial Automatic
## 4      Hornet 4 Drive Automatic
## 1           Mazda RX4    Manual
## 24         Camaro Z28 Automatic
cat("\n## Mobil dalam Test Set ##\n")
## 
## ## Mobil dalam Test Set ##
print(test_data[, c("car_name", "am")])
##               car_name        am
## 2        Mazda RX4 Wag    Manual
## 6              Valiant Automatic
## 12          Merc 450SE Automatic
## 13          Merc 450SL Automatic
## 16 Lincoln Continental Automatic
## 21       Toyota Corona Automatic
## 25    Pontiac Firebird Automatic
# 5. Model KNN
k <- 3
knn_pred <- knn(
  train = train_data[, features],
  test = test_data[, features],
  cl = train_data$am,
  k = k
)

# 6. Evaluasi Model
conf_matrix <- table(Predicted = knn_pred, Actual = test_data$am)
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix)

cat("\n## Confusion Matrix ##\n")
## 
## ## Confusion Matrix ##
print(conf_matrix)
##            Actual
## Predicted   Automatic Manual
##   Automatic         5      0
##   Manual            1      1
cat("\n## Akurasi Model:", round(accuracy * 100, 2), "% ##\n")
## 
## ## Akurasi Model: 85.71 % ##
# 7. Visualisasi Hasil
# Plot 1: MPG vs HP
p1 <- ggplot(mtcars_norm, aes(x = mpg, y = hp, color = am)) +
  geom_point(size = 3) +
  geom_point(data = test_data, aes(x = mpg, y = hp, shape = knn_pred), 
             size = 5, stroke = 1.5) +
  scale_shape_manual(values = c(4, 3)) +
  labs(title = "Klasifikasi Berdasarkan MPG dan HP",
       subtitle = paste("Akurasi:", round(accuracy * 100, 2), "%"),
       x = "MPG (Normalized)",
       y = "Horsepower (Normalized)") +
  theme_minimal()

# Plot 2: MPG vs Weight
p2 <- ggplot(mtcars_norm, aes(x = mpg, y = wt, color = am)) +
  geom_point(size = 3) +
  geom_point(data = test_data, aes(x = mpg, y = wt, shape = knn_pred), 
             size = 5, stroke = 1.5) +
  scale_shape_manual(values = c(4, 3)) +
  labs(title = "Klasifikasi Berdasarkan MPG dan Weight",
       x = "MPG (Normalized)",
       y = "Weight (Normalized)") +
  theme_minimal()

# Menampilkan kedua plot
grid.arrange(p1, p2, ncol = 2)

# 8. Prediksi Mobil Baru
new_car <- data.frame(
  mpg = (24 - min(mtcars$mpg)) / (max(mtcars$mpg) - min(mtcars$mpg)),
  hp = (120 - min(mtcars$hp)) / (max(mtcars$hp) - min(mtcars$hp)),
  wt = (2.8 - min(mtcars$wt)) / (max(mtcars$wt) - min(mtcars$wt))
)

pred_am <- knn(
  train = train_data[, features],
  test = new_car,
  cl = train_data$am,
  k = k
)

cat("\n## Prediksi untuk Mobil Baru ##\n")
## 
## ## Prediksi untuk Mobil Baru ##
cat("Spesifikasi Normalisasi:\n")
## Spesifikasi Normalisasi:
print(new_car)
##         mpg        hp        wt
## 1 0.5787234 0.2402827 0.3290718
cat("\nPrediksi Transmisi:", as.character(pred_am), "\n")
## 
## Prediksi Transmisi: Manual
# 9. Tabel Ringkasan Mobil
cat("\n## Ringkasan Data Mobil ##\n")
## 
## ## Ringkasan Data Mobil ##
mtcars_summary <- cbind(mtcars[, c("mpg", "hp", "wt", "am")], 
                       Predicted = ifelse(row.names(mtcars) %in% test_data$car_name,
                                        as.character(knn_pred), "Training"))
print(mtcars_summary)
##                      mpg  hp    wt        am Predicted
## Mazda RX4           21.0 110 2.620    Manual  Training
## Mazda RX4 Wag       21.0 110 2.875    Manual Automatic
## Datsun 710          22.8  93 2.320    Manual  Training
## Hornet 4 Drive      21.4 110 3.215 Automatic  Training
## Hornet Sportabout   18.7 175 3.440 Automatic  Training
## Valiant             18.1 105 3.460 Automatic    Manual
## Duster 360          14.3 245 3.570 Automatic  Training
## Merc 240D           24.4  62 3.190 Automatic  Training
## Merc 230            22.8  95 3.150 Automatic  Training
## Merc 280            19.2 123 3.440 Automatic  Training
## Merc 280C           17.8 123 3.440 Automatic  Training
## Merc 450SE          16.4 180 4.070 Automatic Automatic
## Merc 450SL          17.3 180 3.730 Automatic    Manual
## Merc 450SLC         15.2 180 3.780 Automatic  Training
## Cadillac Fleetwood  10.4 205 5.250 Automatic  Training
## Lincoln Continental 10.4 215 5.424 Automatic Automatic
## Chrysler Imperial   14.7 230 5.345 Automatic  Training
## Fiat 128            32.4  66 2.200    Manual  Training
## Honda Civic         30.4  52 1.615    Manual  Training
## Toyota Corolla      33.9  65 1.835    Manual  Training
## Toyota Corona       21.5  97 2.465 Automatic Automatic
## Dodge Challenger    15.5 150 3.520 Automatic  Training
## AMC Javelin         15.2 150 3.435 Automatic  Training
## Camaro Z28          13.3 245 3.840 Automatic  Training
## Pontiac Firebird    19.2 175 3.845 Automatic Automatic
## Fiat X1-9           27.3  66 1.935    Manual  Training
## Porsche 914-2       26.0  91 2.140    Manual  Training
## Lotus Europa        30.4 113 1.513    Manual  Training
## Ford Pantera L      15.8 264 3.170    Manual  Training
## Ferrari Dino        19.7 175 2.770    Manual  Training
## Maserati Bora       15.0 335 3.570    Manual  Training
## Volvo 142E          21.4 109 2.780    Manual  Training
# 10. Visualisasi Prediksi Mobil Baru (Tambahan)
# Plot 1: MPG vs HP dengan Mobil Baru
p1_new <- ggplot(mtcars_norm, aes(x = mpg, y = hp, color = am)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_point(data = test_data, aes(x = mpg, y = hp, shape = knn_pred),
             size = 5, stroke = 1.5) +
  geom_point(data = data.frame(new_car), aes(x = mpg, y = hp),
             color = "red", size = 6, shape = 18) +  # Diamond shape for new car
  geom_text(data = data.frame(new_car), aes(x = mpg, y = hp, label = "Mobil Baru"),
            color = "black", vjust = -1.5, fontface = "bold") +
  scale_shape_manual(values = c(4, 3)) +  # Shapes for test data
  labs(title = "Klasifikasi MPG vs HP dengan Mobil Baru",
       subtitle = paste("Prediksi:", as.character(pred_am)),
       x = "MPG (Normalized)",
       y = "Horsepower (Normalized)") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Plot 2: MPG vs Weight dengan Mobil Baru
p2_new <- ggplot(mtcars_norm, aes(x = mpg, y = wt, color = am)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_point(data = test_data, aes(x = mpg, y = wt, shape = knn_pred),
             size = 5, stroke = 1.5) +
  geom_point(data = data.frame(new_car), aes(x = mpg, y = wt),
             color = "red", size = 6, shape = 18) +  # Diamond shape for new car
  geom_text(data = data.frame(new_car), aes(x = mpg, y = wt, label = "Mobil Baru"),
            color = "black", vjust = -1.5, fontface = "bold") +
  scale_shape_manual(values = c(4, 3)) +  # Shapes for test data
  labs(title = "Klasifikasi MPG vs Weight dengan Mobil Baru",
       subtitle = paste("Prediksi:", as.character(pred_am)),
       x = "MPG (Normalized)",
       y = "Weight (Normalized)") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Menampilkan plot khusus untuk mobil baru
grid.arrange(p1_new, p2_new, ncol = 2)