-
Notifications
You must be signed in to change notification settings - Fork 31
/
hrm.R
456 lines (288 loc) · 11.4 KB
/
hrm.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
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
library(ggplot2)
library(dplyr)
library(tidyr)
hrm<-read.csv('HR_comma_sep.csv')
#Structure of the Dataset
str(hrm)
attach(hrm)
#converting left variable to factor variable
hrm$left<-ifelse(hrm$left==1,'True','False')
hrm$left<-factor(hrm$left,labels=c("True","False"))
table(hrm$left)
#Summary Statistics of the dataset
summary(hrm)
by(hrm$satisfaction_level,hrm$salary,summary)
#Histogram
p1<-ggplot(aes(x=satisfaction_level),data=hrm) +
geom_histogram(color="black",fill="red",bins = 30) +
labs(title="Satisfaction level Histogram",x='Satisfaction Level of Employees', y="Frequency")
p1
#Satisfaction level histogram facetted by sallary classes
p2 = p1 + facet_wrap(~salary)
p2
by(satisfaction_level,left,summary)
#As peedicted the satifaction level of employees who left was lower
#Sstisfaction level vs left
ggplot(aes(x = satisfaction_level),data=hrm) +
geom_histogram(color='black',fill='green',bins=35) +
xlab('Satisfaction Level') +
ylab("Frequency") +
facet_wrap(~left)
#Boxplot for Satisfaction level vs left
ggplot(aes(x = left,y=satisfaction_level),data= hrm) +
geom_boxplot() +
ylab('Satisfaction Level') +
xlab("Employee left") +
labs(fill="Salary Classes")
#Boxplot for Satisfaction level vs left facetted by Salary Ranges
ggplot(aes(x = left,y=satisfaction_level),data= hrm) +
geom_boxplot() +
ylab('Satisfaction Level') +
xlab("Employee left") +
facet_wrap(~salary)
table(left , salary)
#Testing for the dependence between left and salary Ranges
#Both are categorial variables so we use Chisq Test statistic
chisq.test(left,salary)
#X-squared value is high and p-value is less i.e results are significant
#both variables are related
#Analysis on number of Projects
hrm$number_project<-factor(hrm$number_project)
ggplot(aes(x=number_project),data = hrm) +
geom_bar(color='black',fill='#234338') +
xlab("Number of Projects") +
ylab("Frequency") +
labs(title="Barplot of Number of projects")
#boxplot of number of projects vs Average monthly hours at workplace of employees
p3=ggplot(aes(x=number_project, y = average_montly_hours),data=hrm)+
geom_boxplot()
p3
p4=p3+facet_wrap(~salary)
p4
p5=p3+facet_wrap(~left) + labs(title="Number projects Vs Avg monthly hours worked faceted by Left")
p5
#facetted by salary
ggplot(aes(x=number_project),data = hrm) +
geom_bar(color='black',fill='#834338') +
xlab("Number of Projects") +
ylab("Frequency") +
labs(title="Barplot of Number of projects faceted by Salary") +
facet_wrap(~salary)
#faceted by If a employee left or not
ggplot(aes(x=number_project),data = hrm) +
geom_bar(color='black',fill='#547398') +
xlab("Number of Projects") +
ylab("Frequency") +
labs(title="Barplot of Number of projects faceted by Left")+
facet_wrap(~left)
#Analysis of average monthly hours
summary(average_montly_hours)
#Somewhat Normally distributed
ggplot(aes(x= average_montly_hours),data = hrm)+
geom_histogram(color='black',fill="yellow",bins = 30)
cor.test(satisfaction_level,average_montly_hours)
#No relation between both the variables
cor.test(time_spend_company,average_montly_hours)
ggplot(aes(y = average_montly_hours, x = as.factor(time_spend_company)),data=hrm)+
geom_boxplot() +
xlab("No of years a Employee has worked in The company") +
ylab("Average Montly hours worked")
ggplot(aes(x = average_montly_hours),data =hrm ) +
geom_histogram(color='black',fill='#443332',bins = 30) +
facet_wrap(~left)
by(average_montly_hours , hrm$left ,summary)
ggplot(aes(y = average_montly_hours, x = hrm$left),data=hrm)+
geom_boxplot() +
xlab("Employee left or not") +
ylab("Average Montly hours worked")
# A thing to notice is that employee who left the company worked more hours than those
# who did not leave.
#Anslysis for variable Time spend at company
table(hrm$time_spend_company)
ggplot(aes(x = factor(time_spend_company)),data = hrm) +
geom_bar(fill = 'purple',color='black') +
xlab("Time spend at compnay in years") +
ylab("Frequency")+
labs(title = "Barplot of Time spend at Company")
#Time spend at company vs Left or not
ggplot(aes(x = factor(time_spend_company)),data = hrm) +
geom_bar(fill = 'grey',color='black') +
xlab("Time spend at compnay in years") +
ylab("Frequency")+
labs(title = "Barplot of Time spend at Company faceted by Left") +
facet_wrap(~left)
by(time_spend_company , hrm$left , summary)
ggplot(aes(x = left , y
= time_spend_company),data = hrm)+
geom_boxplot()
#Time spend vs Satisfaction level of employees as they worked
by(satisfaction_level,factor(time_spend_company),summary)
cor.test(satisfaction_level,time_spend_company)
#both have a negetive correlation
#plots vs Time spend and Satisfaction level
ggplot(aes(x=factor(time_spend_company),y=satisfaction_level),data=hrm)+
geom_boxplot() +
xlab("Time spend at company in years")+
ylab("Satisfaction level")
#Time spend at compnay vs Promotion in last 5 years
table(Promotion=promotion_last_5years,Time_Spend=factor(time_spend_company))
#Employees who have had promotion are very less
ggplot(aes(x = factor(time_spend_company)),data = hrm)+
geom_bar()+
facet_wrap(~promotion_last_5years) +
scale_y_continuous(limits=c(0,4000),breaks=seq(0,4000,500))
#Time spend vs Department of Work
by(time_spend_company,sales,summary)
ggplot(aes(x = sales,y = time_spend_company),data = hrm) +
geom_boxplot() +
coord_flip()
#Analysis of Department of Work
ggplot(aes(x =sales),data = hrm ) +
geom_bar() +
xlab('Department') +
ylab('Counts') +
coord_flip()
#highest count is for Sales department then Technical and least for
#Management
#Department vs sallary
table(Dept = sales , Salary = salary)
ggplot(aes(x =sales),data = hrm ) +
geom_bar(aes(fill=salary)) +
xlab('Department') +
ylab('Counts') +
coord_flip()
ggplot(aes(x =sales),data = hrm ) +
geom_bar(aes(fill=salary)) +
xlab('Department') +
ylab('Counts') +
labs(title = "Department and their count facetted by Salary ranges")+
facet_wrap(~salary) +
coord_flip()
chisq.test(sales,salary)
#Department and Salary is dependent on each other .
#Department vs which employee left
ggplot(aes(x = sales),data =hrm) +
geom_bar(aes(fill=left))
#finding proportions
prop.table(table(Dept = sales , left = left))*100
deptdf<-hrm %>% group_by(sales,left) %>%
summarise(count=n())
#making a data frame of Departments and the count of workers who left or not
deptdf<-spread(deptdf,left,count)
deptdf<-transform(deptdf,Perleft=(True/(True+False))*100 , PerWork=(False/(True+False))*100)
deptdf
chisq.test(sales , hrm$left)
#Hence both Department and left variables are realted
#Plot of Department vs Percentage of Employees who left
ggplot(aes(x=sales, y = Perleft),data = deptdf) +
geom_col(fill='#53ab85',color='#2f3f52') +
coord_flip()+
xlab("Department") +
ylab("Percentage of Employees who left") +
labs(title="Plot of Department vs Percentage of Employee left")
#highest percentage of employees belonged to HR dept then accounting
# least for management dept who left
#Plot of Department vs Percentage of People Working
ggplot(aes(x=sales, y = PerWork),data = deptdf) +
geom_col(fill='#b6a2bf',color='#2f3f52') +
coord_flip()+
xlab("Department") +
ylab("Percentage of Employees who Still Work") +
labs(title="Plot of Department vs Percentage of Employees Working")
#Department vs Satisfaction level
by(satisfaction_level,sales,summary)
#highest mean satisfaction for R&D and Management Dept
ggplot(aes(x = sales, y = satisfaction_level),data = hrm)+
geom_boxplot() +
scale_y_sqrt()+
xlab('Department') +
ylab('Satisfaction Level"') +
coord_flip()
#Highest Median Satisfaction for IT dept, R&D and , Management
#Least Median Satifaction level for HR and Accounting
#Analysis of Department vs Time spend at company
by(time_spend_company,sales,summary)
#Maximum Mean Time spent by Managaement Employees
ggplot(aes(x = sales,y = time_spend_company),data = hrm) +
geom_boxplot() +
xlab('Department') +
ylab("Time Spend at Company") +
coord_flip()
ggplot(aes(x = time_spend_company),data = hrm) +
geom_bar() +
xlab("Time Spend at Company splitted by Department") +
facet_wrap(~sales)
#In every department there is very less count of Employees
# working for over 5 years
#Department vs Time average monthly hours
by(average_montly_hours,sales , summary)
#Highest average working time for IT and Technical departments
ggplot(aes(x = sales , y = average_montly_hours),data =hrm) +
geom_boxplot() +
xlab('Department of Work') +
ylab('Average Monthly Hourse of Work') +
coord_flip()
#Highest Median working time of Management department
#Department vs Work Accident
table(Work_accident)
table(sales,Work_accident)
ggplot(aes(x = sales),data = hrm) +
geom_bar(aes(fill=factor(Work_accident))) +
coord_flip() +
labs(x = "Department",y ="Frequency", fill="Work Accidents" )
hrm$Work_accident<-factor(Work_accident,labels = c('False','True'))
accidentdf<-hrm %>% group_by(sales,Work_accident) %>%
summarise(Count= n())
accidentdf<-spread(accidentdf,Work_accident,Count)
accidentdf<-transform(accidentdf,TrueRate=(True/(True+False))*100,FalseRate=(False/(True+False))*100)
#Plot of Departent vs Accidental Rate
ggplot(aes(x = sales,y = TrueRate),data = accidentdf) +
geom_col(color='black',fill="#b266b2") +
xlab('Department') +
ylab('Accident Percentage') +
coord_flip()
#Hishest number of accidents in R and D department
ggplot(aes(x = sales,y = FalseRate),data = accidentdf) +
geom_col(color='black',fill="#d8b2d8") +
xlab('Department') +
ylab('No Accident Percentage') +
coord_flip()
#Maximum for HR department
#Department vs number_projects made
by(number_project,sales,summary)
ggplot(aes(x = sales, y =factor(number_project)),data = hrm) +
geom_count() +
xlab("Department") +
ylab("Number of projects") +
labs(title = "Plot of Department vs Number of projects and their count ")
#Department vs Promotion in last 5 years
table(sales , hrm$promotion_last_5years)
#TRansforming Promotion Column to Factor with True and False values
hrm$promotion_last_5years<-factor(promotion_last_5years,labels=c('False',"True"))
#Generating a promotions Data frame
promotiondf<-hrm %>% group_by(sales,promotion_last_5years) %>%
summarise(Count = n())
#Sprading the data'
promotiondf<-promotiondf %>% spread(promotion_last_5years,Count)
#changing the names
names(promotiondf)<-c("Department","Nopromotion","Promotion")
#replacing NA valuw with 0
promotiondf[is.na(promotiondf)]<-0
promotiondf<-promotiondf %>% transform(PerPromotion=(Promotion/(Promotion+Nopromotion))*100,
PerNopromotion = (Nopromotion/(Promotion + Nopromotion))*100)
#Most number of Promotions done in Management and Marketing Departments
#Least in IT , Technical and Product Manager
#Plotting Department vs Promotion Percentage
ggplot(aes(x =Department, y =PerPromotion ),data = promotiondf) +
geom_col(color='black',fill = '#453322') +
xlab("Department") +
ylab("Percentage of employees Promoted in last 5 years") +
coord_flip()
#Highest in Management Department
#Plotting Department vs No Promotion Percentage
ggplot(aes(x =Department, y =PerNopromotion ),data = promotiondf) +
geom_col(color="white",fill = "#665443") +
xlab("Department") +
ylab("Percentage of employees Not Promoted in last 5 years") +
coord_flip()
#No promotion in IT and Product Management Dept