Srishti Puri
4/21/2021
Applied logistic regression to the data about human resource to explore which variables affect leaving of the employees.
The total number of employees left is 1055. The model, at the cutoff of 0.49, predicted 1057.
Predicting the variable, ‘left’ :
Of the total loyal, 95.3% of the employees were accurately predicted as loyal.
Of the total loyal, 4.7% were falsely predicted to leave.
Of the total left, 84.9% of the employees were accurately predicted to leave.
Of the total left, 15.1% were inaccurately predicted as loyal.
The model is 92.9% of the times correct.
The model is 7.1% of the times incorrect.
In the final predictive model, variable ‘left’ is effected by:
- satisfaction_level +
last_evaluation +
number_project +
average_montly_hours +
time_spend_company +
satisfaction_level:last_evaluation +
satisfaction_level:number_project +
satisfaction_level:average_montly_hours +
satisfaction_level:time_spend_company +
last_evaluation:number_project +
last_evaluation:time_spend_company +
number_project:average_montly_hours +
number_project:time_spend_company +
number_project:sales +
average_montly_hours:time_spend_company +
time_spend_company:Work_accident##
##
## Cell Contents
## |-------------------------|
## | N |
## | Chi-square contribution |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 4496
##
##
## | vallr$predicted_left01
## vallr$left | 0 | 1 | Row Total |
## -------------|-----------|-----------|-----------|
## 0 | 3280 | 161 | 3441 |
## | 159.522 | 519.014 | |
## | 0.953 | 0.047 | 0.765 |
## | 0.954 | 0.152 | |
## | 0.730 | 0.036 | |
## -------------|-----------|-----------|-----------|
## 1 | 159 | 896 | 1055 |
## | 520.300 | 1692.821 | |
## | 0.151 | 0.849 | 0.235 |
## | 0.046 | 0.848 | |
## | 0.035 | 0.199 | |
## -------------|-----------|-----------|-----------|
## Column Total | 3439 | 1057 | 4496 |
## | 0.765 | 0.235 | |
## -------------|-----------|-----------|-----------|
##
##
There are 8 continuous variables and 2 categorical variables.
Satisfaction level: Most employees are highly satisfied.
Last Evaluation: Most employees are good performers with 75% of the data set being evaluated between 56 percent - 87 percent.
Number of Projects: Most employees do 4 projects and fewer engage in over 6.
Average Monthly Hours: Most employees spend, fairly, a higher amount of hours at work.
Time Spent in Company: Fewer employees stay beyond 4 years.
## var frequency percentage cumulative_perc
## 1 0 12830 85.54 85.54
## 2 1 2169 14.46 100.00
## var frequency percentage cumulative_perc
## 1 0 14680 97.87 97.87
## 2 1 319 2.13 100.00
## var frequency percentage cumulative_perc
## 1 0 11428 76.19 76.19
## 2 1 3571 23.81 100.00
The regression equation was applied to the trainlr data set.
Variable “left” was tested as a factor of all variables along with interactions between them.
Significant P values are:
satisfaction_level, last_evaluation, number_project, average_montly_hours, time_spend_company;
satisfaction_level:last_evaluation, satisfaction_level:number_project, satisfaction_level:average_montly_hours, satisfaction_level:time_spend_company, last_evaluation:number_project, last_evaluation:time_spend_company, number_project:average_montly_hours, number_project:time_spend_company, number_project:sales, average_montly_hours:time_spend_company and time_spend_company:Work_accident.
The final model is:
- satisfaction_level + last_evaluation + number_project + average_montly_hours + time_spend_company + satisfaction_level:last_evaluation + satisfaction_level:number_project + satisfaction_level:average_montly_hours + satisfaction_level:time_spend_company + last_evaluation:number_project + last_evaluation:time_spend_company + number_project:average_montly_hours + number_project:time_spend_company + number_project:sales + average_montly_hours:time_spend_company + time_spend_company:Work_accident.Variables with negative coefficients are inversely proportional to the dependent variable ‘left’ , which are:
- Satisfaction_level,
- last_evaluation,
- number_project,
- average_montly_hours,
- time_spend_companyFor instance, lower satisfaction level would mean greater number of employees leaving, lower number of projects would mean greater number of employees leaving etcetera.
The AIC of the initial model is 8989 whereas the AIC for the final model (fitlr2) is 4591.2
Keeping fitlr2 (final model) due to lower AIC.
##
## Call:
## glm(formula = left ~ ., family = binomial(link = "logit"), data = trainlr)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.2206 -0.6613 -0.3960 -0.1138 3.0280
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.1269779 0.2285857 -4.930 8.21e-07 ***
## satisfaction_level -4.3165425 0.1190481 -36.259 < 2e-16 ***
## last_evaluation 0.6208105 0.1798334 3.452 0.000556 ***
## number_project -0.3113181 0.0256478 -12.138 < 2e-16 ***
## average_montly_hours 0.0040751 0.0006196 6.577 4.81e-11 ***
## time_spend_company 0.2575157 0.0184363 13.968 < 2e-16 ***
## Work_accident -1.5543248 0.1094542 -14.201 < 2e-16 ***
## promotion_last_5years -1.3142357 0.2833255 -4.639 3.51e-06 ***
## saleshr 0.1345257 0.1552961 0.866 0.386352
## salesIT -0.3283652 0.1445313 -2.272 0.023091 *
## salesmanagement -0.3219024 0.1824519 -1.764 0.077679 .
## salesmarketing -0.0082122 0.1549095 -0.053 0.957721
## salesproduct_mng -0.2008565 0.1541501 -1.303 0.192577
## salesRandD -0.7776814 0.1731848 -4.490 7.11e-06 ***
## salessales -0.1576817 0.1199494 -1.315 0.188655
## salessupport -0.0246326 0.1285122 -0.192 0.847997
## salestechnical 0.0061095 0.1251695 0.049 0.961071
## salarylow 1.9435242 0.1519826 12.788 < 2e-16 ***
## salarymedium 1.4551725 0.1526778 9.531 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 11565 on 10502 degrees of freedom
## Residual deviance: 8951 on 10484 degrees of freedom
## AIC: 8989
##
## Number of Fisher Scoring iterations: 5
##
## Call:
## glm(formula = left ~ satisfaction_level + last_evaluation + number_project +
## average_montly_hours + time_spend_company + satisfaction_level:last_evaluation +
## satisfaction_level:number_project + satisfaction_level:average_montly_hours +
## satisfaction_level:time_spend_company + last_evaluation:number_project +
## last_evaluation:time_spend_company + number_project:average_montly_hours +
## number_project:time_spend_company + number_project:sales +
## average_montly_hours:time_spend_company + time_spend_company:Work_accident,
## family = binomial(link = "logit"), data = trainlr)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -3.7288 -0.2939 -0.1518 -0.0074 5.6382
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 4.499e+01 1.034e+00 43.498 < 2e-16
## satisfaction_level -2.762e+01 1.312e+00 -21.054 < 2e-16
## last_evaluation -3.333e+01 1.399e+00 -23.823 < 2e-16
## number_project -7.987e+00 2.346e-01 -34.052 < 2e-16
## average_montly_hours -8.620e-02 4.645e-03 -18.556 < 2e-16
## time_spend_company -2.956e+00 2.002e-01 -14.763 < 2e-16
## satisfaction_level:last_evaluation 2.111e+01 1.371e+00 15.400 < 2e-16
## satisfaction_level:number_project -9.495e-01 1.909e-01 -4.973 6.60e-07
## satisfaction_level:average_montly_hours 2.785e-02 4.735e-03 5.883 4.04e-09
## satisfaction_level:time_spend_company 1.770e+00 1.342e-01 13.192 < 2e-16
## last_evaluation:number_project 5.398e+00 2.502e-01 21.577 < 2e-16
## last_evaluation:time_spend_company 1.151e+00 1.945e-01 5.919 3.25e-09
## number_project:average_montly_hours 1.866e-02 8.598e-04 21.700 < 2e-16
## number_project:time_spend_company 1.485e-01 3.080e-02 4.822 1.42e-06
## number_project:saleshr 5.219e-02 6.272e-02 0.832 0.40532
## number_project:salesIT -3.990e-02 5.556e-02 -0.718 0.47269
## number_project:salesmanagement -2.019e-01 6.607e-02 -3.056 0.00224
## number_project:salesmarketing -3.680e-02 6.173e-02 -0.596 0.55109
## number_project:salesproduct_mng -8.445e-02 5.937e-02 -1.422 0.15489
## number_project:salesRandD -1.392e-01 6.137e-02 -2.268 0.02330
## number_project:salessales -3.363e-02 4.697e-02 -0.716 0.47410
## number_project:salessupport 3.609e-02 4.984e-02 0.724 0.46896
## number_project:salestechnical 2.879e-02 4.875e-02 0.590 0.55488
## average_montly_hours:time_spend_company 3.296e-03 6.977e-04 4.724 2.31e-06
## time_spend_company:Work_accident -3.598e-01 3.664e-02 -9.817 < 2e-16
##
## (Intercept) ***
## satisfaction_level ***
## last_evaluation ***
## number_project ***
## average_montly_hours ***
## time_spend_company ***
## satisfaction_level:last_evaluation ***
## satisfaction_level:number_project ***
## satisfaction_level:average_montly_hours ***
## satisfaction_level:time_spend_company ***
## last_evaluation:number_project ***
## last_evaluation:time_spend_company ***
## number_project:average_montly_hours ***
## number_project:time_spend_company ***
## number_project:saleshr
## number_project:salesIT
## number_project:salesmanagement **
## number_project:salesmarketing
## number_project:salesproduct_mng
## number_project:salesRandD *
## number_project:salessales
## number_project:salessupport
## number_project:salestechnical
## average_montly_hours:time_spend_company ***
## time_spend_company:Work_accident ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 11565.1 on 10502 degrees of freedom
## Residual deviance: 4541.2 on 10478 degrees of freedom
## AIC: 4591.2
##
## Number of Fisher Scoring iterations: 7
Using the validation data set to test the model.
Created the variable ‘logit’ using the function predict.glm in the validation data set. However, this isn’t the predicted value of the dependent variable ‘left.’
The predicted values will exist between 0 and 1 giving us a probability of ‘left’ for each observation.
Created the variable, ‘predicted_left’ using the variable ‘logit.’
The mean for the predicted_left is 0.23 same as that of the variable ‘left’ which is at 0.23.
The mean value of ‘predicted_left’ is reflective of the actual number of employees who left on an average.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00000 0.01006 0.03875 0.23818 0.39450 0.99962
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.2347 0.0000 1.0000
The ‘predicted_left’ gives us a probability between 0 and 1, however, does not enable us to judge if the employee will remain loyal or loyal.
To classify an employee as the one to leave or not, we establish a cutoff.
The cutoff resembles a threshold which will classify an observation as the one to leave or not.
A total of 1055 employees actually left the organization, so, we are going to get 1055 highest probabilities.
Also, 1055 forms lesser than 50 percent of the data making it reasonable to reject median value (0.04) as the cutoff.
To judge a reasonable cutoff, I sorted the variable ‘predicted_left’ in descending order. Further, I filtered the data set up to the row_number of the total number of observations left (row number: 1055).
The new data set ‘cutoff’ displays the row_number 1055 where the predicted_left value is 0.49.
The threshold (cutoff) to classify a predicted value as ‘left’ is set to 0.49.
Mutated ‘predicted_left’ into a new variable ‘predicted_left01’ where the predicted value is classified as 1 if it is greater than or equal to the cutoff (0.49), else, 0.
1 represents predicted to leave and 0 is predicted as not to.
There are a total of 1057 observations predicted to leave after applying the cutoff.
The actual number of employees which left were 1055.
## [1] 1057