library(readr)
library(plotly)
library(dplyr)
hr <- read_csv('https://raw.githubusercontent.com/aiplanethub/Datasets/refs/heads/master/HR_comma_sep.csv')

Chi-square test 1: Salary vs Left

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

Technical Interpretation

p-value 2.2e-16 is very small, therefore the probability of these results being random is very small. There is a dependence between Salary and leaving the company

Non Technical Interpretation

Employees with low salary are more likely to leave the company

prop_data1 <- hr %>%
  group_by(salary) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )
plot_ly(prop_data1) %>%
  add_bars(x = ~salary, y = ~stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~salary, y = ~left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Salary"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees with low salary are more likely to leave the company"
  )

Chi-square test 2: Department vs Left

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

Technical Interpretation

p-value 7.042e-15 is very small, therefore the probability of these results being random is very small. There is a dependence between department and leaving the company

Non Technical Interpretation

Some departments have higher turnovers than others

prop_data2 <- hr %>%
  group_by(Department) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )
plot_ly(prop_data2) %>%
  add_bars(x = ~Department, y = ~stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Department, y = ~left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Some departments have higher employee turnover than others"
  )

Chi-square test 3: Promotion vs Left

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

Technical Interpretation

p-value 6.344e-14 is very small, therefore the probability of these results being random is very small. There is a dependence between being promoted and leaving the company

Non Technical Interpretation

Employees who recently got a promotion usually stay longer

prop_data3 <- hr %>%
  group_by(promotion_last_5years) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )
plot_ly(prop_data3) %>%
  add_bars(x = ~promotion_last_5years, y = ~stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~promotion_last_5years, y = ~left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Promotion Last 5 Years"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees who recently got a promotion usually stay longer"
  )

Chi-square test 4: Work Accident vs Left

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

Technical Interpretation

p-value 2.2e-16 is very small, therefore the probability of these results being random is very small. There is a dependence between work accidents and leaving the company

Non Technical Interpretation

Having a work accident may slightly influence whether an employee leaves or stays

prop_data4 <- hr %>%
  group_by(Work_accident) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )
plot_ly(prop_data4) %>%
  add_bars(x = ~Work_accident, y = ~stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Work_accident, y = ~left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Work Accident"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "A work accident may slightly influence whether an employee leaves or stays"
  )