# 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 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.
# Convert 'left' to factor with descriptive labels
hr$left <- factor(hr$left, levels = c(0,1), labels = c("Stayed with Company", "Left the Company"))
#1. Satisfaction Level by Attrition ###
t1 <- t.test(satisfaction_level ~ left, data = hr)
print(t1)
## 
##  Welch Two Sample t-test
## 
## data:  satisfaction_level by left
## t = 46.636, df = 5167, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Stayed with Company and group Left the Company is not equal to 0
## 95 percent confidence interval:
##  0.2171815 0.2362417
## sample estimates:
## mean in group Stayed with Company    mean in group Left the Company 
##                         0.6668096                         0.4400980
# Visualization
plot_1 <- hr %>%
  plot_ly(x = ~left, y = ~satisfaction_level, type = "box", color = ~left) %>%
  layout(
    title = "Satisfaction Levels: Those Who Left vs. Stayed",
    xaxis = list(title = ""),
    yaxis = list(title = "Satisfaction Level"),
    showlegend = FALSE
  )
### 2. Last Evaluation Score by Attrition ###
t2 <- t.test(last_evaluation ~ left, data = hr)
print(t2)
## 
##  Welch Two Sample t-test
## 
## data:  last_evaluation by left
## t = -0.72534, df = 5154.9, p-value = 0.4683
## alternative hypothesis: true difference in means between group Stayed with Company and group Left the Company is not equal to 0
## 95 percent confidence interval:
##  -0.009772224  0.004493874
## sample estimates:
## mean in group Stayed with Company    mean in group Left the Company 
##                         0.7154734                         0.7181126
# Visualization
plot_2 <- hr %>%
  plot_ly(x = ~left, y = ~last_evaluation, type = "box", color = ~left) %>%
  layout(
    title = "Performance Evaluations: A Look at Who Stayed and Who Left",
    xaxis = list(title = ""),
    yaxis = list(title = "Last Evaluation Score"),
    showlegend = FALSE
  )
### 3. Average Monthly Hours by Attrition ###
t3 <- t.test(average_montly_hours ~ left, data = hr)
print(t3)
## 
##  Welch Two Sample t-test
## 
## data:  average_montly_hours by left
## t = -7.5323, df = 4875.1, p-value = 5.907e-14
## alternative hypothesis: true difference in means between group Stayed with Company and group Left the Company is not equal to 0
## 95 percent confidence interval:
##  -10.534631  -6.183384
## sample estimates:
## mean in group Stayed with Company    mean in group Left the Company 
##                          199.0602                          207.4192
# Visualization
plot_3 <- hr %>%
  plot_ly(x = ~left, y = ~average_montly_hours, type = "box", color = ~left) %>%
  layout(
    title = "Monthly Work Hours: Comparing Employees Who Left and Stayed",
    xaxis = list(title = ""),
    yaxis = list(title = "Average Monthly Hours"),
    showlegend = FALSE
  )
### 4. Time Spent at Company by Attrition ###
t4 <- t.test(time_spend_company ~ left, data = hr)
print(t4)
## 
##  Welch Two Sample t-test
## 
## data:  time_spend_company by left
## t = -22.631, df = 9625.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Stayed with Company and group Left the Company is not equal to 0
## 95 percent confidence interval:
##  -0.5394767 -0.4534706
## sample estimates:
## mean in group Stayed with Company    mean in group Left the Company 
##                          3.380032                          3.876505
# Visualization
plot_4 <- hr %>%
  plot_ly(x = ~left, y = ~time_spend_company, type = "box", color = ~left) %>%
  layout(
    title = "Tenure at the Company: Who Stayed vs. Who Left",
    xaxis = list(title = ""),
    yaxis = list(title = "Years at Company"),
    showlegend = FALSE
  )
# Show all plots 
plot_1
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
plot_2
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
plot_3
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
plot_4
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels