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.
1. chi square test: attrition by salary level
# contingency table
salary_table <- table(hr$salary, hr$left)
#chi square test
chi_test_1 <- chisq.test(salary_table)
#display test result
chi_test_1
##
## Pearson's Chi-squared test
##
## data: salary_table
## X-squared = 381.23, df = 2, p-value < 2.2e-16
- technical interpretation: the p value is
1.6520867^{-83}, indicating whether there is a significant association
between salary level and attrition
- non technical interpretation: employees with lower
salary levels are more likely to leave the company
#visualization for attrition by salary level
prop_salary <- hr %>%
group_by(salary) %>%
summarise(left = sum(left == 1) / n(), stayed = sum(left == 0) / n())
plot_ly(prop_salary) %>%
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 level"),
yaxis = list(title = "proportion", tickformat = ",.0%"),
title = "employees with lower salaries are more likely to leave"
)
2. chi square test - attrition by department
# contingency table
department_table <- table(hr$Department, hr$left)
# chi square test
chi_test_2 <- chisq.test(department_table)
#display test result
chi_test_2
##
## Pearson's Chi-squared test
##
## data: department_table
## X-squared = 86.825, df = 9, p-value = 7.042e-15
- technical interpretation: the p-value is
7.0421305^{-15}, showing the significance of the relationship between
department and attrition
- non technical interpretation: attrition rates vary
significantly across different departments
# visualization for attrition by department
prop_department <- hr %>%
group_by(Department) %>%
summarise(left = sum(left == 1) / n(), stayed = sum(left == 0) / n())
plot_ly(prop_department) %>%
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 = "attrition rates cary significantly acrossd epartments"
)
3. chi square test: attrition by promotion history
# contingency table
promotion_table <- table(hr$promotion_last_5years, hr$left)
# chi square test
chi_test_3 <- chisq.test(promotion_table)
# display test result
chi_test_3
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: promotion_table
## X-squared = 56.262, df = 1, p-value = 6.344e-14
- technical interpretation:the p-value is
6.3441555^{-14}, suggesting the significance of the relationship between
promotions and attrition
- non technical interpretation: employees who have
not been promoted in the last five years are more likely to leave
# visualization
prop_promotion <- hr %>%
group_by(promotion_last_5years) %>%
summarise(left = sum(left == 1) / n(), stayed = sum(left == 0) / n())
plot_ly(prop_promotion) %>%
add_bars(x = ~factor(promotion_last_5years, labels = c("no promotion", "promotion")),
y = ~stayed, name = "stayed",
marker = list(color = "#1f77b4")) %>%
add_bars(x = ~factor(promotion_last_5years, labels = c("no promotion", "promotion")),
y = ~left, name = "left",
marker = list(color = "#ff7f0e")) %>%
layout(
barmode = "stack",
xaxis = list(title = "promotion history"),
yaxis = list(title = "proportion", tickformat = ",.0%"),
title = "employees without promotion are more likely to leave"
)
4. chi square test: attrition by work accident
# contingency table
accident_table <- table(hr$Work_accident, hr$left)
# chi-square test
chi_test_4 <- chisq.test(accident_table)
# display test result
chi_test_4
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: accident_table
## X-squared = 357.56, df = 1, p-value < 2.2e-16
- technical interpretation: the p-value is
9.5582396^{-80}, indicating whether work accidents are associated with
attrition
- non technical interpretation: employees who have
experienced work accidents are less likely to leave
# visualization
prop_accident <- hr %>%
group_by(Work_accident) %>%
summarise(left = sum(left == 1) / n(), stayed = sum(left == 0) / n())
plot_ly(prop_accident) %>%
add_bars(x = ~factor(Work_accident, labels = c("no accident", "accident")),
y = ~stayed, name = "stayed",
marker = list(color = "#1f77b4")) %>%
add_bars(x = ~factor(Work_accident, labels = c("no accident", "accident")),
y = ~left, name = "left",
marker = list(color = "#ff7f0e")) %>%
layout(
barmode = "stack",
xaxis = list(title = "work accident history"),
yaxis = list(title = "proportion", tickformat = ",.0%"),
title = "employees with work accidents are less likely to leave"
)