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.
test1 <- table(hr$Department, hr$left)
chi1 <- chisq.test(test1)
print("Chi-Square Test: Department vs. left")
## [1] "Chi-Square Test: Department vs. left"
print(chi1)
##
## Pearson's Chi-squared test
##
## data: test1
## X-squared = 86.825, df = 9, p-value = 7.042e-15
#technical: p value highly significant
#non-technical: Majority of employees stay in each department. HR and accounting had the highest proportion of employees who left.
dept_data <- 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(dept_data, aes(x = Department, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Proportion of Employees Leaving by Department",
x = "Department",
y = "Proportion",
fill = "Left"
) +
theme_minimal()
test2 <- table(hr$salary, hr$left)
chi2 <- chisq.test(test2)
print("Chi-Square Test: salary vs. left")
## [1] "Chi-Square Test: salary vs. left"
print(chi2)
##
## Pearson's Chi-squared test
##
## data: test2
## X-squared = 381.23, df = 2, p-value < 2.2e-16
#technical: very significant
#non-technical: Employees with low salary are the highest proportion who left the company.
salary_data <- 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(salary_data, aes(x = salary, y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Proportion of Employees Leaving by Salary Level",
x = "Salary Level",
y = "Proportion",
fill = "Left"
) +
theme_minimal()
test3 <- table(hr$promotion_last_5years, hr$left)
chi3 <- chisq.test(test3)
print("Chi-Square Test: promotion_last_5years vs. left")
## [1] "Chi-Square Test: promotion_last_5years vs. left"
print(chi3)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: test3
## X-squared = 56.262, df = 1, p-value = 6.344e-14
#technical: very significant
#non-technical: Employees who got a promotion are less likely to leave the company.
promotion_data <- 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(promotion_data, aes(x = as.factor(promotion_last_5years), y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Proportion of Employees Leaving by Promotion in Last 5 Years",
x = "Promotion in Last 5 Years",
y = "Proportion",
fill = "Left"
) +
theme_minimal()
test4 <- table(hr$Work_accident, hr$left)
chi4 <- chisq.test(test4)
print("Chi-Square Test: Work_accident vs. left")
## [1] "Chi-Square Test: Work_accident vs. left"
print(chi4)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: test4
## X-squared = 357.56, df = 1, p-value < 2.2e-16
#technical: significant
#non-technical: Employees with a work accident are less likely to leave the company
accident_data <- 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(accident_data, aes(x = as.factor(Work_accident), y = prop, fill = as.factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Proportion of Employees Leaving by Work Accident",
x = "Work Accident",
y = "Proportion",
fill = "Left"
) +
theme_minimal()