-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_exploratory_data_analysis.R
executable file
·169 lines (125 loc) · 5.23 KB
/
00_exploratory_data_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
library(tidyverse)
df <- readr::read_csv('data/employee_churn_data.csv')
head(df)
df$department <- factor(df$department)
df$promoted <- factor(df$promoted, levels = c(0,1), labels = c("not_promoted",
"promoted"))
df$salary <- factor(df$salary, levels = c("low", "medium", "high"))
df$bonus <- factor(df$bonus, levels = c(0,1), labels = c("not_received",
"received"))
df$left <- factor(df$left, levels = c("no", "yes"))
head(df)
save(df, file = "data/employee_churn_data.RData")
# churn prop by department
df %>% ggplot(aes(x = department, fill = left)) +
geom_bar(position = "fill") +
labs(x = "Department",
y = "Proportion of Employees") +
theme_bw()
# churn prop by department
# improved plot with %s
# prep df
d_plot <- df %>%
group_by(department, left) %>%
summarize(n = n()) %>%
mutate(pct = n/sum(n),
lbl = scales::percent(pct),
department = as_factor(department),
left = as_factor(left))
# plot
d_plot %>% ggplot(aes(x = department,
y = pct,
fill = left)) +
geom_bar(stat = "identity",
position = "fill") +
scale_y_continuous(breaks = seq(0, 1, .1)) +
geom_text(aes(label = lbl),
size = 3,
position = position_stack(vjust = 0.5)) +
labs(x = "Department",
y = "% of Employees",
title = "Employee Churn",
subtitle = "by department") +
theme_bw() + theme(axis.text.x = element_text(angle = 30))
# churn by salary group
df %>% ggplot(aes(x = salary, fill = left)) +
geom_bar(position = "fill") +
labs(x = "Salary Group",
y = "Proportion of Employees") +
theme_bw()
chisq.test(table(df$salary, df$left))
# Pearson's Chi-squared test with Yates' continuity correction
# X-squared = 1.1484, df = 2, p-value = 0.5632
# churn by promotion
df %>% ggplot(aes(x = promoted, fill = left)) +
geom_bar(position = "fill") +
labs(x = "Salary Group",
y = "Proportion of Employees",
title = "Employee Churn",
subtitle = "by being promoted or not") +
theme_bw()
chisq.test(table(df$promoted, df$left))
# Pearson's Chi-squared test with Yates' continuity correction
# X-squared = 12.436, df = 1, p-value = 0.0004212
# churn by bonus
df %>% ggplot(aes(x = bonus, fill = left)) +
geom_bar(position = "fill") +
labs(x = "Salary Group",
y = "Proportion of Employees") +
theme_bw()
chisq.test(table(df$bonus, df$left))
# Pearson's Chi-squared test with Yates' continuity correction
# X-squared = 1.1973, df = 1, p-value = 0.2739
# see the relationship between 3 continuous variables
# by department & churn
df %>% ggplot(aes(x = review, y = satisfaction, color = avg_hrs_month)) +
geom_jitter(alpha = .3) + scale_colour_gradientn(colours = terrain.colors(10)) +
geom_smooth(method = "lm", se = TRUE, color = "red") +
labs(x = "Composite score the employee received in their last evaluation",
y = "Employee satisfaction score",
title = "The relationship between employee evaluation score & employee satisfaction",
subtitle = "by department & employee churn",
caption = "There seems to be a negative relationship between employee review scores & employee satisfaction:
the higher review scores employees get the less satisfied they are,
\nThe average hours the employee worked in a month also seem to be involved in this relationship
\nVisual inspections suggest that the intercept and slope differences across those who left the company are noteworthy") +
theme_bw() +
facet_grid(left ~ department)
# see the relationship between 3 continuous variables
# by department, promotion & churn
df %>%
ggplot(aes(x = review, y = satisfaction)) +
geom_jitter(aes(color = promoted), alpha = 0.6, show.legend = TRUE) +
geom_smooth(method = "lm", se = TRUE, color = "red") +
theme_bw() +
facet_grid(left ~ department, scales = "free")
# adjust correlations for nested data
library(easystats)
df %>% select(review, satisfaction, avg_hrs_month, department) %>% correlation(multilevel = TRUE)
# see correlations with group by
df %>% select(review, satisfaction, avg_hrs_month, department, promoted, left) %>%
group_by(promoted, left) %>%
correlation()
# function taken from
# https://www.pipinghotdata.com/posts/2021-10-11-estimating-correlations-adjusted-for-group-membership/
# compute correlation adjusted for nested data
compute_adj_corr <- function(data, var_dep, var_ind, var_group){
mixed_formula <- glue::glue("{var_dep} ~ {var_ind} + (1 | {var_group})")
mixed_model <- lme4::lmer(mixed_formula, data)
coef_sign <- mixed_model %>%
broom.mixed::tidy() %>%
filter(term == var_ind) %>%
pull(estimate) %>%
sign()
r2_by_group <- performance::r2_nakagawa(mixed_model, by_group = TRUE)$R2[1]
adj_corr <- coef_sign * sqrt(r2_by_group)
return(adj_corr)
}
adj_corr_by_dept <- df %>% group_by(department) %>% nest() %>%
mutate(corr = map_dbl(data,
compute_adj_corr,
var_dep = "satisfaction",
var_ind = "review",
var_group = "left")) %>%
select(department, corr)
adj_corr_by_dept