Skip to content

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

ColumnDescription
idUnique client identifier
ageClient's age:
  • 0: 16-25
  • 1: 26-39
  • 2: 40-64
  • 3: 65+
genderClient's gender:
  • 0: Female
  • 1: Male
driving_experienceYears the client has been driving:
  • 0: 0-9
  • 1: 10-19
  • 2: 20-29
  • 3: 30+
educationClient's level of education:
  • 0: No education
  • 1: High school
  • 2: University
incomeClient's income level:
  • 0: Poverty
  • 1: Working class
  • 2: Middle class
  • 3: Upper class
credit_scoreClient's credit score (between zero and one)
vehicle_ownershipClient's vehicle ownership status:
  • 0: Does not own their vehilce (paying off finance)
  • 1: Owns their vehicle
vehcile_yearYear of vehicle registration:
  • 0: Before 2015
  • 1: 2015 or later
marriedClient's marital status:
  • 0: Not married
  • 1: Married
childrenClient's number of children
postal_codeClient's postal code
annual_mileageNumber of miles driven by the client each year
vehicle_typeType of car:
  • 0: Sedan
  • 1: Sports car
speeding_violationsTotal number of speeding violations received by the client
duisNumber of times the client has been caught driving under the influence of alcohol
past_accidentsTotal number of previous accidents the client has been involved in
outcomeWhether the client made a claim on their car insurance (response variable):
  • 0: No claim
  • 1: Made a claim
# Import required libraries
library(readr)
library(dplyr)
library(glue)
library(yardstick)


# Start coding!
data_1 <- read_csv("car_insurance.csv")

part 2

str(data_1)
summary(data_1)
#detecting missong vaues
colSums(is.na(data_1))
#Step 2 remove missing values
#and replace them with the mean of them

data_1$credit_score[is.na(data_1$credit_score)] <- mean(data_1$credit_score,na.rm = TRUE)
data_1$credit_score[is.na(data_1$annual_mileage)] <- mean(data_1$annual_mileage,na.rm = TRUE)

Step 3 : create a data frame


features_df <- data.frame(features = c(names(subset(data_1, select = -c(id,outcome)))))
head(features_df)
accuracies <- c()
#finaly building and storing the models

for(col in features_df$features){
	#create the model
	models <- glm(glue("outcome ~ {col}"),data_1,family = "binomial")
      #get predictuion values
          predictions <- round(fitted(models))
#calulate accuracy
accuracy <- length(which(predictions == data_1$outcome)) / length(data_1$outcome)
#add acuracy to features_df
features_df[which(features_df$feature == col), "accuracy"] = accuracy}
#Measuring performance
best_feature <- features_df$features[which.max(features_df$accuracy)]
best_accuracy <- max(features_df$accuracy)
#creating best feature
best_feature_df <- data.frame(best_feature,best_accuracy)

best_feature_df