library(readr)

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.
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)

#1

table1 <- table(hr$Work_accident, hr$left)
chi1 <- chisq.test(table1)
print(chi1)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table1
## X-squared = 357.56, df = 1, p-value < 2.2e-16

**The p-value of 2.2e-16 suggests that the association between Work_accident and leaving the company is statistically significant.

**Employees who have experienced work accidents are more likely to leave the company.

ggplot(hr, aes(x = factor(Work_accident), fill = factor(left))) +
  geom_bar(position = "fill") +
  labs(
    x = "Work Accident",
    y = "Proportion",
    fill = "Left",
    title = "Employees who have experienced work accidents are more likely to leave the company"
  ) +
  theme_minimal()

#2

table2 <- table(hr$promotion_last_5years, hr$left)
chi2 <- chisq.test(table2)
print(chi2)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table2
## X-squared = 56.262, df = 1, p-value = 6.344e-14

**The p-value of 6.344e-14 suggests that the association between Promotion in the Last 5 Years and leaving the company is statistically significant.

**Employees who have not been promoted are more likely to leave the company.

ggplot(hr, aes(x = factor(promotion_last_5years), fill = factor(left))) +
  geom_bar(position = "fill") +
  labs(
    x = "Promotion in Last 5 Years",
    y = "Proportion",
    fill = "Left",
    title = "Employees Who Have Not Been Promoted Are More Likely to Leave the Company"
  ) +
  theme_minimal()

#3

table3 <- table(hr$Department, hr$left)
chi3 <- chisq.test(table3)
print(chi3)
## 
##  Pearson's Chi-squared test
## 
## data:  table3
## X-squared = 86.825, df = 9, p-value = 7.042e-15

**The association between Department and leaving the company is statistically significant.

**Employees from different departments have varying tendencies to stay or leave the company.

ggplot(hr, aes(x = factor(Department), fill = factor(left))) +
  geom_bar(position = "fill") +
  labs(x = "Department", y = "Proportion", fill = "Left",
       title = "Employees from different departments have varying tendencies to stay or leave the company") +
  theme_minimal()

#4

table4 <- table(hr$salary, hr$left)
chi4 <- chisq.test(table4)
print(chi4)
## 
##  Pearson's Chi-squared test
## 
## data:  table4
## X-squared = 381.23, df = 2, p-value < 2.2e-16

**The association between Salary and leaving the company is statistically significant.

**Employees who have a lower salary are more likely ot leave the company.

ggplot(hr, aes(x = salary, fill = factor(left))) +
  geom_bar(position = "fill") +
  labs(x = "Salary", y = "Proportion", fill = "Left",
       title = "Employees who have a lower salary are more likely ot leave tha company.
") +
  theme_minimal()