attrition_raw_tbl <- read_csv("/Users/owner/Desktop/PSU_Data3100/00_data/WA_Fn-UseC_-HR-Employee-Attrition.csv")
## Rows: 1470 Columns: 35
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Attrition, BusinessTravel, Department, EducationField, Gender, Job...
## dbl (26): Age, DailyRate, DistanceFromHome, Education, EmployeeCount, Employ...
##
## ℹ 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.
# If data is not sensitive:
attrition_raw_tbl %>% glimpse()
## Rows: 1,470
## Columns: 35
## $ Age <dbl> 41, 49, 37, 33, 27, 32, 59, 30, 38, 36, 35, 2…
## $ Attrition <chr> "Yes", "No", "Yes", "No", "No", "No", "No", "…
## $ BusinessTravel <chr> "Travel_Rarely", "Travel_Frequently", "Travel…
## $ DailyRate <dbl> 1102, 279, 1373, 1392, 591, 1005, 1324, 1358,…
## $ Department <chr> "Sales", "Research & Development", "Research …
## $ DistanceFromHome <dbl> 1, 8, 2, 3, 2, 2, 3, 24, 23, 27, 16, 15, 26, …
## $ Education <dbl> 2, 1, 2, 4, 1, 2, 3, 1, 3, 3, 3, 2, 1, 2, 3, …
## $ EducationField <chr> "Life Sciences", "Life Sciences", "Other", "L…
## $ EmployeeCount <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ EmployeeNumber <dbl> 1, 2, 4, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16,…
## $ EnvironmentSatisfaction <dbl> 2, 3, 4, 4, 1, 4, 3, 4, 4, 3, 1, 4, 1, 2, 3, …
## $ Gender <chr> "Female", "Male", "Male", "Female", "Male", "…
## $ HourlyRate <dbl> 94, 61, 92, 56, 40, 79, 81, 67, 44, 94, 84, 4…
## $ JobInvolvement <dbl> 3, 2, 2, 3, 3, 3, 4, 3, 2, 3, 4, 2, 3, 3, 2, …
## $ JobLevel <dbl> 2, 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 2, 1, 1, 1, …
## $ JobRole <chr> "Sales Executive", "Research Scientist", "Lab…
## $ JobSatisfaction <dbl> 4, 2, 3, 3, 2, 4, 1, 3, 3, 3, 2, 3, 3, 4, 3, …
## $ MaritalStatus <chr> "Single", "Married", "Single", "Married", "Ma…
## $ MonthlyIncome <dbl> 5993, 5130, 2090, 2909, 3468, 3068, 2670, 269…
## $ MonthlyRate <dbl> 19479, 24907, 2396, 23159, 16632, 11864, 9964…
## $ NumCompaniesWorked <dbl> 8, 1, 6, 1, 9, 0, 4, 1, 0, 6, 0, 0, 1, 0, 5, …
## $ Over18 <chr> "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", …
## $ OverTime <chr> "Yes", "No", "Yes", "Yes", "No", "No", "Yes",…
## $ PercentSalaryHike <dbl> 11, 23, 15, 11, 12, 13, 20, 22, 21, 13, 13, 1…
## $ PerformanceRating <dbl> 3, 4, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 3, …
## $ RelationshipSatisfaction <dbl> 1, 4, 2, 3, 4, 3, 1, 2, 2, 2, 3, 4, 4, 3, 2, …
## $ StandardHours <dbl> 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 8…
## $ StockOptionLevel <dbl> 0, 1, 0, 0, 1, 0, 3, 1, 0, 2, 1, 0, 1, 1, 0, …
## $ TotalWorkingYears <dbl> 8, 10, 7, 8, 6, 8, 12, 1, 10, 17, 6, 10, 5, 3…
## $ TrainingTimesLastYear <dbl> 0, 3, 3, 3, 3, 2, 3, 2, 2, 3, 5, 3, 1, 2, 4, …
## $ WorkLifeBalance <dbl> 1, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, 2, 3, 3, …
## $ YearsAtCompany <dbl> 6, 10, 0, 8, 2, 7, 1, 1, 9, 7, 5, 9, 5, 2, 4,…
## $ YearsInCurrentRole <dbl> 4, 7, 0, 7, 2, 7, 0, 0, 7, 7, 4, 5, 2, 2, 2, …
## $ YearsSinceLastPromotion <dbl> 0, 1, 0, 3, 2, 3, 0, 0, 1, 7, 0, 0, 4, 1, 0, …
## $ YearsWithCurrManager <dbl> 5, 7, 0, 0, 2, 6, 0, 0, 8, 7, 3, 8, 3, 2, 3, …
# If data is sensitive:
attrition_raw_tbl %>%
slice(0) %>%
glimpse()
## Rows: 0
## Columns: 35
## $ Age <dbl>
## $ Attrition <chr>
## $ BusinessTravel <chr>
## $ DailyRate <dbl>
## $ Department <chr>
## $ DistanceFromHome <dbl>
## $ Education <dbl>
## $ EducationField <chr>
## $ EmployeeCount <dbl>
## $ EmployeeNumber <dbl>
## $ EnvironmentSatisfaction <dbl>
## $ Gender <chr>
## $ HourlyRate <dbl>
## $ JobInvolvement <dbl>
## $ JobLevel <dbl>
## $ JobRole <chr>
## $ JobSatisfaction <dbl>
## $ MaritalStatus <chr>
## $ MonthlyIncome <dbl>
## $ MonthlyRate <dbl>
## $ NumCompaniesWorked <dbl>
## $ Over18 <chr>
## $ OverTime <chr>
## $ PercentSalaryHike <dbl>
## $ PerformanceRating <dbl>
## $ RelationshipSatisfaction <dbl>
## $ StandardHours <dbl>
## $ StockOptionLevel <dbl>
## $ TotalWorkingYears <dbl>
## $ TrainingTimesLastYear <dbl>
## $ WorkLifeBalance <dbl>
## $ YearsAtCompany <dbl>
## $ YearsInCurrentRole <dbl>
## $ YearsSinceLastPromotion <dbl>
## $ YearsWithCurrManager <dbl>
h2o.init()
## Connection successful!
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 6 days 23 hours
## H2O cluster timezone: America/New_York
## H2O data parsing timezone: UTC
## H2O cluster version: 3.44.0.3
## H2O cluster version age: 1 year, 4 months and 10 days
## H2O cluster name: H2O_started_from_R_owner_dit581
## H2O cluster total nodes: 1
## H2O cluster total memory: 1.29 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.1 (2023-06-16)
## Warning in h2o.clusterInfo():
## Your H2O cluster version is (1 year, 4 months and 10 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
attrition_tbl <- attrition_raw_tbl %>%
select(-EmployeeCount, -StandardHours, -Over18, -EmployeeNumber) %>%
mutate(Attrition = as.factor(Attrition)) %>%
mutate_if(is.character, as.factor)
set.seed(123)
attrition_split <- initial_split(attrition_tbl, prop = 0.8, strata = Attrition)
train_tbl <- training(attrition_split)
test_tbl <- testing(attrition_split)
attrition_recipe <- recipe(Attrition ~ ., data = train_tbl) %>%
step_dummy(all_nominal_predictors()) %>%
step_zv(all_predictors()) %>%
step_normalize(all_numeric_predictors()) %>%
prep()
train_preprocessed <- bake(attrition_recipe, new_data = train_tbl)
test_preprocessed <- bake(attrition_recipe, new_data = test_tbl)
train_h2o <- as.h2o(train_preprocessed)
##
|
| | 0%
|
|======================================================================| 100%
test_h2o <- as.h2o(test_preprocessed)
##
|
| | 0%
|
|======================================================================| 100%
h2o_model <- h2o.gbm(
x = setdiff(names(train_h2o), "Attrition"),
y = "Attrition",
training_frame = train_h2o,
model_id = "h2o_gbm_model",
seed = 1234
)
##
|
| | 0%
|
|======================================================================| 100%
perf <- h2o.performance(h2o_model, newdata = test_h2o)
print(perf)
## H2OBinomialMetrics: gbm
##
## MSE: 0.0962407
## RMSE: 0.3102269
## LogLoss: 0.3111299
## Mean Per-Class Error: 0.168944
## AUC: 0.8718792
## AUCPR: 0.5822296
## Gini: 0.7437584
## R^2: 0.2935773
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## No Yes Error Rate
## No 215 32 0.129555 =32/247
## Yes 10 38 0.208333 =10/48
## Totals 225 70 0.142373 =42/295
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.217794 0.644068 69
## 2 max f2 0.217794 0.725191 69
## 3 max f0point5 0.370974 0.611702 34
## 4 max accuracy 0.370974 0.874576 34
## 5 max precision 0.963428 1.000000 0
## 6 max recall 0.028507 1.000000 244
## 7 max specificity 0.963428 1.000000 0
## 8 max absolute_mcc 0.217794 0.574461 69
## 9 max min_per_class_accuracy 0.156380 0.801619 87
## 10 max mean_per_class_accuracy 0.217794 0.831056 69
## 11 max tns 0.963428 247.000000 0
## 12 max fns 0.963428 47.000000 0
## 13 max fps 0.006412 247.000000 294
## 14 max tps 0.028507 48.000000 244
## 15 max tnr 0.963428 1.000000 0
## 16 max fnr 0.963428 0.979167 0
## 17 max fpr 0.006412 1.000000 294
## 18 max tpr 0.028507 1.000000 244
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
h2o.auc(perf)
## [1] 0.8718792
h2o.confusionMatrix(perf)
## Confusion Matrix (vertical: actual; across: predicted) for max f1 @ threshold = 0.217794387292298:
## No Yes Error Rate
## No 215 32 0.129555 =32/247
## Yes 10 38 0.208333 =10/48
## Totals 225 70 0.142373 =42/295
plot(perf, type = "roc")
predictions <- h2o.predict(h2o_model, test_h2o)
##
|
| | 0%
|
|======================================================================| 100%
head(predictions)
## predict No Yes
## 1 No 0.9382303 0.06176966
## 2 No 0.9573292 0.04267083
## 3 No 0.9389149 0.06108513
## 4 No 0.9736064 0.02639359
## 5 Yes 0.6792807 0.32071926
## 6 No 0.9769874 0.02301263