# Load necessary libraries
library(tidymodels) # For modeling and evaluation
## Warning: package 'tidymodels' was built under R version 4.3.3
## ── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ──
## ✔ broom 1.0.5 ✔ recipes 1.1.0
## ✔ dials 1.3.0 ✔ rsample 1.2.1
## ✔ dplyr 1.1.4 ✔ tibble 3.2.1
## ✔ ggplot2 3.5.1 ✔ tidyr 1.3.1
## ✔ infer 1.0.7 ✔ tune 1.2.1
## ✔ modeldata 1.4.0 ✔ workflows 1.1.4
## ✔ parsnip 1.2.1 ✔ workflowsets 1.1.0
## ✔ purrr 1.0.2 ✔ yardstick 1.3.1
## Warning: package 'dials' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'infer' was built under R version 4.3.3
## Warning: package 'modeldata' was built under R version 4.3.3
## Warning: package 'parsnip' was built under R version 4.3.3
## Warning: package 'recipes' was built under R version 4.3.3
## Warning: package 'rsample' was built under R version 4.3.3
## Warning: package 'tune' was built under R version 4.3.3
## Warning: package 'workflows' was built under R version 4.3.3
## Warning: package 'workflowsets' was built under R version 4.3.3
## Warning: package 'yardstick' was built under R version 4.3.3
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ recipes::step() masks stats::step()
## • Use tidymodels_prefer() to resolve common conflicts.
library(h2o) # For H2O AutoML
## Warning: package 'h2o' was built under R version 4.3.3
##
## ----------------------------------------------------------------------
##
## Your next step is to start H2O:
## > h2o.init()
##
## For H2O package documentation, ask for help:
## > ??h2o
##
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit https://docs.h2o.ai
##
## ----------------------------------------------------------------------
##
## Attaching package: 'h2o'
## The following objects are masked from 'package:stats':
##
## cor, sd, var
## The following objects are masked from 'package:base':
##
## %*%, %in%, &&, ||, apply, as.factor, as.numeric, colnames,
## colnames<-, ifelse, is.character, is.factor, is.numeric, log,
## log10, log1p, log2, round, signif, trunc
library(lubridate) # For date handling
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:h2o':
##
## day, hour, month, week, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr) # For data manipulation
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ readr::col_factor() masks scales::col_factor()
## ✖ lubridate::day() masks h2o::day()
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ stringr::fixed() masks recipes::fixed()
## ✖ lubridate::hour() masks h2o::hour()
## ✖ dplyr::lag() masks stats::lag()
## ✖ lubridate::month() masks h2o::month()
## ✖ readr::spec() masks yardstick::spec()
## ✖ lubridate::week() masks h2o::week()
## ✖ lubridate::year() masks h2o::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Initialize H2O
h2o.init()
##
## H2O is not running yet, starting it now...
##
## Note: In case of errors look at the following log files:
## C:\Users\nilss\AppData\Local\Temp\RtmpcPJCnz\file640c430f4550/h2o_nilss_started_from_r.out
## C:\Users\nilss\AppData\Local\Temp\RtmpcPJCnz\file640c2859190b/h2o_nilss_started_from_r.err
##
##
## Starting H2O JVM and connecting: Connection successful!
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 4 seconds 876 milliseconds
## H2O cluster timezone: America/New_York
## H2O data parsing timezone: UTC
## H2O cluster version: 3.44.0.3
## H2O cluster version age: 11 months and 16 days
## H2O cluster name: H2O_started_from_R_nilss_xge160
## H2O cluster total nodes: 1
## H2O cluster total memory: 3.92 GB
## H2O cluster total cores: 8
## H2O cluster allowed cores: 8
## H2O cluster healthy: TRUE
## H2O Connection ip: localhost
## H2O Connection port: 54321
## H2O Connection proxy: NA
## H2O Internal Security: FALSE
## R Version: R version 4.3.2 (2023-10-31 ucrt)
## Warning in h2o.clusterInfo():
## Your H2O cluster version is (11 months and 16 days) old. There may be a newer version available.
## Please download and install the latest version from: https://h2o-release.s3.amazonaws.com/h2o/latest_stable.html
data <- read_csv("../00_data/data_wrangled/data_clean_apply.csv") %>%
mutate(across(where(is.character), factor)) # Convert character columns to factors
## New names:
## Rows: 501 Columns: 10
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (1): still_there dbl (8): ...1, fyear, co_per_rol, departure_code,
## ceo_dismissal, tenure_no_... date (1): leftofc
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
set.seed(123) # For reproducibility
data_split <- initial_split(data, prop = 0.8, strata = "ceo_dismissal")
train_data <- training(data_split)
test_data <- testing(data_split)
train_h2o <- as.h2o(train_data)
##
|
| | 0%
|
|======================================================================| 100%
test_h2o <- as.h2o(test_data)
##
|
| | 0%
|
|======================================================================| 100%
response <- "ceo_dismissal" # Replace with the actual name of your target variable
predictors <- setdiff(names(train_data), response)
aml <- h2o.automl(
x = predictors,
y = response,
training_frame = train_h2o,
max_models = 10, # Limit to 10 models for quicker results
seed = 123,
balance_classes = TRUE # Helps with imbalanced datasets
)
##
|
| | 0%
|
|== | 3%
## 20:59:55.429: AutoML: XGBoost is not available; skipping it.
## 20:59:55.534: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
|
|========== | 15%
## 21:00:01.542: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:02.403: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:03.36: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:03.673: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:04.91: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:04.455: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:04.943: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:05.568: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:05.745: DeepLearning_1_AutoML_1_20241206_205955 [DeepLearning def_1] failed: water.exceptions.H2OModelBuilderIllegalArgumentException: Illegal argument(s) for DeepLearning model: DeepLearning_1_AutoML_1_20241206_205955_cv_1. Details: ERRR on field: _balance_classes: balance_classes requires classification.
##
|
|================================================= | 71%
|
|============================================================ | 85%
## 21:00:09.134: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
## 21:00:09.858: _response param, We have detected that your response column has only 2 unique values (0/1). If you wish to train a binary model instead of a regression model, convert your target column to categorical before training.
|
|======================================================================| 100%
print(aml@leaderboard)
## model_id rmse
## 1 GLM_1_AutoML_1_20241206_205955 0.03099643
## 2 StackedEnsemble_BestOfFamily_1_AutoML_1_20241206_205955 0.03374476
## 3 StackedEnsemble_AllModels_1_AutoML_1_20241206_205955 0.03833475
## 4 GBM_5_AutoML_1_20241206_205955 0.05828468
## 5 DRF_1_AutoML_1_20241206_205955 0.07193362
## 6 GBM_grid_1_AutoML_1_20241206_205955_model_5 0.07238425
## mse mae rmsle mean_residual_deviance
## 1 0.0009607787 0.006888772 0.02328145 0.0009607787
## 2 0.0011387086 0.007272514 0.02600179 0.0011387086
## 3 0.0014695532 0.008074401 0.02823698 0.0014695532
## 4 0.0033971037 0.008337298 0.03846539 0.0033971037
## 5 0.0051744457 0.011430993 0.04878225 0.0051744457
## 6 0.0052394797 0.014620759 0.04907490 0.0052394797
##
## [20 rows x 6 columns]
best_model <- aml@leader
predictions <- h2o.predict(best_model, test_h2o)
##
|
| | 0%
|
|======================================================================| 100%
## Warning in doTryCatch(return(expr), name, parentenv, handler): Test/Validation
## dataset column 'still_there' has levels not trained on: ["12/8//2020",
## "12feb2020", "18jan2021"]
predictions_df <- as.data.frame(predictions)
test_data <- as.data.frame(test_h2o)
# test_data <- test_data %>%
# mutate(
# predicted_prob = predictions_df$p1, # Use column 'p1' for the probability of attrition
# predicted_class = if_else(predicted_prob > 0.5, 1, 0))
conf_matrix <- conf_mat( truth = factor(test_data[[response]]), estimate = factor(test_data$predicted_class) )
print(conf_matrix)
metrics <- conf_matrix %>% summary() %>% filter(.metric %in% c(“accuracy”, “precision”, “recall”, “f_meas”)) print(metrics)
h2o.shutdown(prompt = FALSE)