library(e1071)
## Warning: package 'e1071' was built under R version 4.4.3
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) +  # Data asli
  geom_line(aes(x = test_data$Temp, y = lm_pred), color = "blue", linetype = "dashed", size = 1) +  # Regresi Linear
  geom_line(aes(x = test_data$Temp, y = svr_pred), color = "red", linetype = "solid", size = 1) +  # SVR
  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"