# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     language: R
#     name: ir
# ---

#
#
#
#
# https://shiring.github.io/machine_learning/2017/04/23/lime
#
# https://shiring.github.io/machine_learning/2017/04/23/one_r



# +
#install.packages("tidyquant")

# +
#install.packages("h2o")
# -



# +
#install.packages("lime")
# from Terminal:
# sudo apt-get install libmagick++-dev

# I then installed LIME (from CRAN) with no problems.
# Yay! LIME finally installed OK :-)
# -

# Load the following packages
library(tidyquant)  # Loads tidyverse and several other pkgs 
library(readxl)     # Super simple excel reader
library(h2o)        # Professional grade ML pkg
library(lime)       # Explain complex black-box ML models

# Read excel data
hr_data_raw <- read_excel(path = "data/WA_Fn-UseC_-HR-Employee-Attrition.xlsx")

hr_data_raw[1:10,]

# +
# View first 10 rows
# hr_data_raw[1:10,] %>%
#     knitr::kable("html",caption = "First 10 rows")%>%as.character()%>%display_html()
# -

hr_data <- hr_data_raw %>%
    mutate_if(is.character, as.factor) %>%
    select(Attrition, everything())

glimpse(hr_data)

# Modeling Employee Attrition
#
# We are going to use the h2o.automl() function from the H2O platform to model employee attrition.
# H2O
#
# First, we need to initialize the Java Virtual Machine (JVM) that H2O uses locally.

# Initialize H2O JVM
h2o.init() 


h2o.no_progress() # Turn off output of progress bars

# +
# Split data into Train/Validation/Test Sets
hr_data_h2o <- as.h2o(hr_data)

split_h2o <- h2o.splitFrame(hr_data_h2o, c(0.7, 0.15), seed = 1234 )

train_h2o <- h2o.assign(split_h2o[[1]], "train" ) # 70%
valid_h2o <- h2o.assign(split_h2o[[2]], "valid" ) # 15%
test_h2o  <- h2o.assign(split_h2o[[3]], "test" )  # 15%
# -

# Set names for h2o
y <- "Attrition"
x <- setdiff(names(train_h2o), y)

# Now the fun begins. We run the h2o.automl() setting the arguments it needs to run models against. For more information, see the h2o.automl documentation.
#
#     x = x: The names of our feature columns.
#     y = y: The name of our target column.
#     training_frame = train_h2o: Our training set consisting of 70% of the data.
#     leaderboard_frame = valid_h2o: Our validation set consisting of 15% of the data. H2O uses this to ensure the model does not overfit the data.
#     max_runtime_secs = 30: We supply this to speed up H2O’s modeling. The algorithm has a large number of complex models so we want to keep things moving at the expense of some accuracy.
#

# Run the automated machine learning 
automl_models_h2o <- h2o.automl(
    x = x, 
    y = y,
    training_frame    = train_h2o,
    leaderboard_frame = valid_h2o,
    max_runtime_secs  = 30
    )

# Extract leader model
automl_leader <- automl_models_h2o@leader

# Predict on hold-out set, test_h2o
pred_h2o <- h2o.predict(object = automl_leader, newdata = test_h2o)

# https://www.business-science.io/business/2017/09/18/hr_employee_attrition.html



# Prep for performance assessment
test_performance <- test_h2o %>%
    tibble::as_tibble() %>%
    select(Attrition) %>%
    add_column(pred = as.vector(pred_h2o$predict)) %>%
    mutate_if(is.character, as.factor)
test_performance

# Confusion table counts
confusion_matrix <- test_performance %>%
    table() 
confusion_matrix















StarcoderdataJupyterScriptsDedupFiltered