-
Notifications
You must be signed in to change notification settings - Fork 3
/
construct_mtpl_datasets.Rmd
318 lines (222 loc) · 7.09 KB
/
construct_mtpl_datasets.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
---
title: "Construct MTPL Datasets"
author: "Mick Cooney <mickcooney@gmail.com>"
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)
library(curl)
library(CASdatasets)
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)
```
# Load MTPL Data
We want to load the MTPL dataset from the `CASdatasets` package - this data
contains both the policy and claim data.
```{r load_data, echo=TRUE}
data(freMTPLfreq)
data(freMTPLsev)
data(freMTPL2freq)
data(freMTPL2sev)
```
We now take both datasets and try to construct a single dataset in each case.
To do this, we need to check the structure of all these.
```{r check_first_dataset, echo=TRUE}
freMTPLfreq %>% glimpse()
freMTPLsev %>% glimpse()
```
---
We also want to check the structure of the second dataset.
```{r check_second_dataset, echo=TRUE}
freMTPL2freq %>% glimpse()
freMTPL2sev %>% glimpse()
```
# Reconstruct Data
Both sets of data has ID columns of mis-matched types - `PolicyID` and `IDpol`
- so we convert all of them to characters for the purposes of joining them
```{r reconstruct_first_data_cols, echo=TRUE}
freq1_tbl <- freMTPLfreq %>%
as_tibble() %>%
mutate(PolicyID = PolicyID %>% as.character())
freq1_tbl %>% glimpse()
sev1_tbl <- freMTPLsev %>%
as_tibble() %>%
transmute(
PolicyID = PolicyID %>% as.character(),
claim_amount = ClaimAmount
)
sev1_tbl %>% glimpse()
```
We now want to fix the second dataset in a similar fashion.
```{r reconstruct_second_data_cols, echo=TRUE}
freq2_tbl <- freMTPL2freq %>%
as_tibble() %>%
mutate(IDpol = IDpol %>% as.character())
freq2_tbl %>% glimpse()
sev2_tbl <- freMTPL2sev %>%
as_tibble() %>%
transmute(
IDpol = IDpol %>% as.character(),
claim_amount = ClaimAmount
)
sev2_tbl %>% glimpse()
```
# Check Matching IDs
We want to ensure that all data in the both sets have corresponding values in
the other dataset - in particular, we want to ensure that all claim amounts
match the frequency amounts.
```{r match_first_claim_amounts, echo=TRUE}
sev1_tbl %>%
anti_join(freq1_tbl, by = "PolicyID") %>%
glimpse()
```
The first dataset has no mismatched claims as this table has no rows.
We now move on to the second dataset:
```{r match_second_claim_amounts, echo=TRUE}
sev2_tbl %>%
anti_join(freq2_tbl, by = "IDpol") %>%
glimpse()
sev2_tbl %>%
anti_join(freq2_tbl, by = "IDpol") %>%
count(IDpol, name = "claim_count") %>%
glimpse()
```
We see we have almost 200 claims that do not have a matching policy, but those
claims are associated with only six IDs. This poses a conundrum for our
modelling that we will need to address later.
For now though, we just add these `IDpol` values to our frequency table for
now.
```{r add_missing_policies, echo=TRUE}
missing_tbl <- sev2_tbl %>%
anti_join(freq2_tbl, by = "IDpol") %>%
select(IDpol) %>%
distinct()
freq2_tbl <- list(
freq2_tbl %>% select(-ClaimNb),
missing_tbl
) %>%
bind_rows()
freq2_tbl %>% glimpse()
```
# Construct Datasets
We now construct our datasets to combine both policy and claim data so we
can analyse it.
## MTPL1 Data
We first work on MTPL1 - organise some feature engineering and set up the data
ready for modelling.
```{r construct_first_dataset, echo=TRUE}
total_tbl <- sev1_tbl %>%
count(PolicyID, wt = claim_amount, name = "claim_total")
modelling1_data_tbl <- freq1_tbl %>%
nest_join(sev1_tbl, by = "PolicyID", name = "sev_data") %>%
left_join(total_tbl, by = "PolicyID") %>%
select(-ClaimNb) %>%
set_names(names(.) %>% to_snake_case()) %>%
mutate(
claim_count = map_int(sev_data, nrow)
) %>%
replace_na(list(claim_total = 0))
modelling1_data_tbl %>% glimpse()
```
### Derived Variables
We also want to construct a number of new variables derived from existing
values in the table.
```{r mtpl1_construct_derived_variables, echo=TRUE}
modelling1_data_tbl <- modelling1_data_tbl %>%
mutate(
cat_driver_age = cut(driver_age,
breaks = c(17, 22, 26, 42, 74, Inf),
labels = c("17-22", "23-26", "27-42", "43-74", "75+")),
cat_car_age = cut(car_age,
breaks = c(0, 1, 4, 15, Inf),
labels = c("0-1", "2-4", "5-15", "16+"),
include.lowest = TRUE),
cat_density = cut(density,
breaks = c(0, 40, 200, 500, 4500, Inf),
labels = c("0-40", "41-200", "201-500", "501-4500", "4500+"),
include.lowest = TRUE)
) %>%
relocate(sev_data, .after = "cat_density")
modelling1_data_tbl %>% glimpse()
```
## MTPL2 Data
Having constructed the first dataset, we now perform a similar set of
operations to construct the second set of data.
```{r construct_second_dataset, echo=TRUE}
total_tbl <- sev2_tbl %>%
count(IDpol, wt = claim_amount, name = "claim_total")
modelling2_data_tbl <- freq2_tbl %>%
nest_join(sev2_tbl, by = "IDpol", name = "sev_data") %>%
left_join(total_tbl, by = "IDpol") %>%
set_names(names(.) %>% to_snake_case()) %>%
rename(pol_id = i_dpol) %>%
mutate(
pol_id = pol_id %>% as.character(),
claim_count = map_int(sev_data, nrow),
veh_power = veh_power %>% as.character()
)
modelling2_data_tbl %>% glimpse()
```
### Derived Variables
As for `MTPL1`, we also construct a number of variables for use in the
analysis.
# Retrieve Geospatial Data
GADM data licensing does not allow the redistribution of data so the shapefiles
are not included in this repo and need to be downloaded from the website.
```{r download_france_geospatial_data, echo=TRUE}
geospatial_data_file <- "geospatial_data/FRA_adm_shp.zip"
if(!file_exists(geospatial_data_file)) {
curl_download("https://biogeo.ucdavis.edu/data/gadm2.8/shp/FRA_adm_shp.zip",
geospatial_data_file)
}
unzip(geospatial_data_file, exdir = "geospatial_data")
```
# Write to Disk
We now save both datasets to disk.
```{r write_data_to_disk, echo=TRUE}
modelling1_data_tbl %>% write_rds("data/modelling1_data_tbl.rds")
modelling2_data_tbl %>% write_rds("data/modelling2_data_tbl.rds")
```
# R Environment
```{r show_session_info, echo=TRUE, message=TRUE}
sessioninfo::session_info()
```