knitr::opts_chunk$set(echo = TRUE)
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 data
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 variables to factors
hr$left <- as.factor(hr$left)
hr$salary <- as.factor(hr$salary)
hr$Department <- as.factor(hr$Department)
hr$Work_accident <- as.factor(hr$Work_accident)
hr$promotion_last_5years <- as.factor(hr$promotion_last_5years)
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
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") %>%
add_bars(x = ~salary, y = ~left, name = "Left") %>%
layout(
barmode = "stack",
title = "Employees with lower salaries are more likely to leave",
xaxis = list(title = "Salary"),
yaxis = list(title = "Proportion")
)
test2 <- chisq.test(hr$left, hr$Department)
test2
##
## Pearson's Chi-squared test
##
## data: hr$left and hr$Department
## X-squared = 86.825, df = 9, p-value = 7.042e-15
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") %>%
add_bars(x = ~Department, y = ~left, name = "Left") %>%
layout(
barmode = "stack",
title = "Some departments have higher employee turnover than others",
xaxis = list(title = "Department"),
yaxis = list(title = "Proportion")
)
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
prop_data3 <- hr %>%
group_by(Work_accident) %>%
summarise(
stayed = sum(left == 0) / n(),
left = sum(left == 1) / n()
)
plot_ly(prop_data3) %>%
add_bars(x = ~Work_accident, y = ~stayed, name = "Stayed") %>%
add_bars(x = ~Work_accident, y = ~left, name = "Left") %>%
layout(
barmode = "stack",
title = "Employees with accidents are less likely to leave",
xaxis = list(title = "Work Accident (0 = No, 1 = Yes)"),
yaxis = list(title = "Proportion")
)
test4 <- chisq.test(hr$left, hr$promotion_last_5years)
test4
##
## 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
prop_data4 <- hr %>%
group_by(promotion_last_5years) %>%
summarise(
stayed = sum(left == 0) / n(),
left = sum(left == 1) / n()
)
plot_ly(prop_data4) %>%
add_bars(x = ~promotion_last_5years, y = ~stayed, name = "Stayed") %>%
add_bars(x = ~promotion_last_5years, y = ~left, name = "Left") %>%
layout(
barmode = "stack",
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")
)