Insurance companies invest a lot of time and money into optimizing their pricing and accurately estimating the likelihood that customers will make a claim. In many countries, insurance is a legal requirement to have car insurance in order to drive a vehicle on public roads, so the market is very large!
Knowing all of this, On the Road car insurance has requested your services in building a model to predict whether a customer will make a claim on their insurance during the policy period. As they have very little expertise and infrastructure for deploying and monitoring machine learning models, they've asked you to use simple Logistic Regression, identifying the single feature that results in the best-performing model, as measured by accuracy.
They have supplied you with their customer data as a csv file called car_insurance.csv
, along with a table (below) detailing the column names and descriptions below.
The dataset
Column | Description |
---|---|
id | Unique client identifier |
age | Client's age:
|
gender | Client's gender:
|
driving_experience | Years the client has been driving:
|
education | Client's level of education:
|
income | Client's income level:
|
credit_score | Client's credit score (between zero and one) |
vehicle_ownership | Client's vehicle ownership status:
|
vehcile_year | Year of vehicle registration:
|
married | Client's marital status:
|
children | Client's number of children |
postal_code | Client's postal code |
annual_mileage | Number of miles driven by the client each year |
vehicle_type | Type of car:
|
speeding_violations | Total number of speeding violations received by the client |
duis | Number of times the client has been caught driving under the influence of alcohol |
past_accidents | Total number of previous accidents the client has been involved in |
outcome | Whether the client made a claim on their car insurance (response variable):
|
# Import required libraries
install.packages(c("visdat"), verbose = FALSE)
library(readr, verbose = FALSE)
library(dplyr, verbose = FALSE)
library(glue, verbose = FALSE)
library(yardstick, verbose = FALSE)
library(tidyr, verbose = FALSE)
library(purrr, verbose = FALSE)
library(ggplot2, verbose = FALSE)
library(visdat, verbose = FALSE)
library(broom, verbose = FALSE)
library(yardstick, verbose = FALSE)
#library(cluster) # for gower similarity and pam
#library(Rtsne) # for t-SNE plot
# Import the dataset but drop the id column
car_insurance = read_csv("car_insurance.csv", show_col_types = FALSE, col_select = -c("id"))
Exploration
First, we'll explore any missing value in the dataset
# Dataset dimension
dim(car_insurance)
# Observing ranges and missing values
summary(car_insurance)
As we can see, credit_score
and annual_mileage
have similar amount of missing values. Let's investigate this missingness a little deeper.
Missing values imputation
vis_miss(car_insurance[,c("credit_score", "annual_mileage")])
There is no evident relationship between the missingness of these variables. Now let's see if another variable has something to do with this.
# Comparing present and missing values for credit_score
car_insurance %>%
mutate(miss_credit_score = is.na(credit_score)) %>%
group_by(miss_credit_score) %>%
summarise_all(mean, na.rm = TRUE)
# Comparing present and missing values for annual_mileage
car_insurance %>%
mutate(miss_annual_mileage = is.na(annual_mileage)) %>%
group_by(miss_annual_mileage) %>%
summarise_all(mean, na.rm = TRUE)
This brief analysis allows us to conclude that there is no visible factor among the observed variables related to the missing values of credit_score
and annual_mileage
.
So, given that the missing values stand for about 10% of the dataset, we'll fill them in using the information of some of the variables in the dataset.
Given that most variables are categorical, we will impute the missing values per combination of the categories and get the median of credit_score
and annual_mileage
for each of them. However, to avoid finding a single combination which has no value on either credit score or annual mileage, we'll tailor the combinations to intuitively relevant variables for each the targets. For example, for credit score, age
, income
, married
and children
could create relevant strata. While for annual mileage, vehicle_ownership
, vehicle_year
and vehicle_type
could create more suitable strata.
# Categorical variables to create relevant strata for credit_score
catvars_credit_score = c('age', 'income', 'married', 'children')
# Categorical variables to create relevant strata for annual_mileage
catvars_annual_mileage = c('vehicle_ownership', 'vehicle_year', 'vehicle_type')
# Getting all combinations from the categorical variables for credit_score
strata_credit_score = car_insurance %>%
select(all_of(catvars_credit_score)) %>%
unique() %>%
# Creating a combination id
mutate(comb_id_credit = seq(1:nrow(.)))
glimpse(strata_credit_score)
# Getting all combinations from the categorical variables for credit_score
strata_annual_mileage = car_insurance %>%
select(all_of(catvars_annual_mileage)) %>%
unique() %>%
# Creating a combination id
mutate(comb_id_annual = seq(1:nrow(.)))
glimpse(strata_annual_mileage)
# Left join to merge the strata datasets with the main dataset
car_insurance_merged = car_insurance %>%
left_join(strata_credit_score, by = catvars_credit_score) %>%
left_join(strata_annual_mileage, by = catvars_annual_mileage)
glimpse(car_insurance_merged)
# Calculating the credit_score median per stratum
credit_score_fill = car_insurance_merged %>%
aggregate(credit_score ~ comb_id_credit, data = ., median, na.rm = TRUE)
dim(credit_score_fill)
# Checking for missing values
sum(is.na(credit_score_fill))
# Calculating the annual_mileage median per stratum
annual_mileage_fill = car_insurance_merged %>%
aggregate(annual_mileage ~ comb_id_annual, data = ., median, na.rm = TRUE)
dim(annual_mileage_fill)
# Checking for missing values
sum(is.na(annual_mileage_fill))
# Merging all datasets
car_insurance_filled = car_insurance_merged %>%
left_join(credit_score_fill, by = "comb_id_credit", suffix = c("", "_median")) %>%
left_join(annual_mileage_fill, by = "comb_id_annual", suffix = c("", "_median")) %>%
mutate(credit_score_filled = ifelse(is.na(credit_score), credit_score_median, credit_score),
annual_mileage_filled = ifelse(is.na(annual_mileage), annual_mileage_median, annual_mileage))
summary(car_insurance_filled)
Finding the best predictor
In order to model the probability that the client have made a claim on their car insurance, we'll use simple logistic regression. For that purpose, we'll find the feature with the best predictive performance for a car insurance claim.
# Selecting the variables to assess simple logistic regressions
dflogit = car_insurance_filled %>%
select(-c(credit_score, annual_mileage, comb_id_credit, comb_id_annual, credit_score_median, annual_mileage_median)) %>%
rename(credit_score = credit_score_filled, annual_mileage = annual_mileage_filled) %>%
# converting categorical variables in factor type
mutate_at(vars('age', 'gender', 'race', 'driving_experience', 'education', 'income', 'vehicle_ownership', 'vehicle_year', 'married', 'vehicle_type', 'postal_code'), factor)
glimpse(dflogit)