library(readr)
## Warning: package 'readr' was built under R version 4.5.2
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.2
## 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(tidyverse)
## Warning: package 'tidyr' was built under R version 4.5.2
## Warning: package 'dplyr' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ stringr 1.5.2
## ✔ forcats 1.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
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.
hr_renamed <- hr %>%
mutate(left = ifelse(left == 1, "left", "stayed"))
t.test(hr$satisfaction_level~hr$left)
##
## Welch Two Sample t-test
##
## data: hr$satisfaction_level by hr$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_ly(
hr,
x = ~left ,
y = ~satisfaction_level ,
type = 'box',
boxmean = T
) %>% layout(
title = "Employees with lower satisfaction are more likely to leave",
xaxis = list(title = "department"),
yaxis = list(title = "satisfaction" ,
showticklabels = FALSE,
showgrid = FALSE,
zeroline = FALSE)
)
new <- hr %>%
filter(left == 'Stayed')
We reject the Ho because the p-value is less than alpha (.001) meaning that there is a difference in means of satisfaction level between those that stayed vs. those that left Employees with lower satisfaction are more likely to leave.
t.test(hr$average_montly_hours ~ hr_renamed$left)
##
## Welch Two Sample t-test
##
## data: hr$average_montly_hours by hr_renamed$left
## t = 7.5323, df = 4875.1, p-value = 5.907e-14
## alternative hypothesis: true difference in means between group left and group stayed is not equal to 0
## 95 percent confidence interval:
## 6.183384 10.534631
## sample estimates:
## mean in group left mean in group stayed
## 207.4192 199.0602
plot_ly(
data = hr_renamed,
x = ~left,
y = ~average_montly_hours,
type = "box",
boxmean = TRUE
) %>%
layout(
title = "Employees who work longer hours are more likely to leave",
xaxis = list(title = "Employee Status"),
yaxis = list(title = "Average Monthly Hours")
)
We reject the Ho because the p-value is less than alpha (.001) meaning that there is a significant difference in average monthly hours between employees who stayed and those who left Employees who work longer hours are more likely to leave.
t.test(hr$last_evaluation ~ hr_renamed$left)
##
## Welch Two Sample t-test
##
## data: hr$last_evaluation by hr_renamed$left
## t = 0.72534, df = 5154.9, p-value = 0.4683
## alternative hypothesis: true difference in means between group left and group stayed is not equal to 0
## 95 percent confidence interval:
## -0.004493874 0.009772224
## sample estimates:
## mean in group left mean in group stayed
## 0.7181126 0.7154734
plot_ly(
hr_renamed,
x = ~left,
y = ~last_evaluation,
type = 'box',
boxmean = T
) %>%
layout(
title = "Employees with extreme evaluation scores are more likely to leave",
xaxis = list(title = "Employee Status"),
yaxis = list(title = "Last Evaluation Score",
showticklabels = TRUE,
showgrid = FALSE,
zeroline = FALSE)
)
We fail to reject the Ho because the p-value is greater than alpha (.001), meaning that we do not have enough evidence to conclude that there is a statistically significant difference in last evaluation scores between employees that stayed vs. those that left.
t.test(hr$number_project ~ hr_renamed$left)
##
## Welch Two Sample t-test
##
## data: hr$number_project by hr_renamed$left
## t = 2.1663, df = 4236.5, p-value = 0.03034
## alternative hypothesis: true difference in means between group left and group stayed is not equal to 0
## 95 percent confidence interval:
## 0.006540119 0.131136535
## sample estimates:
## mean in group left mean in group stayed
## 3.855503 3.786664
plot_ly(
hr_renamed,
x = ~left,
y = ~number_project,
type = 'box',
boxmean = T
) %>%
layout(
title = "Employees with more projects are more likely to leave",
xaxis = list(title = "Employee Status"),
yaxis = list(title = "Number of Projects",
showticklabels = TRUE,
showgrid = FALSE,
zeroline = FALSE)
)
We fail to reject the Ho because the p-value is greater than alpha (.001), meaning that we do not have enough evidence to conclude that there is a statistically significant difference in the number of projects handled by employees who stayed vs. those who left.