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
library(ggplot2)
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.
Test 1
q1 <- chisq.test(hr$Department , hr$left)
prop_q1 <- hr %>%
group_by(Department, left) %>%
summarise(count = n()) %>%
mutate(prop = count / sum(count))
## `summarise()` has grouped output by 'Department'. You can override using the
## `.groups` argument.
ggplot(prop_q1, aes(x = Department, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "fill") +
labs(title = "Employee department influences employee leaving",
x = "Department", y = "Proportion") +
scale_fill_manual(name = "Left", labels = c("Stayed", "Left"), values = c("green", "red")) +
theme_minimal()

p-value: the p-value is low which shows a significant relationship
between department and employee attrition.
non-technical interpretation: the department an employee is
associated with influences the chance of the employee leaving the
company.
Test 2
q2 <- chisq.test(table(hr$salary, hr$left))
prop_q2 <- hr %>%
group_by(salary, left) %>%
summarise(count = n()) %>%
mutate(prop = count / sum(count))
## `summarise()` has grouped output by 'salary'. You can override using the
## `.groups` argument.
ggplot(prop_q2, aes(x = salary, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "fill") +
labs(title = "Salary influences employee leaving",
x = "Salary Level", y = "Proportion") +
scale_fill_manual(name = "Left", labels = c("Stayed", "Left"), values = c("green", "red")) +
theme_minimal()

p-value: the p-value is low which shows a significant relationship
between salary and employee attrition.
non-technical interpretation: the salary an employee has influences
the chance of the employee leaving the company.
Test 3
q3 <- chisq.test(table(hr$promotion_last_5years, hr$left))
prop_q3 <- hr %>%
group_by(promotion_last_5years, left) %>%
summarise(count = n()) %>%
mutate(prop = count / sum(count))
## `summarise()` has grouped output by 'promotion_last_5years'. You can override
## using the `.groups` argument.
ggplot(prop_q3, aes(x = promotion_last_5years, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "fill") +
labs(title = "Promotion influences employee leaving",
x = "Promotion History", y = "Proportion") +
scale_fill_manual(name = "Left", labels = c("Stayed", "Left"), values = c("green", "red")) +
theme_minimal()

p-value: the p-value is low which shows a significant relationship
between whether or not an employee has received a promotion and employee
attrition.
non-technical interpretation: whether or not an employee has
received a promotion influences the chance of the employee leaving the
company.
Test 4
q4 <- chisq.test(table(hr$Work_accident, hr$left))
prop_q4 <- hr %>%
group_by(Work_accident, left) %>%
summarise(count = n()) %>%
mutate(prop = count / sum(count))
## `summarise()` has grouped output by 'Work_accident'. You can override using the
## `.groups` argument.
ggplot(prop_q4, aes(x = Work_accident, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "fill") +
labs(title = "Work accident influences employee leaving",
x = "Work Accident", y = "Proportion") +
scale_fill_manual(name = "Left", labels = c("Stayed", "Left"), values = c("green", "red")) +
theme_minimal()

p-value: the p-value is low which shows a significant relationship
between whether or not an employee has had a work accident and employee
attrition.
non-technical interpretation: whether or not an employee has had a
work accident influences the chance of the employee leaving the
company.