-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexploring_mtpl2_dataset.Rmd
666 lines (466 loc) · 17.2 KB
/
exploring_mtpl2_dataset.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
---
title: "Exploring the MTPL2 Dataset"
author: "Mick Cooney <[email protected]>"
date: "`r Sys.Date()`"
output:
rmdformats::readthedown:
fig_caption: yes
toc_depth: 3
use_bookdown: yes
html_document:
fig_caption: yes
theme: spacelab
highlight: pygments
number_sections: TRUE
toc: TRUE
toc_depth: 3
toc_float:
smooth_scroll: FALSE
pdf_document: default
---
```{r import_libraries, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(tidy = FALSE,
cache = FALSE,
warning = FALSE,
message = FALSE,
fig.height = 8,
fig.width = 11
)
library(conflicted)
library(tidyverse)
library(scales)
library(cowplot)
library(magrittr)
library(rlang)
library(purrr)
library(vctrs)
library(fs)
library(forcats)
library(snakecase)
library(lubridate)
source("custom_functions.R")
resolve_conflicts(c("magrittr", "rlang", "dplyr", "readr", "purrr", "ggplot2"))
options(width = 80L,
warn = 1,
mc.cores = parallel::detectCores()
)
theme_set(theme_cowplot())
set.seed(42)
```
```{r custom_functions, echo=FALSE}
### Checks if variable is a date/time
is_date <- function(x)
x %>% inherits(c("POSIXt", "POSIXct", "POSIXlt", "Date", "hms"))
### Returns the category of data type passed to it
categorise_datatype <- function(x) {
if (all(are_na(x))) return("na")
if (is_date(x)) "datetime"
else if (!is_null(attributes(x)) ||
all(is_character(x))) "discrete"
else if (all(is_logical(x))) "logical"
else "continuous"
}
### create_coltype_list() splits columns into various types
create_coltype_list <- function(data_tbl) {
coltypes <- data_tbl %>% map_chr(categorise_datatype)
cat_types <- coltypes %>% unique() %>% sort()
split_lst <- cat_types %>% map(~ coltypes[coltypes %in% .x] %>% names())
names(split_lst) <- coltypes %>% unique() %>% sort()
coltype_lst <- list(
split = split_lst,
columns = coltypes
)
return(coltype_lst)
}
```
This workbook was created using the "dataexpks" template:
https://github.com/DublinLearningGroup/dataexpks
# Introduction
This workbook performs the basic data exploration of the dataset.
```{r set_exploration_params, echo=TRUE}
level_exclusion_threshold <- 100
cat_level_count <- 40
hist_bins_count <- 50
```
# Load Data
First we load the dataset.
```{r load_dataset, echo=TRUE}
rawdata_tbl <- read_rds("data/modelling2_data_tbl.rds") %>% select(-sev_data)
rawdata_tbl %>% glimpse()
```
## Perform Quick Data Cleaning
```{r perform_simple_datatype_transforms, echo=TRUE}
cleaned_names <- rawdata_tbl %>% names()
data_tbl <- rawdata_tbl %>% set_colnames(cleaned_names)
data_tbl %>% glimpse()
```
```{r, echo=FALSE}
#knitr::knit_exit()
```
## Create Derived Variables
We now create derived features useful for modelling. These values are
new variables calculated from existing variables in the data.
```{r construct_derived_values, echo=FALSE}
data_tbl <- data_tbl
data_tbl %>% glimpse()
```
## Check Missing Values
Before we do anything with the data, we first check for missing values
in the dataset. In some cases, missing data is coded by a special
character rather than as a blank, so we first correct for this.
```{r replace_missing_character, echo=TRUE}
### _TEMPLATE_
### ADD CODE TO CORRECT FOR DATA ENCODING HERE
```
With missing data properly encoded, we now visualise the missing data in a
number of different ways.
### Univariate Missing Data
We first examine a simple univariate count of all the missing data:
```{r missing_data_univariate_count, echo=TRUE}
row_count <- data_tbl %>% nrow()
missing_univariate_tbl <- data_tbl %>%
summarise_all(list(~sum(are_na(.)))) %>%
gather("variable", "missing_count") %>%
mutate(missing_prop = missing_count / row_count)
ggplot(missing_univariate_tbl) +
geom_bar(aes(x = fct_reorder(variable, -missing_prop),
weight = missing_prop)) +
xlab("Variable") +
ylab("Missing Value Proportion") +
theme(axis.text.x = element_text(angle = 90))
```
We remove all variables where all of the entries are missing
```{r remove_entirely_missing_vars, echo=TRUE}
remove_vars <- missing_univariate_tbl %>%
filter(missing_count == row_count) %>%
pull(variable)
lessmiss_data_tbl <- data_tbl %>%
select(-one_of(remove_vars))
```
With these columns removed, we repeat the exercise.
```{r missing_data_univariate_count_redux, echo=TRUE}
missing_univariate_tbl <- lessmiss_data_tbl %>%
summarise_all(list(~sum(are_na(.)))) %>%
gather("variable", "missing_count") %>%
mutate(missing_prop = missing_count / row_count)
ggplot(missing_univariate_tbl) +
geom_bar(aes(x = fct_reorder(variable, -missing_prop),
weight = missing_prop)) +
xlab("Variable") +
ylab("Missing Value Proportion") +
theme(axis.text.x = element_text(angle = 90))
```
To reduce the scale of this plot, we look at the top twenty missing data
counts.
```{r missing_data_univariate_top10_count, echo=TRUE}
missing_univariate_top_tbl <- missing_univariate_tbl %>%
arrange(desc(missing_count)) %>%
top_n(n = 50, wt = missing_count)
ggplot(missing_univariate_top_tbl) +
geom_bar(aes(x = fct_reorder(variable, -missing_prop),
weight = missing_prop)) +
xlab("Variable") +
ylab("Missing Value Proportion") +
theme(axis.text.x = element_text(angle = 90))
```
### Multivariate Missing Data
It is useful to get an idea of what combinations of variables tend to have
variables with missing values simultaneously, so to construct a visualisation
for this we create a count of all the times given combinations of variables
have missing values, producing a heat map for these combination counts.
```{r missing_data_matrix, echo=TRUE}
row_count <- rawdata_tbl %>% nrow()
count_nas <- ~ .x %>% are_na() %>% vec_cast(integer())
missing_plot_tbl <- rawdata_tbl %>%
mutate_all(count_nas) %>%
mutate(label = pmap_chr(., str_c)) %>%
group_by(label) %>%
summarise_all(list(sum)) %>%
arrange(desc(label)) %>%
select(-label) %>%
mutate(label_count = pmap_int(., pmax)) %>%
gather("col", "count", -label_count) %>%
mutate(miss_prop = count / row_count,
group_label = sprintf("%6.4f", round(label_count / row_count, 4))
)
ggplot(missing_plot_tbl) +
geom_tile(aes(x = col, y = group_label, fill = miss_prop), height = 0.8) +
scale_fill_continuous() +
scale_x_discrete(position = "top") +
xlab("Variable") +
ylab("Missing Value Proportion") +
theme(axis.text.x = element_text(angle = 90))
```
This visualisation takes a little explaining.
Each row represents a combination of variables with simultaneous missing
values. For each row in the graphic, the coloured entries show which particular
variables are missing in that combination. The proportion of rows with that
combination is displayed in both the label for the row and the colouring for
the cells in the row.
## Inspect High-level-count Categorical Variables
With the raw data loaded up we now remove obvious unique or near-unique
variables that are not amenable to basic exploration and plotting.
```{r find_highlevelcount_categorical_variables, echo=TRUE}
coltype_lst <- create_coltype_list(data_tbl)
count_levels <- ~ .x %>% unique() %>% length()
catvar_valuecount_tbl <- data_tbl %>%
summarise_at(coltype_lst$split$discrete, count_levels) %>%
gather("var_name", "level_count") %>%
arrange(-level_count)
print(catvar_valuecount_tbl)
row_count <- nrow(data_tbl)
cat(str_c("Dataset has ", row_count, " rows\n"))
```
Now that we a table of the counts of all the categorical variables we can
automatically exclude unique variables from the exploration, as the level
count will match the row count.
```{r remove_id_variables, echo=TRUE}
unique_vars <- catvar_valuecount_tbl %>%
filter(level_count == row_count) %>%
pull(var_name)
print(unique_vars)
explore_data_tbl <- data_tbl %>%
select(-one_of(unique_vars))
```
Having removed the unique identifier variables from the dataset, we
may also wish to exclude categoricals with high level counts also, so
we create a vector of those variable names.
```{r collect_highcount_variables, echo=TRUE}
highcount_vars <- catvar_valuecount_tbl %>%
filter(level_count >= level_exclusion_threshold,
level_count < row_count) %>%
pull(var_name)
cat(str_c(highcount_vars, collapse = ", "))
```
We now can continue doing some basic exploration of the data. We may
also choose to remove some extra columns from the dataset.
```{r drop_variables, echo=TRUE}
### You may want to comment out these next few lines to customise which
### categoricals are kept in the exploration.
drop_vars <- c(highcount_vars)
if (length(drop_vars) > 0) {
explore_data_tbl <- explore_data_tbl %>%
select(-one_of(drop_vars))
cat(str_c(drop_vars, collapse = ", "))
}
```
```{r, echo=FALSE}
#knitr::knit_exit()
```
# Univariate Data Exploration
Now that we have loaded the data we can prepare it for some basic data
exploration. We first exclude the variables that are unique
identifiers or similar, and tehen split the remaining variables out
into various categories to help with the systematic data exploration.
```{r separate_exploration_cols, echo=TRUE}
coltype_lst <- create_coltype_list(explore_data_tbl)
print(coltype_lst)
```
## Logical Variables
Logical variables only take two values: TRUE or FALSE. It is useful to see
missing data as well though, so we also plot the count of those.
```{r create_univariate_logical_plots, echo=TRUE, warning=FALSE}
logical_vars <- coltype_lst$split$logical %>% sort()
for (plot_varname in logical_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
na_count <- explore_data_tbl %>% pull(!! plot_varname) %>% are_na() %>% sum()
explore_plot <- ggplot(explore_data_tbl) +
geom_bar(aes(x = !! sym(plot_varname))) +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c("Barplot of Counts for Variable: ", plot_varname,
" (", na_count, " missing values)")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
plot(explore_plot)
}
```
## Numeric Variables
Numeric variables are usually continuous in nature, though we also have
integer data.
```{r create_univariate_numeric_plots, echo=TRUE, warning=FALSE}
numeric_vars <- coltype_lst$split$continuous %>% sort()
for (plot_varname in numeric_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_var <- explore_data_tbl %>% pull(!! plot_varname)
na_count <- plot_var %>% are_na() %>% sum()
plot_var %>% summary %>% print
explore_plot <- ggplot(explore_data_tbl) +
geom_histogram(aes(x = !! sym(plot_varname)),
bins = hist_bins_count) +
geom_vline(xintercept = mean(plot_var, na.rm = TRUE),
colour = "red", size = 1.5) +
geom_vline(xintercept = median(plot_var, na.rm = TRUE),
colour = "green", size = 1.5) +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c("Histogram Plot for Variable: ", plot_varname,
" (", na_count, " missing values)"),
subtitle = "(red line is mean, green line is median)")
explore_std_plot <- explore_plot + scale_x_continuous(labels = label_comma())
explore_log_plot <- explore_plot + scale_x_log10 (labels = label_comma())
plot_grid(explore_std_plot,
explore_log_plot, nrow = 2) %>% print()
}
```
## Categorical Variables
Categorical variables only have values from a limited, and usually fixed,
number of possible values
```{r create_univariate_categorical_plots, echo=TRUE, warning=FALSE}
categorical_vars <- coltype_lst$split$discrete %>% sort()
for (plot_varname in categorical_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
na_count <- explore_data_tbl %>% pull(!! plot_varname) %>% are_na() %>% sum()
plot_tbl <- explore_data_tbl %>%
pull(!! plot_varname) %>%
fct_lump(n = cat_level_count) %>%
fct_count() %>%
mutate(f = fct_relabel(f, str_trunc, width = 15))
explore_plot <- ggplot(plot_tbl) +
geom_bar(aes(x = fct_reorder(f, -n), weight = n)) +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c("Barplot of Counts for Variable: ", plot_varname,
" (", na_count, " missing values)")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
plot(explore_plot)
}
```
## Date/Time Variables
Date/Time variables represent calendar or time-based data should as time of the
day, a date, or a timestamp.
```{r create_univariate_datetime_plots, echo=TRUE, warning=FALSE}
datetime_vars <- coltype_lst$split$datetime %>% sort()
for (plot_varname in datetime_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_var <- explore_data_tbl %>% pull(!! plot_varname)
na_count <- plot_var %>% are_na() %>% sum()
plot_var %>% summary() %>% print()
explore_plot <- ggplot(explore_data_tbl) +
geom_histogram(aes(x = !! sym(plot_varname)),
bins = hist_bins_count) +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c("Barplot of Dates/Times in Variable: ", plot_varname,
" (", na_count, " missing values)"))
plot(explore_plot)
}
```
```{r, echo=FALSE}
#knitr::knit_exit()
```
# Bivariate Data Exploration
We now move on to looking at bivariate plots of the data set.
## Facet Plots on Variables
A natural way to explore relationships in data is to create univariate
visualisations facetted by a categorical value.
```{r bivariate_facet_data, echo=TRUE}
facet_varname <- "region"
facet_count_max <- 3
```
### Logical Variables
For logical variables we facet on barplots of the levels, comparing TRUE,
FALSE and missing data.
```{r create_bivariate_logical_plots, echo=TRUE}
logical_vars <- logical_vars[!logical_vars %in% facet_varname] %>% sort()
for (plot_varname in logical_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_tbl <- data_tbl %>% filter(!are_na(!! plot_varname))
explore_plot <- ggplot(plot_tbl) +
geom_bar(aes(x = !! sym(plot_varname))) +
facet_wrap(facet_varname, scales = "free") +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c(facet_varname, "-Faceted Barplots for Variable: ",
plot_varname)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
plot(explore_plot)
}
```
### Numeric Variables
For numeric variables, we facet on histograms of the data.
```{r create_bivariate_numeric_plots, echo=TRUE}
for (plot_varname in numeric_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_tbl <- data_tbl %>% filter(!are_na(!! plot_varname))
explore_plot <- ggplot(plot_tbl) +
geom_histogram(aes(x = !! sym(plot_varname)),
bins = hist_bins_count) +
facet_wrap(facet_varname, scales = "free") +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c(facet_varname, "-Faceted Histogram for Variable: ",
plot_varname)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
print(explore_plot + scale_x_continuous(labels = label_comma()))
print(explore_plot + scale_x_log10 (labels = label_comma()))
}
```
### Categorical Variables
We treat categorical variables like logical variables, faceting the barplots
of the different levels of the data.
```{r create_bivariate_categorical_plots, echo=TRUE}
categorical_vars <- categorical_vars[!categorical_vars %in% facet_varname] %>% sort()
for (plot_varname in categorical_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_tbl <- data_tbl %>%
filter(!are_na(!! plot_varname)) %>%
mutate(
varname_trunc = fct_relabel(!! sym(plot_varname), str_trunc, width = 10)
)
explore_plot <- ggplot(plot_tbl) +
geom_bar(aes(x = varname_trunc)) +
facet_wrap(facet_varname, scales = "free") +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c(facet_varname, "-Faceted Histogram for Variable: ",
plot_varname)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
plot(explore_plot)
}
```
### Date/Time Variables
Like the univariate plots, we facet on histograms of the years in the dates.
```{r create_bivariate_datetime_plots, echo=TRUE}
for (plot_varname in datetime_vars) {
cat("--\n")
cat(str_c(plot_varname, "\n"))
plot_tbl <- data_tbl %>% filter(!are_na(!! plot_varname))
explore_plot <- ggplot(plot_tbl) +
geom_histogram(aes(x = !! sym(plot_varname)),
bins = hist_bins_count) +
facet_wrap(facet_varname, scales = "free") +
xlab(plot_varname) +
ylab("Count") +
scale_y_continuous(labels = label_comma()) +
ggtitle(str_c(facet_varname, "-Faceted Histogram for Variable: ",
plot_varname))
plot(explore_plot)
}
```
```{r free_memory_facetplot, echo=FALSE}
rm(plot_var, plot_tbl)
```
```{r, echo=FALSE}
#knitr::knit_exit()
```
# Custom Explorations
In this section you can add your own multivariate visualations such as
boxplots and so on.
# R Environment
```{r show_session_info, echo=TRUE, message=TRUE}
sessioninfo::session_info()
```