R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# -----------------------------
# LIBRARY
# -----------------------------
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.3
library(class)
## Warning: package 'class' was built under R version 4.4.3
library(e1071)
## Warning: package 'e1071' was built under R version 4.4.3
library(rpart)
## Warning: package 'rpart' was built under R version 4.4.3
# -----------------------------
# CONTOH DATA (bisa ganti dengan data Anda sendiri)
# -----------------------------
set.seed(123)
n <- 300
data <- data.frame(
  APS = runif(n, 60, 100),
  Jumlah_Pendidikan_Tinggi = sample(100:300, n, replace = TRUE),
  Jumlah_Guru = sample(8000:20000, n, replace = TRUE),
  Jumlah_Pengeluaran_Pendidikan = sample(60000:130000, n, replace = TRUE)
)
data$Kategori_APS <- ifelse(data$APS > 80, "tinggi", "rendah")
data$Kategori_APS <- factor(data$Kategori_APS, levels = c("rendah", "tinggi"))

# -----------------------------
# SPLIT DATA
# -----------------------------
set.seed(42)
train_idx <- sample(1:nrow(data), 0.7 * nrow(data))
train <- data[train_idx, ]
test <- data[-train_idx, ]

# -----------------------------
# PREDIKSI SVM
# -----------------------------
svm_model <- svm(Kategori_APS ~ ., data = train, kernel = "radial")
svm_pred <- predict(svm_model, test)

# -----------------------------
# PREDIKSI KNN
# -----------------------------
knn_pred <- knn(
  train = train[, 1:4],
  test = test[, 1:4],
  cl = train$Kategori_APS,
  k = 5
)

# -----------------------------
# PREDIKSI DECISION TREE
# -----------------------------
dt_model <- rpart(Kategori_APS ~ ., data = train, method = "class")
dt_pred <- predict(dt_model, test, type = "class")

# -----------------------------
# DATA GABUNGAN
# -----------------------------
results <- data.frame(
  Actual = test$Kategori_APS,
  SVM = svm_pred,
  KNN = knn_pred,
  DecisionTree = dt_pred
)

results$Actual <- as.numeric(results$Actual) - 1
results$SVM <- as.numeric(results$SVM) - 1
results$KNN <- as.numeric(results$KNN) - 1
results$DecisionTree <- as.numeric(results$DecisionTree) - 1

# -----------------------------
# PLOT PER MODEL
# -----------------------------
svm_plot <- ggplot(results, aes(x = factor(Actual), fill = factor(SVM))) +
  geom_bar(position = "dodge") +
  geom_text(stat = "count", aes(label = after_stat(count)), 
            position = position_dodge(0.9), vjust = -0.5) +
  labs(title = "Prediksi SVM", x = "Aktual", y = "Frekuensi") +
  scale_fill_manual(values = c("red", "blue"), name = "SVM")

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

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

# -----------------------------
# GABUNGKAN KETIGA PLOT
# -----------------------------
grid.arrange(svm_plot, knn_plot, dt_plot, ncol = 3)