library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Load dataset
hr <- read_csv("https://raw.githubusercontent.com/aiplanethub/Datasets/refs/heads/master/HR_comma_sep.csv")
## Rows: 14999 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Department, salary
## dbl (8): satisfaction_level, last_evaluation, number_project, average_montly...
## 
## ℹ 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.
# Convert appropriate variables to factors
hr$left <- factor(hr$left, labels = c("Stayed", "Left"))
hr$salary <- factor(hr$salary)
hr$promotion_last_5years <- factor(hr$promotion_last_5years)
hr$Work_accident <- factor(hr$Work_accident)
hr$Department <- factor(hr$Department)

TEST 1: Salary vs Left

test1 <- chisq.test(hr$left, hr$salary)
test1
## 
##  Pearson's Chi-squared test
## 
## data:  hr$left and hr$salary
## X-squared = 381.23, df = 2, p-value < 2.2e-16

Technical interpretation:

  • p < .001 → there is a statistically significant association between salary and leaving.
  • Employees with different salary levels leave at different rates.

Non-technical interpretation:

  • Employees with low salaries are far more likely to leave than those with medium or high salaries.
# Plot
salary_prop <- hr %>%
  group_by(salary, left) %>%
  summarise(count = n(), .groups = "drop") %>%
  mutate(prop = count / sum(count))

plot_ly(salary_prop, x = ~salary, y = ~prop, color = ~left, type = "bar") %>%
  layout(
    title = "Employees with low salaries are far more likely to leave",
    xaxis = list(title = "Salary Level"),
    yaxis = list(title = "Proportion"),
    barmode = "group"
  )

TEST 2: Promotion Last 5 Years vs Left

test2 <- chisq.test(hr$left, hr$promotion_last_5years)
test2
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  hr$left and hr$promotion_last_5years
## X-squared = 56.262, df = 1, p-value = 6.344e-14

Technical interpretation:

  • p < .001 → there is a statistically significant association between promotion and leaving.
  • Employees who were promoted or not promoted leave at different rates.

Non-technical interpretation:

  • Employees without promotions in the last 5 years are more likely to leave.
# Plot
promo_prop <- hr %>%
  group_by(promotion_last_5years, left) %>%
  summarise(count = n()) %>%
  mutate(prop = count / sum(count))
## `summarise()` has grouped output by 'promotion_last_5years'. You can override
## using the `.groups` argument.
plot_ly(promo_prop, x = ~promotion_last_5years, y = ~prop, color = ~left, type = "bar") %>%
  layout(
    title = "Employees without promotions are more likely to leave",
    xaxis = list(title = "Promotion in Last 5 Years (0 = No, 1 = Yes)"),
    yaxis = list(title = "Proportion"),
    barmode = "group"
  )

TEST 3: Work Accident vs Left

test3 <- chisq.test(hr$left, hr$Work_accident)
test3
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  hr$left and hr$Work_accident
## X-squared = 357.56, df = 1, p-value < 2.2e-16

Technical interpretation:

  • p < .001 → there is a statistically significant association between work accidents and leaving.
  • Employees with or without accidents leave at different rates.

Non-technical interpretation:

  • Employees who experienced work accidents are less likely to leave the company.
# Plot
accident_prop <- hr %>%
  group_by(Work_accident, left) %>%
  summarise(count = n(), .groups = "drop") %>%
  mutate(prop = count / sum(count))

plot_ly(accident_prop, x = ~Work_accident, y = ~prop, color = ~left, type = "bar") %>%
  layout(
    title = "Employees with accidents are less likely to leave",
    xaxis = list(title = "Work Accident (0 = No, 1 = Yes)"),
    yaxis = list(title = "Proportion"),
    barmode = "group"
  )

TEST 4: Department vs Left

test4 <- chisq.test(hr$left, hr$Department)
test4
## 
##  Pearson's Chi-squared test
## 
## data:  hr$left and hr$Department
## X-squared = 86.825, df = 9, p-value = 7.042e-15

Technical interpretation:

  • p < .001 → there is a statistically significant association between department and leaving.
  • Employees from different departments leave at different rates.

Non-technical interpretation:

  • Employees from certain departments such as technical and sales are more likely to leave than others. This might be because these departments have more employees overall.
# Plot
dept_prop <- hr %>%
  group_by(Department, left) %>%
  summarise(count = n(), .groups = "drop") %>%
  mutate(prop = count / sum(count))

plot_ly(dept_prop, x = ~Department, y = ~prop, color = ~left, type = "bar") %>%
  layout(
    title = "Employees from some departments are more likely to leave",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Proportion"),
    barmode = "group"
  )