-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-countrywide-analysis.Rmd
275 lines (212 loc) · 9.89 KB
/
01-countrywide-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
---
title: "Countrywide Analysis"
output: html_document
date: "2023-12-29"
---
This markdown follows monograph section 7.3 Countrywide Model Results
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Package setup
```{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)
```
Load helper functions to create various graphs
```{r echo=FALSE, include=FALSE}
source( "helper_functions.R")
```
Load the modeling data. Set your working directlry as needed.
```{r pressure, echo=FALSE, include=FALSE}
getwd()
# setwd()
modeling_data <- read_parquet("output_file.parquet")
```
Here, we will build a lasso model on the countrywide modeling data
```{r prep-data, echo=FALSE, include = FALSE}
# filter to training rows only
training_data <- modeling_data[stratification %in% c(1, 2, 3, 4), ]
# select modeling variables for Lasso
# The HDtweedie packages uses a different format for predictors, but the included predictors are identical to our GLM
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)))
```
We will fit a GLM to our modeing data. This was completed in the earlier markdown script, but we are fitting it again as only the data was saved from the prior file. This will not take a long time.
```{r glm-fit}
# run our initial GLM model
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) + C(subset),
family = tweedie(1.6, link.power = 0),
offset = log(exposure),
data = training_data
)
```
Fit a lasso model to the CW data. This may take some time, so we will save this model to avoid needing to re-create it.
```{r}
# after the initial run, set recompute to false.
recompute <- TRUE
# recompute <- FALSE
if (recompute) {
# fit the lasso model
lasso_model <- cv.HDtweedie(
x = training_data_matrix,
y = loss_vector, group = NULL, p = 1.6, weights, lambda = NULL,
pred.loss = "deviance",
nfolds = 4, foldid = cv_vector,
standardize = TRUE
)
saveRDS(lasso_model, file = "lasso_model_on_training_data_matrix.rds")
} else {
lasso_model <- readRDS("lasso_model_on_training_data_matrix.rds")
}
```
Let's see CV plot
```{r analyse-error}
# Preparing the data for plotting
data <- data.frame(
lambda = lasso_model$lambda,
cvm = lasso_model$cvm,
cvsd = lasso_model$cvsd
)
data$cvlower = data$cvm - data$cvsd
data$cvupper = data$cvm + data$cvsd
# Plotting
ggplot(data, aes(x = lambda, y = cvm)) +
geom_line() +
geom_ribbon(aes(ymin = cvlower, ymax = cvupper), alpha = 0.2) +
labs(x = "Lambda", y = "Mean Cross-Validated Error") +
theme_minimal()
```
Add the GLM and Lasso predictions to the dataset as new columns.
```{r}
coef(lasso_model, s = c("lambda.min")) # NB if you don't tell it to pick the best lambda, it won't.
coef(lasso_model) # Please note that this does NOT give you the best model.
# get the coefficient table to compare GLM and Lasso predictions
cw_coeff_table <- create_coeff_table(glm_model, lasso_model)
# Base Model Coefficient Table
print(cw_coeff_table)
write.csv(cw_coeff_table, "../Graphs/FullData/CSV/cw_coeff_table.csv")
summary(glm_model)
# add the predictions to our training data
training_data[, glm_prediction := predict.glm(glm_model, training_data, type = "response")]
training_data[, lasso_prediction := predict(lasso_model, training_data_matrix, s = "lambda.min")]
```
Produce relativity plots and prediction plots for all risk characteristics.
```{r}
if(TRUE){
# Full Model Prediction Plots and Relativity Plots
# Figure 7.8
relativity_plot(get_cat_var_table(training_data, cw_coeff_table, "industry_code"))
# Figure 7.9
prediction_plot(get_cat_var_table(training_data, cw_coeff_table, "industry_code"))
relativity_plot(get_cat_var_table(training_data, cw_coeff_table, "MultiPolicy"))
prediction_plot(get_cat_var_table(training_data, cw_coeff_table, "MultiPolicy"))
relativity_plot(get_cat_var_table(training_data, cw_coeff_table, "xTreme_TurnSignal"))
prediction_plot(get_cat_var_table(training_data, cw_coeff_table, "xTreme_TurnSignal"))
relativity_plot(get_cat_var_table(training_data, cw_coeff_table, "car_weight"))
prediction_plot(get_cat_var_table(training_data, cw_coeff_table, "car_weight"))
# Get tables for continuous variables.
driver_age_table <- get_driver_var_table(training_data, cw_coeff_table, variable_name = "driver_age")
vehicle_age_table <- get_vehicle_var_table(training_data, cw_coeff_table, variable_name = "vehicle_age")
# Full Model Continuous Prediction Plots and Relativity Plots
relativity_plot(driver_age_table)
prediction_plot(driver_age_table)
relativity_plot(vehicle_age_table)
prediction_plot(vehicle_age_table)
# Save the tables as csv
write.csv(cw_coeff_table, file = "../Graphs/FullData/CSV/cw_coeff_table.csv", row.names = FALSE)
write.csv(get_cat_var_table(training_data, cw_coeff_table, "industry_code"), file = "../Graphs/FullData/CSV/industry_code_table.csv", row.names = FALSE)
write.csv(get_cat_var_table(training_data, cw_coeff_table, "MultiPolicy"), file = "../Graphs/FullData/CSV/multipolicy_table.csv", row.names = FALSE)
write.csv(get_cat_var_table(training_data, cw_coeff_table, "xTreme_TurnSignal"), file = "../Graphs/FullData/CSV/xtreme_turnsignal_table.csv", row.names = FALSE)
write.csv(get_cat_var_table(training_data, cw_coeff_table, "car_weight"), file = "../Graphs/FullData/CSV/car_weight_table.csv", row.names = FALSE)
write.csv(get_driver_var_table(training_data, cw_coeff_table, variable_name = "driver_age"), file = "../Graphs/FullData/CSV/driver_age_table.csv", row.names = FALSE)
write.csv(get_vehicle_var_table(training_data, cw_coeff_table, variable_name = "vehicle_age"), file = "../Graphs/FullData/CSV/vehicle_age_table.csv", row.names = FALSE)
}
```
Create prediction plots and lift charts on validate data. Note that we compare both to experienced relativities as well as "true" relativities.
```{r}
# Now, we'll see how well we do on validate data. Here, we will only look at predictions as the
# relativities will give us no new information
validate_data[, glm_prediction := predict.glm(glm_model, validate_data, type = "response")]
validate_data[, lasso_prediction := predict(lasso_model, validate_data_matrix, s = "lambda.min")]
prediction_plot(get_cat_var_table(validate_data, cw_coeff_table, "industry_code"))
prediction_plot(get_cat_var_table(validate_data, cw_coeff_table, "MultiPolicy"))
prediction_plot(get_cat_var_table(validate_data, cw_coeff_table, "xTreme_TurnSignal"))
prediction_plot(get_cat_var_table(validate_data, cw_coeff_table, "car_weight"))
driver_age_table <- get_driver_var_table(validate_data, cw_coeff_table, variable_name = "driver_age")
vehicle_age_table <- get_vehicle_var_table(validate_data, cw_coeff_table, variable_name = "vehicle_age")
prediction_plot(driver_age_table)
prediction_plot(vehicle_age_table)
# Lift Charts for Full Dataset
# Figure 7.11
double_lift_chart(
dataset = training_data,
lasso_credibility = "lasso_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
double_lift_chart(
dataset = training_data,
lasso_credibility = "lasso_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
selected_columns <- training_data[, c("lasso_prediction", "glm_prediction", "true_risk", "incurred_loss")]
write.csv(selected_columns, file = "../Graphs/FullData/CSV/training_data_predictions.csv", row.names = FALSE)
# Figure 7.10
double_lift_chart(
dataset = validate_data,
lasso_credibility = "lasso_prediction",
glm = "glm_prediction",
actual = "incurred_loss",
normalize = TRUE
)
double_lift_chart(
dataset = validate_data,
lasso_credibility = "lasso_prediction",
glm = "glm_prediction",
actual = "true_risk",
normalize = TRUE
)
selected_columns <- validate_data[, c("lasso_prediction", "glm_prediction", "true_risk", "incurred_loss")]
write.csv(selected_columns, file = "../Graphs/FullData/CSV/validation_data_predictions.csv", row.names = FALSE)
```
Do not clear your working environment after this script to ensure that all data is available for the next script.