Classification and Regression Tree

Srishti Puri

4/22/2021

Question: Create a model predicting “left” using a classification tree, and interpret the results. Use the HR_comma_sep.csv data.

Requirements:

- Perform any initial data preparations. 
- Split the data into training and validation.
- Develop an initial model.
- Which are the most important variables.
- Report on the accuracy of the model.
- Interpret two (2) complete paths.

Results of the Model

Characterizing loyalty:

Characterizing left:

Confusion Matrix

## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  4459 
## 
##  
##              | val$left_predicted 
##     val$left |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |      3362 |        37 |      3399 | 
##              |   207.226 |   702.243 |           | 
##              |     0.989 |     0.011 |     0.762 | 
##              |     0.976 |     0.036 |           | 
##              |     0.754 |     0.008 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        81 |       979 |      1060 | 
##              |   664.491 |  2251.814 |           | 
##              |     0.076 |     0.924 |     0.238 | 
##              |     0.024 |     0.964 |           | 
##              |     0.018 |     0.220 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |      3443 |      1016 |      4459 | 
##              |     0.772 |     0.228 |           | 
## -------------|-----------|-----------|-----------|
## 
## 

Initial Preparations of the Data set

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

Summary of the variable ‘salary’

##      var frequency percentage cumulative_perc
## 1    low      7316      48.78           48.78
## 2 medium      6446      42.98           91.76
## 3   high      1237       8.25          100.00

Summary of the variable ‘sales’

##            var frequency percentage cumulative_perc
## 1        sales      4140      27.60           27.60
## 2    technical      2720      18.13           45.73
## 3      support      2229      14.86           60.59
## 4           IT      1227       8.18           68.77
## 5  product_mng       902       6.01           74.78
## 6    marketing       858       5.72           80.50
## 7        RandD       787       5.25           85.75
## 8   accounting       767       5.11           90.86
## 9           hr       739       4.93           95.79
## 10  management       630       4.20          100.00

Splitting the Data into Training and Validation:

To split data into training (train) and test set: validation (val)

- train (10540 observations with 10 variables) and
- val (4459 observations with 10 variables).

Creating and Interpreting the Classification Tree:

##           CP nsplit rel error    xerror        xstd
## 1 0.25766627      0 1.0000000 1.0000000 0.017417550
## 2 0.17960972      1 0.7423337 0.7423337 0.015599681
## 3 0.07347670      3 0.3831143 0.3831143 0.011774919
## 4 0.05137395      5 0.2361609 0.2361609 0.009421204
## 5 0.03305456      6 0.1847869 0.1855834 0.008404791
## 6 0.01632816      7 0.1517324 0.1525289 0.007650945
## 7 0.01035444      8 0.1354042 0.1385902 0.007305540
## 8 0.01000000      9 0.1250498 0.1330147 0.007161995

Interpreting Two Complete Paths:

Path 1 : Will Not Leave (loyal)

Path 2 : Will Leave (left)

Variable importance:

##                       variable_importance
## satisfaction_level            2127.439578
## number_project                1074.512028
## average_montly_hours          1052.234047
## last_evaluation               1039.697458
## time_spend_company             814.319253
## Work_accident                   30.746512
## promotion_last_5years            7.838264
## sales                            5.842639

Reporting accuracy of the model:

Using the Validation data set:

##  satisfaction_level last_evaluation  number_project  average_montly_hours
##  Min.   :0.0900     Min.   :0.3600   Min.   :2.000   Min.   : 97.0       
##  1st Qu.:0.4400     1st Qu.:0.5600   1st Qu.:3.000   1st Qu.:156.0       
##  Median :0.6400     Median :0.7200   Median :4.000   Median :200.0       
##  Mean   :0.6112     Mean   :0.7181   Mean   :3.792   Mean   :201.7       
##  3rd Qu.:0.8100     3rd Qu.:0.8700   3rd Qu.:5.000   3rd Qu.:247.0       
##  Max.   :1.0000     Max.   :1.0000   Max.   :7.000   Max.   :310.0       
##                                                                          
##  time_spend_company Work_accident    left     promotion_last_5years
##  Min.   : 2.000     Min.   :0.0000   0:3399   Min.   :0.00000      
##  1st Qu.: 3.000     1st Qu.:0.0000   1:1060   1st Qu.:0.00000      
##  Median : 3.000     Median :0.0000            Median :0.00000      
##  Mean   : 3.493     Mean   :0.1518            Mean   :0.02108      
##  3rd Qu.: 4.000     3rd Qu.:0.0000            3rd Qu.:0.00000      
##  Max.   :10.000     Max.   :1.0000            Max.   :1.00000      
##                                                                    
##          sales         salary     left_predicted
##  sales      :1240   high  : 349   0:3443        
##  technical  : 829   low   :2135   1:1016        
##  support    : 655   medium:1975                 
##  IT         : 367                               
##  product_mng: 271                               
##  marketing  : 267                               
##  (Other)    : 830