library(readr)

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.
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
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

Test 1

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
prop_data <- hr %>%
  group_by(Work_accident) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )

plot_ly(prop_data) %>%
  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 = "Employees that did not have a work accident are than 3 times more likely to leave"
  )

The p-value is very small, meaning that there is a very small chance that these results are random. Therefore we will be rejecting the null hypothesis (HO) that states that there is no dependency.

Techinal: there is a dependency between left and work accident.

Test 2

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
prop_data <- hr %>%
  group_by(promotion_last_5years) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )

plot_ly(prop_data) %>%
  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 in the Last 5 years"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees that did not get a promotion in the last 5 years are 4 times more likely to leave"
    
  )

P-value calculated as 6.344e -14 which means that there is a level of signifigance, but it remains very low. The variables are not independent.

Having a promotion in the last five years gives employees a higher chance of staying.

Test 3

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
prop_data <- hr %>%
  group_by(salary) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )

plot_ly(prop_data) %>%
  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 a low salary are more than 4 times more likely than those with high salaries to leave "
    
  )

The P-value is 2.2e-16, which means that the variables “salary” and whether they employees “left” have relation.

The salary of an employee has a strong effect on whether or not they leave the company. Higher salaries = less likely to leave.

Test 4

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
prop_data <- hr %>%
  group_by(Department) %>%
  summarise(
    stayed = sum(left == 0) / n(),
    left = sum(left == 1) / n()
  )

# Create stacked bar chart
plot_ly(prop_data) %>%
  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 = "Employees in HR are most likely to leave"
    
  )

The P-value is 7.042e-15 which is very low, which means there is a connection between one’s department and whether or not they leave.

The department where an employee works has a strong effect on whether or not they leave. Certain departments are more likely for employees to leave the job.