-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-eda.Rmd
1579 lines (1315 loc) · 62.1 KB
/
03-eda.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Explore the data {#eda}
Before one starts with the actual modeling it is crucial to get to know the data and to bring it to the correct format. This process of getting familiar with the data is well known as Exploratory Data Analysis (EDA). To do this many packages are used.[@tidyverse; @tidymodels; @viridis; @ggtext; @viridisLite ; @patchwork; @visdat; @lubridate; @latexplots; @ggally; @plotly] The most important ones will be loaded below.
```{r load_packages, message=FALSE, warning=FALSE}
library(tidyverse) # general data handling tools
library(tidymodels) # data modeling and preprocessing
# color palettes
library(viridis)
library(viridisLite)
library(patchwork) # composing of ggplots
```
Before the fun can begin a quick outline of the steps performed on each data set.
- A general overview of the classes of the features and a visualization to detect any missing values.
- The distribution and the main effect on the outcome variable of each feature.
- The pairwise relationships of the predictors.
- Optionally some feature engineering.
- Using the gained knowledge to fix all pre-processing and feature engineering steps by using a `recipes::recipe`.
If one is mainly interested in the models themselves one can just have a look at the recipes and skip the rest of this section.
Some might ask why no interactions of predictors are covered in this EDA. If one would use a standard OLS, lasso or ridge regression it would be very important to have a look at them but as the focus here is on tree-based gradient boosting one already includes interactions if one is not restrictive to regression stumps.
It should also be mentioned that especially during the EDA most of the plotting code is not shown in order to support a better reading flow. If one wants to look up the code for any of these visualizations or also the full bookdown project one can have a deep dive at this [Github reposatory](https://github.com/EmanuelSommer/boosting_methods){.uri}. For example the respective code for the EDA can be found in the `03-eda.Rmd` file and the one for the modeling in the `04-modeling.Rmd` file. So having this said let's start with the first data set!
## Burnout data
The data is from the machine learning challenge *HackerEarth Machine Learning Challenge: Are your employees burning out?*. And can be downloaded here: <https://www.kaggle.com/blurredmachine/are-your-employees-burning-out?select=train.csv>
```{r loadBurn, message=FALSE}
# load the data
burnout_data <- read_csv("_data/burn_out_train.csv")
# convert colnames to snake_case
colnames(burnout_data) <- tolower(
stringr::str_replace_all(
colnames(burnout_data),
" ",
"_"
))
# omit missing values in the outcome variable
burnout_data <- burnout_data[!is.na(burnout_data$burn_rate),]
```
### Train-test split
To not allow information leakage the train-test split is performed at the very start of the whole analysis.
```{r traintest}
set.seed(2)
burnout_split <- rsample::initial_split(burnout_data, prop = 0.80)
burnout_train <- rsample::training(burnout_split)
burnout_test <- rsample::testing(burnout_split)
```
```{r, include=FALSE}
remove(burnout_data)
remove(burnout_split)
```
The training data set contains `r nrow(burnout_train)` rows and `r ncol(burnout_train)` variables.
The test data set contains `r nrow(burnout_test)` observations and naturally also `r ncol(burnout_test)` variables.
### Quick general overview
First look at the classes of the variables.
```{r, echo=FALSE}
col_classes_burn <- burnout_train %>%
summarise_all(class)
knitr::kable(
data.frame(column = colnames(burnout_train),
class = as.character(col_classes_burn[1, ]))
)
```
A general visualization of the whole data set to detect missing values below.
```{r visdat, echo=FALSE}
visdat::vis_dat(burnout_train)
```
```{r}
# percentage of missing values in the training data set
mean(rowSums(is.na(burnout_train)) > 0)
```
As we know that XGBoost can handle missing values we do not have to be concerned. Although one could of course think about imputation or even removal.
### What about the outcome variable?
`burn_rate`: For each employee telling the rate of burnout should be in $[0,1]$. The greater the score the worse the burnout (0 means no burnout at all). As the variable is continuous we have a regression task. Yet it has bounds which has to be treated with when predicting.
The five point summary below shows that the full range is covered and no invalid values are in the data.
```{r, echo=FALSE}
summary(burnout_train$burn_rate)
```
Now the distribution of the outcome.
```{r , echo=FALSE, warning=FALSE, message=FALSE}
burn_rate_box <- ggplot(burnout_train, aes(x = 1, y = burn_rate)) +
geom_jitter(alpha = 0.05, col = plasma(1)) +
geom_boxplot(col = "black", size = .8, fill = NA) +
labs(x = "", y = "Burn rate") +
coord_flip() +
theme_light() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank())
burn_rate_hist <- ggplot(burnout_train, aes(x = burn_rate)) +
geom_histogram(aes(y = ..density..),
fill = plasma(1), binwidth = 0.05) +
stat_function(fun = dnorm, args =
list(
mean = mean(burnout_train$burn_rate),
sd = sd(burnout_train$burn_rate)),
col = plasma(3)[2],
size = 1
) +
annotate("text",
x = 0.9, y = 1.6,
label = latex2exp::TeX("$N(\\hat{\\mu}, \\hat{\\sigma}^2)$") ,
col = plasma(3)[2], size = 5) +
labs(x = "", y = "", subtitle = "binwidth = 0.05",
title = "Outcome variable: **Burn Rate**") +
geom_line(
data = data.frame(
x = c(0.75, 0.9),
y = c(0.8, 1.3)
),
aes(x = x, y = y),
inherit.aes = FALSE,
col = plasma(3)[2],
size = .5) +
theme_light() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
burn_rate_raw <- burn_rate_hist / burn_rate_box
burn_rate_raw
ggsave("_pictures/burn_rate_raw.png", plot = burn_rate_raw)
remove(burn_rate_box, burn_rate_raw)
remove(burn_rate_hist)
```
The distribution of the outcome is very much symmetrical and bell shaped around 0.5 and the whole defined region $[0,1]$ is covered quite well. Actually by overlaying a normal distribution with the sample mean $\hat{\mu}$ and the sample standard deviation $\hat{\sigma}^2$ as the parameters one can clearly see that the outcome almost perfectly follows a normal distribution. One could further fit a Q-Q-plot to visualize the normality. **BUT** of course here there is a bounded domain while the normal distribution has the whole $\mathbb{R}$ as domain. This bounded domain does not interfere with the boosted model as tree-based models do not superimpose a distribution assumptions upon the target variable. Nevertheless one can transform the outcome with the empirical logit $log(\frac{y_i+0.5}{1-y_i+0.5})$.By doing this one removes the bounds on the target. One can then re-transform the predictions in the end by applying $\frac{2}{exp(-y)+1}-0.5$. Here to see whether this transformation changes the behavior or improves the boosting model one will have a look not only at the untransformed target `burn_rate` but also at the transformed one `burn_rate_trans`. The focus will be on the untransformed modeling as the low pre-processing strength of such boosting models should be emphasized. Below is the distribution of the transformed one.
```{r}
# Add transformed outcome
burnout_train$burn_rate_trans <- log((burnout_train$burn_rate + 0.5) /
(1.5 - burnout_train$burn_rate))
burnout_test$burn_rate_trans <- log((burnout_test$burn_rate + 0.5) /
(1.5 - burnout_test$burn_rate))
```
```{r , echo=FALSE, warning=FALSE, message=FALSE}
burn_rate_box <- ggplot(burnout_train, aes(x = 1, y = burn_rate_trans)) +
geom_jitter(alpha = 0.05, col = plasma(1)) +
geom_boxplot(col = "black", size = .8, fill = NA) +
labs(x = "", y = "Burn rate transformed") +
coord_flip() +
theme_light() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank())
burn_rate_hist <- ggplot(burnout_train, aes(x = burn_rate_trans)) +
geom_histogram(aes(y = ..density..),
fill = plasma(1), binwidth = 0.1) +
stat_function(fun = dnorm, args =
list(
mean = mean(burnout_train$burn_rate_trans),
sd = sd(burnout_train$burn_rate_trans)),
col = plasma(3)[2],
size = 1
) +
annotate("text",
x = 0.8, y = 0.7,
label = latex2exp::TeX("$N(\\hat{\\mu}, \\hat{\\sigma}^2)$") ,
col = plasma(3)[2], size = 5) +
labs(x = "", y = "", subtitle = "binwidth = 0.1",
title = "Outcome variable: **Burn Rate transformed**") +
geom_line(
data = data.frame(
x = c(0.5, 0.63),
y = c(0.4, 0.6)
),
aes(x = x, y = y),
inherit.aes = FALSE,
col = plasma(3)[2],
size = .5) +
theme_light() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
burn_rate_trans <- burn_rate_hist / burn_rate_box
burn_rate_trans
ggsave("_pictures/burn_rate_trans.png", plot = burn_rate_trans)
remove(burn_rate_box, burn_rate_trans)
remove(burn_rate_hist)
```
The transformed outcome basically resembles exactly the same properties as the untransformed one but the nice thing is that the bounds were removed. The further EDA will be based on the untransformed variable but the implications are the same due to the choice of the transformation via the empirical logit.
### Distribution and main effects of the predictors
#### Employee ID
`employee_id` is just an ID variable and thus is not useful for any prediction model. But one has to check for duplicates.
```{r}
# TRUE if there are NO duplicates
burnout_train %>%
group_by(employee_id) %>%
summarise(n = n()) %>%
nrow() == nrow(burnout_train)
```
Thus there are no duplicates which is good.
#### Date of joining
`date_of_joining` is the date the employee has joined the company. Thus a continuous variable that most likely needs some kind of feature engineering.
```{r, echo=FALSE}
burnout_train %>%
group_by(date_of_joining) %>%
summarise(count = n()) %>%
ggplot(aes(x = date_of_joining, y = count)) +
geom_line(col = plasma(1), na.rm = TRUE) +
theme_light() +
labs(title = "Distribution of the variable **Date of joining**",
x = "Date of joining") +
theme(plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
```
Although there is a lot of variation no major trends in hirings are visible from this plot. Overall the variable seems to be quite equally distributed over the year 2008.
```{r echo=FALSE}
ggplot(burnout_train,
aes(y = date_of_joining,
x = burn_rate)) +
geom_point(alpha = 0.1, col = plasma(1),
na.rm = TRUE) +
labs(y = "Date of joining", x = "Burn rate",
title = "Main effect of the variable **Date of joining**") +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
In its raw form the variable `date_of_joining` seems not to have a notable main effect on the outcome variable. Nevertheless the feature will be used in the model and as tree-based models have an in-built feature selection one can see after the fitting if the feature was helpful overall. The feature will not be included just as an integer (the default format how Dates are represented) but rather some more features like weekday or month will be extracted from the raw variable further down the road.
#### Gender
`gender` represents the gender of the employee. Definitely a categorical variable.
```{r}
# have a look at the discrete distribution
summary(factor(burnout_train$gender))
```
The two classes are well balanced. Now a look at the main effect of the feature.
```{r echo=FALSE}
gender_burn_rate_box <- ggplot(burnout_train,
aes(x = as.factor(gender),
y = burn_rate,
col = as.factor(gender))) +
geom_jitter(alpha = 0.05) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "", y = "Burn rate", col = "") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "None",
axis.text.y = element_blank())
gender_burn_rate_hist <- ggplot(burnout_train,
aes(x = burn_rate,
fill = as.factor(gender))) +
geom_histogram(binwidth = 0.05, alpha = 0.7,
position = "identity") +
scale_fill_viridis_d(option = "C") +
labs(x = "", y = "", subtitle = "binwidth = 0.05",
fill = "",title = "Main effect of the variable **gender**") +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "top",
axis.text.y = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
gender_burn_rate_hist / gender_burn_rate_box
remove(gender_burn_rate_hist)
remove(gender_burn_rate_box)
```
For both classes the distributions are very similar and symmetrical. It seems like the male employees have overall a slightly higher risk of having a higher burn score i.e. a burnout.
#### Company type
`company_type` is a binary categorical variable that indicates whether the company is a service or product company.
```{r}
# have a look at the discrete distribution
summary(factor(burnout_train$company_type))
```
In this case the classes are not fully balanced but each class is still well represented. Now a look at the main effect of the feature.
```{r echo=FALSE}
comptype_burn_rate_box <- ggplot(burnout_train,
aes(x = as.factor(company_type),
y = burn_rate,
col = as.factor(company_type))) +
geom_jitter(alpha = 0.05) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "", y = "Burn rate", col = "") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "None",
axis.text.y = element_blank())
comptype_burn_rate_hist <- ggplot(burnout_train,
aes(x = burn_rate,
fill = as.factor(company_type))) +
geom_histogram(binwidth = 0.05, alpha = 0.6,
position = "identity") +
scale_fill_viridis_d(option = "C") +
labs(x = "", y = "", subtitle = "binwidth = 0.05",
fill = "",title = "Main effect of the variable **Company Type**") +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "top",
axis.text.y = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
comptype_burn_rate_hist / comptype_burn_rate_box
remove(comptype_burn_rate_hist)
remove(comptype_burn_rate_box)
```
For both classes the distributions are almost identical and symmetrical. From an univariate point of view no notable main effect is visible from these visualizations.
#### Work from home setup
`wfh_setup_available` indicates whether a working from home setup is available for the employee. So this is again a binary variable.
```{r}
# have a look at the discrete distribution
summary(factor(burnout_train$wfh_setup_available))
```
The two classes are well balanced. Now a look at the main effect of the feature.
```{r echo=FALSE}
wfh_burn_rate_box <- ggplot(burnout_train,
aes(x = as.factor(wfh_setup_available),
y = burn_rate,
col = as.factor(wfh_setup_available))) +
geom_jitter(alpha = 0.05) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "", y = "Burn rate", col = "") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "None",
axis.text.y = element_blank())
wfh_burn_rate_hist <- ggplot(burnout_train,
aes(x = burn_rate,
fill = as.factor(wfh_setup_available))) +
geom_histogram(binwidth = 0.05, alpha = 0.6,
position = "identity") +
scale_fill_viridis_d(option = "C") +
labs(x = "", y = "", subtitle = "binwidth = 0.05",
fill = "",title = "Main effect of the variable **Work from home setup**") +
theme_light() +
theme(axis.ticks.y = element_blank(),
legend.position = "top",
axis.text.y = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
wfh_burn_rate_hist / wfh_burn_rate_box
remove(wfh_burn_rate_hist)
remove(wfh_burn_rate_box)
```
Again both distributions are quite similar i.e. bell shaped and symmetrical. Here quite a main effect is visible. A work from home setup most likely has a positive influence on the wellbeing and thus lowers the risk for a high burn rate.
#### Designation
`designation` A rate within $[0,5]$ that represents the designation in the company for the employee. High values indicate a greater amount of designation.
```{r}
# unique values of the feature
unique(burnout_train$designation)
```
As the feature has a natural ordering this variable will be treated as an ordinal one i.e. be encoded with the integers and not by one-hot-encoding.
```{r, echo=FALSE}
burnout_train %>%
group_by(designation) %>%
summarize(count = n()) %>%
ggplot(aes(x = factor(designation), y = count)) +
geom_bar(fill = plasma(1), stat = "identity") +
coord_flip() +
theme_light() +
labs(title = "Distribution of the variable **Designation**",
x = "level of designation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
Here clearly the more extreme levels of designation are less represented in the data. This makes total sense w.r.t. the meaning of the variable.
```{r echo=FALSE}
ggplot(burnout_train,
aes(x = as.factor(designation),
y = burn_rate,
col = as.factor(designation))) +
geom_jitter(alpha = 0.1) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "level of designation", y = "Burn rate", col = "",
title = "Main effect of the variable **Designation**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
A strong main effect is visible in the plot. The plot also further strengthens the hypothesis that we should treat the feature as ordinal. A higher level of designation seems to have an influence on the risk of having a burnout. For example employees from the training data set with a level of designation below 3 never even achieved a maximal burn score of one.
#### Resource allocation
`resource_allocation` A rate within $[1,10]$ that represents the resource allocation to the employee. High values indicate more resources allocated to the employee.
```{r}
# unique values of the feature
unique(burnout_train$resource_allocation)
```
Here again the question is whether one should encode this variable as a categorical or an ordinal categorical feature. In this case as there are quite some levels and again a natural ordering the variable will be encoded as a continuous integer score.
```{r, echo=FALSE}
burnout_train %>%
group_by(resource_allocation) %>%
summarize(count = n()) %>%
ggplot(aes(x = factor(resource_allocation), y = count)) +
geom_bar(fill = plasma(1), stat = "identity") +
coord_flip() +
theme_light() +
labs(title = "Distribution of the variable **Resource Allocation**",
x = "Resource Allocation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
A similar behavior as the one of the previous variable is visible. But here there are some missing values (NA's).
```{r echo=FALSE}
burnout_train %>%
mutate(resource_allocation = as.character(resource_allocation),
resource_allocation = if_else(is.na(resource_allocation),
"NA",
resource_allocation),
resource_allocation = as_factor(resource_allocation),
resource_allocation = fct_relevel(resource_allocation,
c("NA", paste(1:10)))) %>%
ggplot(
aes(x = resource_allocation,
y = burn_rate,
col = resource_allocation)) +
geom_jitter(alpha = 0.1, na.rm = TRUE) +
geom_boxplot(size = 0.8, col = "black", fill = NA, na.rm = TRUE) +
labs(x = "Resource Allocation", y = "Burn rate", col = "",
title = "Main effect of the variable **Resource Allocation**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
A strong main effect is visible in the plot. The plot again further strengthens the hypothesis that we should treat this feature as ordinal. A higher amount of resources assigned to an employee seems to have a positive influence on the risk of having a burnout. The missing values do not seem to have some structure as they replicate the base distribution of the outcome variable.
#### Mental fatigue score
`mental_fatigue_score` is the level of mental fatigue the employee is facing.
```{r}
# number of unique values
length(unique(burnout_train$mental_fatigue_score))
```
This variable will without a question be treated in a continuous way.
```{r, echo=FALSE}
ggplot(burnout_train, aes(x = mental_fatigue_score)) +
geom_density(fill = plasma(1), col = plasma(1),
na.rm = TRUE, bw = .8) +
theme_light() +
labs(title = "Distribution of the variable **Mental fatigue score**",
subtitle = "bw = 0.8",
x = "Mental fatigue score") +
theme(plot.title = ggtext::element_markdown(size = 11),
plot.subtitle = ggtext::element_markdown(size = 8))
```
Although there is a very slight skew towards a higher mental fatigue score the overall distribution is still more or less bell shaped and quite symmetrical. Moreover the whole allowed range is covered and the bounds are not violated. Next the main effect of the variable.
```{r echo=FALSE, message=FALSE}
mfs_main <- ggplot(burnout_train,
aes(y = mental_fatigue_score,
x = burn_rate)) +
geom_point(alpha = 0.1, col = plasma(1),
na.rm = TRUE) +
labs(y = "Mental fatigue score", x = "Burn rate",
title = "Main effect of the variable **Mental fatigue score**") +
annotate("text", x = .75, y = 2.5,
label = paste("Pearson Correlation:",
round(
cor(burnout_train$burn_rate,
burnout_train$mental_fatigue_score,
use = "comp"),
3
)),
col = plasma(1), size = 5) +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
mfs_main
ggsave("_pictures/mfs_main.png", plot = mfs_main)
remove(mfs_main)
```
This scatterplot shows drastic results! The mental fatigue score has an almost perfect linear relationship with the outcome variable. This is also underlined by the very high pearson correlation. This indicates that mental fatigue score will be a most important predictor. If a communication with the data collector would be possible it would be important to check whether the two scores have common confounding variables as then one would have to question the practical usability of this predictor. This comes from the fact that no model would be needed if it was as hard to collect the data about the predictors as the outcome data. Moreover there are `r sum(is.na(burnout_train$mental_fatigue_score))` missing values in the feature so for those the model has to rely on the other maybe more weak predictors. It should be noted that when evaluating the final model one should consider to compare its performance to a trivial model (like a single intercept model). When constructing such a trivial model one could and maybe should also use this variable (when available) to get a trivial prediction by scaling the `mental_fatigue_score` feature by a simple scalar.
```{r, include=FALSE}
sum(is.na(burnout_train$mental_fatigue_score))
```
### Relationships between the predictors
An exploration of the relationships between the predictors could also be done by having a look at a correlation and scatterplot matrix. This approach is much quicker than looking at each pairwise relationship individually but also not as precise as the one presented here. Especially for a lot of features such a matrix can get too big to grasp the subtle details. If this is necessary depends on the use case. A very good option if one wants an initial overview is the function `GGally::ggpairs`. So in the following each pairwise relationship will be covered.
#### Date of joining vs. the others
```{r ggpairs, echo=FALSE, message=FALSE, warning=FALSE, out.width='90%'}
# first date of joining vs the purely continuous feature
date_vs_mental <- ggplot(burnout_train,
aes(y = date_of_joining,
x = mental_fatigue_score)) +
geom_point(alpha = 0.1, col = plasma(1),
na.rm = TRUE) +
labs(y = "Mental fatigue score", x = "Date of joining",
title = "") +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
# now the categorical ones
date_vs_cat <- sapply(colnames(burnout_train)[3:7], function(var) {
ggplot(burnout_train, aes(y = date_of_joining,
x = factor(.data[[var]]),
col = factor(.data[[var]]))) +
geom_jitter(alpha = 0.1, na.rm = TRUE) +
geom_boxplot(size = 0.8, col = "black", fill = NA, na.rm = TRUE) +
labs(y = "Date of joining", x = var, col = "",
title = "") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None")
}, simplify = FALSE)
(date_vs_mental | date_vs_cat[[1]]) /
(date_vs_cat[[2]] | date_vs_cat[[3]]) /
(date_vs_cat[[4]] | date_vs_cat[[5]])
remove(date_vs_cat, date_vs_mental)
```
No major relationship can be detected here.
#### Gender vs. the remaining
**Contingency tables for the comparison of two binary features:**
```{r}
# Gender vs Company type
table(burnout_train$gender, burnout_train$company_type)
```
No huge tendency visible.
```{r}
# Gender vs Work from home setup
table(burnout_train$gender, burnout_train$wfh_setup_available)
```
Slightly more women have a work from home setup available.
**Now the ordinal variables:**
```{r, echo=FALSE}
burnout_train %>%
group_by(designation, gender) %>%
summarise(count = n(), .groups = "drop") %>%
ggplot(aes(x = factor(designation), y = count,
fill = gender)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Designation** vs **Gender**",
x = "level of designation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
It has to be noted again that female and male emplyees are almost equally represented in the data set. Thus one can see from the above plot that the biggest difference in distribution is for the levels 1 and 4 with opposing effects. While male employees more often have a quite high designation of 4 females are the much more frequent employee with designation level 1.
```{r, echo=FALSE}
burnout_train %>%
group_by(resource_allocation, gender) %>%
summarise(count = n(), .groups = "drop") %>%
ggplot(aes(x = factor(resource_allocation), y = count,
fill = gender)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Resource allocation** vs **Gender**",
x = "Resource allocation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
Here a major shift in distribution is visible towards men getting more resources allocated to them. This reflects the society that still promotes men much more often to high paying jobs that most often come with resource responsibility.
**Now the mental fatigue score:**
```{r, echo=FALSE}
ggplot(burnout_train,
aes(x = as.factor(gender),
y = mental_fatigue_score,
col = as.factor(gender))) +
geom_jitter(alpha = 0.05, na.rm = TRUE) +
geom_boxplot(size = 0.8, col = "black", fill = NA, na.rm = TRUE) +
labs(x = "", y = "Mental fatigue score", col = "",
title = "**Mental fatigue score** vs **Gender**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
This is of course very similar to the main effect of the `gender` variable as the outcome and the feature `mental_fatigue_score` are highly linearly correlated.
#### Company type vs. the remaining
```{r}
# Company type vs Work from home setup
table(burnout_train$company_type, burnout_train$wfh_setup_available)
```
No notable trend.
```{r, echo=FALSE}
comp_type_plot_list <- list()
comp_type_plot_list$des <- burnout_train %>%
group_by(designation, company_type) %>%
summarise(count = n(), .groups = "drop") %>%
group_by(company_type) %>%
mutate(rel_count = count / sum(count)) %>%
ggplot(aes(x = factor(designation), y = rel_count,
fill = company_type)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Designation** vs **Company type**",
fill = "Company type", y = "relative frequency",
x = "level of designation") +
theme(plot.title = ggtext::element_markdown(size = 11))
comp_type_plot_list$ress <- burnout_train %>%
group_by(resource_allocation, company_type) %>%
summarise(count = n(), .groups = "drop") %>%
group_by(company_type) %>%
mutate(rel_count = count / sum(count)) %>%
ggplot(aes(x = factor(resource_allocation), y = rel_count,
fill = company_type)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Resource allocation** vs **Company type**",
fill = "Company type", y = "relative frequency",
x = "Resource allocation") +
theme(plot.title = ggtext::element_markdown(size = 11))
comp_type_plot_list$mfs <- ggplot(burnout_train,
aes(x = as.factor(company_type),
y = mental_fatigue_score,
col = as.factor(company_type))) +
geom_jitter(alpha = 0.05, na.rm = TRUE) +
geom_boxplot(size = 0.8, col = "black", fill = NA, na.rm = TRUE) +
labs(x = "", y = "Mental fatigue score", col = "Company type",
title = "**Mental fatigue score** vs **Company type**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
(comp_type_plot_list$des | comp_type_plot_list$ress) /
comp_type_plot_list$mfs + plot_layout(guides = 'collect')
remove(comp_type_plot_list)
```
No trend here either.
#### Work from home setup vs. the remaining
```{r, echo=FALSE}
burnout_train %>%
group_by(designation, wfh_setup_available) %>%
summarise(count = n(), .groups = "drop") %>%
ggplot(aes(x = factor(designation), y = count,
fill = wfh_setup_available)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Designation** vs **Work from home setup**",
fill = "Work from home setup",
x = "level of designation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
A work from home setup is way more often available for employees with a lower designation ($\leq 2$).
```{r, echo=FALSE}
burnout_train %>%
group_by(resource_allocation, wfh_setup_available) %>%
summarise(count = n(), .groups = "drop") %>%
ggplot(aes(x = factor(resource_allocation), y = count,
fill = wfh_setup_available)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip() +
scale_fill_viridis_d(option = "C") +
theme_light() +
labs(title = "**Resource allocation** vs **Work from home setup**",
fill = "Work from home setup",
x = "Resource allocation") +
theme(plot.title = ggtext::element_markdown(size = 11))
```
The same structure as in the previous comparison is visible here again. Employees with a lower amount of resources allocated to them have more often a work from home setup available. This could be due to the fewer responsibilities they have in the business.
```{r, echo=FALSE}
ggplot(burnout_train,
aes(x = as.factor(wfh_setup_available),
y = mental_fatigue_score,
col = as.factor(wfh_setup_available))) +
geom_jitter(alpha = 0.05, na.rm = TRUE) +
geom_boxplot(size = 0.8, col = "black", fill = NA, na.rm = TRUE) +
labs(y = "Mental fatigue score", col = "",
x = "Work from home setup",
title = "**Mental fatigue score** vs **Work from home setup**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
Again this is of course very similar to the main effect of the `wfh_setup_available` variable as the outcome and the feature `mental_fatigue_score` are highly linearly correlated.
#### Designation vs the remaining
```{r, echo=FALSE, message=FALSE}
designationVsRessources <- burnout_train %>%
group_by(designation, resource_allocation) %>%
summarise(count = n(), .groups = "drop") %>%
ggplot(aes(x = factor(designation),
y = factor(resource_allocation),
fill = count,
label = as.character(count))) +
geom_tile() +
geom_text() +
scale_fill_viridis_c(option = "C") +
labs(x = "Level of designation",
y = "Resource allocation",
title = "**Designation** vs **Resource allocation**") +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
designationVsRessources
ggsave("_pictures/designationVsRessources.png",
plot = designationVsRessources, )
remove(designationVsRessources)
```
Here a strong quite linear relationship is visible. This is sensible as often more resource responsibility is given to employees with high designation.
The last two relationships will be omitted here as both for the variable `designation` as well as for `resource_allocation` the comparison with `mental_fatigue_score` will be very similar to the main effect of the two variables. This comes again from the high correlation of the latter with the outcome.
Overall some stronger and mainly less strong relationships between the predictors could be detected. Not like in ordinary least squares regression for gradient tree boosting no decorrelation and normalization of the features is needed. But before one fixes the pre-processing one can try to extract some more information from some features through some feature engineering.
### Some feature engineering
The only variable that allows for reasonable feature engineering is the date of joining predictor. One can try to extract some underlying patterns and see if an effect on the outcome is visible.
First extract the day of the week:
```{r echo=FALSE}
burnout_train %>%
mutate(wday = lubridate::wday(date_of_joining),
wday = ordered(wday,
levels = c("2", "3", "4", "5", "6", "7", "1"),
labels = c(
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday"
)
)) %>%
ggplot(
aes(x = as.factor(wday),
y = burn_rate,
col = as.factor(wday))
) +
geom_jitter(alpha = 0.1) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "Weekday", y = "Burn rate", col = "",
title = "Main effect of the variable **Weekday**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
No main effect is visible here. So try the month next.
```{r echo=FALSE}
burnout_train %>%
mutate(wday = lubridate::month(date_of_joining)) %>%
ggplot(
aes(x = as.factor(wday),
y = burn_rate,
col = as.factor(wday))
) +
geom_jitter(alpha = 0.1) +
geom_boxplot(size = 0.8, col = "black", fill = NA) +
labs(x = "Month", y = "Burn rate", col = "",
title = "Main effect of the variable **Month**") +
scale_color_viridis_d(option = "C") +
coord_flip() +
theme_light() +
theme(legend.position = "None",
plot.title = ggtext::element_markdown(size = 11))
```
Again no main effect is visible.
Nevertheless one can include those two variables into the model because as mentioned before tree-based models actually perform a feature selection at each split. So including these just comes at a small computational cost. This minimal pre-processing that is needed when dealing with tree-based models is actually one of its biggest strengths. It is very robust against any kind of weird selection of features with different scales for example. This is one of the reasons, beside the strong predictive power, for the heavy use of such models in data mining applications.[@elements]
### Create the recipe
A recipe is an object that defines a series of steps for data pre-processing and feature engineering.
```{r recipeBurn}
### recipe for xgboost (nominal variables must be dummy variables)
# define outcome, predictors and training data set
burnout_rec_boost <- recipe(burn_rate ~ date_of_joining + gender +
company_type + wfh_setup_available +
designation + resource_allocation +
mental_fatigue_score,
data = burnout_train) %>%
# extract the date features day of the week and month
step_date(date_of_joining, features = c("dow", "month")) %>%
# dummify all nominal features
step_dummy(all_nominal()) %>%
# encode the date as integers
step_mutate(date_of_joining = as.integer(date_of_joining))
### recipe for xgboost (nominal variables must be dummy variables)
### HERE the TRANSFORMED target
burnout_rec_boost_trans <- recipe(burn_rate_trans ~ date_of_joining + gender +
company_type + wfh_setup_available +
designation + resource_allocation +
mental_fatigue_score,
data = burnout_train) %>%
# extract the date features day of the week and month
step_date(date_of_joining, features = c("dow", "month")) %>%
# dummify all nominal features
step_dummy(all_nominal()) %>%
# encode the date as integers
step_mutate(date_of_joining = as.integer(date_of_joining))
### recipe for a random forest model for comparison (no dummy encoding needed)
# same as above without dummification but here the na's have to be
# imputed (here via knn)
burnout_rec_rf <- recipe(burn_rate ~ date_of_joining + gender +
company_type + wfh_setup_available +
designation + resource_allocation +
mental_fatigue_score,
data = burnout_train) %>%
step_string2factor(all_nominal()) %>%
step_impute_knn(resource_allocation, neighbors = 5) %>%
step_impute_knn(mental_fatigue_score, neighbors = 5) %>%
step_date(date_of_joining, features = c("dow", "month")) %>%
step_mutate(date_of_joining = as.integer(date_of_joining))
```
```{r, eval=FALSE, include=FALSE}
bake(prep(burnout_rec_boost), new_data = NULL) %>%
select(!starts_with("date_of_joining_")) %>%
visdat::gather_cor() %>%
mutate(
row_1 = str_replace_all(row_1, "_", " "),
row_2 = str_replace_all(row_2, "_", " "),
across(starts_with("row"), function(string) {
string <- str_remove(string, " Yes")
string <- str_remove(string, " Male")
string <- str_remove(string, " Service")
fac <- as.factor(string)
fac
}),
row_1 = fct_reorder(row_1, value, sum),
row_2 = fct_reorder(row_2, value, sum)
) %>%
ggplot(aes(x = row_1,
y = row_2,
fill = value,
label = as.character(round(value, 2)))) +
geom_tile() +
geom_text() +
scale_fill_gradient2(low = plasma(3)[1],
high = plasma(3)[2],
mid = "white",
midpoint = 0) +
labs(x = "",
y = "", fill = "",
title = "Pearson correlation matrix") +
theme_light() +
coord_equal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1,
vjust = 1),
panel.border = element_blank(),
panel.grid.major = element_blank(),
plot.title = ggtext::element_markdown(size = 11),
axis.text = ggtext::element_markdown(size = 11))
```
## Insurence data
The insurance data set is part of the book *Machine Learning with R* by Brett Lantz. It can be downloaded here: <https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/insurance.csv>
```{r loadIns, message=FALSE}
# load the data
insurance_data <- read_csv("_data/insurance.csv")
```
### Train-test split
```{r traintestIns}
set.seed(2)
ins_split <- rsample::initial_split(insurance_data, prop = 0.80)
ins_train <- rsample::training(ins_split)
ins_test <- rsample::testing(ins_split)
```
```{r, include=FALSE}
remove(insurance_data)
remove(ins_split)
```
The training data set contains `r nrow(ins_train)` rows and `r ncol(ins_train)` variables.
The test data set contains `r nrow(ins_test)` observations and naturally also `r ncol(ins_test)` variables.
### A general overview