Employee Attrition Prediction using R language
Machine Learning Project
Introducing a machine learning project centered on employee attrition prediction using the Python programming language. Employees are the backbone of any organization, directly impacting its success or failure. Consequently, when trained, skilled, and experienced employees depart for better opportunities, it poses significant challenges for the organization to address. To get the current working directory, we use the below command using r language.
Employee attrition is downsizing in any organization where employees resign. Employees are valuable assets of any organization. It is necessary to know whether the employees are dissatisfied or whether there are other reasons for leaving their respective jobs.
Nowadays, for better opportunities, employees are eager to move from one organization to another. But if they quit their jobs unexpectedly, it can result in a huge loss for the organization. A new hire will consume money and time, and newly hired employees will also take time to make the respective organization profitable.
Retaining skilled and hardworking employees is one of the most critical challenges many organizations face. Therefore, by improving employee satisfaction and providing a desirable working environment, we can certainly reduce this problem significantly.
In this section, I will take you through a Machine Learning project on predicting Employee Attrition prediction with R programming language. I will start this task by importing the necessary R libraries that we need for this task:
current_directory <- getwd()
We can import the required packages and modules,Here..
if (!require(“ggplot2”)) install.packages(“ggplot2”)
if (!require(“gridExtra”)) install.packages(“gridExtra”)
if (!require(“psych”)) install.packages(“psych”)
if (!require(“stats”)) install.packages(“stats”)
if (!require(“caTools”)) install.packages(“caTools”)
if (!require(“randomForest”)) install.packages(“randomForest”)
if (!require(“ggcorrplot”)) install.packages(“ggcorrplot”)
if (!require(“caret”)) install.packages(“caret”)
if (!require(“gam”)) install.packages(“gam”)
library(ggplot2)
library(gridExtra)
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(stats)
library(caTools)
library(ggcorrplot)
library(caret)
## Loading required package: lattice
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:psych':
##
## outlier
## The following object is masked from 'package:gridExtra':
##
## combine
## The following object is masked from 'package:ggplot2':
##
## margin
library(gam)
## Loading required package: splines
## Loading required package: foreach
## Loaded gam 1.22-3
Importing HR Employee Attrition data in to variable ‘data’.
data <- read.csv("HR-Employee-Attrition.csv")
preview of data
head(data)
## Age Attrition BusinessTravel DailyRate Department
## 1 41 Yes Travel_Rarely 1102 Sales
## 2 49 No Travel_Frequently 279 Research & Development
## 3 37 Yes Travel_Rarely 1373 Research & Development
## 4 33 No Travel_Frequently 1392 Research & Development
## 5 27 No Travel_Rarely 591 Research & Development
## 6 32 No Travel_Frequently 1005 Research & Development
## DistanceFromHome Education EducationField EmployeeCount EmployeeNumber
## 1 1 2 Life Sciences 1 1
## 2 8 1 Life Sciences 1 2
## 3 2 2 Other 1 4
## 4 3 4 Life Sciences 1 5
## 5 2 1 Medical 1 7
## 6 2 2 Life Sciences 1 8
## EnvironmentSatisfaction Gender HourlyRate JobInvolvement JobLevel
## 1 2 Female 94 3 2
## 2 3 Male 61 2 2
## 3 4 Male 92 2 1
## 4 4 Female 56 3 1
## 5 1 Male 40 3 1
## 6 4 Male 79 3 1
## JobRole JobSatisfaction MaritalStatus MonthlyIncome MonthlyRate
## 1 Sales Executive 4 Single 5993 19479
## 2 Research Scientist 2 Married 5130 24907
## 3 Laboratory Technician 3 Single 2090 2396
## 4 Research Scientist 3 Married 2909 23159
## 5 Laboratory Technician 2 Married 3468 16632
## 6 Laboratory Technician 4 Single 3068 11864
## NumCompaniesWorked Over18 OverTime PercentSalaryHike PerformanceRating
## 1 8 Y Yes 11 3
## 2 1 Y No 23 4
## 3 6 Y Yes 15 3
## 4 1 Y Yes 11 3
## 5 9 Y No 12 3
## 6 0 Y No 13 3
## RelationshipSatisfaction StandardHours StockOptionLevel TotalWorkingYears
## 1 1 80 0 8
## 2 4 80 1 10
## 3 2 80 0 7
## 4 3 80 0 8
## 5 4 80 1 6
## 6 3 80 0 8
## TrainingTimesLastYear WorkLifeBalance YearsAtCompany YearsInCurrentRole
## 1 0 1 6 4
## 2 3 3 10 7
## 3 3 3 0 0
## 4 3 3 8 7
## 5 3 3 2 2
## 6 2 2 7 7
## YearsSinceLastPromotion YearsWithCurrManager
## 1 0 5
## 2 1 7
## 3 0 0
## 4 3 0
## 5 2 2
## 6 3 6
lets see the dimensions of our data.
dimensions=dim(data)
print(dimensions)
## [1] 1470 35
we can get the number of columns using the command
ncol()
number_of_columns=ncol(data)
cat("\nNumber of columns=",number_of_columns)
##
## Number of columns= 35
similarly we can see the number of rows in the data using
nrow(data)
number_of_rows=nrow(data)
cat("\nNumber of rows=",number_of_rows)
##
## Number of rows= 1470
lets see, how is the Attrition outcomes are distributed.
attributes_count=table(data$Attrition)
print(attributes_count)
##
## No Yes
## 1233 237
# Add data labels on top of each bar
text(
x=barplot(attributes_count,names.arg = c("No","Yes"), xlab = "Attrition Values",
ylab = "Number of people", col = c("red","green"),
main = "Distribution of Attrition values",
ylim = c(0, max(attributes_count) * 1.2)),
y = attributes_count + 1, labels = attributes_count, pos = 3, cex = 1.2, col = "black"
)
To see the nature of the data
describe(data)
## vars n mean sd median trimmed mad
## Age 1 1470 36.92 9.14 36.0 36.47 8.90
## Attrition* 2 1470 1.16 0.37 1.0 1.08 0.00
## BusinessTravel* 3 1470 2.61 0.67 3.0 2.76 0.00
## DailyRate 4 1470 802.49 403.51 802.0 803.83 510.01
## Department* 5 1470 2.26 0.53 2.0 2.25 0.00
## DistanceFromHome 6 1470 9.19 8.11 7.0 8.08 7.41
## Education 7 1470 2.91 1.02 3.0 2.98 1.48
## EducationField* 8 1470 3.25 1.33 3.0 3.10 1.48
## EmployeeCount 9 1470 1.00 0.00 1.0 1.00 0.00
## EmployeeNumber 10 1470 1024.87 602.02 1020.5 1023.40 790.97
## EnvironmentSatisfaction 11 1470 2.72 1.09 3.0 2.78 1.48
## Gender* 12 1470 1.60 0.49 2.0 1.62 0.00
## HourlyRate 13 1470 65.89 20.33 66.0 66.02 26.69
## JobInvolvement 14 1470 2.73 0.71 3.0 2.74 0.00
## JobLevel 15 1470 2.06 1.11 2.0 1.90 1.48
## JobRole* 16 1470 5.46 2.46 6.0 5.61 2.97
## JobSatisfaction 17 1470 2.73 1.10 3.0 2.79 1.48
## MaritalStatus* 18 1470 2.10 0.73 2.0 2.12 1.48
## MonthlyIncome 19 1470 6502.93 4707.96 4919.0 5667.24 3260.24
## MonthlyRate 20 1470 14313.10 7117.79 14235.5 14286.48 9201.76
## NumCompaniesWorked 21 1470 2.69 2.50 2.0 2.36 1.48
## Over18* 22 1470 1.00 0.00 1.0 1.00 0.00
## OverTime* 23 1470 1.28 0.45 1.0 1.23 0.00
## PercentSalaryHike 24 1470 15.21 3.66 14.0 14.80 2.97
## PerformanceRating 25 1470 3.15 0.36 3.0 3.07 0.00
## RelationshipSatisfaction 26 1470 2.71 1.08 3.0 2.77 1.48
## StandardHours 27 1470 80.00 0.00 80.0 80.00 0.00
## StockOptionLevel 28 1470 0.79 0.85 1.0 0.67 1.48
## TotalWorkingYears 29 1470 11.28 7.78 10.0 10.37 5.93
## TrainingTimesLastYear 30 1470 2.80 1.29 3.0 2.72 1.48
## WorkLifeBalance 31 1470 2.76 0.71 3.0 2.77 0.00
## YearsAtCompany 32 1470 7.01 6.13 5.0 5.99 4.45
## YearsInCurrentRole 33 1470 4.23 3.62 3.0 3.85 4.45
## YearsSinceLastPromotion 34 1470 2.19 3.22 1.0 1.48 1.48
## YearsWithCurrManager 35 1470 4.12 3.57 3.0 3.77 4.45
## min max range skew kurtosis se
## Age 18 60 42 0.41 -0.41 0.24
## Attrition* 1 2 1 1.84 1.39 0.01
## BusinessTravel* 1 3 2 -1.44 0.69 0.02
## DailyRate 102 1499 1397 0.00 -1.21 10.52
## Department* 1 3 2 0.17 -0.40 0.01
## DistanceFromHome 1 29 28 0.96 -0.23 0.21
## Education 1 5 4 -0.29 -0.56 0.03
## EducationField* 1 6 5 0.55 -0.69 0.03
## EmployeeCount 1 1 0 NaN NaN 0.00
## EmployeeNumber 1 2068 2067 0.02 -1.23 15.70
## EnvironmentSatisfaction 1 4 3 -0.32 -1.20 0.03
## Gender* 1 2 1 -0.41 -1.83 0.01
## HourlyRate 30 100 70 -0.03 -1.20 0.53
## JobInvolvement 1 4 3 -0.50 0.26 0.02
## JobLevel 1 5 4 1.02 0.39 0.03
## JobRole* 1 9 8 -0.36 -1.20 0.06
## JobSatisfaction 1 4 3 -0.33 -1.22 0.03
## MaritalStatus* 1 3 2 -0.15 -1.12 0.02
## MonthlyIncome 1009 19999 18990 1.37 0.99 122.79
## MonthlyRate 2094 26999 24905 0.02 -1.22 185.65
## NumCompaniesWorked 0 9 9 1.02 0.00 0.07
## Over18* 1 1 0 NaN NaN 0.00
## OverTime* 1 2 1 0.96 -1.07 0.01
## PercentSalaryHike 11 25 14 0.82 -0.31 0.10
## PerformanceRating 3 4 1 1.92 1.68 0.01
## RelationshipSatisfaction 1 4 3 -0.30 -1.19 0.03
## StandardHours 80 80 0 NaN NaN 0.00
## StockOptionLevel 0 3 3 0.97 0.35 0.02
## TotalWorkingYears 0 40 40 1.11 0.91 0.20
## TrainingTimesLastYear 0 6 6 0.55 0.48 0.03
## WorkLifeBalance 1 4 3 -0.55 0.41 0.02
## YearsAtCompany 0 40 40 1.76 3.91 0.16
## YearsInCurrentRole 0 18 18 0.92 0.47 0.09
## YearsSinceLastPromotion 0 15 15 1.98 3.59 0.08
## YearsWithCurrManager 0 17 17 0.83 0.16 0.09
#Unique values of every feature
char_cols=sapply(data,is.character)
unique_vals=lapply(data[,char_cols],unique)
for (i in seq_along(unique_vals)){
cat("unique values of feature: ", names(unique_vals)[i],"\n")
print(unique_vals[i])
}
## unique values of feature: Attrition
## $Attrition
## [1] "Yes" "No"
##
## unique values of feature: BusinessTravel
## $BusinessTravel
## [1] "Travel_Rarely" "Travel_Frequently" "Non-Travel"
##
## unique values of feature: Department
## $Department
## [1] "Sales" "Research & Development" "Human Resources"
##
## unique values of feature: EducationField
## $EducationField
## [1] "Life Sciences" "Other" "Medical" "Marketing"
## [5] "Technical Degree" "Human Resources"
##
## unique values of feature: Gender
## $Gender
## [1] "Female" "Male"
##
## unique values of feature: JobRole
## $JobRole
## [1] "Sales Executive" "Research Scientist"
## [3] "Laboratory Technician" "Manufacturing Director"
## [5] "Healthcare Representative" "Manager"
## [7] "Sales Representative" "Research Director"
## [9] "Human Resources"
##
## unique values of feature: MaritalStatus
## $MaritalStatus
## [1] "Single" "Married" "Divorced"
##
## unique values of feature: Over18
## $Over18
## [1] "Y"
##
## unique values of feature: OverTime
## $OverTime
## [1] "Yes" "No"
# Create the plot
p <- ggplot(data, aes(x = Age, y = factor(Attrition))) +
geom_violin() +
labs(x = "Age", y = "Attrition") +
ggtitle("Attrition vs Age") +
theme_minimal()
# Add rectangular region to highlight
p + annotate("rect", xmin = 25, xmax = 35, ymin = 1.5, ymax = 2.5, fill = "red", alpha = 0.3)+annotate("rect", xmin = 25, xmax = 45, ymin = 0.5, ymax = 1.5, fill = "lightgreen", alpha = 0.3)
ggplot(data, aes(x = Attrition, y = factor(BusinessTravel))) +
geom_violin() +
labs(y = "BusinessTravel", x = "Attrition") +
ggtitle("Attrition vs BusinessTravel") +
theme_minimal()
ggplot(data, aes(x = EnvironmentSatisfaction, y = factor(Attrition))) +
geom_violin() +
labs(y = "BusinessTravel", x = "Attrition") +
ggtitle("Attrition vs BusinessTravel") +
theme_minimal()
ggplot(data, aes(x = EnvironmentSatisfaction, fill = Attrition )) +
geom_bar(position = "stack") +
labs(x = "Environment Satisfaction", y = "Count") +
ggtitle("Stacked Bar Plot of Attrition vs EnvironmentSatisfaction") +
theme_minimal()
Education Field
ggplot(data, aes(x = Attrition, fill = EducationField)) +
geom_bar(position = "stack") +
labs(x = "Attrition", y = "Count") +
ggtitle("Stacked Bar Plot of Attrition vs Department") +
theme_minimal()
Daily Rate
drp<-ggplot(data, aes(x = DailyRate, y = factor(Attrition))) +
geom_violin() +
labs(x = "DailyRate", y = "Attrition") +
ggtitle("Attrition vs DailyRate") +
theme_minimal()
# Add rectangular region to highlight
drp + annotate("rect", xmin = 150, xmax = 600, ymin = 1.5, ymax = 2.5, fill = "red", alpha = 0.3)+annotate("rect", xmin = 750, xmax = 1500, ymin = 0.5, ymax = 1.5, fill = "lightgreen", alpha = 0.3)
ggplot(data, aes(x = factor(Attrition), y = DailyRate, color = factor(Attrition))) +
geom_jitter(position = position_jitter(width = 0.2), alpha = 0.7) +
labs(x = "Attrition", y = "DailyRate") +
ggtitle("Attrition vs DailyRate") +
theme_minimal()
ggplot(data, aes(x = Attrition, fill = Department)) +
geom_bar(position = "stack") +
labs(x = "Attrition", y = "Count") +
ggtitle("Stacked Bar Plot of Attrition vs Department") +
theme_minimal()
Distance From Home
ggplot(data, aes(x = DistanceFromHome, y = factor(Attrition))) +
geom_violin() +
labs(x = "DistanceFromHome", y = "Attrition") +
ggtitle("Attrition vs Distance From Home") +
theme_minimal()
ggplot(data, aes(x = Attrition, fill = Gender)) +
geom_bar(position = "dodge", width = 0.7) +
labs(x = "Attrition", y = "Count") +
ggtitle("Grouped Bar Plot of Attrition vs Gender") +
theme_minimal()
Now lets see the data types of different columns
str(data)
## 'data.frame': 1470 obs. of 35 variables:
## $ Age : int 41 49 37 33 27 32 59 30 38 36 ...
## $ Attrition : chr "Yes" "No" "Yes" "No" ...
## $ BusinessTravel : chr "Travel_Rarely" "Travel_Frequently" "Travel_Rarely" "Travel_Frequently" ...
## $ DailyRate : int 1102 279 1373 1392 591 1005 1324 1358 216 1299 ...
## $ Department : chr "Sales" "Research & Development" "Research & Development" "Research & Development" ...
## $ DistanceFromHome : int 1 8 2 3 2 2 3 24 23 27 ...
## $ Education : int 2 1 2 4 1 2 3 1 3 3 ...
## $ EducationField : chr "Life Sciences" "Life Sciences" "Other" "Life Sciences" ...
## $ EmployeeCount : int 1 1 1 1 1 1 1 1 1 1 ...
## $ EmployeeNumber : int 1 2 4 5 7 8 10 11 12 13 ...
## $ EnvironmentSatisfaction : int 2 3 4 4 1 4 3 4 4 3 ...
## $ Gender : chr "Female" "Male" "Male" "Female" ...
## $ HourlyRate : int 94 61 92 56 40 79 81 67 44 94 ...
## $ JobInvolvement : int 3 2 2 3 3 3 4 3 2 3 ...
## $ JobLevel : int 2 2 1 1 1 1 1 1 3 2 ...
## $ JobRole : chr "Sales Executive" "Research Scientist" "Laboratory Technician" "Research Scientist" ...
## $ JobSatisfaction : int 4 2 3 3 2 4 1 3 3 3 ...
## $ MaritalStatus : chr "Single" "Married" "Single" "Married" ...
## $ MonthlyIncome : int 5993 5130 2090 2909 3468 3068 2670 2693 9526 5237 ...
## $ MonthlyRate : int 19479 24907 2396 23159 16632 11864 9964 13335 8787 16577 ...
## $ NumCompaniesWorked : int 8 1 6 1 9 0 4 1 0 6 ...
## $ Over18 : chr "Y" "Y" "Y" "Y" ...
## $ OverTime : chr "Yes" "No" "Yes" "Yes" ...
## $ PercentSalaryHike : int 11 23 15 11 12 13 20 22 21 13 ...
## $ PerformanceRating : int 3 4 3 3 3 3 4 4 4 3 ...
## $ RelationshipSatisfaction: int 1 4 2 3 4 3 1 2 2 2 ...
## $ StandardHours : int 80 80 80 80 80 80 80 80 80 80 ...
## $ StockOptionLevel : int 0 1 0 0 1 0 3 1 0 2 ...
## $ TotalWorkingYears : int 8 10 7 8 6 8 12 1 10 17 ...
## $ TrainingTimesLastYear : int 0 3 3 3 3 2 3 2 2 3 ...
## $ WorkLifeBalance : int 1 3 3 3 3 2 2 3 3 2 ...
## $ YearsAtCompany : int 6 10 0 8 2 7 1 1 9 7 ...
## $ YearsInCurrentRole : int 4 7 0 7 2 7 0 0 7 7 ...
## $ YearsSinceLastPromotion : int 0 1 0 3 2 3 0 0 1 7 ...
## $ YearsWithCurrManager : int 5 7 0 0 2 6 0 0 8 7 ...
Now lets summarize the data.
summary(data)
## 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
From the data we have numerical columns and categorical columns. so, We will be writing a function which classifies the data into Categorical and Non Categorical columns.
char_cols=sapply(data,is.character)
char_cols
## Age Attrition BusinessTravel
## FALSE TRUE TRUE
## DailyRate Department DistanceFromHome
## FALSE TRUE FALSE
## Education EducationField EmployeeCount
## FALSE TRUE FALSE
## EmployeeNumber EnvironmentSatisfaction Gender
## FALSE FALSE TRUE
## HourlyRate JobInvolvement JobLevel
## FALSE FALSE FALSE
## JobRole JobSatisfaction MaritalStatus
## TRUE FALSE TRUE
## MonthlyIncome MonthlyRate NumCompaniesWorked
## FALSE FALSE FALSE
## Over18 OverTime PercentSalaryHike
## TRUE TRUE FALSE
## PerformanceRating RelationshipSatisfaction StandardHours
## FALSE FALSE FALSE
## StockOptionLevel TotalWorkingYears TrainingTimesLastYear
## FALSE FALSE FALSE
## WorkLifeBalance YearsAtCompany YearsInCurrentRole
## FALSE FALSE FALSE
## YearsSinceLastPromotion YearsWithCurrManager
## FALSE FALSE
char_col_names=names(char_cols[char_cols])
char_col_names
## [1] "Attrition" "BusinessTravel" "Department" "EducationField"
## [5] "Gender" "JobRole" "MaritalStatus" "Over18"
## [9] "OverTime"
#converting categorical binary values into numerical binary values
data$Attrition=ifelse(data$Attrition=="Yes",1,0)
unique(data$Attrition)
## [1] 1 0
data$OverTime=ifelse(data$OverTime=="Yes",1,0)
unique(data$OverTime)
## [1] 1 0
data$Gender=ifelse(data$Gender=="Male",1,0)
unique(data$Gender)
## [1] 0 1
sapply(data,class)
## Age Attrition BusinessTravel
## "integer" "numeric" "character"
## DailyRate Department DistanceFromHome
## "integer" "character" "integer"
## Education EducationField EmployeeCount
## "integer" "character" "integer"
## EmployeeNumber EnvironmentSatisfaction Gender
## "integer" "integer" "numeric"
## HourlyRate JobInvolvement JobLevel
## "integer" "integer" "integer"
## JobRole JobSatisfaction MaritalStatus
## "character" "integer" "character"
## MonthlyIncome MonthlyRate NumCompaniesWorked
## "integer" "integer" "integer"
## Over18 OverTime PercentSalaryHike
## "character" "numeric" "integer"
## PerformanceRating RelationshipSatisfaction StandardHours
## "integer" "integer" "integer"
## StockOptionLevel TotalWorkingYears TrainingTimesLastYear
## "integer" "integer" "integer"
## WorkLifeBalance YearsAtCompany YearsInCurrentRole
## "integer" "integer" "integer"
## YearsSinceLastPromotion YearsWithCurrManager
## "integer" "integer"
#categorical variables
categorical_vars <- c("BusinessTravel", "Department",
"EducationField", "Gender", "JobRole",
"MaritalStatus", "OverTime")
#F-test to know the co-relation between categorical variables and target variable
for(var in categorical_vars) {
cat("Fisher test for", var, ":\n")
result=fisher.test(table(data$Attrition, data[[var]]),simulate.p.value = TRUE)
print(result)
}
## Fisher test for BusinessTravel :
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.0004998
## alternative hypothesis: two.sided
##
## Fisher test for Department :
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.006497
## alternative hypothesis: two.sided
##
## Fisher test for EducationField :
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.009495
## alternative hypothesis: two.sided
##
## Fisher test for Gender :
##
## Fisher's Exact Test for Count Data
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.2778
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.8775556 1.5933584
## sample estimates:
## odds ratio
## 1.179913
##
## Fisher test for JobRole :
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.0004998
## alternative hypothesis: two.sided
##
## Fisher test for MaritalStatus :
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(data$Attrition, data[[var]])
## p-value = 0.0004998
## alternative hypothesis: two.sided
##
## Fisher test for OverTime :
##
## Fisher's Exact Test for Count Data
##
## data: table(data$Attrition, data[[var]])
## p-value < 2.2e-16
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 2.799096 5.078460
## sample estimates:
## odds ratio
## 3.767353
#continous variaables
continuous_vars <- c("Age", "DailyRate","DistanceFromHome","Education","EnvironmentSatisfaction",
"HourlyRate","JobInvolvement","JobLevel","JobSatisfaction","NumCompaniesWorked",
"MonthlyIncome", "MonthlyRate", "PercentSalaryHike","PerformanceRating",
"RelationshipSatisfaction","StockOptionLevel",
"TotalWorkingYears","TrainingTimesLastYear","WorkLifeBalance",
"YearsAtCompany", "YearsInCurrentRole", "YearsSinceLastPromotion",
"YearsWithCurrManager")
#T-test to know the relation between continous variables and target variable
for(var in continuous_vars) {
cat("T-test for", var, ":\n")
res=t.test(data[data$Attrition == 1, var], data[data$Attrition == 0, var])
print(res)
}
## T-test for Age :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -5.828, df = 316.93, p-value = 1.38e-08
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -5.288346 -2.618930
## sample estimates:
## mean of x mean of y
## 33.60759 37.56123
##
## T-test for DailyRate :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -2.1789, df = 333.76, p-value = 0.03004
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -118.243100 -6.040083
## sample estimates:
## mean of x mean of y
## 750.3629 812.5045
##
## T-test for DistanceFromHome :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = 2.8882, df = 322.72, p-value = 0.004137
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.5475146 2.8870025
## sample estimates:
## mean of x mean of y
## 10.632911 8.915653
##
## T-test for Education :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -1.2177, df = 336.95, p-value = 0.2242
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.22843290 0.05374319
## sample estimates:
## mean of x mean of y
## 2.839662 2.927007
##
## T-test for EnvironmentSatisfaction :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -3.7513, df = 316.62, p-value = 0.0002092
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.468253 -0.146056
## sample estimates:
## mean of x mean of y
## 2.464135 2.771290
##
## T-test for HourlyRate :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -0.26477, df = 335.98, p-value = 0.7914
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.188891 2.432272
## sample estimates:
## mean of x mean of y
## 65.57384 65.95215
##
## T-test for JobInvolvement :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -4.6602, df = 312.81, p-value = 4.681e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.3576727 -0.1453097
## sample estimates:
## mean of x mean of y
## 2.518987 2.770479
##
## T-test for JobLevel :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -7.3859, df = 376.25, p-value = 9.845e-13
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6443231 -0.3733861
## sample estimates:
## mean of x mean of y
## 1.637131 2.145985
##
## T-test for JobSatisfaction :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -3.9261, df = 328.59, p-value = 0.0001052
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.4656797 -0.1547890
## sample estimates:
## mean of x mean of y
## 2.468354 2.778589
##
## T-test for NumCompaniesWorked :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = 1.5747, df = 317.14, p-value = 0.1163
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.07367926 0.66437603
## sample estimates:
## mean of x mean of y
## 2.940928 2.645580
##
## T-test for MonthlyIncome :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -7.4826, df = 412.74, p-value = 4.434e-13
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2583.050 -1508.244
## sample estimates:
## mean of x mean of y
## 4787.093 6832.740
##
## T-test for MonthlyRate :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = 0.5755, df = 330.1, p-value = 0.5653
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -709.8084 1296.8656
## sample estimates:
## mean of x mean of y
## 14559.31 14265.78
##
## T-test for PercentSalaryHike :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -0.50424, df = 326.11, p-value = 0.6144
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6572652 0.3890709
## sample estimates:
## mean of x mean of y
## 15.09705 15.23114
##
## T-test for PerformanceRating :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = 0.10999, df = 331.22, p-value = 0.9125
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.04784086 0.05350780
## sample estimates:
## mean of x mean of y
## 3.156118 3.153285
##
## T-test for RelationshipSatisfaction :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -1.7019, df = 323.54, p-value = 0.08973
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.29067575 0.02102367
## sample estimates:
## mean of x mean of y
## 2.599156 2.733982
##
## T-test for StockOptionLevel :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -5.2442, df = 329.67, p-value = 2.812e-07
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.4368288 -0.1985054
## sample estimates:
## mean of x mean of y
## 0.5274262 0.8450933
##
## T-test for TotalWorkingYears :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -7.0192, df = 350.88, p-value = 1.16e-11
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -4.632019 -2.604401
## sample estimates:
## mean of x mean of y
## 8.244726 11.862936
##
## T-test for TrainingTimesLastYear :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -2.3305, df = 339.56, p-value = 0.02036
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.38439273 -0.03251776
## sample estimates:
## mean of x mean of y
## 2.624473 2.832928
##
## T-test for WorkLifeBalance :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -2.1742, df = 302.49, p-value = 0.03047
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.23393357 -0.01165453
## sample estimates:
## mean of x mean of y
## 2.658228 2.781022
##
## T-test for YearsAtCompany :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -5.2826, df = 338.21, p-value = 2.286e-07
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.071629 -1.404805
## sample estimates:
## mean of x mean of y
## 5.130802 7.369019
##
## T-test for YearsInCurrentRole :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -6.8471, df = 366.57, p-value = 3.187e-11
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.035355 -1.127107
## sample estimates:
## mean of x mean of y
## 2.902954 4.484185
##
## T-test for YearsSinceLastPromotion :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -1.2879, df = 338.49, p-value = 0.1987
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.7309843 0.1525043
## sample estimates:
## mean of x mean of y
## 1.945148 2.234388
##
## T-test for YearsWithCurrManager :
##
## Welch Two Sample t-test
##
## data: data[data$Attrition == 1, var] and data[data$Attrition == 0, var]
## t = -6.6334, df = 365.1, p-value = 1.185e-10
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -1.964223 -1.065929
## sample estimates:
## mean of x mean of y
## 2.852321 4.367397
Results of the tests for the categorical and numerical columns :
Finally, ended up with the following features after feature selection
req_data <- subset(data, select = -c(Gender, Education,EmployeeCount, StandardHours, Over18, HourlyRate, NumCompaniesWorked, MonthlyRate, PercentSalaryHike, EmployeeNumber, PerformanceRating,
RelationshipSatisfaction, YearsSinceLastPromotion))
names(req_data)
## [1] "Age" "Attrition"
## [3] "BusinessTravel" "DailyRate"
## [5] "Department" "DistanceFromHome"
## [7] "EducationField" "EnvironmentSatisfaction"
## [9] "JobInvolvement" "JobLevel"
## [11] "JobRole" "JobSatisfaction"
## [13] "MaritalStatus" "MonthlyIncome"
## [15] "OverTime" "StockOptionLevel"
## [17] "TotalWorkingYears" "TrainingTimesLastYear"
## [19] "WorkLifeBalance" "YearsAtCompany"
## [21] "YearsInCurrentRole" "YearsWithCurrManager"
dim(req_data)
## [1] 1470 22
Our data is not good to proceed because as we know that out data has yes:no=1:5 so it is not good to go with the data here we can upscale the diminishing feature of the output by up sampling methods like
1) Upsampling Minority Class
2) Downsampling Majority Class
3) Generate Synthetic Data
4) Combine Upsampling & Downsampling Techniques
5) Balanced Class Weight
We used Upsampling Minority Class method,which is better fit for our data.
#Random Forest-model
# Find indices of rows with 1s in Attrition
indices_1 <- which(req_data$Attrition == 1)
# Get the rows with 1s
rows_with_1s <- req_data[indices_1, ]
1233/237
## [1] 5.202532
# Duplicate rows with 1s
duplicated_rows <- rows_with_1s[rep(1:nrow(rows_with_1s), times =4 ), ]
# Combine original data with duplicated rows
req_data1 <- rbind(req_data, duplicated_rows)
# Shuffle the rows
req_data1 <- req_data1[sample(nrow(req_data1)), ]
# Reset row names
rownames(req_data1) <- NULL
Check the counts after augmentation.
attrition_counts <- table(req_data1$Attrition)
print(attrition_counts)
##
## 0 1
## 1233 1185
library('ggcorrplot')
# Assuming your correlation matrix is stored in 'correlation_matrix'
# You can compute it using the cor() function on your data frame
# Example correlation matrix
corr<- round(cor(data[,continuous_vars]),2) # Assuming you have the iris dataset loaded
# Compute a matrix of correlation p-values
p.mat <- cor_pmat(data[,continuous_vars])
#p.mat
# Leave blank on no significant coefficient
ggcorrplot(corr,
p.mat = p.mat, hc.order = TRUE,tl.srt=90,tl.cex=8,
type = "lower", insig = "blank"
)
split=sample.split(req_data1,SplitRatio = 0.80)
dim(split)
## NULL
print(split)
## [1] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE
## [13] TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE
train=subset(req_data1,split=="TRUE")
names(train)
## [1] "Age" "Attrition"
## [3] "BusinessTravel" "DailyRate"
## [5] "Department" "DistanceFromHome"
## [7] "EducationField" "EnvironmentSatisfaction"
## [9] "JobInvolvement" "JobLevel"
## [11] "JobRole" "JobSatisfaction"
## [13] "MaritalStatus" "MonthlyIncome"
## [15] "OverTime" "StockOptionLevel"
## [17] "TotalWorkingYears" "TrainingTimesLastYear"
## [19] "WorkLifeBalance" "YearsAtCompany"
## [21] "YearsInCurrentRole" "YearsWithCurrManager"
test=subset(req_data1,split=="FALSE")
names(test)
## [1] "Age" "Attrition"
## [3] "BusinessTravel" "DailyRate"
## [5] "Department" "DistanceFromHome"
## [7] "EducationField" "EnvironmentSatisfaction"
## [9] "JobInvolvement" "JobLevel"
## [11] "JobRole" "JobSatisfaction"
## [13] "MaritalStatus" "MonthlyIncome"
## [15] "OverTime" "StockOptionLevel"
## [17] "TotalWorkingYears" "TrainingTimesLastYear"
## [19] "WorkLifeBalance" "YearsAtCompany"
## [21] "YearsInCurrentRole" "YearsWithCurrManager"
ncol(test)
## [1] 22
set.seed(42)
train$Attrition <- factor(train$Attrition)
classifier_RF= randomForest(x=train[-2],
y=train$Attrition,
ntree=900)
classifier_RF
##
## Call:
## randomForest(x = train[-2], y = train$Attrition, ntree = 900)
## Type of random forest: classification
## Number of trees: 900
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 2.73%
## Confusion matrix:
## 0 1 class.error
## 0 913 48 0.049947971
## 1 3 905 0.003303965
head(test[-2])
## Age BusinessTravel DailyRate Department DistanceFromHome
## 4 32 Travel_Rarely 1089 Research & Development 7
## 10 41 Travel_Rarely 1102 Sales 1
## 15 37 Travel_Frequently 504 Research & Development 10
## 18 39 Travel_Rarely 492 Research & Development 12
## 22 35 Travel_Rarely 528 Human Resources 8
## 26 27 Travel_Frequently 1410 Sales 3
## EducationField EnvironmentSatisfaction JobInvolvement JobLevel
## 4 Life Sciences 4 3 2
## 10 Life Sciences 2 3 2
## 15 Medical 1 3 3
## 18 Medical 4 3 2
## 22 Technical Degree 3 3 1
## 26 Medical 4 4 2
## JobRole JobSatisfaction MaritalStatus MonthlyIncome OverTime
## 4 Laboratory Technician 3 Married 4883 0
## 10 Sales Executive 4 Single 5993 1
## 15 Manufacturing Director 3 Divorced 10048 0
## 18 Manufacturing Director 2 Married 5295 0
## 22 Human Resources 3 Single 4323 0
## 26 Sales Executive 4 Divorced 4647 1
## StockOptionLevel TotalWorkingYears TrainingTimesLastYear WorkLifeBalance
## 4 1 10 3 3
## 10 0 8 0 1
## 15 2 17 5 3
## 18 0 7 3 3
## 22 0 6 2 1
## 26 2 6 3 3
## YearsAtCompany YearsInCurrentRole YearsWithCurrManager
## 4 10 4 1
## 10 6 4 5
## 15 1 0 0
## 18 5 4 0
## 22 5 4 4
## 26 6 5 4
y_pred=predict(classifier_RF,newdata=test[-2])
temp=test[-2]
temp=temp[1:5,]
y_pre=predict(classifier_RF,newdata=temp)
y_pre
## 4 10 15 18 22
## 1 1 1 0 0
## Levels: 0 1
confusion_mtx=table(test[,2],y_pred)
confusion_mtx
## y_pred
## 0 1
## 0 260 12
## 1 0 277
eData<-classifier_RF[4]
eData_df <- data.frame(eData)
# Define the data
x <- 1:900
y1 <- eData_df$err.rate.OOB
y2 <- eData_df$err.rate.0
y3 <- eData_df$err.rate.1
# Plot each data series
plot(x, y1, type="l", col="red", lwd=2, ylim=c(0, max(c(y1, y2, y3))),
main="Random Forest Classifier", xlab="Number of trees", ylab="Error Rate")
points(x, y2, col="blue",type="l")
points(x, y3, col="black",type="l")
# Add legend
legend("topright", legend=c("OOB", "Label-0", "Label-1"),
col=c("red", "blue", "black"), pch=c(NA, 16, 16), lty=c(1, NA, NA),
lwd=c(2, 1, 1), bty="n")
importance(classifier_RF)
## MeanDecreaseGini
## Age 74.49428
## BusinessTravel 19.33821
## DailyRate 70.60853
## Department 15.14409
## DistanceFromHome 60.35919
## EducationField 27.54914
## EnvironmentSatisfaction 35.70781
## JobInvolvement 29.58843
## JobLevel 34.12893
## JobRole 37.75762
## JobSatisfaction 43.49527
## MaritalStatus 28.76501
## MonthlyIncome 87.90099
## OverTime 67.77564
## StockOptionLevel 42.70598
## TotalWorkingYears 55.18944
## TrainingTimesLastYear 33.48825
## WorkLifeBalance 26.42034
## YearsAtCompany 59.05526
## YearsInCurrentRole 37.85385
## YearsWithCurrManager 44.33763
varImpPlot(classifier_RF)
# Calculate accuracy
accuracy <- mean(y_pred == test$Attrition)
# Calculate True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN)
TP <- confusion_mtx[2, 2]
TN <- confusion_mtx[1, 1]
FP <- confusion_mtx[1, 2]
FN <- confusion_mtx[2, 1]
# Calculate accuracy
accuracy <- (TP + TN) / sum(confusion_mtx)
# Calculate precision
precision <- TP / (TP + FP)
# Calculate recall (sensitivity)
recall <- TP / (TP + FN)
# Calculate F1-score
f1_score <- 2 * (precision * recall) / (precision + recall)
# Print the calculated metrics
print(paste("Accuracy:", round(accuracy, 4)))
## [1] "Accuracy: 0.9781"
print(paste("Precision:", round(precision, 4)))
## [1] "Precision: 0.9585"
print(paste("Recall:", round(recall, 4)))
## [1] "Recall: 1"
print(paste("F1-score:", round(f1_score, 4)))
## [1] "F1-score: 0.9788"
confusion_mtx
## y_pred
## 0 1
## 0 260 12
## 1 0 277
new_input <- data.frame(
# Provide values for predictor variables except the one you excluded (-2)
Age = 33, BusinessTravel="Travel_Rarely", DailyRate=211,Department="Sales",
DistanceFromHome=16,EducationField="Life Sciences",EnvironmentSatisfaction=1,
JobInvolvement=3,JobLevel=3,JobRole="Sales Executive",
JobSatisfaction=1,MaritalStatus="Single",MonthlyIncome=8564,
OverTime=1,StockOptionLevel=0,TotalWorkingYears=11,TrainingTimesLastYear=2,
WorkLifeBalance=2,YearsAtCompany=0,YearsInCurrentRole=0,
YearsWithCurrManager=0
)
prediction <- predict(classifier_RF, newdata = new_input)
#prediction
newinput=temp[1,]
#newinput
# Make predictions using the trained random forest model
prediction <- predict(classifier_RF, newdata = newinput)
if(prediction==2){
cat("Predicted Attrition is No");
}else{
cat("Predicted Attrition is Yes");
}
## Predicted Attrition is Yes
saveRDS(classifier_RF, "random_forest_model.rds")
loaded_model <- readRDS("random_forest_model.rds")
#prediction <- predict(loaded_model, newdata = new_input)