-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project attrition.Rmd
471 lines (275 loc) · 9.55 KB
/
Project attrition.Rmd
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
---
title: "Project Attrition"
author: "Anthony Yeung"
date: "April 4, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Load Library
```
library(dplyr)
library(plyr)
library(ggplot2)
```
### Read Attrition csv file
```
atr <- read.csv("C:/Users/AY/Desktop/attrition.csv", header=TRUE)
names(atr)
str(atr)
```
### Replacing Attrition column yes and no with 1 and 0
```
atr$Attrition <- revalue(atr$Attrition, c("Yes"=1))
atr$Attrition<- revalue(atr$Attrition, c("No"=0))
```
### Replacing Attrition column with atr$Attrition
```
Attrition <- atr$Attrition
```
### Change Attrition column into numeric
```
atr$Attrition <- as.numeric(as.character(atr$Attrition))
atr[1:3,1:3]
str(atr)
```
### Replacing Gender column Female and Male with 1 and 0
```
atr$Gender <- revalue(atr$Gender, c("Female"=1))
atr$Gender <- revalue(atr$Gender, c("Male"=0))
```
### Replacing Gender column with atr$Gender
```
Gender <- atr$Gender
```
### Change Gender column into numeric
```
atr$Gender <- as.numeric(as.character(atr$Gender))
atr[1:3,11:13]
str(atr)
```
### Replacing MaritalStatus column Single and Married with 1 and 0
```
atr$MaritalStatus <- revalue(atr$MaritalStatus, c("Single"=1))
atr$MaritalStatus <- revalue(atr$MaritalStatus, c("Married"=0))
```
### Replacing MaritalStatus column with atr$MaritalStatus
```
MaritalStatus <- atr$MaritalStatus
```
### Change MaritalStatus column into numeric
```
atr$MaritalStatus <- as.numeric(as.character(atr$MaritalStatus))
atr[1:3,17:19]
str(atr)
```
### Replacing Over18 column Y with 1
```
atr$Over18 <- revalue(atr$Over18, c("Y"=1))
```
### Replacing Over18 column with atr$Over18
```
Over18 <- atr$Over18
```
### Change Over18 column into numeric
```
atr$Over18 <- as.numeric(as.character(atr$Over18))
atrstr(atr)[1:3,21:23]
str(atr)
```
### Replacing OverTime column Yes and No with 1 and 0
```
atr$OverTime <- revalue(atr$OverTime, c("Yes"=1))
atr$OverTime <- revalue(atr$OverTime, c("No"=0))
```
### Replacing OverTime column with atr$OverTime
```
OverTime <- atr$OverTime
```
### Change Overtime column into numeric
```
atr$OverTime <- as.numeric(as.character(atr$Overtime))
atr[1:3,22:24]
str(atr)
```
## **Correlation Calculation**
cor(atr$Age, atr$Attrition,atr$DailyRate)
```
corr_age<- cor(atr$Attrition,atr$Age )
corr_age
corr_DailyRate <- cor(atr$Attrition, atr$DailyRate)
corr_DailyRate
corr_DistanceFromHome <- cor(atr$Attrition, atr$DistanceFromHome)
corr_DistanceFromHome
corr_Education <- cor(atr$Attrition, atr$Education)
corr_Education
corr_EmployeeCount <- cor(atr$Attrition, atr$EmployeeCount)
corr_EmployeeCount
corr_EmployeeNumber <- cor(atr$Attrition, atr$EmployeeNumber)
corr_EmployeeNumber
corr_EnvironmentSatisfaction <- cor(atr$Attrition, atr$EnvironmentSatisfaction)
corr_EnvironmentSatisfaction
corr_Gender <- cor(atr$Attrition, atr$Gender)
corr_Gender
corr_HourlyRate <- cor(atr$Attrition, atr$HourlyRate)
corr_HourlyRate
corr_JobInvolvement <- cor(atr$Attrition, atr$JobInvolvement)
corr_JobInvolvement
corr_JobLevel <- cor(atr$Attrition, atr$JobLevel)
corr_JobLevel
corr_JobSatisfaction <- cor(atr$Attrition, atr$JobSatisfaction)
corr_JobSatisfaction
corr_MaitalStatus <- cor(atr$Attrition, atr$MaritalStatus)
corr_MaitalStatus
corr_MonthlyIncome <- cor(atr$Attrition, atr$MonthlyIncome)
corr_MonthlyIncome
corr_MonthlyRate <- cor(atr$Attrition, atr$MonthlyRate)
corr_MonthlyRate
corr_NumCompaniesWorked <- cor(atr$Attrition, atr$NumCompaniesWorked)
corr_NumCompaniesWorked
corr_Over18 <- cor(atr$Attrition, atr$Over18)
corr_Over18
corr_OverTime <- cor(atr$Attrition, atr$OverTime)
corr_OverTime
corr_PercentSalaryHike <- cor(atr$Attrition, atr$PercentSalaryHike)
corr_PercentSalaryHike
corr_PerformanceRating <- cor(atr$Attrition, atr$PerformanceRating)
corr_PerformanceRating
corr_RelationshipSatisfaction <- cor(atr$Attrition, atr$RelationshipSatisfaction)
corr_RelationshipSatisfaction
corr_StandardHours <- cor(atr$Attrition, atr$StandardHours)
corr_StandardHours
corr_StockOptionLevel <- cor(atr$Attrition, atr$StockOptionLevel)
corr_StockOptionLevel
corr_TotalWorkingYears <- cor(atr$Attrition, atr$TotalWorkingYears)
corr_TotalWorkingYears
corr_TrainingTimesLastYear <- cor(atr$Attrition, atr$TrainingTimesLastYear)
corr_TrainingTimesLastYear
corr_WorkLifeBalance <- cor(atr$Attrition, atr$WorkLifeBalance)
corr_WorkLifeBalance
corr_YearsAtCompany <- cor(atr$Attrition, atr$YearsAtCompany)
corr_YearsAtCompany
corr_YearsInCurrentRole <- cor(atr$Attrition, atr$YearsInCurrentRole)
corr_YearsInCurrentRole
corr_YearsSinceLastPromotion <- cor(atr$Attrition, atr$YearsSinceLastPromotion)
corr_YearsSinceLastPromotion
corr_YearsWithCurrManager <- cor(atr$Attrition, atr$YearsWithCurrManager)
corr_YearsWithCurrManager
```
## **Multiple Regesssion of Attrition against all variables**
```
MR_atr_select <- lm(Attrition ~ Age+ DailyRate+ DistanceFromHome+ Education+ EmployeeNumber+ EnvironmentSatisfaction+ Gender+ HourlyRate+ JobInvolvement+ JobLevel+ JobSatisfaction+ MonthlyIncome+ MonthlyRate+ NumCompaniesWorked+ PercentSalaryHike+ PerformanceRating+ RelationshipSatisfaction+ StockOptionLevel+ TotalWorkingYears+ TrainingTimesLastYear+ WorkLifeBalance+ YearsAtCompany+ YearsInCurrentRole+ YearsSinceLastPromotion+ YearsWithCurrManager, data=atr)
MR_atr_select
summary(MR_atr_select)
```
###**Re Run Multiple Regression Model after eliminating all insignificants varaibles**
```
MR_atr_select <- lm(Attrition ~ Age+ DistanceFromHome+ EnvironmentSatisfaction+ JobInvolvement+ JobSatisfaction+ MonthlyIncome+ NumCompaniesWorked+ StockOptionLevel, data=atr)
MR_atr_select
summary(MR_atr_select)
```
##**Attrition Multiple Regression Model**
```
Regression maodel is
Atttrition = 0.7444+
(-0.00494)*(Age)+
(0.003806)*(distanceFroHome)+
(-0.003503)*(EnvironmentSatisfaction)+
(-0.06747)*(JobInvolemtn)+
(-0.03369)*(JobStatisfaction)+
(-0.00000899)*(MonthlyIncome)+
(0.01496)*(NumCompaniesWorked)+
(-0.05806)*(StockOptionLevel)
```
###Question C Correlation between age and Monthly Income
```
#Relationship of age and Income
cor_age_income<- cor(atr$Age, atr$MonthlyIncome)
cor_age_income
```
### **Correlation between Age and Monthly Income is 0.49785**
##ggplot of Age and Monthly Income
```
ggplot(atr,aes(x=atr$Age,y=atr$MonthlyIncome))+geom_point(color="blue")+
xlab("Age")+
ylab("Monthly Income")+
ggtitle(" Age and Monthly Income")+
stat_smooth(method="lm", color="green")
```
##ggplot age variable on histogram
```
ggplot(atr,aes(x=atr$Age))+geom_histogram(binwidth=1,color="red", fill="yellow")+
xlab("Age")+
ggtitle("Age")
```
##ggplot Monthly Income variable on histogram
```
ggplot(atr,aes(x=atr$MonthlyIncome))+geom_histogram(binwidth=1000,color="blue", fill="red")+
xlab("Monthly Income")+
ggtitle("Monthly Income")
```
##ggplot Gender variable on histogram
```
ggplot(atr,aes(x=atr$Education))+geom_histogram(binwidth=1,color="blue", fill="brown")+
xlab("Education")+
ggtitle("Education")
```
## **Principal Component Analysis**
####** PCA descriptive statistics**
```
atr_select <- atr %>% select(Age, Attrition, DailyRate,DistanceFromHome,Education,EmployeeNumber,EnvironmentSatisfaction,Gender, HourlyRate,JobInvolvement, JobLevel, JobSatisfaction, MonthlyIncome, MonthlyRate,NumCompaniesWorked, PercentSalaryHike, PerformanceRating,RelationshipSatisfaction, StockOptionLevel,TotalWorkingYears, TrainingTimesLastYear, WorkLifeBalance, YearsAtCompany,YearsInCurrentRole, YearsSinceLastPromotion,YearsWithCurrManager)
summary(atr_select)
cor(atr_select)
```
####**Correlation matrix of Attrition against all variables**
```
cor(atr_select,atr_select$Attrition)
```
#### **Attrition has positive correlation with 4 variables**
```
DistanceFromHome 0.077923583
MonthlyRate 0.015170213
NumCompaniesWorked 0.043493739
PerformanceRating 0.002888752
[,1]
Age -0.159205007
Attrition 1.000000000
DailyRate -0.056651992
DistanceFromHome 0.077923583
Education -0.031372820
EmployeeNumber -0.010577243
EnvironmentSatisfaction -0.103368978
Gender -0.029453253
HourlyRate -0.006845550
JobInvolvement -0.130015957
JobLevel -0.169104751
JobSatisfaction -0.103481126
MonthlyIncome -0.159839582
MonthlyRate 0.015170213
NumCompaniesWorked 0.043493739
PercentSalaryHike -0.013478202
PerformanceRating 0.002888752
RelationshipSatisfaction -0.045872279
StockOptionLevel -0.137144919
TotalWorkingYears -0.171063246
TrainingTimesLastYear -0.059477799
WorkLifeBalance -0.063939047
YearsAtCompany -0.134392214
YearsInCurrentRole -0.160545004
YearsSinceLastPromotion -0.033018775
YearsWithCurrManager -0.156199316
```
### PCA
```
atr_select <- princomp(atr_select, score=TRUE, cor=TRUE)
summary(atr_select)
```
### **Screeplot of PCA**
```
plot(atr_select)
```
### **Scree Plot of PCA**
```
screeplot(atr_select,type="l",main="Scree Plot")
```