Random Forest

Srishti Puri

4/28/2021

Question: Create a model predicting ‘left’ using random forest, and interpret the results. Use the HR_comma_sep.csv data.

Requirements:

1. Perform any initial data preparations. 
2. Split the data into training and validation.
3. Develop an initial model. 
4. Which are the most important variables.
5. Report on the accuracy of the model.

Results:

## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  4459 
## 
##  
##              | val_rf$predicted_left 
##  val_rf$left |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |      3390 |         9 |      3399 | 
##              |   231.507 |   768.774 |           | 
##              |     0.997 |     0.003 |     0.762 | 
##              |     0.989 |     0.009 |           | 
##              |     0.760 |     0.002 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        37 |      1023 |      1060 | 
##              |   742.352 |  2465.155 |           | 
##              |     0.035 |     0.965 |     0.238 | 
##              |     0.011 |     0.991 |           | 
##              |     0.008 |     0.229 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |      3427 |      1032 |      4459 | 
##              |     0.769 |     0.231 |           | 
## -------------|-----------|-----------|-----------|
## 
## 

Initial data preparations:

Explore 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 Data into Training and Validation:

##  satisfaction_level last_evaluation  number_project  average_montly_hours
##  Min.   :0.0900     Min.   :0.3600   Min.   :2.000   Min.   : 96.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.6128     Mean   :0.7161   Mean   :3.803   Mean   :201.1       
##  3rd Qu.:0.8200     3rd Qu.:0.8700   3rd Qu.:5.000   3rd Qu.:245.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     0:12830       0:11428   0:14680              
##  1st Qu.: 3.000     1: 2169       1: 3571   1:  319              
##  Median : 3.000                                                  
##  Mean   : 3.498                                                  
##  3rd Qu.: 4.000                                                  
##  Max.   :10.000                                                  
##                                                                  
##          sales         salary         random         
##  sales      :4140   high  :1237   Min.   :0.0000653  
##  technical  :2720   low   :7316   1st Qu.:0.2513756  
##  support    :2229   medium:6446   Median :0.4936664  
##  IT         :1227                 Mean   :0.4979971  
##  product_mng: 902                 3rd Qu.:0.7470968  
##  marketing  : 858                 Max.   :0.9999414  
##  (Other)    :2923

Model:

##                 Length Class  Mode     
## call                5  -none- call     
## type                1  -none- character
## predicted       10540  factor numeric  
## err.rate         1500  -none- numeric  
## confusion           6  -none- numeric  
## votes           21080  matrix numeric  
## oob.times       10540  -none- numeric  
## classes             2  -none- character
## importance         36  -none- numeric  
## importanceSD       27  -none- numeric  
## localImportance     0  -none- NULL     
## proximity           0  -none- NULL     
## ntree               1  -none- numeric  
## mtry                1  -none- numeric  
## forest             14  -none- list     
## y               10540  factor numeric  
## test                0  -none- NULL     
## inbag               0  -none- NULL     
## terms               3  terms  call
##    0    1 
## 3427 1032