-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemployee_attrition.R
399 lines (295 loc) · 13 KB
/
employee_attrition.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'''
Name: Sadiq Olusegun Balogun
Topic: Predicting Employee Attrition Using Logistic Regression
'''
#----- Installing necessary libraries --------------
install.packages('caret')
install.packages('dplyr')
install.packages('ggplot2')
install.packages('smotefamily')
install.packages('purrr')
install.packages('forcats')
install.packages('tidyr')
install.packages('ggcorrplot')
install.packages('bootStepAIC')
install.packages('pROC')
#-------- Importing the libraries----------------
library(caret) # for machine learning
library(dplyr) # for data manipulation
library(ggplot2) # for data visualization
library(smotefamily) # for oversampling with SMOTE
library(purrr) # for data manipulation
library(forcats) # To handle categorical variables
library(tidyr) # for data manipulation
library(ggcorrplot) # for correlation plot
library(bootStepAIC) # for feature selection
library(pROC) # for ROC plot
# ----------------------- IMPORTING DATA -------------------------
e_attrition <- read.csv("HR-Employee-Attrition.csv", sep = ",",
header = TRUE, stringsAsFactors = TRUE)
# ------------------ DATA PRE-PROCESSING ---------------------------------------
# check dimension and first few rows of data
dim(e_attrition)
head(e_attrition)
# Check structure of features
str(e_attrition)
# check for missing and duplicate values
anyNA(e_attrition)
anyDuplicated(e_attrition)
# No missing or duplicate values found
# statistic summary of the features
summary(e_attrition)
# Looking at the summary, EmployeeCount,EmployeeNumber because they are just serial numbers;
# Over18, StandardHours will aslo be removed because they have constant value in all observations
# Removing the columns
e_attrition <- e_attrition %>%
dplyr::select(-c(EmployeeCount,EmployeeNumber, Over18, StandardHours))
dim(e_attrition)
# ----------------- EXPLORATORY DATA ANALYSIS -----------------------------------------
# Target Variable Distribution
ggplot(data = e_attrition, mapping = aes(x=Attrition, fill=Attrition)) +
geom_bar(show.legend = FALSE) +
geom_text(
stat='count',
aes(label=paste0(round(after_stat(prop*100), digits=1), "%"),group=1),
vjust=-0.4,
size=4) +
labs( x = "", y = "", title = "Attrition Distribution")
# Attrition by OverTime
ggplot(e_attrition,
aes(x = OverTime, group = Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = -.5) +
labs(y = "Percentage", fill= "OverTime") +
facet_grid(~Attrition) +
scale_fill_manual(values = c("#0D0628","#660000")) +
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition by Over Time")
# Attrition by Business Travel
ggplot(e_attrition,
aes(x= BusinessTravel, group=Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = -.5) +
labs(y = "Percentage", fill="Business Travel") +
facet_grid(~Attrition) +
scale_fill_manual(values = c("#0D0628","#660000", "#023618")) +
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition by Business Travel")
# Attrition by Education
ggplot(e_attrition,
aes(x = Education, group = Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = -.5) +
labs(y = "Percentage", fill= "Education") +
facet_grid(~Attrition) +
scale_fill_manual(values = c("#0D0628","#660000","#023618","#06BCC1","#175676")) +
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition by Education")
# Attrition by Gender
ggplot(e_attrition,
aes(x = Gender, group = Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = -.5) +
labs(y = "Percentage", fill= "Gender") +
facet_grid(~Attrition) +
scale_fill_manual(values = c("#660000","#0D0628")) +
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition by Gender")
# Attrition by Marital Status
ggplot(e_attrition,
aes(x = MaritalStatus, group = Attrition)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)),
stat="count",
alpha = 0.7) +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ),
stat= "count",
vjust = -.5) +
labs(y = "Percentage", fill= "MaritalStatus") +
facet_grid(~Attrition) +
scale_fill_manual(values = c("#0D0628","#660000","#023618"))+
theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) +
ggtitle("Attrition")
# Avergae Income by Gender
plotdata <- e_attrition %>%
group_by(Gender) %>%
summarize(mean_salary = mean(MonthlyIncome))
ggplot(plotdata,
aes(x = Gender, y = mean_salary)) +
geom_bar(stat = "identity",
fill = "cornflowerblue") +
geom_text(aes(label = round(mean_salary,2)),
vjust = -0.25, size=3) +
scale_y_continuous(breaks = seq(0, 3000, 7000)) +
labs(title = "AVG MonthlyIncome by Gender", x = "",y = "Monthly Income")
# Average Income by Job Role
plotJobRole <- e_attrition %>%
group_by(JobRole) %>%
summarize(mean_salary = mean(MonthlyIncome))
JR <- ggplot(plotJobRole,
aes(x = reorder(JobRole, -mean_salary), y = mean_salary)) +
geom_bar(stat = "identity",
fill = "cornflowerblue") +
geom_text(aes(label = round(mean_salary,2)),
hjust = .005, size = 3) +
scale_y_continuous(breaks = seq(0, 3000, 7000)) +
labs(title = "AVG MonthlyIncome by Job Role", x = "",y = "Monthly Income")+
theme_classic()
JR + coord_flip()
# Salary distribution
ggplot(e_attrition, aes(x = MonthlyIncome))+
theme_classic()+
geom_histogram(binwidth = 1000, fill = "blue")+
labs(x="Monthly Income", y="",title = "Monthly Income Distribution")
# Age Distribution
ggplot(e_attrition, aes(x = Age))+
geom_histogram(aes(y=..density..),binwidth = 5, fill = "blue")+
geom_density(color='black')+geom_rug()+
labs(x="Age", y="",title = "Age Distribution")
# Scatter plot of Age, Income and Attrition
ggplot(e_attrition)+
geom_point(aes(x=MonthlyIncome, y=Age, colour= Attrition))+
ggtitle(label = "Attrition Scatterplot by Income and Age")
# employees with high salaries seems to stay at the company
# correlation of numeric features
numeric_cols <- dplyr::select_if(e_attrition, is.numeric)
# check the correlations of the features
r <- cor(numeric_cols, use="complete.obs")
round(r,2)
ggcorrplot(r, hc.order = TRUE, type = "full",lab = TRUE, lab_size = 2.5)
# -------------- DATA PREPARATION FOR MACHINE LEARNING ALGORITHM ---------------
# Data Type Conversion
e_atn <- e_attrition # making a copy of the dataframe
e_atn <- mutate(e_atn, Attrition = ifelse(Attrition=="Yes",1,0))
# convert the categorical data to numeric
e_atn$BusinessTravel <- as.numeric(e_atn$BusinessTravel)
e_atn$Department <- as.numeric(e_atn$Department)
e_atn$EducationField <- as.numeric(e_atn$EducationField)
e_atn$Gender <- as.numeric(e_atn$Gender)
e_atn$JobRole <- as.numeric(e_atn$JobRole)
e_atn$MaritalStatus <- as.numeric(e_atn$MaritalStatus)
e_atn$OverTime <- as.numeric(e_atn$OverTime)
# Data Normalization
normalize <- function(x) {+ return ((x - min(x)) / (max(x) - min(x))) }
nlzed_df <- as.data.frame(lapply(e_atn[,1:31], normalize))
nlzed_df$Attrition <- as.factor(nlzed_df$Attrition)
View(nlzed_df)
# ------------- BUILDING A LR MODEL WITHOUT FS + UNBALANCED DATA ------------------------
a_lrm <- glm(Attrition ~ ., family=binomial(link='logit'),
data = nlzed_df)
# Training and Testing Data Split
set.seed(154)
inTrain = createDataPartition(nlzed_df$Attrition, p = .7)[[1]]
# Assign the 70% of observations to training data
train <- nlzed_df[inTrain,]
table(train$Attrition)
test <- nlzed_df[-inTrain,] # remaining 30% for test
# Using the training data for LR model
model_all <- glm(Attrition~., data = train, family = 'binomial')
summary(model_all)
# Apply the prediction
predict_all <- predict(model_all, newdata= test, type = "response")
predict_all <- ifelse(predict_all > 0.5, 1, 0)
# Check the accuracy of the prediction model by printing the confusion matrix
print(confusionMatrix(as.factor(predict_all), test$Attrition))
#Recall
print(confusionMatrix(as.factor(predict_all), test$Attrition, mode = "prec_recall"))
# ROC Plot
rf.Plot1<- plot.roc (as.numeric(test$Attrition),
as.numeric(predict_all),
lwd=2, type="b", print.auc=TRUE,
col ="red",
main= "ROC Curve with Unbalanced Data and All Features")
# ------------- BUILDING A LOGISTIC REGRESSION MODEL WITH UNBALANCED DATA + FS---------------------------
# Create a logistic regression model
a_lrm <- glm(Attrition ~ ., family=binomial(link='logit'),
data = nlzed_df)
# stepwise regression for feature selection
both <- stepAIC(a_lrm, direction = "both")
# new df based on feature selection
newdf <- nlzed_df[, c('Attrition', 'Age', 'DailyRate', 'Department',
'DistanceFromHome','EnvironmentSatisfaction','Gender','JobInvolvement','JobLevel',
'JobRole','JobSatisfaction','MaritalStatus',
'NumCompaniesWorked','OverTime',
'RelationshipSatisfaction','StockOptionLevel','TotalWorkingYears',
'TrainingTimesLastYear','WorkLifeBalance','YearsAtCompany',
'YearsInCurrentRole','YearsSinceLastPromotion','YearsWithCurrManager'
)]
# Training and Testing Data Split
set.seed(194)
inTrain = createDataPartition(newdf$Attrition, p = .7)[[1]]
# Assign the 70% of observations to training data
training <- newdf[inTrain,]
testing <- newdf[-inTrain,] # remaining 30% for test
# Build the model
model_unb <- glm(Attrition~., data = training, family = 'binomial')
summary(model_unb)
# Apply the prediction
prediction <- predict(model_unb, newdata= testing, type = "response")
prediction <- ifelse(prediction > 0.5, 1, 0)
# Check the accuracy of the prediction model by printing the confusion matrix
print(confusionMatrix(as.factor(prediction), testing$Attrition))
#Recall
print(confusionMatrix(as.factor(prediction), testing$Attrition, mode = "prec_recall"))
# ROC Plot
rf.Plot2<- plot.roc (as.numeric(testing$Attrition),
as.numeric(prediction),
lwd=2, type="b", print.auc=TRUE,
col ="red",
main= "ROC Curve with Unbalanced Data and 22 Features")
# ----------------- BUILDING LR MODEL WITH OVERSAMPLED DATA + FS --------------------------------
smote_train <- SMOTE(training[-1], training$Attrition)
smote_train <- smote_train$data
table(smote_train$class)
str(smote_train)
# class feature is a character, we'll convert it to numeric
smote_train$class <- as.numeric(smote_train$class)
smote_train$class <- as.factor(smote_train$class)
# LR
smoteLR <- glm(class~., data = smote_train, family = 'binomial')
smote_predict <- predict(smoteLR, newdata= testing, type = "response")
smote_predict <- ifelse(smote_predict > 0.5, 1, 0)
# Check the accuracy of the prediction model by printing the confusion matrix
print(confusionMatrix(as.factor(smote_predict), testing$Attrition))
print(confusionMatrix(as.factor(smote_predict), testing$Attrition, mode = "prec_recall"))
# ROC Curve
rf.Plot3<- plot.roc (as.numeric(testing$Attrition),
as.numeric(smote_predict),
lwd=2, type="b", print.auc=TRUE,
col ="red",
main= "ROC Curve with Balanced Data and 22 Features")
# --------------BUILDING LR MODEL WITH OVERSAMPLED DATA + NO FS -------------------
s_train <- SMOTE(train[-2], train$Attrition)
s_train <- s_train$data
table(s_train$class)
str(s_train)
# LR
s_train$class <- as.numeric(s_train$class)
s_train$class <- as.factor(s_train$class)
sLR <- glm(class~., data = s_train, family = 'binomial')
s_predict <- predict(sLR, newdata= test, type = "response")
s_predict <- ifelse(s_predict > 0.5, 1, 0)
# Check the accuracy of the prediction model by printing the confusion matrix
print(confusionMatrix(as.factor(s_predict), test$Attrition))
print(confusionMatrix(as.factor(s_predict), test$Attrition, mode = "prec_recall"))
# ROC Curve
rf.Plot4<- plot.roc (as.numeric(test$Attrition),
as.numeric(s_predict),
lwd=2, type="b", print.auc=TRUE,
col ="red",
main= "ROC Curve with Balanced Data and All Features")