library(live)
library(mlr)
library(breakDown)
library(randomForest)

head(HR_data)
##   satisfaction_level last_evaluation number_project average_montly_hours
## 1               0.38            0.53              2                  157
## 2               0.80            0.86              5                  262
## 3               0.11            0.88              7                  272
## 4               0.72            0.87              5                  223
## 5               0.37            0.52              2                  159
## 6               0.41            0.50              2                  153
##   time_spend_company Work_accident left promotion_last_5years sales salary
## 1                  3             0    1                     0 sales    low
## 2                  6             0    1                     0 sales medium
## 3                  4             0    1                     0 sales medium
## 4                  5             0    1                     0 sales    low
## 5                  3             0    1                     0 sales    low
## 6                  3             0    1                     0 sales    low
HR_data$left <- as.numeric(HR_data$left)
trees <- randomForest(left~., data = HR_data, ntree=1000)

similar <- sample_locally(data = HR_data,
                          explained_instance = HR_data[1,],
                          explained_var = "left", 
                          size = 2000)
head(similar$data)
##   satisfaction_level last_evaluation number_project average_montly_hours
## 1               0.38            0.53              4                  157
## 2               0.38            0.53              5                  157
## 3               0.38            0.53              2                  157
## 4               0.38            0.53              2                  157
## 5               0.38            0.53              2                  157
## 6               0.99            0.53              2                  157
##   time_spend_company Work_accident promotion_last_5years sales salary
## 1                  3             0                     0 sales    low
## 2                  3             0                     0 sales    low
## 3                  3             0                     0 sales    low
## 4                  4             0                     0 sales    low
## 5                  3             0                     0 sales    low
## 6                  3             0                     0 sales    low
similar1 <- add_predictions(HR_Data, similar, black_box_model = trees)

trained <- fit_explanation(live_object = similar1, 
                           white_box = "regr.lm", 
                           selection = FALSE)

plot_explanation(trained, "waterfallplot", 
                 explained_instance = HR_data[1,])

plot_explanation(trained, "forestplot", 
                 explained_instance = HR_data[1,])