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.
### Test One 

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
t_test_result <- t.test(satisfaction_level ~ left, data = hr)
print(t_test_result)
## 
##  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 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  0.2171815 0.2362417
## sample estimates:
## mean in group 0 mean in group 1 
##       0.6668096       0.4400980
plot_data <- hr %>%
  mutate(left_status = ifelse(left == 1, "Left", "Stayed"))

plot <- plot_ly(plot_data, x = ~left_status, y = ~satisfaction_level, type = "box",
                boxpoints = "all", jitter = 0.3, pointpos = -1.8,
                marker = list(color = 'rgb(7, 40, 89)'),
                line = list(color = 'rgb(7, 40, 89)')) %>%
  layout(title = "Satisfaction Level by Employee Status",
         xaxis = list(title = "Employee Status"),
         yaxis = list(title = "Satisfaction Level"))

plot
# Technical Interpretation: The p-value (~2.326e-12) is far below 0.05, 
# indicating a statistically significant difference in satisfaction levels 
# between employees who left and those who stayed.

# Non-Technical Interpretation: Employees who left the company had lower 
# satisfaction levels than those who stayed. This suggests that improving 
# employee satisfaction could help reduce turnover.
### Test Two

t_test_hours <- t.test(hr$average_montly_hours ~ hr$left)
print(t_test_hours)
## 
##  Welch Two Sample t-test
## 
## data:  hr$average_montly_hours by hr$left
## t = -7.5323, df = 4875.1, p-value = 5.907e-14
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -10.534631  -6.183384
## sample estimates:
## mean in group 0 mean in group 1 
##        199.0602        207.4192
plot_data_hours <- hr %>%
  mutate(left_status = ifelse(left == 1, "Left", "Stayed"))

plot_hours <- plot_ly(plot_data_hours, x = ~left_status, y = ~average_montly_hours, type = "box",
                      boxpoints = "all", jitter = 0.3, pointpos = -1.8,
                      marker = list(color = 'rgb(214, 39, 40)'),
                      line = list(color = 'rgb(214, 39, 40)')) %>%
  layout(title = "Average Monthly Hours by Employee Status",
         xaxis = list(title = "Employee Status"),
         yaxis = list(title = "Average Monthly Hours"))

plot_hours
# Technical Interpretation: The t-test comparing average monthly hours between 
# employees who left the company (left = 1) and those who stayed (left = 0) 
# yields a p-value (check exact value from output). Since this p-value is likely 
# below the typical significance level of 0.05, it suggests a statistically 
# significant difference in average monthly hours between the two groups.

# Non-Technical Interpretation: Employees who left the company worked noticeably 
# different hours on average compared to those who stayed. This indicates that 
# the number of hours an employee works per month could be linked to their likelihood 
# of leaving the company, potentially due to factors such as workload or work-life balance.
### Test Three

t_test_evaluation <- t.test(hr$last_evaluation ~ hr$left)
print(t_test_evaluation)
## 
##  Welch Two Sample t-test
## 
## data:  hr$last_evaluation by hr$left
## t = -0.72534, df = 5154.9, p-value = 0.4683
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -0.009772224  0.004493874
## sample estimates:
## mean in group 0 mean in group 1 
##       0.7154734       0.7181126
plot_data_evaluation <- hr %>%
  mutate(left_status = ifelse(left == 1, "Left", "Stayed"))

plot_evaluation <- plot_ly(plot_data_evaluation, x = ~left_status, y = ~last_evaluation, type = "box",
                           boxpoints = "all", jitter = 0.3, pointpos = -1.8,
                           marker = list(color = 'rgb(44, 160, 44)'),
                           line = list(color = 'rgb(44, 160, 44)')) %>%
  layout(title = "Last Evaluation Score by Employee Status",
         xaxis = list(title = "Employee Status"),
         yaxis = list(title = "Last Evaluation Score"))

plot_evaluation
# Technical Interpretation: The t-test comparing last evaluation scores between 
# employees who left the company (left = 1) and those who stayed (left = 0) 
# yields a p-value (check exact value from output). If this p-value is below the 
# 0.05 significance level, it indicates a statistically significant difference 
# in last evaluation scores between the two groups.

# Non-Technical Interpretation: Employees who left the company had different 
# evaluation scores on average compared to those who stayed. This suggests that 
# performance evaluation scores might be linked to the likelihood of leaving the 
# company, potentially indicating that higher or lower scores could relate to turnover.
### test Four 

t_test_time_at_company <- t.test(hr$time_spend_company ~ hr$left)
print(t_test_time_at_company)
## 
##  Welch Two Sample t-test
## 
## data:  hr$time_spend_company by hr$left
## t = -22.631, df = 9625.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -0.5394767 -0.4534706
## sample estimates:
## mean in group 0 mean in group 1 
##        3.380032        3.876505
plot_data_time <- hr %>%
  mutate(left_status = ifelse(left == 1, "Left", "Stayed"))

plot_time <- plot_ly(plot_data_time, x = ~left_status, y = ~time_spend_company, type = "box",
                     boxpoints = "all", jitter = 0.3, pointpos = -1.8,
                     marker = list(color = 'rgb(255, 127, 14)'),
                     line = list(color = 'rgb(255, 127, 14)')) %>%
  layout(title = "Time Spent at Company by Employee Status",
         xaxis = list(title = "Employee Status"),
         yaxis = list(title = "Time Spent at Company (Years)"))

plot_time
# Technical Interpretation: The t-test comparing time spent at the company 
# between employees who left (left = 1) and those who stayed (left = 0) 
# yields a p-value (check exact value from output). If this p-value is below 
# 0.05, it indicates a statistically significant difference in time spent at 
# the company between the two groups.

# Non-Technical Interpretation: Employees who left the company had a different 
# amount of time at the company on average compared to those who stayed. This 
# suggests that the length of time someone has been with the company might be 
# related to whether they decide to leave, possibly indicating that longer 
# tenure could impact turnover likelihood.