-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexploring_mtpl1_dataset.Rmd
1125 lines (795 loc) · 30.6 KB
/
exploring_mtpl1_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
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
---
title: "Exploring the MTPL1 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(glue)
library(purrr)
library(vctrs)
library(fs)
library(forcats)
library(snakecase)
library(stringi)
library(lubridate)
library(rsample)
library(sf)
library(evir)
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}
modeldata_tbl <- read_rds("data/modelling1_data_tbl.rds")
rawdata_tbl <- modeldata_tbl %>% 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))
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()
```
# Frequency Exploration
In this section you can add your own multivariate visualations such as
boxplots and so on.
## Explore Claim Rates
The first parameter we wish to focus on is the *claim rate* - that is, the
expected count of claims per year:
\[
\text{Claim Rate} = \frac{\text{Claim Count}}{\text{Time of Exposure}}
\]
We can calculate this on an aggregated basis having split the data down
various dimensions. To reduce the amount of copy/paste of code, we construct
a quick function to produce this.
```{r construct_claimrate_calculator, echo=TRUE}
calculate_claim_rate <- function(data_tbl, ...) {
group_vars <- enquos(...)
claimrate_tbl <- data_tbl %>%
group_by(!!! group_vars) %>%
summarise(
.groups = "drop",
total_claimcount = sum(claim_count),
total_exposure = sum(exposure),
claim_rate = total_claimcount / total_exposure
)
return(claimrate_tbl)
}
```
### Calculate Basic Claim Rates
First we look at the overall claim rate for the whole book of business:
```{r overall_claim_rate, echo=TRUE}
data_tbl %>% calculate_claim_rate()
```
We now want to start looking at the claim rate by different dimensions.
```{r create_claim_rate_plots, echo=TRUE}
construct_claimrate_data_plots <- function(label, varname, data_tbl) {
segdata_tbl <- data_tbl %>% calculate_claim_rate(!! sym(varname))
segdata_plot <- ggplot(segdata_tbl) +
geom_col(aes(x = !! sym(varname), y = claim_rate, fill = total_exposure)) +
labs(x = label, y = "Claim Rate", fill = "Total Exposure") +
expand_limits(y = 0) +
scale_fill_continuous(labels = label_comma()) +
ggtitle(glue("Claim Rate by {label}"))
if(varname %in% c("region", "brand")) {
segdata_plot <- segdata_plot +
theme(axis.text.x = element_text(angle = 90))
}
return_lst <- list(
data_tbl = segdata_tbl,
data_plot = segdata_plot
)
return(return_lst)
}
vardata_tbl <- tribble(
~label, ~varname,
"Power", "power",
"Car Age", "car_age",
"Driver Age", "driver_age",
"Brand", "brand",
"Fuel Type", "gas",
"Region", "region",
"Discrete Car Age", "cat_car_age",
"Discrete Driver Age", "cat_driver_age",
"Discrete Density", "cat_car_age"
)
claimrate_vardata_tbl <- vardata_tbl %>%
mutate(data = map2(label, varname,
construct_claimrate_data_plots,
data_tbl = data_tbl))
```
Having generated different summary tables and plots, we now want to inspect
them. A few of them need some tweaking for appeal though.
```{r plot_segmented_claim_rate, echo=TRUE}
for(i in 1:nrow(claimrate_vardata_tbl)) {
claimrate_vardata_tbl %>% pull(data) %>% .[[i]] %>% .$data_plot %>% print()
}
```
Our analysis above calculates a single claim rate for each of the data splits,
giving us just a single estimate for the claim rate.
This point estimate is useful, but it is better to get an idea of the possible
range of values consistent with the data observed.
We take two approaches to this problem: we use the bootstrap to get a
distribution of bootstrap estimates for each claim rate, and we use Bayesian
analysis to combine prior knowledge of insurance claim rates with the data
to produce a posterior distribution of the claim rate.
### Calculate Bootstrap Claim Rate Estimates
As we will re-use the bootstrap a number of times in this project, I will
first construct a bootstrap dataset that we can re-use whenever we need it.
```{r construct_bootstrapped_dataset, echo=TRUE}
bootstrap_index_tbl <- data_tbl %>%
bootstraps(times = 200)
bootstrap_index_tbl %>% glimpse()
```
To save on storage and memory, full replicas of the dataset are not stored in
this datastructure, but rather than index to each of the rows - we then use
the `analysis()` function to return the full sample.
Now that we have our bootstrap data to work from, we construct a bootstrap
distribution of claim rates when segmented by our variables.
```{r construct_bootstrap_claimrate_estimates, echo=TRUE}
construct_bootdata <- ~ claimrate_vardata_tbl %>%
mutate(data = map2(label, varname,
construct_claimrate_data_plots,
data_tbl = .x %>% analysis()))
extract_data <- ~ .x %>% mutate(data_tbl = map(data, "data_tbl"))
bootstrap_claimrate_tbl <- bootstrap_index_tbl %>%
mutate(boot_data = map(splits, construct_bootdata)) %>%
mutate(tmpdata = map(boot_data, extract_data)) %>%
select(id, tmpdata) %>%
unnest(tmpdata) %>%
select(-data) %>%
group_nest(label, varname) %>%
mutate(newdata = map(data, ~ .x %>% unnest(data_tbl))) %>%
select(label, varname, newdata)
construct_plot <- function(varname, data_tbl) {
data_plot <- ggplot(data_tbl) +
geom_boxplot(aes(x = !! sym(varname), y = claim_rate,
group = !! sym(varname))) +
expand_limits(y = 0) +
labs(x = varname, y = "Claim Rate", fill = "Mean Total Exposure") +
ggtitle(glue("Bootstrap Estimates of the Claim Rate - {varname}"))
if(varname %in% c("brand", "region")) {
data_plot <- data_plot + theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
}
return(data_plot)
}
bootstrap_claimrate_tbl %>%
mutate(data_plot = map2(varname, newdata, construct_plot)) %>%
pull(data_plot)
```
## Explore Density Patterns
It appears that the `density` variable captures the population density of the
residence for the policy holder. Accidents tend to happen in area of higher
population density as this is where the cars are, so it is worth doing a bit
of exploration here to see if there is correlation between it and other
parameters.
### Density vs Region
We first look to see how `density` is distributed across the `region` values
to get an idea of how this works.
```{r explore_region_density, echo=TRUE}
region_density_plot <- ggplot(data_tbl) +
geom_boxplot(aes(x = region, y = density)) +
xlab("Region") +
ylab("Density") +
expand_limits(y = 0) +
ggtitle("Boxplot of Density by Region") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
region_density_plot + scale_y_log10 (labels = label_comma())
region_density_plot + scale_y_continuous(labels = label_comma())
```
There appears to be a pattern here so we want to look at histograms (both
standard and log-scale) across the regions.
```{r plot_density_facet_histogram, echo=TRUE}
hist_plot <- ggplot(data_tbl) +
geom_histogram(aes(x = density), bins = 50) +
xlab("Density") +
ylab("Frequency") +
facet_wrap(vars(region), scales = "free") +
ggtitle("Facet Plot of Histograms")
hist_plot + scale_x_continuous(labels = label_comma())
hist_plot + scale_x_log10 (labels = label_comma())
```
### Density vs Power
The `power` value for the vehicle is often a risk factor to be accounted for
so we also want to see how `density` distributes across it.
```{r explore_power_density, echo=TRUE}
power_density_plot <- ggplot(data_tbl) +
geom_boxplot(aes(x = power, y = density)) +
xlab("Vehicle Power") +
ylab("Density") +
expand_limits(y = 0) +
ggtitle("Boxplot of Density by Vehicle Power") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
power_density_plot + scale_y_log10 (labels = label_comma())
power_density_plot + scale_y_continuous(labels = label_comma())
```
## Distibution of Claims vs Non-Claims Policy
We also want to investigate whether or not we observe a difference between
the distribution of variables for policies with or without a claim.
```{r distribution_of_claims_noclaims, echo=TRUE}
data_tbl <- data_tbl %>% mutate(has_claim = claim_count > 0)
construct_comparison_plot <- function(label, varname) {
data_plot <- ggplot(data_tbl) +
geom_bar(aes(x = !! sym(varname))) +
facet_wrap(vars(has_claim), scales = "free_y", nrow = 2) +
scale_y_continuous(label = label_comma()) +
labs(x = label, y = "Frequency", fill = "Claims") +
ggtitle(glue("Comparison Plot of {label} by Presence of Claim"))
if(varname %in% c("brand", "region")) {
data_plot <- data_plot + theme(axis.text.x = element_text(angle = 90))
}
return(data_plot)
}
vardata_tbl %>%
mutate(data_plot = map2(label, varname, construct_comparison_plot)) %>%
pull(data_plot)
```
Finally, we have a quick look at how `exposure` differs across the claim-free
and claim set of policies. This may not be useful from a predictive point of
view, but it can be relevant in terms of operations and risk management.
```{r comparison_plot_exposure, echo=TRUE}
ggplot(data_tbl) +
geom_histogram(aes(x = exposure), bins = 50) +
facet_wrap(vars(has_claim), scales = "free_y", nrow = 2) +
scale_y_continuous(label = label_comma()) +
ggtitle("Comparison Plot of Exposure by Presence of Claim")
```
Finally, we want to take a look a `density` across the two subsets of the data.
This may help us check the perceived wisdom of risk increasing for policy
holders living in areas with more people.
```{r comparison_plot_density, echo=TRUE}
density_plot <- ggplot(data_tbl) +
geom_histogram(aes(x = density), bins = 50) +
facet_wrap(vars(has_claim), scales = "free_y", nrow = 2) +
scale_y_continuous(label = label_comma()) +
labs(x = "Density", y = "Frequency", fill = "Claims") +
ggtitle("Comparison Plot of Density by Presence of Claim")
(density_plot + scale_x_continuous(label = label_comma())) %>% plot()
(density_plot + scale_x_log10 (label = label_comma())) %>% plot()
```
We do not see a huge difference between these two subsets, though this does
not disprove that there is a relationship.
## Geospatial Visualisations
We have some geospatial data on the various regions in France, so we need to
load the shapefiles and then add all region-related data to this to create
the visualisation plots.
```{r load_shapefile_data, echo=TRUE}
fra_adm_sf <- st_read("geospatial_data/", layer = "FRA_adm1")
fra_adm_sf %>% plot()
```
Now that we have loaded the data we redo
```{r append_region_values, echo=TRUE}
fra_adm_sf <- fra_adm_sf %>%
mutate(
region = NAME_1 %>%
stri_trans_general("LATIN-ASCII") %>%
str_replace_all(" ", "-")
)
fra_adm_sf %>% glimpse()
```
We first want to see a plot of the claim rate by region on a choropleth map.
```{r plot_claim_rate_choropleth, echo=TRUE}
plot_sf <- fra_adm_sf %>%
left_join(data_tbl %>% calculate_claim_rate(region), by = "region")
ggplot(plot_sf) +
geom_sf(aes(fill = claim_rate)) +
geom_sf_text(aes(label = region)) +