library(e1071)
## Warning: package 'e1071' was built under R version 4.4.3
library(randomForest)
## 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.
library(rpart)
## Warning: package 'rpart' was built under R version 4.4.3
library(readr)
## Warning: package 'readr' was built under R version 4.4.3
LOAD DATA
train_data <- read_csv("datatraining.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.
test_data <- read_csv("datatesting.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.
kategori_vars <- c("jenis_kelamin", "dukungan_orang_tua", "fasilitas_belajar",
"minat_pada_pelajaran", "kesulitan_ekonomi", "motivasi_belajar")
train_data[kategori_vars] <- lapply(train_data[kategori_vars], as.factor)
test_data[kategori_vars[-length(kategori_vars)]] <- lapply(test_data[kategori_vars[-length(kategori_vars)]], as.factor)
SVM
model_svm <- svm(motivasi_belajar ~ ., data = train_data)
pred_svm <- predict(model_svm, newdata = test_data)
RANDOM FOREST
model_rf <- randomForest(motivasi_belajar ~ ., data = train_data)
pred_rf <- predict(model_rf, newdata = test_data)
DECISION TREE
model_dt <- rpart(motivasi_belajar ~ ., data = train_data)
pred_dt <- predict(model_dt, newdata = test_data, type = "class")
HASIL PRESIKSI
# Gabungkan prediksi ke data test
hasil_svm <- cbind(test_data, prediksi_svm = pred_svm)
hasil_rf <- cbind(test_data, prediksi_rf = pred_rf)
hasil_dt <- cbind(test_data, prediksi_dt = pred_dt)
hasil_svm <- read_csv("hasil_prediksi_svm.csv")
## Rows: 15 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.
print(hasil_svm)
## # A tibble: 15 × 11
## usia jenis_kelamin nilai_rata_rata dukungan_orang_tua fasilitas_belajar
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 15 1 90.2 2 2
## 2 19 0 79.6 1 1
## 3 17 0 66.9 2 3
## 4 20 1 85.6 3 1
## 5 16 1 65.9 3 1
## 6 21 0 70.8 2 3
## 7 21 0 86.3 3 2
## 8 19 1 84.3 3 3
## 9 17 0 79.5 2 2
## 10 18 0 74.4 1 2
## 11 16 1 76.9 3 2
## 12 15 1 81.9 2 2
## 13 17 1 83.2 3 2
## 14 17 0 65.2 3 3
## 15 20 1 62 2 1
## # ℹ 6 more variables: jam_belajar_per_hari <dbl>, kehadiran_persen <dbl>,
## # minat_pada_pelajaran <dbl>, kesulitan_ekonomi <dbl>,
## # jarak_rumah_sekolah <dbl>, prediksi_svm <dbl>
hasil_rf <- read_csv("hasil_prediksi_rf.csv")
## Rows: 15 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.
print(hasil_rf)
## # A tibble: 15 × 11
## usia jenis_kelamin nilai_rata_rata dukungan_orang_tua fasilitas_belajar
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 15 1 90.2 2 2
## 2 19 0 79.6 1 1
## 3 17 0 66.9 2 3
## 4 20 1 85.6 3 1
## 5 16 1 65.9 3 1
## 6 21 0 70.8 2 3
## 7 21 0 86.3 3 2
## 8 19 1 84.3 3 3
## 9 17 0 79.5 2 2
## 10 18 0 74.4 1 2
## 11 16 1 76.9 3 2
## 12 15 1 81.9 2 2
## 13 17 1 83.2 3 2
## 14 17 0 65.2 3 3
## 15 20 1 62 2 1
## # ℹ 6 more variables: jam_belajar_per_hari <dbl>, kehadiran_persen <dbl>,
## # minat_pada_pelajaran <dbl>, kesulitan_ekonomi <dbl>,
## # jarak_rumah_sekolah <dbl>, prediksi_rf <dbl>
hasil_dt <- read_csv("hasil_prediksi_dt.csv")
## Rows: 15 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.
print(hasil_dt)
## # A tibble: 15 × 11
## usia jenis_kelamin nilai_rata_rata dukungan_orang_tua fasilitas_belajar
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 15 1 90.2 2 2
## 2 19 0 79.6 1 1
## 3 17 0 66.9 2 3
## 4 20 1 85.6 3 1
## 5 16 1 65.9 3 1
## 6 21 0 70.8 2 3
## 7 21 0 86.3 3 2
## 8 19 1 84.3 3 3
## 9 17 0 79.5 2 2
## 10 18 0 74.4 1 2
## 11 16 1 76.9 3 2
## 12 15 1 81.9 2 2
## 13 17 1 83.2 3 2
## 14 17 0 65.2 3 3
## 15 20 1 62 2 1
## # ℹ 6 more variables: jam_belajar_per_hari <dbl>, kehadiran_persen <dbl>,
## # minat_pada_pelajaran <dbl>, kesulitan_ekonomi <dbl>,
## # jarak_rumah_sekolah <dbl>, prediksi_dt <dbl>