Jacob Stoughton and Jakub Kepa

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(readr)
library(plotly)
library(AER)
## Loading required package: car
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
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.

Perform four (4) t-tests using any appropriate variables (continuous) by the variable left. Note that the variable left describes whether the employee left the company (left = 1), or not (left = 0)

T-Test 1: Average Monthly Hours

t.test(hr$average_montly_hours ~ hr$left)
## 
##  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

P-Value: The p-value is very low therefore there is a significant difference of average monthly hours between employees who have left and stayed.

T-Test: The t-test shows that the mean difference in hours betweeen the two groups is significant

Non-technical Interpretation: Employees who work more monthly hours are more likely to leave.

Visual:

plot_ly(hr, 
        x = ~factor(left, levels = c(0,1), labels = c("Stayed", "Left")) ,
        y = ~average_montly_hours , 
        type = 'box') %>%
  layout(title = "People Who Work More Hours are More Likely to Leave",
         yaxis = list(title = "Average Monthly Hours"),
         xaxis = list(title = "Employment Status"))

T-Test 2: Time Spent at Company

t.test(hr$time_spend_company ~ hr$left)
## 
##  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

P-Value: The p-value is very low therefore the difference in the time spent at the company between employees who stayed and left is not due to chance.

T-test: The t-test shows that the mean difference in time spent between those who stayed and left is significant.

Non-technical Interpretation: Employees who have worked at the company longer are more likely to leave.

Visual:

plot_ly(hr, 
        x = ~factor(left, levels = c(0,1), labels = c("Stayed", "Left")) ,
        y = ~time_spend_company , 
        type = 'box') %>%
        layout(title = "Longer Tenured Employees are More Likely to Leave",
               yaxis = list(title = "Time Spent At Company"),
               xaxis = list(title = "Employment Status"))

T-Test 3: Satisfaction Level

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

P-Value: The p-value is very low therefore the difference in the satisfaction level of employees who stayed and left is not due to chance.

T-Test: The t-test shows us that the mean satisfaction levels of the employees who stayed and left are significantly different.

Non-technical Interpretation: Employees with a Lower Satisfaction Level are More Likely to Leave.

Visual:

plot_ly(hr, 
        x = ~factor(left, levels = c(0,1), labels = c("Stayed", "Left")) ,
        y = ~satisfaction_level , 
        type = 'box') %>%
  layout(title = "Employees with Lower Satisfaction Level are More Likely to Leave",
         yaxis = list(title = "Employee Satisfaction Level"),
         xaxis = list(title = "Employment Status"))

T-Test 4: Number of Projects

t.test(hr$number_project ~ hr$left)
## 
##  Welch Two Sample t-test
## 
## data:  hr$number_project by hr$left
## t = -2.1663, df = 4236.5, p-value = 0.03034
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -0.131136535 -0.006540119
## sample estimates:
## mean in group 0 mean in group 1 
##        3.786664        3.855503

P-Value: Using the significance level of 0.01 (due to large dataset), the p-value is small, therefore any relations between the data of the two groups is likely insignificant

T-Test: The means for number of projects between employees who left and stayed are similar and not significant

Non-technical Interpretation: Number of projects has little to no effect on employees leaving

Visual:

plot_ly(hr, 
        x = ~factor(left, levels = c(0,1), labels = c("Stayed", "Left")) ,
        y = ~number_project , 
        type = 'box') %>%
  layout(title = "Number of Projects has Little Effect on Employees Leaving",
         yaxis = list(title = "Number of Projects"),
         xaxis = list(title = "Employment Status"))