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.
Chi-square Test 1
chisq.test(hr$left , hr$salary)
##
## Pearson's Chi-squared test
##
## data: hr$left and hr$salary
## X-squared = 381.23, df = 2, p-value < 2.2e-16
p -value interpretation: The p-value is very small and as is the
probability of these results being random.
Chi-square test interpretation: There is a dependence between salary
and whether or not employees left.
Technical interpretation: Employees who are most likely to stay tend
to have a medium to high salary.
# Calculate proportions
prop_data <- hr %>%
group_by(left) %>%
summarise(
low = sum(salary == "low") / n(),
medium = sum(salary == "medium") / n(),
high = sum(salary == "high") / n()
)
# Create stacked bar chart using Plotly
plot_ly(prop_data) %>%
add_bars(x = ~left, y = ~low, name = "Low",
marker = list(color = "blue")) %>%
add_bars(x = ~left, y = ~medium, name = "Medium",
marker = list(color = "pink")) %>%
add_bars(x = ~left, y = ~high, name = "High",
marker = list(color = "green")) %>%
layout(
barmode = "stack",
xaxis = list(title = "Employee Left (0 = Stayed, 1 = Left)"),
yaxis = list(title = "Proportion", tickformat = ",.0%"),
title = "Proportion of Employees by Salary Level and Departure Status",
showlegend = TRUE
)
Chi-square Test 2
chisq.test(hr$Department , hr$salary)
##
## Pearson's Chi-squared test
##
## data: hr$Department and hr$salary
## X-squared = 700.92, df = 18, p-value < 2.2e-16
p -value interpretation: The p-value is very small and as is the
probability of these results being random.
Chi-square test interpretation: There is a dependence between
department and salary level.
Technical interpretation: Most departments are similar, but
employees in the Management department are more likely to be paid medium
to high salaries.
# Calculate proportions
prop_data <- hr %>%
group_by(Department) %>%
summarise(
low = sum(salary == "low") / n(),
medium = sum(salary == "medium") / n(),
high = sum(salary == "high") / n()
)
# Create a stacked bar chart
plot_ly(prop_data) %>%
add_bars(x = ~Department, y = ~low, name = "Low",
marker = list(color = "pink")) %>%
add_bars(x = ~Department, y = ~medium, name = "Medium",
marker = list(color = "blue")) %>%
add_bars(x = ~Department, y = ~high, name = "High",
marker = list(color = "green")) %>%
layout(
barmode = "stack",
xaxis = list(title = "Department"),
yaxis = list(title = "Proportion", tickformat = ",.0%"),
title = "Proportion of Employees by Salary Level and Department"
)
Chi-square Test 3
chisq.test(hr$left , hr$Department)
##
## Pearson's Chi-squared test
##
## data: hr$left and hr$Department
## X-squared = 86.825, df = 9, p-value = 7.042e-15
p -value interpretation: The p-value is very small and as is the
probability of these results being random.
Chi-square test interpretation: There is a dependence between
department and whether employees stayed or left.
Technical interpretation: Less than 30% of employees in each
department left. Employees in the R&D and Management departments are
least likely to leave.
# Calculate proportions
prop_data <- hr %>%
group_by(Department) %>%
summarise(
left = sum(left == 1) / n(), # Proportion of employees who left
stayed = sum(left == 0) / n() # Proportion of employees who stayed
)
# Create a stacked bar chart
plot_ly(prop_data) %>%
add_bars(x = ~Department, y = ~left, name = "Left",
marker = list(color = "green")) %>%
add_bars(x = ~Department, y = ~stayed, name = "Stayed",
marker = list(color = "blue")) %>%
layout(
barmode = "stack", # Stack the bars
xaxis = list(title = "Department"),
yaxis = list(title = "Proportion", tickformat = ",.0%"),
title = "Proportion of Employees Who Left vs Stayed by Department"
)
Chi-square Test 4
chisq.test(hr$promotion_last_5years, hr$Department)
##
## Pearson's Chi-squared test
##
## data: hr$promotion_last_5years and hr$Department
## X-squared = 350.91, df = 9, p-value < 2.2e-16
p -value interpretation: The p-value is very small and as is the
probability of these results being random.
Chi-square test interpretation: There is a dependence between
department and whether employees had a promotion within the last 5
years.