# Load libraries
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
## Warning: package 'ggplot2' was built under R version 4.4.3
## 
## 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 the 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.
# View column names to verify structure
names(hr)
##  [1] "satisfaction_level"    "last_evaluation"       "number_project"       
##  [4] "average_montly_hours"  "time_spend_company"    "Work_accident"        
##  [7] "left"                  "promotion_last_5years" "Department"           
## [10] "salary"
# Convert 'left' to descriptive factor
hr$left <- factor(hr$left, levels = c(0, 1), labels = c("Stayed", "Left"))
# === 1. Work Accident vs. Left ===
table1 <- table(hr$Work_accident, hr$left)
chisq1 <- chisq.test(table1)
print(chisq1)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table1
## X-squared = 357.56, df = 1, p-value < 2.2e-16
# Prepare proportions for stacked bar chart
prop1 <- hr %>%
  group_by(Work_accident) %>%
  summarise(
    Stayed = sum(left == "Stayed") / n(),
    Left = sum(left == "Left") / n()
  )

plot_1 <- plot_ly(prop1) %>%
  add_bars(x = ~Work_accident, y = ~Stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Work_accident, y = ~Left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Work Accident (0 = No, 1 = Yes)"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees without work accidents are more likely to leave"
  )
# === 2. Promotion in Last 5 Years vs. Left ===
table2 <- table(hr$promotion_last_5years, hr$left)
chisq2 <- chisq.test(table2)
print(chisq2)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table2
## X-squared = 56.262, df = 1, p-value = 6.344e-14
prop2 <- hr %>%
  group_by(promotion_last_5years) %>%
  summarise(
    Stayed = sum(left == "Stayed") / n(),
    Left = sum(left == "Left") / n()
  )

plot_2 <- plot_ly(prop2) %>%
  add_bars(x = ~promotion_last_5years, y = ~Stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~promotion_last_5years, y = ~Left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Promotion in Last 5 Years (0 = No, 1 = Yes)"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees without recent promotions are more likely to leave"
  )
# === 3. Department vs. Left ===
table3 <- table(hr$Department, hr$left)
chisq3 <- chisq.test(table3)
print(chisq3)
## 
##  Pearson's Chi-squared test
## 
## data:  table3
## X-squared = 86.825, df = 9, p-value = 7.042e-15
# Proportions by department
prop3 <- hr %>%
  group_by(Department) %>%
  summarise(
    Stayed = sum(left == "Stayed") / n(),
    Left = sum(left == "Left") / n()
  )

plot_3 <- plot_ly(prop3) %>%
  add_bars(x = ~Department, y = ~Stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Department, y = ~Left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Department", tickangle = -45),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Attrition patterns differ across departments"
  )
# === 4. Salary vs. Left ===
table4 <- table(hr$salary, hr$left)
chisq4 <- chisq.test(table4)
print(chisq4)
## 
##  Pearson's Chi-squared test
## 
## data:  table4
## X-squared = 381.23, df = 2, p-value < 2.2e-16
prop4 <- hr %>%
  group_by(salary) %>%
  summarise(
    Stayed = sum(left == "Stayed") / n(),
    Left = sum(left == "Left") / n()
  )

plot_4 <- plot_ly(prop4) %>%
  add_bars(x = ~salary, y = ~Stayed, name = "Stayed", marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~salary, y = ~Left, name = "Left", marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Salary Level"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Lower-paid employees are more likely to leave"
  )
# === Display plots one at a time or all together ===
plot_1
plot_2
plot_3
plot_4