-
Notifications
You must be signed in to change notification settings - Fork 4
/
HR-Analytics_ModelAnalysis_V01.R
289 lines (220 loc) · 10.4 KB
/
HR-Analytics_ModelAnalysis_V01.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
library(ggplot2)
library(corrplot)
library(gplots)
library(gains)
library(leaps)
library(caret)
library(MASS)
library(class)
library(gmodels)
#Read the HR Dataset
hr.df <- read.csv("HR.csv", header = TRUE)
####################Prediction Model analysis####################
# Will the employee leave the company(Running GLM and LDA)
#Dataset for Logistic Regression
hr.logit <- hr.df[,5:30]
set.seed(13)
#Partitioning data into training (60%) and validation(40%) for logistic regression
train.index <- createDataPartition(hr.logit$left_Company , p = 0.6, list = FALSE)
train.df <-hr.logit[train.index,]
valid.df <- hr.logit[-train.index,]
#Logistic Regression for Leaving the company
lc<- glm(left_Company ~ ., data = train.df, family = "binomial")
options(scipen=999)
summary(lc)
#calculate e to the power coefficients
exp(coef(lc))
### Evaluate Performance of the Logit Model
### Predict propensities
pred <- predict(lc, valid.df[, -15], type = "response")
#Gains
gain <- gains(valid.df$left_Company , pred, groups = 10)
gain
#Lift
plot(c(0,gain$cume.pct.of.total*sum(pred))~c(0,gain$cume.obs),
xlab = "# cases", ylab = "Cumulative", main = "", type = "l")
lines(c(0,sum(pred))~c(0, dim(valid.df)[1]), lty = 5)
#decile chart and values
heights <- gain$mean.resp/mean(valid.df$left_Company)
midpoints <- barplot(heights, names.arg = gain$depth, ylim = c(0,9), col = "blue",
xlab = "Percentile", ylab = "Decile lift",
main = "Decile-chart")
text(midpoints, heights+0.5, labels=round(heights, 1), cex = 0.8)
pred.scale <- ifelse( pred > 0.5, 1,0)
#Confusion Matrix
#confusionMatrix(data = pred.scale, reference = valid.df$left_Company)
confusiontable <- table(Predicted = as.numeric(pred.scale) , Actual =as.numeric(valid.df$left_Company))
confusiontable
#Accuracy of Logistic Regression on predicting if the employee will leave the company
mean(pred.scale==valid.df$left_Company)*100
#### --------------------------linear discriminant Regression------------------------------
lda1 <- lda(left_Company~., data = train.df, family="binomial")
# predict values
predict1 = predict(lda1, valid.df[,-c(15)], type = "response")
names(predict1)
# model Accuracy
table(predict=predict1$class, actual=valid.df$left_Company)
mean(predict1$class == valid.df$left_Company)
#gains lift and decile
gain1 <- gains(valid.df$left_Company, predict1$posterior[,2], groups = 10)
### Plot Lift Chart
plot(c(0,gain1$cume.pct.of.total*sum(valid.df$left_Company))~c(0,gain1$cume.obs),
xlab = "# cases", ylab = "Cumulative", main = "", type = "l")
lines(c(0,sum(valid.df$left_Company))~c(0, dim(valid.df)[1]), lty = 5)
### Plot decile-wise chart
Decile <- gain1$mean.resp/mean(valid.df$left_Company)
Decile1 <- barplot(Decile, names.arg = gain1$depth, ylim = c(0,5), col = "red",
xlab = "%ile", ylab = "Decile lift",
main = "Decile Chart")
text(midpoints, heights+0.5, labels=round(heights, 1), cex = 0.8)
#--------------------------------------------B) What is the likelihood of employees getting a promotion?-----------------------------------------------
# Convert Category values to Factors
hr.df$salary <- factor(hr.df$salary, levels = c("high", "low", "medium"),
labels = c(1, 3, 2))
hr.df$Gender <- factor(hr.df$Gender, levels = c("F", "M"),
labels = c(0, 1))
hr.df$Role <- factor(hr.df$Role, levels = c("Director","Level 1","Level 2-4","Manager","Senior Director","Senior Manager","VP"),
labels = c(3,7,6,5,2,4,1))
#Convert Factors into Numeric
hr.df$salary = as.numeric(paste(hr.df$salary))
hr.df$Gender = as.numeric(paste(hr.df$Gender))
hr.df$Role = as.numeric(paste(hr.df$Role))
hrform.df <- hr.df[,-c(1,2,3,4,11)]
#Partitioning data into training (60%) and validation(40%) for linear regression on "Rising_Star"
train.lm.rs.index <- createDataPartition(hrform.df$Rising_Star , p= 0.6, list = FALSE)
train.linear.rs <-hrform.df[train.lm.rs.index,]
valid.linear.rs <- hrform.df[-train.lm.rs.index,]
# Linear Regression for Rising Star
hr.rise <- lm(Rising_Star ~ ., data = train.linear.rs)
summary(hr.rise)
pred.linear.rs <- predict(hr.rise, valid.linear.rs)
#Gains
gain.linear.rs <- gains(valid.linear.rs$Rising_Star , pred.linear.rs, groups = 10)
gain.linear.rs
#Lift
plot(c(0,gain.linear.rs$cume.pct.of.total*sum(pred.linear.rs))~c(0,gain.linear.rs$cume.obs),
xlab = "# cases", ylab = "Cumulative", main = "", type = "l")
lines(c(0,sum(pred.linear.rs))~c(0, dim(valid.linear.rs)[1]), lty = 5)
#decile chart and values
heights <- gain.linear.rs$mean.resp/mean(valid.linear.rs$Rising_Star)
midpoints <- barplot(heights, names.arg = gain.linear.rs$depth, ylim = c(0,9), col = "blue",
xlab = "Percentile", ylab = "Decile lift",
main = "Decile-chart")
text(midpoints, heights+0.5, labels=round(heights, 1), cex = 0.8)
pred.linear.rs.round <- round(pred.linear.rs,0)
#Confusion Matrix
confusiontable.linear.rs <- table(Predicted = pred.linear.rs.round , Actual = valid.linear.rs$Rising_Star )
confusiontable.linear.rs
#Accuracy
mean(pred.linear.rs.round==valid.linear.rs$Rising_Star)
hr.logit$Role <- factor(hr.logit$Role, levels = c("Director","Level 1","Level 2-4","Manager","Senior Director","Senior Manager","VP"),
labels = c(3,7,6,5,2,4,1))
###KNN
### Partitioning data
set.seed(123456789)
train.index <- sample(row.names(hr.logit), 0.6*dim(hr.logit)[1])
valid.index <- setdiff(row.names(hr.logit), train.index)
train.df <- hr.logit[train.index, ]
valid.df <- hr.logit[valid.index, ]
train.norm.df <- train.df
valid.norm.df <- valid.df
hr.norm.df <- hr.logit
### Normalize data using preProcess() from CARET
norm.values <- preProcess(train.df[, c(1,3:26)], method=c("center", "scale"))
train.norm.df[, c(1,3:26)] <- predict(norm.values, train.df[, c(1,3:26)])
valid.norm.df[, c(1,3:26)] <- predict(norm.values, valid.df[, c(1,3:26)])
train.norm.df$salary <- factor(train.norm.df$salary, levels = c("high", "low", "medium"),
labels = c(1, 3, 2))
train.norm.df$Gender <- factor(train.norm.df$Gender, levels = c("F", "M"),
labels = c(0, 1))
valid.norm.df$salary <- factor(valid.norm.df$salary, levels = c("high", "low", "medium"),
labels = c(1, 3, 2))
valid.norm.df$Gender <- factor(valid.norm.df$Gender, levels = c("F","M"),
labels = c(0,1))
### Run K-NN
nn <- knn(train = train.norm.df[, c(1,3:26)], test = valid.norm.df[, c(1,3:26)],
cl = train.norm.df[, 2], k = 5)
### Nearest-neighbor Index
row.names(train.df)[attr(nn, "nn.index")]
### Showing the accuracy by using confusion matrix
table(nn, valid.norm.df$Rising_Star)
CrossTable(x=nn,y=valid.norm.df$Rising_Star,prop.chisq=F)
#-------------------------------------------C) How much time will the employee spend in company?------------------------------------------------
#Linear Regression for time spend in company
set.seed(123)
#Partitioning data into training (60%) and validation(40%) for linear regression
train.lm.ts.index <- createDataPartition(hrform.df$time_spend_company , p= 0.6, list = FALSE)
train.linear.ts <-hrform.df[train.lm.ts.index,]
valid.linear.ts <- hrform.df[-train.lm.ts.index,]
hr_time.lm <- lm(time_spend_company ~ ., data = train.linear.ts )
summary(hr_time.lm)
pred.linear.ts <- predict(hr_time.lm, valid.linear.ts)
#gains
gain.linear.ts <- gains(valid.linear.ts$time_spend_company , pred.linear.ts, groups = 10)
gain.linear.ts
#Lift
plot(c(0,gain.linear.ts$cume.pct.of.total*sum(pred.linear.ts))~c(0,gain.linear.ts$cume.obs),
xlab = "# cases", ylab = "Cumulative", main = "", type = "l")
lines(c(0,sum(pred.linear.ts))~c(0, dim(valid.linear.ts)[1]), lty = 5)
#decile chart and values
heights <- gain.linear.ts$mean.resp/mean(valid.linear.ts$time_spend_company)
midpoints <- barplot(heights, names.arg = gain.linear.ts$depth, ylim = c(0,9), col = "blue",
xlab = "Percentile", ylab = "Decile lift",
main = "Decile-chart")
text(midpoints, heights+0.5, labels=round(heights, 1), cex = 0.8)
pred.linear.ts.round <- round(pred.linear.ts,0)
#Accuracy
mean(pred.linear.ts.round==valid.linear.ts$time_spend_company)
#---------------------------------------------D) How satisfied are the employees in company?----------------------------------------------
#Linear Regression for Employee Satisfaction
set.seed(123)
#Partitioning data into training (60%) and validation(40%) for linear regression
train.lm.es.index <- createDataPartition(hrform.df$Emp_Satisfaction , p= 0.6, list = FALSE)
train.linear.es <-hrform.df[train.lm.es.index,]
valid.linear.es <- hrform.df[-train.lm.es.index,]
hr_emp_sat.lm <- lm(Emp_Satisfaction ~ ., data = train.linear.es )
summary(hr_emp_sat.lm)
pred.linear.es <- predict(hr_emp_sat.lm, valid.linear.es)
#gains
gain.linear.es <- gains(valid.linear.es$Emp_Satisfaction , pred.linear.es, groups = 10)
gain.linear.es
#Lift
plot(c(0,gain.linear.es$cume.pct.of.total*sum(pred.linear.es))~c(0,gain.linear.es$cume.obs),
xlab = "# cases", ylab = "Cumulative", main = "", type = "l")
lines(c(0,sum(pred.linear.es))~c(0, dim(valid.linear.es)[1]), lty = 5)
#decile chart and values
heights <- gain.linear.es$mean.resp/mean(valid.linear.es$Emp_Satisfaction)
midpoints <- barplot(heights, names.arg = gain.linear.es$depth, ylim = c(0,9), col = "blue",
xlab = "Percentile", ylab = "Decile lift",
main = "Decile-chart")
text(midpoints, heights+0.5, labels=round(heights, 1), cex = 0.8)
pred.linear.es.round <- round(pred.linear.es,0)
#Accuracy
mean(pred.linear.es.round==valid.linear.es$Emp_Satisfaction)
mean(hrform.df$Emp_Satisfaction)
#--------------------------------E) Running K-mean Clustering to find out which set of employees are more likely to exit-----------------------
###Normalize the data
hrform.norm.df <- as.data.frame(sapply(hrform.df, scale))
###Function to calculate the AIC
kmeansAIC = function(km){
m = ncol(km$centers)
n = length(km$cluster)
k = nrow(km$centers)
D = km$tot.withinss
return(D + 2*m*k)
}
###Finding the optimal k with lowest AIC
set.seed(123)
km <- kmeans(hrform.norm.df, 20)
for (k in 1:30) {
km <- kmeans(hrform.norm.df, k)
print(k)
print(kmeansAIC(km))
}
###k-Means Clustering
km <- kmeans(hrform.norm.df, 20)
### Cluster size
km$size
### Cluster centroids-Check the column left_company for cluster values of 1+
km$centers
#-------------------------------------------------------------------------------------------