R Markdown

library(readr)
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
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.

First test

1. Perform the chi-square test

chisq.test(hr$promotion_last_5years, hr$left)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  hr$promotion_last_5years and hr$left
## X-squared = 56.262, df = 1, p-value = 6.344e-14

X-squared = 56.262, df = 1, p-value = 6.344e-14

2. Interpret the results in technical terms.

The p-value is 6.344e-14. This is very small, meaning there is a very small chance that this is random. There is a dependence between the promotion in the last 5 years and the employee leaving.

3. Interpret the results in non-technical terms.

People promoted in the last 5 years are less likely to leave. 18% more people stayed after getting promoted.

4. Create a plot that helps visualize the chi-square test.

Calculate proportions

prop_data <- hr %>%
  mutate(promotion_last_5years = as.factor(promotion_last_5years)) %>%
  group_by(promotion_last_5years) %>% 
  summarise(
    Stayed = sum(left == 0) / n(),
    Left = sum(left == 1) / n()
  )

Create stacked bar chart

plot_ly(prop_data) %>% 
  add_bars(x = ~promotion_last_5years, y = ~Stayed, name = "Stayed", 
           marker = list(color = "#f5428a")) %>% 
  add_bars(x = ~promotion_last_5years, y = ~Left, name = "Left", 
           marker = list(color = "#42e6f5")) %>%
  
  layout(
    barmode = "stack",
    xaxis = list(title = "Promotion Last 5 Years"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "18% More People Stayed After Getting Promoted in The Last 5 Years"
  )

Second test

1. Perform the chi-square test

chisq.test(hr$Department, hr$left)
## 
##  Pearson's Chi-squared test
## 
## data:  hr$Department and hr$left
## X-squared = 86.825, df = 9, p-value = 7.042e-15

X-squared = 86.825, df = 9, p-value = 7.042e-15

2. Interpret the results in technical terms.

The p-value is 7.042e-15. This is very small, meaning there is a very small chance that this is random. There is a dependence between the Department and the employee leaving.

3. Interpret the results in non-technical terms.

The department is going to have an affect on people leaving. HR (29%) is 2x more likely to leave than Management (14%).

4. Create a plot that helps visualize the chi-square test.

prop_data1 <- hr %>%
  mutate(Department = as.factor(Department)) %>%
  group_by(Department) %>% 
  summarise(
    Stayed = sum(left == 0) / n(),
    Left = sum(left == 1) / n()
  )
plot_ly(prop_data1) %>% 
  add_bars(x = ~Department, y = ~Stayed, name = "Stayed", 
           marker = list(color = "#03fcd7")) %>% 
  add_bars(x = ~Department, y = ~Left, name = "Left", 
           marker = list(color = "#8403fc")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "HR is 2x More Likely to Leave Than Management"
  )

Third test

1. Perform the chi-square test

chisq.test(hr$Work_accident , hr$left)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  hr$Work_accident and hr$left
## X-squared = 357.56, df = 1, p-value < 2.2e-16

X-squared = 357.56, df = 1, p-value < 2.2e-16

2. Interpret the results in technical terms.

p-value is extremely small, it is very unlikely these results were random. There is a dependence between the work accidents and working at the company.

3. Interpret the results in non-technical terms.

Employees who have work accidents are less likely to no longer be working for the company.

4. Create a plot that helps visualize the chi-square test.

prop_data <- hr %>%
  group_by(Work_accident) %>%
  summarise(
    Stayed = sum(left == 0) / n(),
    Left = sum(left == 1) / n()
  )

Create stacked bar chart

plot_ly(prop_data) %>%
  add_bars(x = ~Work_accident, y = ~Stayed, name = "Stayed",
           marker = list(color = "#f7e20e")) %>%
  add_bars(x = ~Work_accident, y = ~Left, name = "Left",
           marker = list(color = "#503203")) %>%
layout(
    barmode = "stack",
    xaxis = list(title = "Work Accidents"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Workers who get into accidents are 19% less likely to leave"
  )

Fourth test

1. Perform the chi-square test.

chisq.test(hr$salary , hr$left)
## 
##  Pearson's Chi-squared test
## 
## data:  hr$salary and hr$left
## X-squared = 381.23, df = 2, p-value < 2.2e-16

X-squared = 381.23, df = 2, p-value < 2.2e-16

2. Interpret the results in technical terms.

p-value is extremely small, it is very unlikely these results were random. There is a dependence between salary and working for the company.

3. Interpret the results in non-technical terms.

Workers who have lower salaries are more likely to leave.

4. Create a plot that helps visualize the chi-square test.

prop_data <- hr %>%
  group_by(salary) %>%
  summarise(
    Stayed = sum(left == 0) / n(),
    Left = sum(left == 1) / n()
  )

Create stacked bar chart

plot_ly(prop_data) %>%
  add_bars(x = ~salary, y = ~Stayed, name = "Stayed",
           marker = list(color = "#f7e")) %>%
  add_bars(x = ~salary, y = ~Left, name = "Left",
           marker = list(color = "#32e1f9")) %>%
   layout(
    barmode = "stack",
    xaxis = list(title = "Salary Level"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Workers who have lower salary are more likely to leave"
  )