title: “Klasifikasi Spesies Penguin dengan SVM” author: “Jesicha Aulia Adam” date: “2025-06-06” output: html_document
if (!require(palmerpenguins)) { install.packages(“palmerpenguins”) library(palmerpenguins) }
library(tidyverse) library(palmerpenguins) library(e1071) library(caret) library(gridExtra)
#1.Muat Dataset dan Cek Data # Gunakan dataset penguins dan hapus missing values data(“penguins”) penguins <- na.omit(penguins) head(penguins)
#2.Visualisasi Awal ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) + geom_point() + labs(title = “Bill Length vs Bill Depth by Species”)
#3.Persiapan Data # Hanya pakai dua fitur untuk visualisasi 2D df <- penguins %>% select(species, bill_length_mm, bill_depth_mm)
df\(species <- factor(df\)species)
set.seed(123) train_idx <- createDataPartition(df$species, p = 0.8, list = FALSE) train <- df[train_idx, ] test <- df[-train_idx, ]
#4.Bnagun model svm #svm linear svm_linear <- svm(species ~ ., data = train, kernel = “linear”) pred_linear <- predict(svm_linear, test)
#svm nonlinear(RBF) svm_rbf <- svm(species ~ ., data = train, kernel = “radial”) pred_rbf <- predict(svm_rbf, test)
#5.Evaluasi model confusionMatrix(pred_linear, test\(species) confusionMatrix(pred_rbf, test\)species)
#6.Visualisasi Decision Boundary # Buat grid prediksi make_grid <- function(data, n = 200) { x_range <- seq(min(data\(bill_length_mm), max(data\)bill_length_mm), length.out = n) y_range <- seq(min(data\(bill_depth_mm), max(data\)bill_depth_mm), length.out = n) expand.grid(bill_length_mm = x_range, bill_depth_mm = y_range) }
grid <- make_grid(df)
grid\(pred_linear <- predict(svm_linear, grid) grid\)pred_rbf <- predict(svm_rbf, grid)
p_linear <- ggplot() + geom_point(data = grid, aes(x = bill_length_mm, y = bill_depth_mm, color = pred_linear), alpha = 0.3) + geom_point(data = df, aes(x = bill_length_mm, y = bill_depth_mm, color = species), shape = 1) + labs(title = “Decision Boundary - SVM Linear”)
p_rbf <- ggplot() + geom_point(data = grid, aes(x = bill_length_mm, y = bill_depth_mm, color = pred_rbf), alpha = 0.3) + geom_point(data = df, aes(x = bill_length_mm, y = bill_depth_mm, color = species), shape = 1) + labs(title = “Decision Boundary - SVM RBF”)
grid.arrange(p_linear, p_rbf, ncol = 2)
#7.Interpretasi Parameter C & Gamma tune_result <- tune( svm, species ~ ., data = train, kernel = “radial”, ranges = list(cost = c(0.1, 1, 10), gamma = c(0.01, 0.1, 1)) ) summary(tune_result)