library(e1071)
library(ggplot2)
data("airquality")
aq_clean <- na.omit(airquality)
set.seed(42)
index <- sample(1:nrow(aq_clean), 0.7 * nrow(aq_clean))
train_data <- aq_clean[index, ]
test_data <- aq_clean[-index, ]
lm_model <- lm(Ozone ~ Temp, data = train_data)
lm_pred <- predict(lm_model, newdata = test_data)
svr_model <- svm(Ozone ~ Temp, data = train_data, type = "eps-regression")
svr_pred <- predict(svr_model, newdata = test_data)
ggplot() +
  geom_point(aes(x = test_data$Temp, y = test_data$Ozone), color = "black", size = 2) +
  geom_line(aes(x = test_data$Temp, y = lm_pred), color = "blue", linetype = "dashed", size = 1) + 
  geom_line(aes(x = test_data$Temp, y = svr_pred), color = "red", linetype = "solid", size = 1) +
  labs(title = "Perbandingan SVR vs Regresi Linear dalam Prediksi Ozone",
       x = "Temperature (Fahrenheit)",
       y = "Ozone (ppb)") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

lm_mse <- mean((lm_pred - test_data$Ozone)^2)
svr_mse <- mean((svr_pred - test_data$Ozone)^2)
print(paste("MSE Regresi Linear:", round(lm_mse, 2)))
## [1] "MSE Regresi Linear: 609.59"
print(paste("MSE SVR:", round(svr_mse, 2)))
## [1] "MSE SVR: 599.56"

Praktikum SVM

library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.3
library(e1071)
library(class)
library(rpart)
library(caret)
## Loading required package: lattice
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.3
data("PimaIndiansDiabetes")
df <- PimaIndiansDiabetes

set.seed(42)
index <- createDataPartition(df$diabetes, p = 0.7, list = FALSE)
train <- df[index, ]
test <- df[-index, ]

svm_model <- svm(diabetes ~ ., data = train, kernel = "radial")
svm_pred <- predict(svm_model, newdata = test)
svm_acc <- mean(svm_pred == test$diabetes)

knn_pred <- knn(train = train[, -9], test = test[, -9],
                cl = train$diabetes, k = 5)
knn_acc <- mean(knn_pred == test$diabetes)

dt_model <- rpart(diabetes ~ ., data = train, method = "class")
dt_pred <- predict(dt_model, test, type = "class")
dt_acc <- mean(dt_pred == test$diabetes)

cat("Akurasi SVM    :", round(svm_acc * 100, 2), "%\n")
## Akurasi SVM    : 76.09 %
cat("Akurasi KNN (k = 5) :", round(knn_acc * 100, 2), "%\n")
## Akurasi KNN (k = 5) : 70 %
cat("Akurasi Decision Tree:", round(dt_acc * 100, 2), "%\n")
## Akurasi Decision Tree: 73.91 %
library(ggplot2)
library(gridExtra)

results <- data.frame(
  Actual = test$diabetes,
  SVM = svm_pred,
  KNN = knn_pred,
  DecisionTree = dt_pred
)

results$Actual <- factor(results$Actual, levels = c("neg", "pos"))
results$SVM <- factor(results$SVM, levels = c("neg", "pos"))
results$KNN <- factor(results$KNN, levels = c("neg", "pos"))
results$DecisionTree <- factor(results$DecisionTree, levels = c("neg", "pos"))

svm_plot <- ggplot(results, aes(x = Actual, fill = SVM)) +
  geom_bar(position = "dodge") +
  geom_text(stat = "count", aes(label = ..count..), position = position_dodge(0.8), vjust = -0.5) +
  labs(title = "Prediksi SVM", x = "Aktual", y = "Frekuensi") +
  scale_fill_manual(values = c("red", "blue"))

knn_plot <- ggplot(results, aes(x = Actual, fill = KNN)) +
  geom_bar(position = "dodge") +
  geom_text(stat = "count", aes(label = ..count..), position = position_dodge(0.8), vjust = -0.5) +
  labs(title = "Prediksi KNN (k = 5)", x = "Aktual", y = "Frekuensi") +
  scale_fill_manual(values = c("red", "blue"))

dt_plot <- ggplot(results, aes(x = Actual, fill = DecisionTree)) +
  geom_bar(position = "dodge") +
  geom_text(stat = "count", aes(label = ..count..), position = position_dodge(0.8), vjust = -0.5) +
  labs(title = "Prediksi Decision Tree", x = "Aktual", y = "Frekuensi") +
  scale_fill_manual(values = c("red", "blue"))

grid.arrange(svm_plot, knn_plot, dt_plot, ncol = 3)
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.