R语言数据分析实例一:离职率分析与建模预测.pdf
- 文档编号:18632975
- 上传时间:2023-08-23
- 格式:PDF
- 页数:23
- 大小:1.02MB
R语言数据分析实例一:离职率分析与建模预测.pdf
《R语言数据分析实例一:离职率分析与建模预测.pdf》由会员分享,可在线阅读,更多相关《R语言数据分析实例一:离职率分析与建模预测.pdf(23页珍藏版)》请在冰点文库上搜索。
R语数据分析实例:
离职率分析与建模预测R语数据分析实例:
离职率分析与建模预测、背景说明、背景说明本分析利IBM离职员数据进分析。
在对离职率的影响因素进观察的基础上,建模型并预测哪些员更易离职。
般,数据分析分为三个步骤:
数据收集与清洗、探索性分析和建模预测。
本的数据集是IBM于研究员预测的模拟数据,数据分完整,需清洗。
因此,本主要分为三个部分:
对于些重要的变量进探索性分析;分析导致员离职的因素,并挖掘相关因素的影响程度;通过算法构建模型,预测哪些员有可能离职。
通过对IBM离职员数据实践,本希望发掘出影响员流失的因素,并对利R语进数据分析过程进复习,深化对数据分析作意义的理解。
、数据集说明、数据集说明IBM离职员数据集共有35个变量,1470个观测个案。
部分需要重点关注的变量如下:
重点变量信息上述变量可以分为三个部分:
基本的份信息变量:
性别、年龄、学历、任职过的企业数量、婚姻状况;员公司份变量:
龄、在公司作的时间、职位、职级、薪酬与福利变量:
薪、作投、绩效评分、认购优先股的级别、涨薪列、上年度培训次数、距离上次升职的时间间隔活质量相关变量:
作环境满意度、作满意度、关系满意度、作与活平衡情况、上班距离、是否加班、出差情况三、探索性数据分析三、探索性数据分析载分析包和数据集library(tidyverse)library(rpart)library(rpart.plot)library(randomForest)library(gbm)library(ggplot2)library(ggthemes)#提供describe函数library(Hmisc)#提供grid.arrange()函数,于排列图library(gridExtra)#提供roc函数library(pROC)Attr_data-read.csv(file=D:
/MY/数据分析/RWorld/实验室/IBM_Employee_Attrition.csv)()描述性统计信息()描述性统计信息str(Attr_data)describe(Attr_data)描述性统计信息描述性统计信息描述性统计信息三通过描述性统计可以初步观测到:
员员平均年龄约36岁,最的60岁,最的18岁;全部1470名员中,离职的237,离职率16%;员平均收6500,中位收4919,最1009,最19999;()可视化探索()可视化探索1.基本份信息1.基本份信息p_Gender-ggplot(data=Attr_data,aes(x=Gender)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized_2()+labs(title=性别VS离职,x=性别,y=例)p_Age-ggplot(data=Attr_data,aes(x=Age)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized_2()+labs(title=年龄VS离职,x=年龄,y=)+scale_x_continuous(breaks=seq(18,60,5)p_Education-ggplot(data=Attr_data,aes(x=Education)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized_2()+labs(title=教育程度VS离职,x=教育程度,y=例)p_MaritalStatus-ggplot(data=Attr_data,aes(x=MaritalStatus)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized_2()+labs(title=婚姻状况VS离职,x=婚姻状况,y=例)p_NumCompaniesWorked-ggplot(data=Attr_data,aes(x=NumCompaniesWorked)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized_2()+labs(title=作过的企业数量VS离职,x=作过的企业数量,y=)+scale_x_continuous(breaks=seq(0,9,1)性别与离职率年龄与离职率受教育程度与离职率婚姻状况与离职率作企业数与离职率分析结果:
离职率与性别关系不;33岁以下的更易离职;受教育程度越,离职率越低,但是区别并不是特别明显;相较,未婚单群更易离职:
任职企业数超过5家的更易离职:
2.员公司份信息2.员公司份信息p_TotalWorkingYears-ggplot(data=Attr_data,aes(x=TotalWorkingYears)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=总龄VS离职,x=总龄,y=)+scale_x_continuous(breaks=seq(0,40,5)p_YearsAtCompany-ggplot(data=Attr_data,aes(x=YearsAtCompany)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=本公司龄VS离职,x=本公司龄,y=)+scale_x_continuous(breaks=seq(0,40,5)p_JobRole-ggplot(data=Attr_data,aes(x=JobRole)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=职位VS离职,x=职位,y=例)+theme(axis.text.x=element_text(angle=90)p_JobLevel-ggplot(data=Attr_data,aes(x=JobLevel)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=职级VS离职,x=职级,y=例)总龄与离职本公司龄与离职职级与离职职位与离职分析结果:
总龄于8年的更易离职;在本公司作时间于4年的更易离职;职级偏低的员更易离职;销售部门员离职率偏;3.薪资与福利信息3.薪资与福利信息
(1)薪、作投和绩效评分
(1)薪、作投和绩效评分p_MonthlyIncome-ggplot(data=Attr_data,aes(x=MonthlyIncome)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=薪VS离职,x=薪,y=)+scale_x_continuous(breaks=seq(0,20000,3000)+theme(axis.text.x=element_text(angle=15)p_JobInvolvement-ggplot(data=Attr_data,aes(x=JobInvolvement)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=作投VS离职,x=作投,y=例)p_PerformanceRating-ggplot(data=Attr_data,aes(x=PerformanceRating)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=绩效评分VS离职,x=绩效评分,y=例)+scale_x_continuous(breaks=seq(3,4,1)Attr_data$JobInvolvement1-as.character(Attr_data$JobInvolvement)p_JobInvolvement_MonthlyIncome-ggplot(data=Attr_data,aes(x=JobInvolvement1,y=MonthlyIncome)+geom_boxplot(aes(fill=Attrition)+theme_solarized_2()+labs(title=作投与薪,x=作投,y=薪)作投与离职绩效评分与离职薪与离职投产出与离职分析结果:
作投越,离职率越低绩效评分对离职影响不;薪在4000以下和10000左右的员离职率更;在同等投下,离职员的薪未离职的员偏低;
(2)福利相关变量
(2)福利相关变量p_StockOptionLevel-ggplot(data=Attr_data,aes(x=StockOptionLevel)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=股权认购优先级别VS离职,x=股权认购优先级别,y=列)p_PercentSalaryHike-ggplot(data=Attr_data,aes(x=PercentSalaryHike)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=涨薪例VS离职,x=涨薪例,y=)+scale_x_continuous(breaks=seq(0,26,2)p_TrainingTimesLastYear-ggplot(data=Attr_data,aes(x=TrainingTimesLastYear)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=上年度培训次数VS离职,x=上年度培训次数,y=例)+scale_x_continuous(breaks=seq(0,6,1)p_YearsSinceLastPromotion-ggplot(data=Attr_data,aes(x=YearsSinceLastPromotion)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=距上次升职间隔VS离职,x=距上次升职间隔,y=)+scale_x_continuous(breaks=seq(0,15,1)优先股认购权与离职涨薪列与离职培训与离职升职与离职分析结果:
股权认购优先级别最和最低的均更有可能离职;涨薪在15%和17%,及于22%左右的离职率较;总体,培训次数越少越有可能离职;距离上次涨薪间隔于半年和7年左右的更易离职;4.活质量相关4.活质量相关
(1)主观满意度调查
(1)主观满意度调查p_EnvirnomentSatisfaction-ggplot(data=Attr_data,aes(x=EnvironmentSatisfaction)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=作环境满意度VS离职,x=作环境满意度,y=列)p_JobSatisfication-ggplot(data=Attr_data,aes(x=JobSatisfaction)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=作满意度VS离职,x=作满意度,y=列)p_RelationshipSatisfaction-ggplot(data=Attr_data,aes(x=RelationshipSatisfaction)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=关系满意度VS离职,x=关系满意度,y=列)p_WorkLifeBalance-ggplot(data=Attr_data,aes(x=WorkLifeBalance)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=作与活平衡VS离职,x=作与活平衡,y=列)作环境满意度与离职作满意度与离职作关系满意度与离职作活平衡与离职分析结果:
各项满意度较低的更易离职:
作与活不平衡的最易离职;
(2)客观作活冲突
(2)客观作活冲突p_DistanceFromHome-ggplot(data=Attr_data,aes(x=DistanceFromHome)+geom_density(aes(fill=Attrition),alpha=0.7)+theme_solarized()+labs(title=上班距离VS离职,x=上班距离,y=)+scale_x_continuous(breaks=seq(1,29,1)p_OverTime-ggplot(data=Attr_data,aes(x=OverTime)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=加班情况VS离职,x=加班情况,y=列)p_BusinessTravel-ggplot(data=Attr_data,aes(x=BusinessTravel)+geom_bar(aes(fill=Attrition),position=fill)+theme_solarized()+labs(title=出差VS离职,x=出差,y=列)+theme(axis.text.x=element_text(angle=90)加班与离职上班距离与离职出差与离职分析结果:
加班多的更易离职;上班距离于11公的更易离职:
经常出差的更易离职;(三)探索性分析结论(三)探索性分析结论基于对数据的探索性分析,员离职有多因素的影响,主要有:
1.作与活的不平衡加班、离家远和出差等;2.作投如果不能获得相匹配的回报,员更倾向离职;3.优先股认购等福利是员较为关注的回报形式;4.年龄、任职过的公司数量的因素也会影响员离职率;四、训练模型四、训练模型()决策树模型()决策树模型1.变量整理1.变量整理删除需要的变量:
EmployeeCount,EmployeeNumber,Over18,StandardHours变量重新编码:
JobRole,EducationFiledAttr_data_predicted%select(-EmployeeCount,-EmployeeNumber,-Over18,-StandardHours)levels(Attr_data$JobRole)-c(HC,HR,Lab,Man,MDir,RsD,RsSci,SlEx,SlRep)levels(Attr_data$EducationField)-c(HR,LS,MRK,MED,NA,TD)2.分割数据2.分割数据set.seed(3221)n-nrow(Attr_data_predicted)rnd-sample(n,n*0.7)train-Attr_data_predictedrnd,test-Attr_data_predicted-rnd,3.建模预测3.建模预测dtree-rpart(Attrition.,data=trian)preds-predict(dtree,test,type=class)rcov-roc(as.numeric(test$Attrition),as.numeric(preds)#观察模型可性rcov$aucprop.table(table(test$Attrition,preds,dnn=c(Actual,Predicted),1)#绘制决策树图dtreepr-prune(dtree,cp=0.01666667)predspr-predict(dtreepr,test,type=class)rocvpr-roc(as.numeric(test$Attrition),as.numeric(predspr)rocvpr$aucrpart.plot(dtreepr,type=4,tweak=0.9,fallen.leaves=F,cex=0.7)Areaunderthecurve:
0.6417PredictedActualNoYesNo0.935483870.06451613Yes0.652173910.34782609Areaunderthecurve:
0.6334决策树模型分析结果表明:
ROC曲线以下的积(AUC)为0.6417,偏低;特异度虽然有0.9354,但是灵敏度仅有0.3478;经修剪之后的决策树AUC仍然有0.633,并未损多少精确度。
这说明决策树模型来预测的话,得到的正确结果不。
同时,决策树模型表明,加班、收、员优先认股权占据主要原因。
()随机森林模型()随机森林模型set.seed(2343)fit_forest-randomForest(Attrition.,data=train)rfpreds-predict(fit_forest,test,type=class)#计算AUC积rocrf-roc(as.numeric(test$Attrition),as.numeric(rfpreds)rocrf$aucAreaunderthecurve:
0.5612随机森林所得的AUC值为0.5612,于决策树模型。
(三)GBM模型(三)GBM模型set.seed(3433)#定义10折交叉检验的控制器于下所有GBM模型的训练ctrl-trainControl(method=cv,number=10,summaryFunction=twoClassSummary,classProbs=TRUE)fit_gbm-train(Attrition.,data=train,method=gbm,verbose=FALSE,metric=ROC,trControl=ctrl)gbmpreds-predict(fit_gbm,test)rocgbm-roc(as.numeric(test$Attrition),as.numeric(gbmpreds)rocgbm$aucAreaunderthecurve:
0.5915GBM模型得到的AUC值为0.5915(四)对GBM模型进优化(四)对GBM模型进优化对于对于随机森林和GBM的法,AUC值于单决策树模型的AUC值的情况较少见,这显然说明单的树拟合得更好或者更稳定的情况。
(般需要得到AUC值于0.75的模型)当结果分类变量之间的列是1:
10或者更的时候,通常需要考虑优化模型。
本例中,离职变量的列是1:
5左右,但仍然可能是合理的,因为在决策树中看到的主要问题是预测那些实际离开的(敏感度)。
#设置与之前GBM建模控制器致的种ctrl$seeds-fit_gbm$control$seeds1.通过加权的式优化GBM模型1.通过加权的式优化GBM模型加权旨在降低少数群体中的错误,这是离职群体。
#设置权重参数,提离职群体的样本权重model_weights-ifelse(train$Attrition=No,(1/table(train$Attrition)1),(1/table(train$Attrition)2)weightedfit-train(Attrition.,data=train,method=gbm,verbose=FALSE,weights=model_weights,metric=ROC,trControl=ctrl)weightedpreds-predict(weightedfit,test)rocweight-roc(as.numeric(test$Attrition),as.numeric(weightedpreds)rocweight$aucAreaunderthecurve:
0.78032.向上采样2.向上采样向上采样(up-sampling)指从多数类中随机删除实例。
ctrl$sampling-upset.seed(3433)upfit-train(Attrition.,data=train,method=gbm,verbose=FALSE,metric=ROC,trControl=ctrl)uppreds-predict(upfit,test)rocup-roc(as.numeric(test$Attrition),as.numeric(uppreds)rocup$aucAreaunderthecurve:
0.73873.向下采样3.向下采样向下采样(down-sampling)指从少数类中复制实例。
ctrl$sampling-downset.seed(3433)downfit-train(Attrition.,data=train,method=gbm,verbose=FALSE,metric=ROC,trControl=ctrl)downpreds-predict(downfit,test)rocdown-roc(as.numeric(test$Attrition),as.numeric(downpreds)rocdown$aucAreaunderthecurve:
0.7491分析结果表明:
加权调整的模型表现最好,相较于单纯的随机森林和GBM模型,AUC值从0.5612上升0.7803,灵敏度也达到了0.7276。
据此,后续将采加权调整后的模型进预测。
prop.table(table(test$Attrition,weightedpreds,dnn=c(Actual,Predicted),1)PredictedActualNoYesNo0.83602150.1639785Yes0.27536230.7246377五、模型应五、模型应已经训练出个表现较好的模型。
将其应于实践时,需要注意以下个:
检查变量重要性列表。
此例中就需要就是确定哪些因素有助于确定员可能会离开;利模型计算每个离开的可能性。
在此例中,通过计算出来的可能性,可以对应的成个新的变量,例如:
个离开的可能性,且有较的绩效评级,为公司做出杰出贡献。
那么这个变量就可以成新的变量,建个重点关注指标,帮助管理员理解需要哪些员是重点关注对象,并以种机智的式进管理或交谈。
利上述计算出的概率评估公司的组织构架。
例如可以评估哪个部门或离开的可能性最,然后在指导公司的作向,或者对该部门或进额外的分析,以制定合适的策略。
()变量重要性列表()变量重要性列表varImp(weightedfit)gbmvariableimportanceonly20mostimportantvariablesshown(outof47)OverallOverTimeYes100.00MonthlyIncome57.94JobLevel56.12Age41.13NumCompaniesWorked34.17JobSatisfaction33.12YearsAtCompany25.67DistanceFromHome24.09EnvironmentSatisfaction23.28StockOptionLevel22.58YearsWithCurrManager22.55DailyR
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 数据 分析 实例 离职 建模 预测