Tasks
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."
)