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(ggplot2)
library(plotly)
##
## 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
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.
- Perform the chi-square test (.5 point)
hr$satisfaction_level_cat <- cut(hr$satisfaction_level,
breaks = c(-Inf, 0.3, 0.6, 1),
labels = c("Low", "Medium", "High"))
chisq_test_1 <- chisq.test(table(hr$satisfaction_level_cat, hr$left))
chisq_test_1
##
## Pearson's Chi-squared test
##
## data: table(hr$satisfaction_level_cat, hr$left)
## X-squared = 1634.7, df = 2, p-value < 2.2e-16
- Interpret the results in technical terms (.5 point) The p-value is
small, indicating that the satisfaction level of employees has a
statistically significant impact on whether they leave the company. The
chi-square test showed a significant relationship between satisfaction
level and employee attrition (p-value = 0.001)
- Interpret the results in non-technical terms (1 point) Employees who
have lower satisfaction levels are more likely to leave the
company.
- Create a plot that helps visualize the chi-square test (.5
point)
hr %>%
group_by(satisfaction_level_cat, left) %>%
summarise(count = n()) %>%
ggplot(aes(x = satisfaction_level_cat, y = count, fill = factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("red", "green"), labels = c("Left", "Stayed")) +
labs(title = "Employees with lower satisfaction levels are more likely to leave the company",
x = "Satisfaction Level", y = "Count of Employees", fill = "Attrition") +
theme_minimal()
## `summarise()` has grouped output by 'satisfaction_level_cat'. You can override
## using the `.groups` argument.

- Perform the chi-square test (.5 point)
chisq_test_2 <- chisq.test(table(hr$promotion_last_5years, hr$left))
chisq_test_2
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(hr$promotion_last_5years, hr$left)
## X-squared = 56.262, df = 1, p-value = 6.344e-14
- Interpret the results in technical terms (.5 point) The p-value is
small, indicating that there is a statistically significant relationship
between whether employees received a promotion in the last 5 years and
whether they left the company. The chi-square test shows a significant
relationship between promotion history and employee attrition (p-value
< 0.05).
- Interpret the results in non-technical terms (1 point) Employees who
did not receive promotions in the last 5 years are more likely to leave
the company compared to employees who were promoted.
- Create a plot that helps visualize the chi-square test (.5
point)
hr %>%
group_by(promotion_last_5years, left) %>%
summarise(count = n()) %>%
ggplot(aes(x = factor(promotion_last_5years), y = count, fill = factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("red", "green"), labels = c("Left", "Stayed")) +
labs(title = "Employees who did not receive promotions are more likely to leave the company",
x = "Promotion History (Last 5 Years)", y = "Count of Employees", fill = "Attrition") +
theme_minimal()
## `summarise()` has grouped output by 'promotion_last_5years'. You can override
## using the `.groups` argument.

- Perform the chi-square test (.5 point)
chisq_test_3 <- chisq.test(table(hr$promotion_last_5years, hr$left))
chisq_test_3
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(hr$promotion_last_5years, hr$left)
## X-squared = 56.262, df = 1, p-value = 6.344e-14
- Interpret the results in technical terms (.5 point) A significant
p-value indicates that employees who received promotions in the past
five years are less likely to leave the company.
- Interpret the results in non-technical terms (1 point) Employees who
have received promotions are less likely to leave the company.
- Create a plot that helps visualize the chi-square test (.5
point)
hr %>%
group_by(promotion_last_5years, left) %>%
summarise(count = n()) %>%
ggplot(aes(x = factor(promotion_last_5years), y = count, fill = factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("red", "green"), labels = c("Left", "Stayed")) +
labs(title = "Employees who received promotions are less likely to leave the company",
x = "Promotion History", y = "Count of Employees", fill = "Attrition") +
theme_minimal()
## `summarise()` has grouped output by 'promotion_last_5years'. You can override
## using the `.groups` argument.

- Perform the chi-square test (.5 point)
chisq_test_4 <- chisq.test(table(hr$Department, hr$left))
chisq_test_4
##
## Pearson's Chi-squared test
##
## data: table(hr$Department, hr$left)
## X-squared = 86.825, df = 9, p-value = 7.042e-15
- Interpret the results in technical terms (.5 point) The p-value
suggests that attrition rates differ across departments, meaning some
departments have higher turnover than others.
- Interpret the results in non-technical terms (1 point) Employees in
certain departments are more likely to leave the company.
- Create a plot that helps visualize the chi-square test (.5
point)
hr %>%
group_by(Department, left) %>%
summarise(count = n()) %>%
ggplot(aes(x = Department, y = count, fill = factor(left))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("red", "green"), labels = c("Left", "Stayed")) +
labs(title = "Employees in certain departments are more likely to leave the company",
x = "Department", y = "Count of Employees", fill = "Attrition") +
theme_minimal()
## `summarise()` has grouped output by 'Department'. You can override using the
## `.groups` argument.
