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
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(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'forcats' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(caret)
## Warning: package 'caret' was built under R version 4.4.3
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
library(e1071) # Untuk SVM
## Warning: package 'e1071' was built under R version 4.4.3
library(randomForest) # Untuk Random Forest
## Warning: package 'randomForest' was built under R version 4.4.3
## randomForest 4.7-1.2
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
##
## The following object is masked from 'package:dplyr':
##
## combine
##
## The following object is masked from 'package:ggplot2':
##
## margin
library(rpart) # Untuk Decision Tree
#BACA DATA
datatraining_xlsx_Sheet1<- read_csv("C:/Users/LENOVO/Downloads/datatraining.xlsx - Sheet1.csv")
## Rows: 200 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (11): usia, jenis_kelamin, nilai_rata_rata, dukungan_orang_tua, fasilita...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(datatraining_xlsx_Sheet1)
datatesting_xlsx_Sheet1 <- read_csv("C:/Users/LENOVO/Downloads/datatesting.xlsx - Sheet1.csv")
## Rows: 15 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (10): usia, jenis_kelamin, nilai_rata_rata, dukungan_orang_tua, fasilita...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(datatesting_xlsx_Sheet1)
anyNA(datatraining_xlsx_Sheet1) #cek apakah ada NA
## [1] FALSE
anyNA(datatesting_xlsx_Sheet1) #cek apakah ada NA
## [1] FALSE
colSums(is.na(datatraining_xlsx_Sheet1))
## usia jenis_kelamin nilai_rata_rata
## 0 0 0
## dukungan_orang_tua fasilitas_belajar jam_belajar_per_hari
## 0 0 0
## kehadiran_persen minat_pada_pelajaran kesulitan_ekonomi
## 0 0 0
## jarak_rumah_sekolah motivasi_belajar
## 0 0
colSums(is.na(datatesting_xlsx_Sheet1))
## usia jenis_kelamin nilai_rata_rata
## 0 0 0
## dukungan_orang_tua fasilitas_belajar jam_belajar_per_hari
## 0 0 0
## kehadiran_persen minat_pada_pelajaran kesulitan_ekonomi
## 0 0 0
## jarak_rumah_sekolah
## 0
berdasarkan output tersebut, tidak ditemukan adanya NA sehingga tidak perlu melakukan pembersihan data
datatraining_xlsx_Sheet1[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi", "motivasi_belajar")] <- lapply(datatraining_xlsx_Sheet1[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi", "motivasi_belajar")],as.factor)
str(datatraining_xlsx_Sheet1)
## spc_tbl_ [200 × 11] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ usia : num [1:200] 15 19 15 15 16 18 16 16 15 21 ...
## $ jenis_kelamin : Factor w/ 2 levels "0","1": 1 1 1 2 2 2 2 1 2 2 ...
## $ nilai_rata_rata : num [1:200] 85.2 60.9 60.5 78.6 66.2 85.9 45.2 91.6 74.1 88.2 ...
## $ dukungan_orang_tua : Factor w/ 3 levels "1","2","3": 1 2 2 3 2 2 1 2 2 3 ...
## $ fasilitas_belajar : Factor w/ 3 levels "1","2","3": 2 3 1 2 1 1 3 2 1 3 ...
## $ jam_belajar_per_hari: num [1:200] 4.1 3.8 6.1 5.1 5.5 3.7 2.8 6.2 4.6 3 ...
## $ kehadiran_persen : num [1:200] 79.7 89.8 86.7 78.5 81 85.4 78.9 78.1 96.8 82.1 ...
## $ minat_pada_pelajaran: Factor w/ 3 levels "1","2","3": 2 2 1 3 1 2 1 2 2 3 ...
## $ kesulitan_ekonomi : Factor w/ 2 levels "0","1": 1 2 2 2 2 2 1 1 1 2 ...
## $ jarak_rumah_sekolah : num [1:200] 11.5 6.3 15.9 9.8 3.5 1 6.4 4.9 6.5 9.5 ...
## $ motivasi_belajar : Factor w/ 3 levels "1","2","3": 1 2 1 2 1 1 1 2 1 3 ...
## - attr(*, "spec")=
## .. cols(
## .. usia = col_double(),
## .. jenis_kelamin = col_double(),
## .. nilai_rata_rata = col_double(),
## .. dukungan_orang_tua = col_double(),
## .. fasilitas_belajar = col_double(),
## .. jam_belajar_per_hari = col_double(),
## .. kehadiran_persen = col_double(),
## .. minat_pada_pelajaran = col_double(),
## .. kesulitan_ekonomi = col_double(),
## .. jarak_rumah_sekolah = col_double(),
## .. motivasi_belajar = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
datatesting_xlsx_Sheet1[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi")] <- lapply(datatesting_xlsx_Sheet1[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi")],as.factor)
str(datatesting_xlsx_Sheet1)
## spc_tbl_ [15 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ usia : num [1:15] 15 19 17 20 16 21 21 19 17 18 ...
## $ jenis_kelamin : Factor w/ 2 levels "0","1": 2 1 1 2 2 1 1 2 1 1 ...
## $ nilai_rata_rata : num [1:15] 90.2 79.6 66.9 85.6 65.9 70.8 86.3 84.3 79.5 74.4 ...
## $ dukungan_orang_tua : Factor w/ 3 levels "1","2","3": 2 1 2 3 3 2 3 3 2 1 ...
## $ fasilitas_belajar : Factor w/ 3 levels "1","2","3": 2 1 3 1 1 3 2 3 2 2 ...
## $ jam_belajar_per_hari: num [1:15] 5.4 4.6 2.9 3.9 5.3 1.9 4.4 1.5 2 5.5 ...
## $ kehadiran_persen : num [1:15] 78.3 88.2 76.3 89.4 74 100 100 92.1 84.5 90.3 ...
## $ minat_pada_pelajaran: Factor w/ 3 levels "1","2","3": 2 3 1 1 1 1 1 1 3 3 ...
## $ kesulitan_ekonomi : Factor w/ 2 levels "0","1": 2 1 1 1 2 2 1 1 1 1 ...
## $ jarak_rumah_sekolah : num [1:15] 4.7 10.8 11.8 5.7 3.1 7.1 8.2 5.4 4.2 10.7 ...
## - attr(*, "spec")=
## .. cols(
## .. usia = col_double(),
## .. jenis_kelamin = col_double(),
## .. nilai_rata_rata = col_double(),
## .. dukungan_orang_tua = col_double(),
## .. fasilitas_belajar = col_double(),
## .. jam_belajar_per_hari = col_double(),
## .. kehadiran_persen = col_double(),
## .. minat_pada_pelajaran = col_double(),
## .. kesulitan_ekonomi = col_double(),
## .. jarak_rumah_sekolah = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
# Split data
library(caret)
set.seed(123) # supaya hasil reproducible
train_index <- createDataPartition(datatraining_xlsx_Sheet1$motivasi_belajar, p = 0.7, list = FALSE)
# convert matrix index to vector
train_index_vec <- train_index[,1]
train <- datatraining_xlsx_Sheet1[train_index_vec, ]
test <- datatraining_xlsx_Sheet1[-train_index_vec, ]
dim(train)
## [1] 141 11
dim(test)
## [1] 59 11
table(train$motivasi_belajar)
##
## 1 2 3
## 57 74 10
table(test$motivasi_belajar)
##
## 1 2 3
## 24 31 4
a). Support Vector Machine (SVM)
library(e1071)
svm_model <- svm(motivasi_belajar ~ ., data = train, kernel = "radial")
svm_pred <- predict(svm_model, newdata = test) # Prediksi pada data testing
svm_acc <- mean(svm_pred == test$motivasi_belajar) # Hitung akurasi
print(paste("Akurasi SVM:", round(svm_acc * 100, 2), "%"))
## [1] "Akurasi SVM: 64.41 %"
# Tampilkan confusion matrix
confusionMatrix(svm_pred, test$motivasi_belajar)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 16 9 0
## 2 8 22 4
## 3 0 0 0
##
## Overall Statistics
##
## Accuracy : 0.6441
## 95% CI : (0.5087, 0.7645)
## No Information Rate : 0.5254
## P-Value [Acc > NIR] : 0.04424
##
## Kappa : 0.3218
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.6667 0.7097 0.0000
## Specificity 0.7429 0.5714 1.0000
## Pos Pred Value 0.6400 0.6471 NaN
## Neg Pred Value 0.7647 0.6400 0.9322
## Prevalence 0.4068 0.5254 0.0678
## Detection Rate 0.2712 0.3729 0.0000
## Detection Prevalence 0.4237 0.5763 0.0000
## Balanced Accuracy 0.7048 0.6406 0.5000
b). Random Forest
library(randomForest)
set.seed(123)
rf_model <- randomForest(as.factor(motivasi_belajar) ~ ., data = train, importance = TRUE)
# Prediksi menggunakan model pada data test
rf_pred <- predict(rf_model, newdata = test)
# Hitung akurasi
rf_acc <- mean(rf_pred == test$motivasi_belajar)
print(paste("Akurasi Random Forest:", round(rf_acc * 100, 2), "%"))
## [1] "Akurasi Random Forest: 64.41 %"
# Tampilkan confusion matrix
confusionMatrix(rf_pred, test$motivasi_belajar)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 10 3 0
## 2 14 28 4
## 3 0 0 0
##
## Overall Statistics
##
## Accuracy : 0.6441
## 95% CI : (0.5087, 0.7645)
## No Information Rate : 0.5254
## P-Value [Acc > NIR] : 0.04424
##
## Kappa : 0.2892
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.4167 0.9032 0.0000
## Specificity 0.9143 0.3571 1.0000
## Pos Pred Value 0.7692 0.6087 NaN
## Neg Pred Value 0.6957 0.7692 0.9322
## Prevalence 0.4068 0.5254 0.0678
## Detection Rate 0.1695 0.4746 0.0000
## Detection Prevalence 0.2203 0.7797 0.0000
## Balanced Accuracy 0.6655 0.6302 0.5000
c). Decision Tree
library(rpart)
library(caret)
# Membuat model decision tree
set.seed(123)
dt_model <- rpart(motivasi_belajar ~ ., data = train, method = "class")
# Melihat ringkasan model (opsional)
print(dt_model)
## n= 141
##
## node), split, n, loss, yval, (yprob)
## * denotes terminal node
##
## 1) root 141 67 2 (0.40425532 0.52482270 0.07092199)
## 2) fasilitas_belajar=1,2 98 46 1 (0.53061224 0.44897959 0.02040816)
## 4) nilai_rata_rata< 70.5 35 6 1 (0.82857143 0.17142857 0.00000000)
## 8) kehadiran_persen< 89.35 26 1 1 (0.96153846 0.03846154 0.00000000) *
## 9) kehadiran_persen>=89.35 9 4 2 (0.44444444 0.55555556 0.00000000) *
## 5) nilai_rata_rata>=70.5 63 25 2 (0.36507937 0.60317460 0.03174603)
## 10) kehadiran_persen< 83.5 27 11 1 (0.59259259 0.37037037 0.03703704)
## 20) minat_pada_pelajaran=1,2 19 4 1 (0.78947368 0.21052632 0.00000000) *
## 21) minat_pada_pelajaran=3 8 2 2 (0.12500000 0.75000000 0.12500000) *
## 11) kehadiran_persen>=83.5 36 8 2 (0.19444444 0.77777778 0.02777778) *
## 3) fasilitas_belajar=3 43 13 2 (0.11627907 0.69767442 0.18604651) *
# Visualisasi pohon:
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.4.3
rpart.plot(dt_model)
# Prediksi pada data test
dt_pred <- predict(dt_model, newdata = test, type = "class")
# Hitung akurasi
dt_acc <- mean(dt_pred == test$motivasi_belajar)
print(paste("Akurasi Decision Tree:", round(dt_acc * 100, 2), "%"))
## [1] "Akurasi Decision Tree: 61.02 %"
# Tampilkan confusion matrix
confusionMatrix(dt_pred, test$motivasi_belajar)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 10 5 0
## 2 14 26 4
## 3 0 0 0
##
## Overall Statistics
##
## Accuracy : 0.6102
## 95% CI : (0.4744, 0.7345)
## No Information Rate : 0.5254
## P-Value [Acc > NIR] : 0.1201
##
## Kappa : 0.2277
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.4167 0.8387 0.0000
## Specificity 0.8571 0.3571 1.0000
## Pos Pred Value 0.6667 0.5909 NaN
## Neg Pred Value 0.6818 0.6667 0.9322
## Prevalence 0.4068 0.5254 0.0678
## Detection Rate 0.1695 0.4407 0.0000
## Detection Prevalence 0.2542 0.7458 0.0000
## Balanced Accuracy 0.6369 0.5979 0.5000
# Ambil 15 data testing pertama
test_15 <- datatesting_xlsx_Sheet1[1:15, ]
# Pastikan tipe faktor sudah benar di test_15 juga
test_15[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi")] <-
lapply(test_15[c("jenis_kelamin", "dukungan_orang_tua","fasilitas_belajar", "minat_pada_pelajaran", "kesulitan_ekonomi")], as.factor)
# PREDIKSI MENGGUNAKAN MODEL
# 1. Decision Tree
dt_pred_15 <- predict(dt_model, newdata = test_15, type = "class")
print("Prediksi Decision Tree:")
## [1] "Prediksi Decision Tree:"
print(dt_pred_15)
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## 1 2 2 2 1 2 2 2 2 2 1 1 2 2 1
## Levels: 1 2 3
# 2. Support Vector Machine
svm_pred_15 <- predict(svm_model, newdata = test_15)
print("Prediksi SVM:")
## [1] "Prediksi SVM:"
print(svm_pred_15)
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## 2 2 1 2 1 2 2 2 2 2 1 1 2 1 1
## Levels: 1 2 3
# 3. Random Forest
rf_pred_15 <- predict(rf_model, newdata = test_15)
print("Prediksi Random Forest:")
## [1] "Prediksi Random Forest:"
print(rf_pred_15)
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## 2 2 1 2 1 2 2 2 2 2 1 1 2 2 1
## Levels: 1 2 3
# Jika ingin menggabungkan hasil prediksi ke data test_15
test_15$pred_dt <- dt_pred_15
test_15$pred_svm <- svm_pred_15
test_15$pred_rf <- rf_pred_15
hasil_prediksi <- data.frame(
Siswa = 1:15,
Decision_Tree = as.character(dt_pred_15),
SVM = as.character(svm_pred_15),
Random_Forest = as.character(rf_pred_15)
)
print(hasil_prediksi)
## Siswa Decision_Tree SVM Random_Forest
## 1 1 1 2 2
## 2 2 2 2 2
## 3 3 2 1 1
## 4 4 2 2 2
## 5 5 1 1 1
## 6 6 2 2 2
## 7 7 2 2 2
## 8 8 2 2 2
## 9 9 2 2 2
## 10 10 2 2 2
## 11 11 1 1 1
## 12 12 1 1 1
## 13 13 2 2 2
## 14 14 2 1 2
## 15 15 1 1 1
4.KESIMPULAN
# Buat dataframe hasil prediksi untuk 15 data testing
hasil_prediksi <- data.frame(
Siswa = 1:15,
Decision_Tree = as.character(dt_pred_15),
SVM = as.character(svm_pred_15),
Random_Forest = as.character(rf_pred_15)
)
# Simpan ke CSV di folder lokal
write.csv(hasil_prediksi, "hasil_prediksi_15siswa.csv", row.names = FALSE)
# Kalau mau lihat dulu
print(hasil_prediksi)
## Siswa Decision_Tree SVM Random_Forest
## 1 1 1 2 2
## 2 2 2 2 2
## 3 3 2 1 1
## 4 4 2 2 2
## 5 5 1 1 1
## 6 6 2 2 2
## 7 7 2 2 2
## 8 8 2 2 2
## 9 9 2 2 2
## 10 10 2 2 2
## 11 11 1 1 1
## 12 12 1 1 1
## 13 13 2 2 2
## 14 14 2 1 2
## 15 15 1 1 1