Dalam tugas ini, tujuan utama kami adalah untuk mengembangkan sebuah model klasifikasi menggunakan jaringan saraf (neural network) untuk memahami faktor-faktor yang menyebabkan tingkat attrisi karyawan (employee attrition) dalam dataset yang diberikan oleh IBM data scientists. Dataset ini mencakup berbagai atribut seperti informasi pekerjaan, pendidikan, gaji bulanan, jarak dari rumah, serta status attrition (perpisahan) karyawan.
Dataset terdiri dari 1,470 rows dan 35 columns.
| Nama Kolom | Deskripsi |
|---|---|
| attrition | Apakah karyawan meninggalkan organisasi atau tidak. |
| age | Usia karyawan dalam tahun. |
| gender | Jenis kelamin karyawan. |
| business_travel | Frekuensi perjalanan bisnis karyawan. |
| daily_rate | Gaji harian karyawan. |
| department | Departemen tempat karyawan bekerja. |
| distance_from_home | Jarak dari rumah karyawan ke tempat kerja dalam mil. |
| education | Tingkat pendidikan yang dicapai oleh karyawan. |
| education_field | Bidang studi karyawan. |
| employee_count | Jumlah total karyawan dalam organisasi. |
| employee_number | Identifikasi unik untuk setiap catatan karyawan. |
| environment_satisfaction | Tingkat kepuasan karyawan dengan lingkungan kerja mereka. |
| hourly_rate | Gaji per jam karyawan. |
| job_involvement | Tingkat keterlibatan yang dibutuhkan untuk pekerjaan karyawan. |
| job_level | Tingkat posisi karyawan. |
| job_role | Peran karyawan dalam organisasi. |
| job_satisfaction | Kepuasan karyawan terhadap pekerjaan mereka. |
| marital_status | Status pernikahan karyawan. |
| monthly_income | Penghasilan bulanan karyawan. |
| monthly_rate | Tarif gaji bulanan karyawan. |
| num_companies_worked | Jumlah perusahaan tempat karyawan sebelumnya bekerja. |
| over_18 | Apakah karyawan berusia di atas 18 tahun atau tidak. |
| over_time | Apakah karyawan bekerja lembur atau tidak. |
| percent_salary_hike | Tingkat kenaikan gaji karyawan. |
| performance_rating | Penilaian kinerja karyawan. |
| relationship_satisfaction | Kepuasan karyawan terhadap hubungan mereka. |
| standard_hours | Jam kerja standar untuk karyawan. |
| stock_option_level | Tingkat opsi saham karyawan. |
| total_working_years | Total jumlah tahun yang telah bekerja karyawan. |
| training_times_last_year | Jumlah kali karyawan mengikuti pelatihan dalam setahun terakhir. |
| work_life_balance | Persepsi karyawan tentang keseimbangan antara pekerjaan dan kehidupan. |
| years_at_company | Jumlah tahun yang telah bekerja karyawan di perusahaan ini. |
| years_in_current_role | Jumlah tahun yang telah karyawan di peran saat ini. |
| years_since_last_promotion | Jumlah tahun sejak karyawan terakhir kali dipromosikan. |
| years_with_current_manager | Jumlah tahun karyawan telah bekerja dengan manajer saat ini. |
library(tidyverse)
library(tidymodels)
library(dplyr)
library(lubridate)
library(padr)
library(rsample)
library(caret)
library(keras)
library(tensorflow)
library(reticulate)
library(stringi)
library(randomForest)
library(mlbench)
library(ROSE)#> Age Attrition BusinessTravel DailyRate
#> Min. :18.00 Length:1470 Length:1470 Min. : 102.0
#> 1st Qu.:30.00 Class :character Class :character 1st Qu.: 465.0
#> Median :36.00 Mode :character Mode :character Median : 802.0
#> Mean :36.92 Mean : 802.5
#> 3rd Qu.:43.00 3rd Qu.:1157.0
#> Max. :60.00 Max. :1499.0
#> Department DistanceFromHome Education EducationField
#> Length:1470 Min. : 1.000 Min. :1.000 Length:1470
#> Class :character 1st Qu.: 2.000 1st Qu.:2.000 Class :character
#> Mode :character Median : 7.000 Median :3.000 Mode :character
#> Mean : 9.193 Mean :2.913
#> 3rd Qu.:14.000 3rd Qu.:4.000
#> Max. :29.000 Max. :5.000
#> EmployeeCount EmployeeNumber EnvironmentSatisfaction Gender
#> Min. :1 Min. : 1.0 Min. :1.000 Length:1470
#> 1st Qu.:1 1st Qu.: 491.2 1st Qu.:2.000 Class :character
#> Median :1 Median :1020.5 Median :3.000 Mode :character
#> Mean :1 Mean :1024.9 Mean :2.722
#> 3rd Qu.:1 3rd Qu.:1555.8 3rd Qu.:4.000
#> Max. :1 Max. :2068.0 Max. :4.000
#> HourlyRate JobInvolvement JobLevel JobRole
#> Min. : 30.00 Min. :1.00 Min. :1.000 Length:1470
#> 1st Qu.: 48.00 1st Qu.:2.00 1st Qu.:1.000 Class :character
#> Median : 66.00 Median :3.00 Median :2.000 Mode :character
#> Mean : 65.89 Mean :2.73 Mean :2.064
#> 3rd Qu.: 83.75 3rd Qu.:3.00 3rd Qu.:3.000
#> Max. :100.00 Max. :4.00 Max. :5.000
#> JobSatisfaction MaritalStatus MonthlyIncome MonthlyRate
#> Min. :1.000 Length:1470 Min. : 1009 Min. : 2094
#> 1st Qu.:2.000 Class :character 1st Qu.: 2911 1st Qu.: 8047
#> Median :3.000 Mode :character Median : 4919 Median :14236
#> Mean :2.729 Mean : 6503 Mean :14313
#> 3rd Qu.:4.000 3rd Qu.: 8379 3rd Qu.:20462
#> Max. :4.000 Max. :19999 Max. :26999
#> NumCompaniesWorked Over18 OverTime PercentSalaryHike
#> Min. :0.000 Length:1470 Length:1470 Min. :11.00
#> 1st Qu.:1.000 Class :character Class :character 1st Qu.:12.00
#> Median :2.000 Mode :character Mode :character Median :14.00
#> Mean :2.693 Mean :15.21
#> 3rd Qu.:4.000 3rd Qu.:18.00
#> Max. :9.000 Max. :25.00
#> PerformanceRating RelationshipSatisfaction StandardHours StockOptionLevel
#> Min. :3.000 Min. :1.000 Min. :80 Min. :0.0000
#> 1st Qu.:3.000 1st Qu.:2.000 1st Qu.:80 1st Qu.:0.0000
#> Median :3.000 Median :3.000 Median :80 Median :1.0000
#> Mean :3.154 Mean :2.712 Mean :80 Mean :0.7939
#> 3rd Qu.:3.000 3rd Qu.:4.000 3rd Qu.:80 3rd Qu.:1.0000
#> Max. :4.000 Max. :4.000 Max. :80 Max. :3.0000
#> TotalWorkingYears TrainingTimesLastYear WorkLifeBalance YearsAtCompany
#> Min. : 0.00 Min. :0.000 Min. :1.000 Min. : 0.000
#> 1st Qu.: 6.00 1st Qu.:2.000 1st Qu.:2.000 1st Qu.: 3.000
#> Median :10.00 Median :3.000 Median :3.000 Median : 5.000
#> Mean :11.28 Mean :2.799 Mean :2.761 Mean : 7.008
#> 3rd Qu.:15.00 3rd Qu.:3.000 3rd Qu.:3.000 3rd Qu.: 9.000
#> Max. :40.00 Max. :6.000 Max. :4.000 Max. :40.000
#> YearsInCurrentRole YearsSinceLastPromotion YearsWithCurrManager
#> Min. : 0.000 Min. : 0.000 Min. : 0.000
#> 1st Qu.: 2.000 1st Qu.: 0.000 1st Qu.: 2.000
#> Median : 3.000 Median : 1.000 Median : 3.000
#> Mean : 4.229 Mean : 2.188 Mean : 4.123
#> 3rd Qu.: 7.000 3rd Qu.: 3.000 3rd Qu.: 7.000
#> Max. :18.000 Max. :15.000 Max. :17.000
#> Age Attrition BusinessTravel
#> 0 0 0
#> DailyRate Department DistanceFromHome
#> 0 0 0
#> Education EducationField EmployeeCount
#> 0 0 0
#> EmployeeNumber EnvironmentSatisfaction Gender
#> 0 0 0
#> HourlyRate JobInvolvement JobLevel
#> 0 0 0
#> JobRole JobSatisfaction MaritalStatus
#> 0 0 0
#> MonthlyIncome MonthlyRate NumCompaniesWorked
#> 0 0 0
#> Over18 OverTime PercentSalaryHike
#> 0 0 0
#> PerformanceRating RelationshipSatisfaction StandardHours
#> 0 0 0
#> StockOptionLevel TotalWorkingYears TrainingTimesLastYear
#> 0 0 0
#> WorkLifeBalance YearsAtCompany YearsInCurrentRole
#> 0 0 0
#> YearsSinceLastPromotion YearsWithCurrManager
#> 0 0
Tidak terdapat missing values.
# Ubah tipe data
df <- df %>%
mutate_at(.vars = c( 'Attrition', 'WorkLifeBalance', "BusinessTravel", 'StockOptionLevel' ,'RelationshipSatisfaction', 'PerformanceRating', "Department", "EducationField", "Gender", "MaritalStatus", "Over18", 'JobSatisfaction','JobInvolvement','EnvironmentSatisfaction','Education','JobLevel', "JobRole" ), as.factor)df_clean %>%
group_by(Attrition) %>%
summarise(count = n()) %>%
ggplot(aes(x = Attrition, y = count, fill = Attrition)) +
geom_col() #> List of 97
#> $ line :List of 6
#> ..$ colour : chr "black"
#> ..$ linewidth : num 0.5
#> ..$ linetype : num 1
#> ..$ lineend : chr "butt"
#> ..$ arrow : logi FALSE
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ rect :List of 5
#> ..$ fill : chr "white"
#> ..$ colour : chr "black"
#> ..$ linewidth : num 0.5
#> ..$ linetype : num 1
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ text :List of 11
#> ..$ family : chr ""
#> ..$ face : chr "plain"
#> ..$ colour : chr "black"
#> ..$ size : num 11
#> ..$ hjust : num 0.5
#> ..$ vjust : num 0.5
#> ..$ angle : num 0
#> ..$ lineheight : num 0.9
#> ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : logi FALSE
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ title : NULL
#> $ aspect.ratio : NULL
#> $ axis.title : NULL
#> $ axis.title.x :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.title.x.top :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 0
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.title.x.bottom : NULL
#> $ axis.title.y :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 1
#> ..$ angle : num 90
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.title.y.left : NULL
#> $ axis.title.y.right :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 0
#> ..$ angle : num -90
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : chr "grey30"
#> ..$ size : 'rel' num 0.8
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.x :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.x.top :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : num 0
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.x.bottom : NULL
#> $ axis.text.y :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 1
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.text.y.left : NULL
#> $ axis.text.y.right :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 0
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ axis.ticks :List of 6
#> ..$ colour : chr "grey20"
#> ..$ linewidth : NULL
#> ..$ linetype : NULL
#> ..$ lineend : NULL
#> ..$ arrow : logi FALSE
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ axis.ticks.x : NULL
#> $ axis.ticks.x.top : NULL
#> $ axis.ticks.x.bottom : NULL
#> $ axis.ticks.y : NULL
#> $ axis.ticks.y.left : NULL
#> $ axis.ticks.y.right : NULL
#> $ axis.ticks.length : 'simpleUnit' num 2.75points
#> ..- attr(*, "unit")= int 8
#> $ axis.ticks.length.x : NULL
#> $ axis.ticks.length.x.top : NULL
#> $ axis.ticks.length.x.bottom: NULL
#> $ axis.ticks.length.y : NULL
#> $ axis.ticks.length.y.left : NULL
#> $ axis.ticks.length.y.right : NULL
#> $ axis.line :List of 6
#> ..$ colour : chr "black"
#> ..$ linewidth : 'rel' num 1
#> ..$ linetype : NULL
#> ..$ lineend : NULL
#> ..$ arrow : logi FALSE
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ axis.line.x : NULL
#> $ axis.line.x.top : NULL
#> $ axis.line.x.bottom : NULL
#> $ axis.line.y : NULL
#> $ axis.line.y.left : NULL
#> $ axis.line.y.right : NULL
#> $ legend.background :List of 5
#> ..$ fill : NULL
#> ..$ colour : logi NA
#> ..$ linewidth : NULL
#> ..$ linetype : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
#> ..- attr(*, "unit")= int 8
#> $ legend.spacing : 'simpleUnit' num 11points
#> ..- attr(*, "unit")= int 8
#> $ legend.spacing.x : NULL
#> $ legend.spacing.y : NULL
#> $ legend.key : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ legend.key.size : 'simpleUnit' num 1.2lines
#> ..- attr(*, "unit")= int 3
#> $ legend.key.height : NULL
#> $ legend.key.width : NULL
#> $ legend.text :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 0.8
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ legend.text.align : NULL
#> $ legend.title :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 0
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ legend.title.align : NULL
#> $ legend.position : chr "right"
#> $ legend.direction : NULL
#> $ legend.justification : chr "center"
#> $ legend.box : NULL
#> $ legend.box.just : NULL
#> $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
#> ..- attr(*, "unit")= int 1
#> $ legend.box.background : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ legend.box.spacing : 'simpleUnit' num 11points
#> ..- attr(*, "unit")= int 8
#> $ panel.background :List of 5
#> ..$ fill : chr "white"
#> ..$ colour : logi NA
#> ..$ linewidth : NULL
#> ..$ linetype : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ panel.border : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ panel.spacing : 'simpleUnit' num 5.5points
#> ..- attr(*, "unit")= int 8
#> $ panel.spacing.x : NULL
#> $ panel.spacing.y : NULL
#> $ panel.grid :List of 6
#> ..$ colour : chr "grey92"
#> ..$ linewidth : NULL
#> ..$ linetype : NULL
#> ..$ lineend : NULL
#> ..$ arrow : logi FALSE
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_line" "element"
#> $ panel.grid.major : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ panel.grid.minor : list()
#> ..- attr(*, "class")= chr [1:2] "element_blank" "element"
#> $ panel.grid.major.x : NULL
#> $ panel.grid.major.y : NULL
#> $ panel.grid.minor.x : NULL
#> $ panel.grid.minor.y : NULL
#> $ panel.ontop : logi FALSE
#> $ plot.background :List of 5
#> ..$ fill : NULL
#> ..$ colour : chr "white"
#> ..$ linewidth : NULL
#> ..$ linetype : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ plot.title :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 1.2
#> ..$ hjust : num 0
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.title.position : chr "panel"
#> $ plot.subtitle :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : num 0
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.caption :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 0.8
#> ..$ hjust : num 1
#> ..$ vjust : num 1
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 5.5points 0points 0points 0points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.caption.position : chr "panel"
#> $ plot.tag :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : 'rel' num 1.2
#> ..$ hjust : num 0.5
#> ..$ vjust : num 0.5
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ plot.tag.position : chr "topleft"
#> $ plot.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
#> ..- attr(*, "unit")= int 8
#> $ strip.background :List of 5
#> ..$ fill : chr "white"
#> ..$ colour : chr "black"
#> ..$ linewidth : 'rel' num 2
#> ..$ linetype : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_rect" "element"
#> $ strip.background.x : NULL
#> $ strip.background.y : NULL
#> $ strip.clip : chr "inherit"
#> $ strip.placement : chr "inside"
#> $ strip.text :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : chr "grey10"
#> ..$ size : 'rel' num 0.8
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : NULL
#> ..$ lineheight : NULL
#> ..$ margin : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
#> .. ..- attr(*, "unit")= int 8
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.text.x : NULL
#> $ strip.text.x.bottom : NULL
#> $ strip.text.x.top : NULL
#> $ strip.text.y :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : num -90
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.text.y.left :List of 11
#> ..$ family : NULL
#> ..$ face : NULL
#> ..$ colour : NULL
#> ..$ size : NULL
#> ..$ hjust : NULL
#> ..$ vjust : NULL
#> ..$ angle : num 90
#> ..$ lineheight : NULL
#> ..$ margin : NULL
#> ..$ debug : NULL
#> ..$ inherit.blank: logi TRUE
#> ..- attr(*, "class")= chr [1:2] "element_text" "element"
#> $ strip.text.y.right : NULL
#> $ strip.switch.pad.grid : 'simpleUnit' num 2.75points
#> ..- attr(*, "unit")= int 8
#> $ strip.switch.pad.wrap : 'simpleUnit' num 2.75points
#> ..- attr(*, "unit")= int 8
#> - attr(*, "class")= chr [1:2] "theme" "gg"
#> - attr(*, "complete")= logi TRUE
#> - attr(*, "validate")= logi TRUE
Proporsi kelas target dapat dikatakan tidak seimbang dimana kelas
no jauh lebih banyak dari kelas yes.
Kita akan melakukan spliting data menjadi training dan testing dengan proporsi 70:30
train_balance <- ovun.sample(Attrition ~ .,
data = train,
method = "both",
N = n)
table(train_balance$data$Attrition) %>% prop.table()#>
#> No Yes
#> 0.5018149 0.4981851
rec_obj <- recipe(Attrition ~ ., data = train_balance$data) %>%
step_center(all_numeric_predictors()) %>%
step_scale(all_numeric_predictors()) %>%
step_dummy(all_nominal_predictors()) %>%
prep(data = train_balance$data)
rec_objtrain_x <- bake(object = rec_obj, new_data = train_balance$data) %>% select(-Attrition) %>% as.matrix()
train_y <- bake(object = rec_obj, new_data = train_balance$data) %>% select(Attrition)
test_x <- bake(object = rec_obj, new_data = test) %>% select(-Attrition) %>% as.matrix()
test_y <- bake(object = rec_obj, new_data = test) %>% select(Attrition)
train_x <- array_reshape(train_x, dim(train_x))
test_x <- array_reshape(test_x, dim(test_x))
train_y <- ifelse(pull(train_y) == "No", 1, 0)
test_y <- ifelse(pull(test_y) == "No", 1, 0)
train_y <- to_categorical(train_y)
test_y <- to_categorical(test_y, num_classes = 2)
n_input <- dim(train_x)[2]Kita membuat model NN dengan activation tanh pada hiden
layer, 3 hiden layer masing-masing meiliki 256 neuron. kemudian output
layer dengan activation sigmoid dengan 2 neuron.
model <- keras_model_sequential(name = "Model1") %>%
layer_dense(activation = "tanh",
units = 256,
input_shape = n_input) %>%
layer_dense(activation = "tanh",
units = 256) %>%
layer_dense(activation = "tanh",
units = 256) %>%
layer_dense(activation = "sigmoid",
units = 2,
name = "output")model kita menggunakan ’binary_crossentropy` dan optimizer adam dengan learning rate 0.01
model %>%
compile(loss = "binary_crossentropy",
metrics = c("accuracy"),
optimizer = optimizer_adam(learning_rate = 0.01))
model#> Model: "Model1"
#> ________________________________________________________________________________
#> Layer (type) Output Shape Param #
#> ================================================================================
#> dense_2 (Dense) (None, 256) 16128
#> dense_1 (Dense) (None, 256) 65792
#> dense (Dense) (None, 256) 65792
#> output (Dense) (None, 2) 514
#> ================================================================================
#> Total params: 148,226
#> Trainable params: 148,226
#> Non-trainable params: 0
#> ________________________________________________________________________________
Untuk training model, kita memakai epoch 25 dan batch size sebesar 100
set.seed(123)
history <- model %>%
fit(x = train_x,
y = train_y,
batch_size = 100,
epoch = 50,
validation_data = list(test_x, test_y),
verbose = 1)#> Epoch 1/50
#>
#> 1/12 [=>............................] - ETA: 6s - loss: 0.7478 - accuracy: 0.4000
#> 12/12 [==============================] - 1s 14ms/step - loss: 1.0126 - accuracy: 0.5971 - val_loss: 0.8759 - val_accuracy: 0.5978
#> Epoch 2/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.7017 - accuracy: 0.7200
#> 12/12 [==============================] - 0s 5ms/step - loss: 0.6725 - accuracy: 0.6924 - val_loss: 0.4887 - val_accuracy: 0.7853
#> Epoch 3/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.6785 - accuracy: 0.6000
#> 12/12 [==============================] - 0s 5ms/step - loss: 0.6531 - accuracy: 0.6887 - val_loss: 0.4571 - val_accuracy: 0.7935
#> Epoch 4/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.6455 - accuracy: 0.6400
#> 12/12 [==============================] - 0s 5ms/step - loss: 0.5803 - accuracy: 0.7196 - val_loss: 0.7489 - val_accuracy: 0.6413
#> Epoch 5/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.5157 - accuracy: 0.7700
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.4877 - accuracy: 0.7804 - val_loss: 0.4529 - val_accuracy: 0.7826
#> Epoch 6/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.4507 - accuracy: 0.7800
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.4179 - accuracy: 0.8285 - val_loss: 0.6266 - val_accuracy: 0.6793
#> Epoch 7/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.4002 - accuracy: 0.8100
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.5344 - accuracy: 0.7296 - val_loss: 0.4897 - val_accuracy: 0.7880
#> Epoch 8/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.3659 - accuracy: 0.8600
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.3954 - accuracy: 0.8348 - val_loss: 0.4294 - val_accuracy: 0.8207
#> Epoch 9/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.3134 - accuracy: 0.8600
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.3644 - accuracy: 0.8521 - val_loss: 0.5260 - val_accuracy: 0.7826
#> Epoch 10/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.2471 - accuracy: 0.8900
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.3353 - accuracy: 0.8521 - val_loss: 0.4990 - val_accuracy: 0.8016
#> Epoch 11/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.3134 - accuracy: 0.8700
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.2655 - accuracy: 0.8866 - val_loss: 0.5121 - val_accuracy: 0.7880
#> Epoch 12/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.2074 - accuracy: 0.9300
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.2176 - accuracy: 0.9220 - val_loss: 0.5618 - val_accuracy: 0.7826
#> Epoch 13/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.1094 - accuracy: 0.9700
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.2227 - accuracy: 0.9183 - val_loss: 0.4956 - val_accuracy: 0.8288
#> Epoch 14/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.2127 - accuracy: 0.9200
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.1685 - accuracy: 0.9401 - val_loss: 0.4797 - val_accuracy: 0.8668
#> Epoch 15/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.2241 - accuracy: 0.8700
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.1345 - accuracy: 0.9492 - val_loss: 0.5211 - val_accuracy: 0.8397
#> Epoch 16/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.1100 - accuracy: 0.9600
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0831 - accuracy: 0.9764 - val_loss: 0.5669 - val_accuracy: 0.8397
#> Epoch 17/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0279 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0551 - accuracy: 0.9891 - val_loss: 0.5674 - val_accuracy: 0.8505
#> Epoch 18/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0490 - accuracy: 0.9900
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0386 - accuracy: 0.9918 - val_loss: 0.6083 - val_accuracy: 0.8342
#> Epoch 19/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0390 - accuracy: 0.9900
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0284 - accuracy: 0.9936 - val_loss: 0.5918 - val_accuracy: 0.8451
#> Epoch 20/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0134 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0208 - accuracy: 0.9946 - val_loss: 0.6518 - val_accuracy: 0.8370
#> Epoch 21/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0084 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0144 - accuracy: 0.9946 - val_loss: 0.6596 - val_accuracy: 0.8424
#> Epoch 22/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0173 - accuracy: 0.9900
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0104 - accuracy: 0.9955 - val_loss: 0.6882 - val_accuracy: 0.8424
#> Epoch 23/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0137 - accuracy: 0.9900
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0076 - accuracy: 0.9982 - val_loss: 0.7132 - val_accuracy: 0.8397
#> Epoch 24/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0083 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0053 - accuracy: 0.9991 - val_loss: 0.7057 - val_accuracy: 0.8397
#> Epoch 25/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0029 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0100 - accuracy: 0.9982 - val_loss: 0.6726 - val_accuracy: 0.8478
#> Epoch 26/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0083 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0052 - accuracy: 1.0000 - val_loss: 0.7817 - val_accuracy: 0.8370
#> Epoch 27/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0043 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0033 - accuracy: 1.0000 - val_loss: 0.7557 - val_accuracy: 0.8424
#> Epoch 28/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0043 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0022 - accuracy: 1.0000 - val_loss: 0.8035 - val_accuracy: 0.8370
#> Epoch 29/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0023 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0016 - accuracy: 1.0000 - val_loss: 0.8251 - val_accuracy: 0.8397
#> Epoch 30/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0018 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 0.0013 - accuracy: 1.0000 - val_loss: 0.8306 - val_accuracy: 0.8451
#> Epoch 31/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 8.5366e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 5ms/step - loss: 0.0011 - accuracy: 1.0000 - val_loss: 0.8392 - val_accuracy: 0.8451
#> Epoch 32/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0013 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 9.4452e-04 - accuracy: 1.0000 - val_loss: 0.8473 - val_accuracy: 0.8424
#> Epoch 33/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0012 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 8.5230e-04 - accuracy: 1.0000 - val_loss: 0.8526 - val_accuracy: 0.8451
#> Epoch 34/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 0.0012 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 7.6615e-04 - accuracy: 1.0000 - val_loss: 0.8583 - val_accuracy: 0.8424
#> Epoch 35/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 5.8145e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 7.0106e-04 - accuracy: 1.0000 - val_loss: 0.8667 - val_accuracy: 0.8424
#> Epoch 36/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 4.2869e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 6.4602e-04 - accuracy: 1.0000 - val_loss: 0.8727 - val_accuracy: 0.8424
#> Epoch 37/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 4.2686e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 5.9179e-04 - accuracy: 1.0000 - val_loss: 0.8708 - val_accuracy: 0.8397
#> Epoch 38/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 4.2937e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 5.5780e-04 - accuracy: 1.0000 - val_loss: 0.8735 - val_accuracy: 0.8424
#> Epoch 39/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 3.6009e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 5.0836e-04 - accuracy: 1.0000 - val_loss: 0.8795 - val_accuracy: 0.8424
#> Epoch 40/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 3.8060e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 4.7062e-04 - accuracy: 1.0000 - val_loss: 0.8916 - val_accuracy: 0.8424
#> Epoch 41/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 4.8769e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 4.3816e-04 - accuracy: 1.0000 - val_loss: 0.8960 - val_accuracy: 0.8424
#> Epoch 42/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 3.7951e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 4.0972e-04 - accuracy: 1.0000 - val_loss: 0.8958 - val_accuracy: 0.8397
#> Epoch 43/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.7069e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 3.8429e-04 - accuracy: 1.0000 - val_loss: 0.9038 - val_accuracy: 0.8397
#> Epoch 44/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.7276e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 3.6219e-04 - accuracy: 1.0000 - val_loss: 0.9116 - val_accuracy: 0.8397
#> Epoch 45/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.9941e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 3.4306e-04 - accuracy: 1.0000 - val_loss: 0.9240 - val_accuracy: 0.8451
#> Epoch 46/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.6450e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 3.2587e-04 - accuracy: 1.0000 - val_loss: 0.9287 - val_accuracy: 0.8451
#> Epoch 47/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.7121e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 3.0972e-04 - accuracy: 1.0000 - val_loss: 0.9310 - val_accuracy: 0.8451
#> Epoch 48/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.1698e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 5ms/step - loss: 2.9441e-04 - accuracy: 1.0000 - val_loss: 0.9338 - val_accuracy: 0.8451
#> Epoch 49/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 2.6312e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 2.8148e-04 - accuracy: 1.0000 - val_loss: 0.9360 - val_accuracy: 0.8451
#> Epoch 50/50
#>
#> 1/12 [=>............................] - ETA: 0s - loss: 3.2559e-04 - accuracy: 1.0000
#> 12/12 [==============================] - 0s 4ms/step - loss: 2.6861e-04 - accuracy: 1.0000 - val_loss: 0.9412 - val_accuracy: 0.8451
## Evaluation
#> 12/12 - 0s - 66ms/epoch - 5ms/step
preidct_test <- ifelse(preidct_test == 1, "No", "Yes")
confusionMatrix(as.factor(preidct_test), test$Attrition)#> Confusion Matrix and Statistics
#>
#> Reference
#> Prediction No Yes
#> No 268 23
#> Yes 34 43
#>
#> Accuracy : 0.8451
#> 95% CI : (0.804, 0.8805)
#> No Information Rate : 0.8207
#> P-Value [Acc > NIR] : 0.1229
#>
#> Kappa : 0.506
#>
#> Mcnemar's Test P-Value : 0.1853
#>
#> Sensitivity : 0.8874
#> Specificity : 0.6515
#> Pos Pred Value : 0.9210
#> Neg Pred Value : 0.5584
#> Prevalence : 0.8207
#> Detection Rate : 0.7283
#> Detection Prevalence : 0.7908
#> Balanced Accuracy : 0.7695
#>
#> 'Positive' Class : No
#>
Percobaan ini bertujuan untuk membantu mengembangkan sebuah model klasifikasi menggunakan jaringan saraf (neural network) untuk memahami faktor-faktor yang menyebabkan tingkat attrisi karyawan (employee attrition). Berdasarkan hasil percobaan, didapatkan bahwa model NN memiliki akurasi 83.97%. namun model lemah dalam memprediksi nilai negatif dengan benar (Specificity : 70.97%). Untuk percobaan kedepan dapat menggunakan PCA (Principal Componen Analysis) atau melakukan feature engginering untuk meningkatkan performa model.