forked from Hendrik147/HR_Analytics_in_R_book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-pay-gap.Rmd
329 lines (269 loc) · 10.4 KB
/
12-pay-gap.Rmd
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# (PART) HR Analytics practical examples{-}
# Gender Pay Gap {#pay-gap}
```{r pay-gap-analysis, include=FALSE}
chap <- 13
lc <- 0
rq <- 0
# **`r paste0("(LC", chap, ".", (lc <- lc + 1), ")")`**
# **`r paste0("(RQ", chap, ".", (rq <- rq + 1), ")")`**
knitr::opts_chunk$set(
tidy = FALSE,
out.width = '\\textwidth',
fig.height = 4,
warning = FALSE
)
options(scipen = 99, digits = 3)
# Set random number generator see value for replicable pseudorandomness. Why 76?
# https://www.youtube.com/watch?v=xjJ7FheCkCU
set.seed(76)
```
According to a recent Glassdoor survey, more than two-thirds (67 percent)
of U.S. employees say they would not apply for jobs at employers where they
believe a gender pay gap exists.1 Today, the gender pay gap is more than
a social or legal issue. It’s an issue that can affect the ability of employers to
attract and retain talent.
How should HR practitioners react to concerns about the gender pay gap?
One increasingly popular way is to perform an internal gender pay audit to
understand whether a gap exists at your company. This involves examining
your own payroll data for evidence of a gender pay gap, and making
recommendations to senior management about ways to lower gender
barriers in recruitment, hiring, pay and promotion before they arise as
broader organizational concerns.
The following example appeared on the Glassdor website in March 2017, with the title "How to Audit Your Gender Pay Gap: An Employers Guide Using R" and was written by Andrew Chamberlain, Ph.D.
```{r packages}
# Load R libraries.
library(tidyverse)
library(tidymodels)
library(devtools)
library(HRAnalytics) # Helper package
#Turn off scientific notation.
options(scipen = 999)
```
```{r load data}
# Load data.
gd_data = read_csv("https://glassdoor.box.com/shared/static/beukjzgrsu35fqe59f7502hruribd5tt.csv")
# N = 1000 total observations.
```
## Data Cleaning and Prep.
Before we can analyse whether we have gender pay gap, we need to prepare our data so it is cleaned up and has useful columns we can analyse.
We will perform the following steps in our process:
1. Make a number of sensible age brackets e.g. <25, 25-34, 35-44, 45-54, >54
2. Create total compensation variable (base pay + bonus)
3. Take natural logarithm of compensation variables (for percentage pay gap interpretation in regressions).
4. Convert values like department into values that can act as flags in our model and making the most commonly occuring value the default which everything else gets compared to in our model.
```{r Summary Statistics}
gd_data %>%
# Age brackets
mutate(age_bin = cut(age,
breaks = c(0, 25, 35, 45, 55, Inf),
right = FALSE)) %>%
# Total compensation
mutate(total_pay = basePay + bonus) %>%
# Log of compensation
mutate(log_base = log(basePay, base = exp(1)),
log_total = log(total_pay, base = exp(1)),
# Adds 1 to allow for log of 0 bonus values.
log_bonus = log(bonus + 1, base = exp(1))) %>%
# Make flags
mutate_if(is_character, fct_infreq) %>%
mutate(age_bin = fct_infreq(age_bin)) ->
gd_data_clean
```
## Summary Statistics by gender
We can look at summary statistics by gender.
```{r gd_summary_gender_base}
# Base pay summary stats.
gd_data_clean %>%
# Exclude stuff with missing info
filter(!is.na(basePay)) %>%
# Analyse by gender
group_by(gender) %>%
# Retrieve summary statistics
summarise(
mean_base = mean(basePay),
median_base = median(basePay),
count = n()
) ->
gd_summary_gender_base
gd_summary_gender_base
```
```{r gd_summary_gender_total}
# Total pay summary stats.
gd_data_clean %>%
# Use a helper function to save typing
HRAnalytics::gap_summary(gender, total_pay) ->
gd_summary_gender_total
gd_summary_gender_total
```
```{r gd_summary_gender_bonus}
# Bonus summary stats.
gd_data_clean %>%
# Use a helper function to save typing
HRAnalytics::gap_summary(gender, bonus) ->
gd_summary_gender_bonus
gd_summary_gender_bonus
```
```{r gd_summary_gender_perf}
# Performance evaluations summary stats.
gd_data_clean %>%
# Use a helper function to save typing
HRAnalytics::gap_summary(gender, perfEval) ->
gd_summary_gender_perf
gd_summary_gender_perf
```
## Avoiding Simpson's Paradox
Simspson's Paradox is where some high level aggregations lead to a different conclusion than would have been arrived at if a lower level of detail had been looked at. We should check out values split by department and job title to see if they showcase wage differences.
```{r gd_summary_dept_gender_total}
# Performance evaluations summary stats.
gd_data_clean %>%
filter(!is.na(total_pay)) %>%
# Create a summary by department and gender
group_by(dept, gender) %>%
summarise(
mean_perf = mean(total_pay),
median_perf = median(total_pay),
count = n()
) %>%
# "Unpivot" the data
gather(measure, value, mean_perf:count) %>%
# Combine the gender with measure
unite(combo, measure, gender) %>%
# "Pivot" the data to see all the measures split by gender
spread(combo, value) ->
gd_summary_dept_gender_total
gd_summary_dept_gender_total
```
```{r gd_summary_job_gender_total}
# Performance evaluations summary stats.
gd_data_clean %>%
filter(!is.na(total_pay)) %>%
# Create a summary by job and gender
group_by(jobTitle, gender) %>%
summarise(
mean_perf = mean(total_pay),
median_perf = median(total_pay),
count = n()
) %>%
# "Unpivot" the data
gather(measure, value, mean_perf:count) %>%
# Combine the gender with measure
unite(combo, measure, gender) %>%
# "Pivot" the data to see all the measures split by gender
spread(combo, value) ->
gd_summary_job_gender_total
gd_summary_job_gender_total
```
## Model Estimation: OLS with controls.
Coefficient on "male" has the interpretation of approximate male pay advantage ("gender pay gap").
### Logarithm of Base Pay
```{r Linear models}
# No controls. ("unadjusted" pay gap.)
lm_gender <- lm(log_base ~ gender, data = gd_data_clean)
# Adding "human capital" controls (performance evals, age and education).
lm_humancapital <- lm(log_base ~ gender + perfEval + age_bin + edu, data = gd_data_clean)
# Adding all controls. ("adjusted" pay gap.)
lm_allcontrols <- lm(log_base ~ gender + perfEval + age_bin + edu + dept + seniority + jobTitle, data = gd_data_clean)
```
```{r lm_gender}
lm_gender %>%
summary()
```
```{r lm_gender viz}
lm_gender %>%
# Get the predicted values
augment() %>%
# Rename columns to more friendly names
rename(actual = log_base, predicted = .fitted) %>%
# Build a chart
ggplot() +
# Add columns to it
aes(x=actual, y=predicted) +
# Choose chart type
geom_point() +
# Add a diagonal line representing perfect predictions
geom_abline(colour="blue", slope=1, intercept=0)+
# Split by gender
facet_wrap(~gender) +
# Add some labels
labs(title="Actual vs predicted",
subtitle="Values predicted using a linear model containing gender")
```
```{r lm_humancapital}
lm_humancapital %>%
summary()
```
```{r lm_humancapital viz}
lm_humancapital %>%
# Get the predicted values
augment() %>%
# Rename columns to more friendly names
rename(actual = log_base, predicted = .fitted) %>%
HRAnalytics::pred_vs_actuals() +
# Add some labels
labs(title="Actual vs predicted",
subtitle="Values predicted using a linear model containing human capital measures")
```
```{r lm_allcontrols}
lm_allcontrols %>%
summary()
```
```{r lm_allcontrols viz}
lm_allcontrols %>%
# Get the predicted values
augment() %>%
# Rename columns to more friendly names
rename(actual = log_base, predicted = .fitted) %>%
HRAnalytics::pred_vs_actuals() +
# Add some labels
labs(title="Actual vs predicted",
subtitle="Values predicted using a linear model all controls")
```
```{r coefficients}
# Gather up all the models
list(lm_gender, lm_humancapital, lm_allcontrols) %>%
# Extract coefficients for all models at once and combine into a single table
map_df(tidy, .id = "model") %>%
# Let's look at the impact of gender
filter(term=="genderFemale") %>%
# P values less than 0.05 are usually taken to mean an estimate is reliable
select(model, log_gap=estimate, p.value)
```
Note that too little data can make results unreliable for a complex model because there are so few records to use for each combination of values.
### Results by Department
(Interaction of gender x dept)
To test for differences by department, examine significance of each "gender x dept" coefficient.
For the gender pay gap by department, add the "gender" + "gender x dept" coefficients from this model.
```{r lm_allcontrols_dept}
# All controls with department interaction terms.
lm_allcontrols_dept <- lm(log_base ~ gender*dept + perfEval + age_bin + edu + seniority + jobTitle, data = gd_data_clean)
tidy(lm_allcontrols_dept)
lm_allcontrols_dept %>%
# Get the predicted values
augment() %>%
# Rename columns to more friendly names
rename(actual = log_base, predicted = .fitted) %>%
HRAnalytics::pred_vs_actuals() +
# Add some labels
labs(title="Actual vs predicted",
subtitle="Values predicted using a linear model all controls & department interaction")
```
### Results by Job Title
(Interaction of gender x job )
To test for differences by department, examine significance of each "gender x job title" coefficient.
For the gender pay gap by job, add the "gender" + "gender x job title" coefficients from this model.
```{r lm_allcontrols_job}
# All controls with department interaction terms.
lm_allcontrols_job <- lm(log_base ~ gender*jobTitle + perfEval + age_bin + edu + seniority + dept, data = gd_data_clean)
tidy(lm_allcontrols_job)
lm_allcontrols_job %>%
# Get the predicted values
augment() %>%
# Rename columns to more friendly names
rename(actual = log_base, predicted = .fitted) %>%
# Build a chart
HRAnalytics::pred_vs_actuals() +
# Add some labels
labs(title="Actual vs predicted",
subtitle="Values predicted using a linear model all controls & job interaction")
```
For additional analysis via Oaxaca-Blinder decomposition, please see documentation for the [`oaxaca` package](https://cran.r-project.org/package=oaxaca) in R.