Logistic Regression Model

Srishti Puri

4/21/2021

Question: Create a model predicting “left” using logistic regression and interpret the results.

Applied logistic regression to the data about human resource to explore which variables affect leaving of the employees.

Result of the model:

CrossTable(vallr$left , vallr$predicted_left01)
## 
##  
##    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 |           | 
## -------------|-----------|-----------|-----------|
## 
## 

Initial data preparations.

Exploring Data

There are 8 continuous variables and 2 categorical variables.

Summary of the variable ‘Work_accident’

##   var frequency percentage cumulative_perc
## 1   0     12830      85.54           85.54
## 2   1      2169      14.46          100.00

Summary of the variable ‘promotion_last_5years’

##   var frequency percentage cumulative_perc
## 1   0     14680      97.87           97.87
## 2   1       319       2.13          100.00

Summary of the variable ‘left’

##   var frequency percentage cumulative_perc
## 1   0     11428      76.19           76.19
## 2   1      3571      23.81          100.00

Spliting data into training and validation.

Developing an initial model

Final Model:

Interpreting the coefficients of the final model:

For instance, lower satisfaction level would mean greater number of employees leaving, lower number of projects would mean greater number of employees leaving etcetera.

Summary of the initial model:

## 
## 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

Summary of the final model:

## 
## 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

Report on the accuracy of the model:

Summary of ‘predicted_left’

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.01006 0.03875 0.23818 0.39450 0.99962

Summary of ‘left’

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  0.0000  0.2347  0.0000  1.0000

How many ‘left’ observations in the validation data set?

Judging a cutoff:

Applying the new cutoff:

## [1] 1057