library(readr)
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
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.
chisq.test(hr$left , hr$Work_accident)
##
## 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
p-value: The p-value is very small, therefore the probability of
these results being random is very small.
chi-square test interpretation: There is a dependency between the
work accident and leaving the company.
Non-technical: Employees that did not have a work accident are more
likely to leave
Calculate proportions
prop_data <- hr %>%
group_by(Work_accident) %>%
summarise(
Stayed = sum(left == 0) / n(),
Left = sum(left == 1) / n()
)
Create a stacked bar chart
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 more than 3 times likely to leave"
)
chisq.test(hr$left , hr$promotion_last_5years)
##
## 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
p-value: The p-value is very small, therefore the probability of
these results being random is very small.
chi-square test interpretation: The result is statistically
significant, suggesting that promotion status plays a role in whether
employees leave.
p-value:There is strong evidence of an association between salary
levels and whether an employee left the company.
chi-square test interpretation: Salary might be a factor that
affects retention, and improving salary levels could help reduce
employee turnover.”
Non-technical: Employees with lower salaries are more likely to
leave the company compared to those with medium or high salaries.
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 that had different salaries either stayed or left"
)
chisq.test(hr$left , hr$Department)
##
## Pearson's Chi-squared test
##
## data: hr$left and hr$Department
## X-squared = 86.825, df = 9, p-value = 7.042e-15
p-value:The p-value is very small, therefore the probability of
these results being random is very small
chi-square interpretation: This indicates that department and
attrition are not independent, suggesting a statistically significant
relationship between an employee’s department and their likelihood of
leaving the company.
Non-technical: Employees in some departments are more likely to
leave the company compared to others.
prop_data <- hr %>%
group_by(Department) %>%
summarise(
Stayed = sum(left == 0) / n(),
Left = sum(left == 1) / n()
)
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 of departments mostly stayed"
)