In order to assist Regork with their new telecommunication service, we have been tasked with building a model that can determine customer retention rates. To create this model, we began by visualizing trends in the data set that was provided. Then we proceeded to build, train, and test three different types of machine learning models that we found to be most fitting for the specific scenarios we were looking at. We decided to build a logistic regression model, a decision tree, and a random forest model then we measured the performance of each model using a standard metric, area under the curve (AUC).
In order to process the data and produce the results desired for this study, the following packages have been installed and called onto the environment:
library(tidymodels)
library(tidyverse)
library(baguette)
library(vip)
library(pdp)
library(here)
library(kernlab)
library(ggplot2)
library(ranger)
library(earth)
Firstly, we process our dataset in order to further interpret it and its corresponding response variables:
retention <- read.csv("customer_retention.csv")
retention <- retention %>%
dplyr::mutate(Status = as.factor(Status))
retention <- drop_na(retention)
retention %>% is.na() %>% sum()
## [1] 0
retention %>%
ggplot(aes(InternetService, fill=Status))+
geom_bar() +
facet_wrap(~PhoneService) +
labs(title = "Status of Users and Phone and Internet Service Used")
This plot shows the relationship between users, their status under our data base as either a current or past user, and the relationships between internet and phone service that is being/ was used by them. The graph is divided by user that have or don’t have the internet service, and the different services that they occupied or currently occupy.
retention %>%
ggplot(aes(Status)) +
geom_bar(fill = "red") +
facet_wrap(~Contract) +
ggtitle("Status of Users and Tenure Used") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(y = "Count of Customers", x = "Tenure")
This graph shows the relationship between Regork, its current and non-current costumers and the kind of tenure they had with the organization, broken up into 3 categories: month-to-month, one year, and two year tenures.
retention %>%
ggplot(aes(StreamingTV)) +
geom_bar(fill = "blue") +
facet_wrap(~Status) +
ggtitle("Status of Users and Stream TV Used") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(y = "Count of Customers", x = "Stream TV")
This graph shows the relationship between current and non current customers of Regork, and their TV streaming activities defined by Yes & No for people that had the internet service AND used the TV streaming, as well as the customers that didn’t have the internet service altogether.
# Split data
set.seed(123)
data_split <- initial_split(retention, prop = .7, strata = Status)
data_train <- training(data_split)
data_test <- testing(data_split)
# Cross-validation 5-fold
kfolds <- vfold_cv(data_train, v = 5, strata = Status)
And now we have set up the baseline for our 3 different machine learning models that we will adopt into our data sets in order to find the best fit.
Based on the preliminary exploratory analysis, the following machine learning models were programmed in order to find the model that best fits the relationship of the analysis that the task is aspiring to complete.
For our first machine learning model we decided to use a logistic regression model as the baseline for measuring AUC correlations throughout our models.
# average AUC LR
results <- logistic_reg() %>%
fit_resamples(Status ~ ., kfolds)
collect_metrics(results) %>% filter(.metric == "roc_auc")
## # A tibble: 1 × 6
## .metric .estimator mean n std_err .config
## <chr> <chr> <dbl> <int> <dbl> <chr>
## 1 roc_auc binary 0.845 5 0.00590 Preprocessor1_Model1
# Confusion matrix LR
lr_model <- logistic_reg() %>%
fit(Status ~ ., data = data_train)
lr_model %>%
predict(data_test) %>%
bind_cols(data_test %>% select(Status)) %>%
conf_mat(truth = Status, estimate = .pred_class)
## Truth
## Prediction Current Left
## Current 1362 225
## Left 178 332
In order to test the validity of the trained model to the actual data, a confusion matrix was created to identify any potential false positives or false negatives within the data of the Logistic Regression.
# Feature plot
vip(lr_model$fit, num_features = 20)
As you can see from the plot above, the most important feature is tenure, followed by 2 year contract and then 1 year contract.
dt_mod <- decision_tree(
mode = "classification",
cost_complexity = tune(),
tree_depth = tune(),
min_n = tune()) %>% set_engine("rpart")
model_recipe <- recipe(
Status ~ .,
data = data_train
)
# create the hyperparameter grid
hyper_grid <- grid_regular(
cost_complexity(),
tree_depth(),
min_n()
)
# train DT model across the hyper parameter grid
dt_results <- tune_grid(dt_mod, model_recipe, resamples = kfolds, grid = hyper_grid)
# Best DT model
show_best(dt_results, metric = "roc_auc", n = 10)
## # A tibble: 10 × 9
## cost_complexity tree_depth min_n .metric .estimator mean n std_err
## <dbl> <int> <int> <chr> <chr> <dbl> <int> <dbl>
## 1 0.0000000001 8 40 roc_auc binary 0.821 5 0.00615
## 2 0.00000316 8 40 roc_auc binary 0.821 5 0.00615
## 3 0.0000000001 15 40 roc_auc binary 0.821 5 0.00627
## 4 0.00000316 15 40 roc_auc binary 0.821 5 0.00627
## 5 0.0000000001 8 21 roc_auc binary 0.820 5 0.00592
## 6 0.00000316 8 21 roc_auc binary 0.820 5 0.00592
## 7 0.0000000001 15 21 roc_auc binary 0.813 5 0.00863
## 8 0.00000316 15 21 roc_auc binary 0.813 5 0.00863
## 9 0.0000000001 8 2 roc_auc binary 0.799 5 0.00827
## 10 0.00000316 8 2 roc_auc binary 0.799 5 0.00827
## # ℹ 1 more variable: .config <chr>
best_dt <- select_best(dt_results, metric = "roc_auc")
# put together final workflow
final_wf <- workflow() %>%
add_recipe(model_recipe) %>%
add_model(dt_mod) %>%
finalize_workflow(best_dt)
# fit final workflow across entire training data
final_dt <- final_wf %>%
fit(data = data_train)
# Decision Tree Confusion Matrix
final_dt %>%
predict(data_test) %>%
bind_cols(data_test %>% select(Status)) %>%
conf_mat(truth = Status, estimate = .pred_class)
## Truth
## Prediction Current Left
## Current 1337 252
## Left 203 305
In order to test the validity of the trained model to the actual data, a confusion matrix was created to identify any potential false positives or false negatives within the data of the Logistic Regression.
# plot DT feature importance
final_dt %>%
extract_fit_parsnip() %>%
vip(20)
As we can see from the plot above, the most important feature in this model is contract followed by tenure and then total charges.
rf_mod <- rand_forest(mode = "classification", trees = 1000, mtry = tune(), min_n = tune()) %>%
set_engine("ranger", importance = "permutation")
# create the hyperparameter grid
hyper_grid <- grid_regular(
mtry(range = c(2, 80)),
min_n(range = c(1, 20)),
levels = 5
)
# Train RR model using hypergrid
rf_results <- tune_grid(rf_mod, model_recipe, resamples = kfolds, grid = hyper_grid)
show_best(rf_results, metric = "roc_auc", n = 10)
## # A tibble: 10 × 8
## mtry min_n .metric .estimator mean n std_err .config
## <int> <int> <chr> <chr> <dbl> <int> <dbl> <chr>
## 1 2 15 roc_auc binary 0.845 5 0.00575 Preprocessor1_Model16
## 2 2 20 roc_auc binary 0.845 5 0.00565 Preprocessor1_Model21
## 3 2 10 roc_auc binary 0.844 5 0.00588 Preprocessor1_Model11
## 4 2 5 roc_auc binary 0.843 5 0.00575 Preprocessor1_Model06
## 5 2 1 roc_auc binary 0.842 5 0.00593 Preprocessor1_Model01
## 6 21 20 roc_auc binary 0.837 5 0.00747 Preprocessor1_Model22
## 7 60 20 roc_auc binary 0.837 5 0.00755 Preprocessor1_Model24
## 8 80 20 roc_auc binary 0.837 5 0.00746 Preprocessor1_Model25
## 9 41 20 roc_auc binary 0.836 5 0.00755 Preprocessor1_Model23
## 10 80 15 roc_auc binary 0.835 5 0.00752 Preprocessor1_Model20
best_rf <- select_best(rf_results, metric = "roc_auc")
final_rf_wf <- workflow() %>%
add_recipe(model_recipe) %>%
add_model(rf_mod) %>%
finalize_workflow(best_rf)
# fit final workflow object
final_rf <- final_rf_wf %>%
fit(data = data_train)
# plot feature importance
final_rf %>%
extract_fit_parsnip() %>%
vip(num_features = 20)
As we can see from the graph above, tenure is the most important feature followed by contract and then total charges.
With an average of 0.845 AUC, we feel confident that our models reflect important factors that Regork should focus on in order to increase customer retention rates when continuing to operate in the telecommunications market. Furthermore, we believe that through the suggestions mentioned above, Regork could drive customer retention rates up with its current existing customer base by incentivizing longer term contracts with customers, or driving their monetary perception of the company (how much is being spent) up by providing higher value for their purchases. We believe that thanks to the studies, the models and the suggestions above Regork can confidently plan for its continued operations in the Telecomunications market with higher customer retention rates.