-
Notifications
You must be signed in to change notification settings - Fork 1
/
04-small-state-analysis.Rmd
583 lines (417 loc) · 26 KB
/
04-small-state-analysis.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
---
title: "Small State Analysis"
output: html_document
date: "2023-12-28"
---
Small State Model: GLM vs Lasso Credibility
This markdown is intended to be run after the countrywide, large, and medium state analysis so that the data needed are still in our environment.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Read in packages as needed
```{r packages, echo=FALSE, include=FALSE}
# Function to install and load packages
install_and_load <- function(package) {
if (!require(package, character.only = TRUE)) {
install.packages(package)
library(package, character.only = TRUE)
}
}
# List of packages
packages <- c("data.table", "statmod", "ggplot2", "tweedie",
"glmnet", "HDtweedie", "plotly", "arrow", "knitr")
# Install and load all packages
sapply(packages, install_and_load)
```
Read in helper functions
```{r echo=FALSE, include=FALSE}
source( "helper_functions.R")
```
Read in the data as needed
```{r pressure, echo=FALSE, include=FALSE}
modeling_data <- read_parquet("output_file.parquet")
```
Prepare to model the small state data
```{r, echo=FALSE, include=FALSE}
# Subset the modeling_data table
subset_data <- modeling_data[subset == "base_data", ]
# Convert the data.table to a data.frame
subset_df <- as.data.frame(subset_data)
# Write the subset_df as a Parquet file
# filter to training rows only
training_data <- modeling_data[stratification %in% c(1, 2, 3, 4), ]
# select modeling variables for Lasso
lasso_modeling_variables <- c(
"driver_age_18_38_hinge", "driver_age_38_76_hinge", "driver_age_76_99_hinge",
"vehicle_age_0_10_hinge", "vehicle_age_10_99_hinge",
"multi_yes", "xTreme_yes",
"weight_heavy", "weight_light", "weight_extra_light",
"ind_construction", "ind_farming", "ind_finance_and_insurance",
"ind_fine_arts", "ind_fireworks", "ind_food_services",
"ind_health_care", "ind_real_estate", "ind_retail",
"ind_large_state", "ind_medium_state",
"ind_small_state", "ind_small_state_cw_dist"
)
# HDTweedie requires a matrix format for predictor variables. First, we will subset to all of the columns that we need.
train_cols_for_matrix <- modeling_data[stratification %in% c(1, 2, 3, 4), lasso_modeling_variables, with = FALSE]
validate_cols_for_matrix <- modeling_data[stratification %in% c(5), lasso_modeling_variables, with = FALSE]
# Reformatting to a matrix for HDtweedie so that Lasso Tweedie can work.
training_data_matrix <- as.matrix(train_cols_for_matrix, rownames = FALSE)
validate_data_matrix <- as.matrix(validate_cols_for_matrix, rownames = FALSE)
# This is the data format needed for creating our loss vector and CV vector below
training_data <- modeling_data[stratification %in% c(1, 2, 3, 4), ]
validate_data <- modeling_data[stratification %in% c(5), ]
# Losses and CV need to be a vector
loss_vector <- as.vector(training_data$incurred_loss)
cv_vector <- as.vector(training_data$stratification)
# As exposures are all 1, we will create this vector.
weights <- rep(1, length(drop(loss_vector)))
```
Fit lasso credibility and GLM models
```{r, echo=FALSE, include=FALSE}
# We no longer need the state control variable
lasso_modeling_variables <- lasso_modeling_variables[!(lasso_modeling_variables %in% c("ind_small_state", "ind_base_data", "ind_medium_state", "ind_large_state", "ind_small_state_cw_dist"))]
#### Small State Model ####
small_state_training_data <- training_data[subset == "small_state"]
small_state_validate_data <- validate_data[subset == "small_state"]
cw_model_complement_of_credibility_vector <- small_state_training_data$cw_model_complement_of_credibility
# This is the data format needed for HDtweedie.
small_state_train_cols_for_matrix <- small_state_training_data[, lasso_modeling_variables, with = FALSE]
small_state_validate_cols_for_matrix <- small_state_validate_data[, lasso_modeling_variables, with = FALSE]
# Reformatting to a matrix for HDtweedie so that Lasso Tweedie can work.
small_state_training_data_matrix <- as.matrix(small_state_train_cols_for_matrix, rownames = FALSE)
small_state_validate_data_matrix <- as.matrix(small_state_validate_cols_for_matrix, rownames = FALSE)
# Losses and CV need to be a vector
small_state_training_loss_vector <- as.vector(small_state_training_data$incurred_loss)
small_state_training_cv_vector <- as.vector(small_state_training_data$stratification)
# fit the lasso model with an offset
recompute <- TRUE
if (recompute) {
small_state_offset_lasso_model <- cv.HDtweedie(
x = small_state_training_data_matrix,
y = (small_state_training_loss_vector * (cw_model_complement_of_credibility_vector^(1 - 1.6)) / (cw_model_complement_of_credibility_vector^(2 - 1.6))),
group = NULL, p = 1.6,
weights = small_state_training_data$exposure * (cw_model_complement_of_credibility_vector^(2 - 1.6)), lambda = NULL,
pred.loss = "deviance",
nfolds = 4, foldid = small_state_training_cv_vector,
standardize = TRUE
)
saveRDS(small_state_offset_lasso_model, file = "small_state_offset_lasso_model.rds")
} else {
medium_state_offset_lasso_model <- readRDS("small_state_offset_lasso_model.rds")
}
# fit the GLM model (without an offset)
small_state_glm_model <- glm(
formula = incurred_loss ~
driver_age_18_38_hinge + driver_age_38_76_hinge + driver_age_76_99_hinge +
vehicle_age_0_10_hinge + vehicle_age_10_99_hinge +
C(MultiPolicy) + C(xTreme_TurnSignal) + C(car_weight) + C(industry_code),
family = tweedie(1.6, link.power = 0),
weights = exposure,
data = small_state_training_data
)
```
Show statistics of the glm. Note that many of these variables are insignificant. It would take significant work to make a reasonable model from this start.
```{r}
summary(small_state_glm_model)
```
Show the CV plot for HDTweedie
```{r plot}
plot_lasso_model_cv_error(small_state_offset_lasso_model)
```
Now, we will compare the GLM vs. the Lasso with a complement of credibility
```{r small-state-comparison}
# add the predictions to our training data
small_state_training_data[, glm_prediction := predict.glm(small_state_glm_model, small_state_training_data, type = "response")]
small_state_training_data[, credibility_weighted_prediction := predict(small_state_offset_lasso_model, small_state_training_data_matrix,
s = "lambda.min",
weights = exposure
) * cw_model_complement_of_credibility]
small_state_validate_data[, glm_prediction := predict.glm(small_state_glm_model, small_state_validate_data, type = "response")]
small_state_validate_data[, credibility_weighted_prediction := predict(small_state_offset_lasso_model, small_state_validate_data_matrix,
s = "lambda.min",
weights = exposure
) * cw_model_complement_of_credibility]
small_state_credibility_coeff_table <- create_credibility_coeff_table(
glm_model = small_state_glm_model,
penalized_model = small_state_offset_lasso_model,
complement_model = lasso_model
)
# Small Subset 1 GLM coefficients
capture.output(summary(small_state_glm_model))[c(13:36)]
# Small Subset 1 Credibility Coeff Table
summary(small_state_glm_model)
print(small_state_credibility_coeff_table)
write.csv(small_state_credibility_coeff_table, file = "../Graphs/SmallState/CSV/smallstate_coeff_table.csv", row.names = FALSE)
# Small Subset 1 MultiPolicy Discount
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "MultiPolicy"))
# Small Subset 1 xTreme Turn Signal Discount
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "xTreme_TurnSignal"))
# Small Subset 1 Industry Code
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "industry_code"))
# Additional Prediction and Relativity Plots
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "industry_code"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "MultiPolicy"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "MultiPolicy"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "car_weight"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_training_data, small_state_credibility_coeff_table, "car_weight"))
small_state_driver_age_table <- get_credibility_driver_var_table(small_state_training_data, small_state_credibility_coeff_table, variable_name = "driver_age")
small_state_vehicle_age_table <- get_credibility_vehicle_var_table(small_state_training_data, small_state_credibility_coeff_table, variable_name = "vehicle_age")
credibility_relativity_plot(small_state_driver_age_table)
credibility_prediction_plot(small_state_driver_age_table)
credibility_relativity_plot(small_state_vehicle_age_table)
credibility_prediction_plot(small_state_vehicle_age_table)
# add the predictions to our holdout data and create charts
small_state_validate_data[, glm_prediction := predict.glm(small_state_glm_model, small_state_validate_data, type = "response")]
small_state_validate_data[, credibility_weighted_prediction := predict(small_state_offset_lasso_model, small_state_validate_data_matrix, s = "lambda.min") *
small_state_validate_data$cw_model_complement_of_credibility]
print(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "MultiPolicy"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "car_weight"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "industry_code"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "MultiPolicy"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_prediction_plot(get_credibility_cat_var_table(small_state_validate_data, small_state_credibility_coeff_table, "car_weight"))
small_state_driver_age_table <- get_credibility_driver_var_table(small_state_validate_data, small_state_credibility_coeff_table, variable_name = "driver_age")
small_state_vehicle_age_table <- get_credibility_vehicle_var_table(small_state_validate_data, small_state_credibility_coeff_table, variable_name = "vehicle_age")
credibility_prediction_plot(small_state_driver_age_table)
credibility_prediction_plot(small_state_vehicle_age_table)
```
In the lift charts below, we can see that our lasso credibility model outperforms both the GLM and the countrywide model. However, this is not the most optimal model, as some of the univariate charts suggest that we should try a higher credibility standard.
```{r small-state-double-lift}
## Small Data Lift Chart Lasso vs. GLM
# Figure 7.28
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
## Small Data Lift Chart Lasso vs. CW
# Figure 7.29
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "cw_model_complement_of_credibility",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "cw_model_complement_of_credibility",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
double_lift_chart(
dataset = small_state_validate_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_state_validate_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
### Small State Increased Penalty Term ###
small_state_offset_lasso_model$lambda
small_state_offset_lasso_model$lambda.min
increased_penalty_coeff <- create_credibility_coeff_table_alternate_lambda(
glm_model = small_state_glm_model,
penalized_model = small_state_offset_lasso_model,
complement_model = lasso_model,
alternate_lambda = 1.374307155
)
print(increased_penalty_coeff)
small_state_training_data[, increased_penalty_prediction := predict(small_state_offset_lasso_model, small_state_training_data_matrix,
s = 0.556990859,
weights = exposure
) * cw_model_complement_of_credibility]
```
By applying actuarial judgment to increase our credibility standard, we arrive at a more actuarially sound model.
```{r small-state-cw-increased-penalty}
# Increased Penalty Driver Age
credibility_relativity_plot(get_credibility_driver_var_table(small_state_training_data, increased_penalty_coeff, variable_name = "driver_age"))
# Increased Penalty Vehicle Age
credibility_relativity_plot(get_credibility_vehicle_var_table(small_state_training_data, increased_penalty_coeff, variable_name = "vehicle_age"))
# Increased Penalty Industry Code
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, increased_penalty_coeff, "industry_code"))
# Other plots for increased penalty model
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, increased_penalty_coeff, "MultiPolicy"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, increased_penalty_coeff, "xTreme_TurnSignal"))
credibility_relativity_plot(get_credibility_cat_var_table(small_state_training_data, increased_penalty_coeff, "car_weight"))
## Small State Increased Penalty vs. CW
# Figure 7.30
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "increased_penalty_prediction",
glm = "cw_model_complement_of_credibility",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_state_training_data,
lasso_credibility = "increased_penalty_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
selected_columns <- small_state_training_data[, c("increased_penalty_prediction", "cw_model_complement_of_credibility",
"glm_prediction", "credibility_weighted_prediction", "true_risk")]
write.csv(selected_columns, file = "../Graphs/SmallState/CSV/small_state_training_data_predictions.csv", row.names = FALSE)
```
We will fit our last models on the second small state subset which contains the same relativities as the base modeling data.
```{r small-state-cw}
#### Small State with Base Modeling Data Relativities ###
small_cw_dist_state_training_data <- training_data[subset == "small_cw_dist_state"]
small_cw_dist_state_validate_data <- validate_data[subset == "small_cw_dist_state"]
cw_model_complement_of_credibility_vector <- small_cw_dist_state_training_data$cw_model_complement_of_credibility
# This is the data format needed for HDtweedie.
small_cw_dist_state_train_cols_for_matrix <- small_cw_dist_state_training_data[, lasso_modeling_variables, with = FALSE]
small_cw_dist_state_validate_cols_for_matrix <- small_cw_dist_state_validate_data[, lasso_modeling_variables, with = FALSE]
# Reformatting to a matrix for HDtweedie so that Lasso Tweedie can work.
small_cw_dist_state_training_data_matrix <- as.matrix(small_cw_dist_state_train_cols_for_matrix, rownames = FALSE)
small_cw_dist_state_validate_data_matrix <- as.matrix(small_cw_dist_state_validate_cols_for_matrix, rownames = FALSE)
# Losses and CV need to be a vector
small_cw_dist_state_training_loss_vector <- as.vector(small_cw_dist_state_training_data$incurred_loss)
small_cw_dist_state_training_cv_vector <- as.vector(small_cw_dist_state_training_data$stratification)
# fit the lasso model with an offset
recompute <- FALSE
if (recompute) {
small_cw_dist_state_offset_lasso_model <- cv.HDtweedie(
x = small_cw_dist_state_training_data_matrix,
y = (small_cw_dist_state_training_loss_vector * (cw_model_complement_of_credibility_vector^(1 - 1.6)) / (cw_model_complement_of_credibility_vector^(2 - 1.6))),
group = NULL, p = 1.6,
weights = small_cw_dist_state_training_data$exposure * (cw_model_complement_of_credibility_vector^(2 - 1.6)), lambda = NULL,
pred.loss = "deviance",
nfolds = 4, foldid = small_cw_dist_state_training_cv_vector,
standardize = TRUE
)
saveRDS(small_cw_dist_state_offset_lasso_model, file = "small_cw_dist_state_offset_lasso_model.rds")
} else {
small_cw_dist_state_offset_lasso_model <- readRDS("lsmall_cw_dist_state_offset_lasso_model.rds")
}
# Fit the GLM model without an offset
small_cw_dist_state_glm_model <- glm(
formula = incurred_loss ~
driver_age_18_38_hinge + driver_age_38_76_hinge + driver_age_76_99_hinge +
vehicle_age_0_10_hinge + vehicle_age_10_99_hinge +
C(MultiPolicy) + C(xTreme_TurnSignal) + C(car_weight) + C(industry_code),
family = tweedie(1.6, link.power = 0),
weights = exposure,
data = small_cw_dist_state_training_data
)
```
The charts below show that the optimal lasso credibility model has collapsed completely to the complement. All of the code to create additional graphs is provided for completeness and reusability.
```{r comparison-sm}
# Now, we will compare the GLM vs. the Lasso with a complement of credibility
# add the predictions to our training data
small_cw_dist_state_training_data[, glm_prediction := predict.glm(small_cw_dist_state_glm_model, small_cw_dist_state_training_data, type = "response")]
small_cw_dist_state_training_data[, credibility_weighted_prediction := predict(small_cw_dist_state_offset_lasso_model, small_cw_dist_state_training_data_matrix,
s = "lambda.min",
weights = exposure
) * cw_model_complement_of_credibility]
small_cw_dist_state_validate_data[, glm_prediction := predict.glm(small_cw_dist_state_glm_model, small_cw_dist_state_validate_data, type = "response")]
small_cw_dist_state_validate_data[, credibility_weighted_prediction := predict(small_cw_dist_state_offset_lasso_model, small_cw_dist_state_validate_data_matrix,
s = "lambda.min",
weights = exposure
) * cw_model_complement_of_credibility]
small_cw_dist_state_credibility_coeff_table <- create_credibility_coeff_table(
glm_model = small_cw_dist_state_glm_model,
penalized_model = small_cw_dist_state_offset_lasso_model,
complement_model = lasso_model
)
# Small Subset 2 GLM coefficients
capture.output(summary(small_cw_dist_state_glm_model))[c(13:36)]
# Small Subset 2 Credibility Coeff Table
print(small_cw_dist_state_credibility_coeff_table)
write.csv(small_cw_dist_state_credibility_coeff_table, file = "../Graphs/SmallState/CSV/smallstate_cw_dist_coeff_table.csv", row.names = FALSE)
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "MultiPolicy"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "car_weight"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "industry_code"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "MultiPolicy"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "car_weight"))
small_cw_dist_state_driver_age_table <- get_credibility_driver_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, variable_name = "driver_age")
small_cw_dist_state_vehicle_age_table <- get_credibility_vehicle_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, variable_name = "vehicle_age")
credibility_relativity_plot(small_cw_dist_state_driver_age_table)
credibility_relativity_plot(small_cw_dist_state_vehicle_age_table)
credibility_prediction_plot(small_cw_dist_state_driver_age_table)
credibility_prediction_plot(small_cw_dist_state_vehicle_age_table)
write.csv(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "industry_code"), file = "../Graphs/SmallState/CSV/industry_code_cw_dist_table.csv", row.names = FALSE)
write.csv(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "MultiPolicy"), file = "../Graphs/SmallState/CSV/multipolicy_cw_dist_table.csv", row.names = FALSE)
write.csv(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "xTreme_TurnSignal"), file = "../Graphs/SmallState/CSV/xtreme_turnsignal_cw_dist_table.csv", row.names = FALSE)
write.csv(get_credibility_cat_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, "car_weight"), file = "../Graphs/SmallState/CSV/car_weight_cw_dist_table.csv", row.names = FALSE)
write.csv(get_credibility_driver_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, variable_name = "driver_age"), file = "../Graphs/SmallState/CSV/driver_age_cw_dist_table.csv", row.names = FALSE)
write.csv(get_credibility_vehicle_var_table(small_cw_dist_state_training_data, small_cw_dist_state_credibility_coeff_table, variable_name = "vehicle_age"), file = "../Graphs/SmallState/CSV/vehicle_age_cw_dist_table.csv", row.names = FALSE)
# add the predictions to our holdout data
small_cw_dist_state_validate_data[, glm_prediction := predict.glm(small_cw_dist_state_glm_model, small_cw_dist_state_validate_data, type = "response")]
small_cw_dist_state_validate_data[, credibility_weighted_prediction := predict(small_cw_dist_state_offset_lasso_model, small_cw_dist_state_validate_data_matrix, s = "lambda.min") *
small_cw_dist_state_validate_data$cw_model_complement_of_credibility]
small_cw_dist_state_credibility_coeff_table <- create_credibility_coeff_table(
glm_model = small_cw_dist_state_glm_model,
penalized_model = small_cw_dist_state_offset_lasso_model,
complement_model = lasso_model
)
print(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "industry_code"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "MultiPolicy"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_relativity_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "car_weight"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "industry_code"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "MultiPolicy"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "xTreme_TurnSignal"))
credibility_prediction_plot(get_credibility_cat_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, "car_weight"))
small_cw_dist_state_driver_age_table <- get_credibility_driver_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, variable_name = "driver_age")
small_cw_dist_state_vehicle_age_table <- get_credibility_vehicle_var_table(small_cw_dist_state_validate_data, small_cw_dist_state_credibility_coeff_table, variable_name = "vehicle_age")
credibility_prediction_plot(small_cw_dist_state_driver_age_table)
credibility_prediction_plot(small_cw_dist_state_vehicle_age_table)
# Lift Charts
double_lift_chart(
dataset = small_cw_dist_state_training_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_cw_dist_state_training_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
double_lift_chart(
dataset = small_cw_dist_state_validate_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = small_cw_dist_state_validate_data,
lasso_credibility = "credibility_weighted_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
```