# Some key packages 
pkgs00 <- c("devtools", "RCurl")
install.packages(pkgs00)
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
# 
pkgs <- c(
    "tidyverse",
    "readr"
)
install.packages(pkgs)
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ purrr     1.0.1
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.2.0
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
path_HR_Attrition <- "WA_Fn-UseC_-HR-Employee-Attrition.csv"
HR_Attrition_raw_tbl <- read_csv(path_HR_Attrition)
## 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.
# Analysis of attrition by department
dept_job_role_tbl <- HR_Attrition_raw_tbl %>% 
                     select(EmployeeNumber, Department, JobRole, 
                            PerformanceRating, Attrition)
# department attrition
dept_job_role_tbl %>% group_by(Department, Attrition) %>%
                      summarise(n = n()) %>%
                      ungroup() %>%
                      #group_by(Department) %>%
                      mutate(pct = n * 100 /sum(n))
## `summarise()` has grouped output by 'Department'. You can override using the
## `.groups` argument.
## # A tibble: 6 × 4
##   Department             Attrition     n    pct
##   <chr>                  <chr>     <int>  <dbl>
## 1 Human Resources        No           51  3.47 
## 2 Human Resources        Yes          12  0.816
## 3 Research & Development No          828 56.3  
## 4 Research & Development Yes         133  9.05 
## 5 Sales                  No          354 24.1  
## 6 Sales                  Yes          92  6.26
# JobRole attrition
dept_job_role_tbl %>% group_by(Department, JobRole, Attrition) %>%
                      summarise(n = n()) %>%
                      ungroup() %>%
                      group_by(Department, JobRole) %>%
                      mutate(pct = n * 100 /sum(n)) %>%
                      ungroup() %>%
                      filter(Attrition %in% c("Yes"))
## `summarise()` has grouped output by 'Department', 'JobRole'. You can override
## using the `.groups` argument.
## # A tibble: 10 × 5
##    Department             JobRole                   Attrition     n   pct
##    <chr>                  <chr>                     <chr>     <int> <dbl>
##  1 Human Resources        Human Resources           Yes          12 23.1 
##  2 Research & Development Healthcare Representative Yes           9  6.87
##  3 Research & Development Laboratory Technician     Yes          62 23.9 
##  4 Research & Development Manager                   Yes           3  5.56
##  5 Research & Development Manufacturing Director    Yes          10  6.90
##  6 Research & Development Research Director         Yes           2  2.5 
##  7 Research & Development Research Scientist        Yes          47 16.1 
##  8 Sales                  Manager                   Yes           2  5.41
##  9 Sales                  Sales Executive           Yes          57 17.5 
## 10 Sales                  Sales Representative      Yes          33 39.8
calculate_attrition_cost <- function(
    
  # Employee
  n                    = 3000,
  salary               = 80000,
  
  # Direct Costs
  separation_cost      = 500,
  vacancy_cost         = 10000,
  acquisition_cost     = 4900,
  placement_cost       = 3500,
  
  # Productivity Costs
  net_revenue_per_employee = 250000,
  workdays_per_year        = 240,
  workdays_position_open   = 40,
  workdays_onboarding      = 60,
  onboarding_efficiency    = 0.50
  
) {
  
  # Direct Costs
  direct_cost <- sum(separation_cost, vacancy_cost, acquisition_cost, placement_cost)
  
  # Lost Productivity Costs
  productivity_cost <- net_revenue_per_employee / workdays_per_year * 
    (workdays_position_open + workdays_onboarding * onboarding_efficiency) 
  
  # Savings of Salary & Benefits (Cost Reduction)
  salary_benefit_reduction <- salary / workdays_per_year * workdays_position_open
  
  # Estimated Turnover Per Employee
  cost_per_employee <- direct_cost + productivity_cost - salary_benefit_reduction
  
  # Total Cost of Employee Turnover
  total_cost <- n * cost_per_employee
  
  return(total_cost)
  
}

print(paste0(calculate_attrition_cost(), " $ attrition cost with 3000 employees."))
## [1] "235450000 $ attrition cost with 3000 employees."