用R语言分析与预测员工离职

大数据 数据分析
在实验室搬砖之后,继续我们的kaggle数据分析之旅,这次数据也是答主在kaggle上选择的比较火的一份关于人力资源的数据集,关注点在于员工离职的分析和预测,依然还是从数据读取,数据预处理,EDA和机器学习建模这几个部分开始进行,最后使用集成学习中比较火的random forest算法来预测离职情况。

在实验室搬砖之后,继续我们的kaggle数据分析之旅,这次数据也是答主在kaggle上选择的比较火的一份关于人力资源的数据集,关注点在于员工离职的分析和预测,依然还是从数据读取,数据预处理,EDA和机器学习建模这几个部分开始进行,***使用集成学习中比较火的random forest算法来预测离职情况。

数据读取

  1. setwd("E:/kaggle/human resource"
  2. library(data.table
  3. library(plotly) 
  4. library(corrplot) 
  5. library(randomForest) 
  6. library(pROC) 
  7. library(tidyverse) 
  8. library(caret) 
  9. hr<-as.tibble(fread("HR_comma_sep.csv")) 
  10. glimpse(hr) 
  11. sapply(hr,function(x){sum(is.na(x))}) 
  12. ———————————————————————————————————————————————————————————————————————————————————— 
  13. Observations: 14,999 
  14. Variables: 10 
  15. $ satisfaction_level    <dbl> 0.38, 0.80, 0.11, 0.72, 0.37, 0.41, 0.10, 0.92, 0.89, 0.42, 0.45, 0.11, 0.84, 0.41, 0.36, 0.38, 0.45, 0.78, 0.45, 0.76, 0.11, 0.3... 
  16. $ last_evaluation       <dbl> 0.53, 0.86, 0.88, 0.87, 0.52, 0.50, 0.77, 0.85, 1.00, 0.53, 0.54, 0.81, 0.92, 0.55, 0.56, 0.54, 0.47, 0.99, 0.51, 0.89, 0.83, 0.5... 
  17. $ number_project        <int> 2, 5, 7, 5, 2, 2, 6, 5, 5, 2, 2, 6, 4, 2, 2, 2, 2, 4, 2, 5, 6, 2, 6, 2, 2, 5, 4, 2, 2, 2, 6, 2, 2, 2, 4, 6, 2, 2, 6, 2, 5, 2, 2, ... 
  18. $ average_montly_hours  <int> 157, 262, 272, 223, 159, 153, 247, 259, 224, 142, 135, 305, 234, 148, 137, 143, 160, 255, 160, 262, 282, 147, 304, 139, 158, 242,... 
  19. $ time_spend_company    <int> 3, 6, 4, 5, 3, 3, 4, 5, 5, 3, 3, 4, 5, 3, 3, 3, 3, 6, 3, 5, 4, 3, 4, 3, 3, 5, 5, 3, 3, 3, 4, 3, 3, 3, 6, 4, 3, 3, 4, 3, 5, 3, 3, ... 
  20. $ Work_accident         <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... 
  21. left                  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... 
  22. $ promotion_last_5years <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... 
  23. $ sales                 <chr> "sales""sales""sales""sales""sales""sales""sales""sales""sales""sales""sales""sales""sales""sales", "sa... 
  24. $ salary                <chr> "low""medium""medium""low""low""low""low""low""low""low""low""low""low""low""low""low""low", "low... 
  25.  
  26.  satisfaction_level       last_evaluation        number_project  average_montly_hours    time_spend_company         Work_accident                  left  
  27.                     0                     0                     0                     0                     0                     0                     0  
  28. promotion_last_5years                 sales                salary  
  29.                     0                     0                     0  

数据集情况如下,一共10维数据,14999个观测值,变量的代表名称分别是

satisfaction_level--满意度,last_evaluation--***一次评估,number_project--参与项目数量,average_montly_hours--每月平均工作时间,time_spend_company--公司停留时间,Work_accident--工作事故次数,left--是否离职,promotion_last_5years--过去五年升值状况,sales--工种,salary--工资。

而且简单的观测了一下,没有发现缺失值,那么我就可以直接进入数据分析阶段了。

数据预处理

根据每一个特征的数值情况,我们可以将不少特征因子化,方便后期做不同类别的差异分析。

  1. hr$sales<-as.factor(hr$sales) 
  2. hr$salary<-as.factor(hr$salary) 
  3. hr$left<-as.factor(hr$left
  4. hr$Work_accident<-as.factor(hr$Work_accident) 
  5. hr$left<-recode(hr$left,'1'="yes",'0'="no"
  6. hr$promotion_last_5years<-as.factor(hr$promotion_last_5years) 

看的出大部分数据都是数值型的,我们使用相关性来衡量不同变量之间的相关性高低:

  1. cor.hr<-hr %>% select(-sales,-salary) 
  2. cor.hr$Work_accident<-as.numeric(as.character(cor.hr$Work_accident)) 
  3. cor.hr$promotion_last_5years<-as.numeric(as.character(cor.hr$promotion_last_5years)) 
  4. cor.hr$left<-as.numeric(as.character(cor.hr$left)) 
  5. corrplot(corr = cor(cor.hr),type = "lower",method = "square",title="变量相关性",order="AOE"

 

用R语言分析与预测员工离职

直观的来看,是否离职和满意度高低就有很高的关联性啊。

EDA

  1. ggplot(group_by(hr,sales),aes(x=sales,fill=sales))+geom_bar(width = 1)+coord_polar(theta = "x")+ggtitle("不同职业的人数"
  2. ggplot(hr,aes(x=sales,y=satisfaction_level,fill=sales))+geom_boxplot()+ggtitle("不同职业的满意度")+stat_summary(fun.y = mean,size=3,color='white',geom = "point")+ 
  3.   theme(legend.position = "none"
  4. ggplot(hr,aes(x=sales,y=satisfaction_level,fill=left))+geom_boxplot()+ggtitle("不同职业的满意度"
  5. ggplot(hr,aes(x=sales,y=average_montly_hours,fill=left))+geom_boxplot()+ggtitle("不同职业的工作时长"
  6. ggplot(hr,aes(x=sales,y=number_project,fill=left))+geom_boxplot()+ggtitle("不同职业的项目情况"

 

用R语言分析与预测员工离职

首先观察不同岗位的工作人数。搞销售的人数真的是不少,难道有不少我大生科的同学吗??(哈哈哈哈哈哈哈,开个玩笑而已,不过说实话做生物真的很累啊)。销售,后期支持,和技术岗人数占据人数排行榜前三。

用R语言分析与预测员工离职

不同的职业满意度的分布大体相当,不过accounting的小伙伴们似乎打分都不高哦,其他的几个工种均值和中位数都没有明显差别,接下来我们看看不同职业是否离职的情况和打分的高低情况:

用R语言分析与预测员工离职

和想象中结果几乎没有区别,离职和不离职的打分区分度很高,和职业几乎没有关系。

用R语言分析与预测员工离职

那么不同职业的平均工作时长呢,看图而言,没有离职的人群工作时间都很稳定,但是离职人群的工作时间呈现两极分化的趋势,看来太忙和太闲都不是很好,这对hr的考验还是很大的。

后面我们来一次关注一下不同特征和离职的关系问题:

  1. ggplot(hr,aes(x=satisfaction_level,color=left))+geom_line(stat = "density")+ggtitle("满意度和离职的关系"
  2. ggplot(hr,aes(x=salary,fill=left))+geom_histogram(stat="count")+ggtitle("工资和离职的关系"
  3. ggplot(hr,aes(x=promotion_last_5years,fill=left))+geom_histogram(stat="count")+ggtitle("近5年升值和离职的关系"
  4. ggplot(hr,aes(x=last_evaluation,color=left))+geom_point(stat = "count")+ggtitle("***一次评价和离职的关系"
  5. hr %>% group_by(sales) %>% ggplot(aes(x=sales,fill=Work_accident))+geom_bar()+coord_flip()+ 
  6.   theme(axis.text.x = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank())+scale_fill_discrete(labels=c("no accident","at least once")) 

 

用R语言分析与预测员工离职

没有离职的人群打分已知非常稳定,而离职人群的打分就有点难以估摸了

用R语言分析与预测员工离职

还是那句话,“有钱好办事啊”

用R语言分析与预测员工离职

你不给宝宝升职,宝宝就生气离职

用R语言分析与预测员工离职

和前面的面积图差不多,hr也要警惕那些***一次打分很高的,虽然大部分是不准备离职的,但是有些为了给老东家面子还是会来点“善意的谎言”的。

用R语言分析与预测员工离职

不出错是不可能的,出错人数多少基本和总人数成正比,所以这个对于离职来说不是问题。

模型构建和评估

  1. index<-sample(2,nrow(hr),replace = T,prob = c(0.7,0.3)) 
  2. train<-hr[index==1,];test<-hr[index==2,] 
  3. model<-randomForest(left~.,data = train) 
  4. predict.hr<-predict(model,test) 
  5. confusionMatrix(test$left,predict.hr) 
  6.  
  7. prob.hr<-predict(model,test,type="prob"
  8. roc.hr<-roc(test$left,prob.hr[,2],levels=levels(test$left)) 
  9. plot(roc.hr,type="S",col="red",main = paste("AUC=",roc.hr$auc,sep = "")) 

根据前面的特征分析,本次答主并没有觉得有很好的特征来提取,就直接扔进算法里面计算去了,计算出来的混淆矩阵的情况效果还是杠杠的:

  1. Confusion Matrix and Statistics 
  2.  
  3.           Reference 
  4. Prediction   no  yes 
  5.        no  3429    5 
  6.        yes   28 1010 
  7.                                            
  8.                Accuracy : 0.9926           
  9.                  95% CI : (0.9897, 0.9949) 
  10.     No Information Rate : 0.773            
  11.     P-Value [Acc > NIR] : < 2.2e-16        
  12.                                            
  13.                   Kappa : 0.9791           
  14.  Mcnemar's Test P-Value : 0.0001283        
  15.                                            
  16.             Sensitivity : 0.9919           
  17.             Specificity : 0.9951           
  18.          Pos Pred Value : 0.9985           
  19.          Neg Pred Value : 0.9730           
  20.              Prevalence : 0.7730           
  21.          Detection Rate : 0.7668           
  22.    Detection Prevalence : 0.7679           
  23.       Balanced Accuracy : 0.9935           
  24.                                            
  25.        'Positive' Class : no    

acc=0.9926,recall=0.9951,precision=0.9730,基本都是逆天的数据了,看来kaggle的数据集已经清洗的很棒了,rf算法也是一如既往地给力。***贴出ROC曲线的图

用R语言分析与预测员工离职

写在***

本次分析其实并没有很多的技巧可言,答主的ggplot2水平也遇到了瓶颈期,后期需要不断加强,而且只会调包不懂算法后面的原理更是不可以的,所以最近在慢慢把概率论,线性代数,还是统计学捡起来,当然R语言的数据分析实践还是不会停下来的,答主英语还不错,可以和实验室的老外教授“忽悠”几句,也算是有了不少的进步。

道阻且长,大家共勉~~~

责任编辑:未丽燕 来源: 经管人学数据分析
相关推荐

2009-05-21 16:08:56

谷歌演算程序员工离职

2009-03-09 09:02:37

雅虎微软离职计划

2014-08-19 09:56:37

程序员

2013-04-10 10:31:21

R语言

2020-05-15 15:09:51

R语言数据分析

2010-08-23 10:54:26

2015-02-27 13:51:32

数据安全

2023-07-10 16:01:56

2015-06-16 10:58:59

苹果创业员工

2009-06-10 10:33:33

华为离职员工定律

2015-10-30 11:52:09

数据挖掘术语

2013-05-22 08:55:14

R语言

2010-06-25 11:09:27

隐性裁员

2010-01-27 09:33:11

松下员工

2009-02-24 11:37:30

遣散费补偿金裁员

2010-10-19 10:39:45

鲍尔默软件架构师

2009-05-15 16:55:28

自费离职裁员

2014-07-03 21:25:28

TeradataR语言

2020-07-09 15:21:58

大数据RStudioR语言

2009-01-20 09:02:33

谷歌离职员工薪水
点赞
收藏

51CTO技术栈公众号