library(readr)
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(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
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 the chi-square test (.5 point) Choose any two appropriate variables from the data and perform the chi-square test, displaying the results.

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

Interpret the results in technical terms (.5 point) For each chi-square test, explain what the test’s p-value means (significance).

The p-value is a very small, that means that the probability of these results being random is very small. There is a dependence between work accidents and if the employee left or did not leave.

Interpret the results in non-technical terms (1 point) For each chi-square test, what do the results mean in non-techical terms.

Those who did not have a work accident are more likely to leave.

Create a plot that helps visualize the chi-square test (.5 point) For each chi-square test, create a graph to help visualize the difference between means, if any. The title must be the non-technical interpretation.

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



plot_ly(prop_data) %>%
  add_bars(x = ~Work_accident, y = ~Not_left, name = "Not Left", 
           marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Work_accident, y = ~Left, name = "Left", 
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Work Accidents"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Those who did not have a work accident are more likely to leave."
  )

Perform the chi-square test (.5 point) Choose any two appropriate variables from the data and perform the chi-square test, displaying the results.

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

Interpret the results in technical terms (.5 point) For each chi-square test, explain what the test’s p-value means (significance).

The p-value is a very small, that means that the probability of these results being random is very small. There is a dependence between promotions and if the employee left or did not leave.

Interpret the results in non-technical terms (1 point) For each chi-square test, what do the results mean in non-techical terms.

Those who did not have a promotion are more likely to leave.

Create a plot that helps visualize the chi-square test (.5 point) For each chi-square test, create a graph to help visualize the difference between means, if any. The title must be the non-technical interpretation.

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



plot_ly(prop_data) %>%
  add_bars(x = ~promotion_last_5years, y = ~Not_left, name = "Not Left", 
           marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~promotion_last_5years, y = ~Left, name = "Left", 
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Promotions Last 5 Years"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Those who did not have a promotion are more likely to leave."
  )

Perform the chi-square test (.5 point) Choose any two appropriate variables from the data and perform the chi-square test, displaying the results.

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

Interpret the results in technical terms (.5 point) For each chi-square test, explain what the test’s p-value means (significance).

The p-value is a very small, that means that the probability of these results being random is very small. There is a dependence between department and if the employee left or did not leave.

Interpret the results in non-technical terms (1 point) For each chi-square test, what do the results mean in non-techical terms.

Those who work in HR are most likely to leave.

Create a plot that helps visualize the chi-square test (.5 point) For each chi-square test, create a graph to help visualize the difference between means, if any. The title must be the non-technical interpretation.

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


plot_ly(prop_data) %>%
  add_bars(x = ~Department, y = ~Not_left, name = "Not Left", 
           marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Department, y = ~Left, name = "Left", 
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Those who work in HR are most likely to leave."
  )

Perform the chi-square test (.5 point) Choose any two appropriate variables from the data and perform the chi-square test, displaying the results.

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

Interpret the results in technical terms (.5 point) For each chi-square test, explain what the test’s p-value means (significance).

The p-value is a very small, that means that the probability of these results being random is very small. There is a dependence between salary and if the employee left or did not leave.

Interpret the results in non-technical terms (1 point) For each chi-square test, what do the results mean in non-techical terms.

Those who have a low salary are most likely to leave.

Create a plot that helps visualize the chi-square test (.5 point) For each chi-square test, create a graph to help visualize the difference between means, if any. The title must be the non-technical interpretation.

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


plot_ly(prop_data) %>%
  add_bars(x = ~salary, y = ~Not_left, name = "Not Left", 
           marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~salary, y = ~Left, name = "Left", 
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Salary"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Those who have a low salary are most likely to leave."
  )