Tasks

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


For each of the four chi square tests:

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

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

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

D. 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.


1.A

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

1.B

P-value is very small, therefore the probability of these results being random is very small, the chi-square test shows us there is dependency between the work accident and leaving the company.

1.C

Employees that did not have a work accident are more than 3 times more likely to leave.

1.D

accident_data <- hr %>% 
  mutate(Work_accident = as.factor(Work_accident)) %>% 
  group_by(Work_accident) %>% #this is the variable that needs to change
  summarise(
    Stayed = sum(left ==0)/n(),
    Left = sum(left == 1)/n()
  )

plot_ly(accident_data) %>%
  add_bars(x = ~Work_accident, y = ~Stayed, name = "Stayed",
         marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~Work_accident, y = ~Left, name = "Left",
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Work Accident"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees with no work accidents are more than 3 times more likely to leave"
  )

2.A

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

2.B

P-value is very small, therefore the probability of these results being random is very small and the chi-square test shows us there is dependency between the promotion in last 5 years and leaving the company.

2.C

Employees that did not receive a promotion in the past 5 years are more than 4 times more likely to leave.

2.D

promo_data <- hr %>% 
  mutate(promotion_last_5years = as.factor(promotion_last_5years)) %>% 
  group_by(promotion_last_5years) %>% #this is the variable that needs to change
  summarise(
    Stayed = sum(left ==0)/n(),
    Left = sum(left == 1)/n()
  )


plot_ly(promo_data) %>%
  add_bars(x = ~promotion_last_5years, y = ~Stayed, name = "Stayed",
           marker = list(color = "#1f77b4")) %>%
  add_bars(x = ~promotion_last_5years , y = ~Left, name = "Left",
           marker = list(color = "#ff7f0e")) %>%
  layout(
    barmode = "stack",
    xaxis = list(title = "Promotion Last 5 Years"),
    yaxis = list(title = "Proportion", tickformat = ",.0%"),
    title = "Employees that did not receive a promotion in the past 5 years are more than 4 times more likely to leave"
)

3.A

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

3.B

P-value is very small, therefore the probability of these results being random is very small and the chi-square test shows us there is dependency between the Department and leaving the company.

3.C

Most departments have similar attrition rate (around 25%), except management and R&D (around 15%).

3.D

Dept_data <- hr %>% 
  mutate(Department = as.factor(Department)) %>% 
  group_by(Department) %>% #this is the variable that needs to change
  summarise(
    Stayed = sum(left ==0)/n(),
    Left = sum(left == 1)/n()
  )


plot_ly(Dept_data) %>%
  add_bars(x = ~Department, y = ~Stayed, name = "Stayed", 
           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 = "Most departments have similar attrition rate (around 25%), except management and R&D (aorund 15%)."
  )

4.A

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

4.B

P-value is very small, therefore the probability of these results being random is very small and the chi-square test shows us there is dependency between the salary and leaving the company.

4.C

Employees with a low salary are almost 2 times more likely to leave compared to the overall group.

4.D

Salary_data <- hr %>% 
  mutate(salary = as.factor(salary)) %>% 
  group_by(salary) %>% #this is the variable that needs to change
  summarise(
    Stayed = sum(left ==0)/n(),
    Left = sum(left == 1)/n()
  )


plot_ly(Salary_data) %>%
  add_bars(x = ~salary, y = ~Stayed, name = "Stayed", 
           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 = "Employees with a low salary are almost 2 times more likely to leave compared to the overall group."
  )