Final Project

Libraries and Data

These libraries will be needed to load in prior to analysis

library(tidymodels)
library(tidyverse)
library(baguette)
library(vip)
library(pdp)
library(here)
library(kernlab)
library(ggplot2)
library(ranger)
library(earth)

These are needed to store in the global environment

df <- read.csv("customer_retention.csv")
df <- mutate(df, Status = factor(Status))
df <- na.omit(df)

Exploratory Data Analysis

ggplot(df, aes(Status)) + 
  geom_bar() + 
  ggtitle("Count of Customers Current vs Left") +
  theme(plot.title = element_text(hjust = 0.25)) +
  labs(y = "Count of Customers", x = "Type of Internet Service")

I think it is important to know our data, and how much date we have. Here we see that we have much more data on current customers vs custormers who left, which is prettty expected.

ggplot(df, aes(Tenure)) +
  geom_density() +
  facet_wrap(~Contract) +
  ggtitle("Tenure and Length of Contract ") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(y = "Count of Customers", x = "Length of Tenure")

This graph shows that the length of contract matters in retaining customers. The shorter the contract, the harder it is to keep those customers, but with longer contracts we see that we keep customers around for longer.

Machine Learnng

Logistic Regression Model

The first model to look at is a simple logistic regression model.

set.seed(123)
log_split <- initial_split(df, prop = .7, strata = Status)
log_train <- training(log_split)
log_test <- testing(log_split)

set.seed(123)
log_kfolds <- vfold_cv(log_train, v = 5, strata = Status)

logistic_reg() %>% 
  fit_resamples(Status ~ ., log_kfolds) %>%
  collect_metrics()
## # A tibble: 2 × 6
##   .metric  .estimator  mean     n std_err .config             
##   <chr>    <chr>      <dbl> <int>   <dbl> <chr>               
## 1 accuracy binary     0.799     5 0.00401 Preprocessor1_Model1
## 2 roc_auc  binary     0.845     5 0.00521 Preprocessor1_Model1

There are better models than this one, but the Roc_AOC in this model is still in a good place.

Random forest Model

Let’s look at a Random Forest Model

set.seed(123)
rf_split <- initial_split(df, prop = 0.7, strata = Status)
rf_train <- training(rf_split)
rf_test <- testing(rf_split)

rf_recipe <- recipe(Status ~ ., data = rf_train)

rf_mod <- rand_forest(mode = "classification") %>%
  set_engine("ranger")

set.seed(123)
rf_kfold <- vfold_cv(rf_train, v = 5)

results <- fit_resamples(rf_mod, rf_recipe, rf_kfold)

collect_metrics(results)
## # A tibble: 2 × 6
##   .metric  .estimator  mean     n std_err .config             
##   <chr>    <chr>      <dbl> <int>   <dbl> <chr>               
## 1 accuracy binary     0.797     5 0.00643 Preprocessor1_Model1
## 2 roc_auc  binary     0.837     5 0.0104  Preprocessor1_Model1

We’ll try to get a better Roc_AUC by adjusting hyperparameters.

rf_mod <- rand_forest(mode = "classification", trees = tune(), mtry = tune(), min_n = tune()) %>%
  set_engine("ranger", importance = "impurity")

rf_hyper_grid <- grid_regular(trees(range = c(100, 1000)), 
                              mtry(range = c(1, 50)),
                              min_n(range = c(1, 20)),
                              levels = 5)

set.seed(123)
results <- tune_grid(rf_mod, rf_recipe, resamples = rf_kfold, grid = rf_hyper_grid)

show_best(results)
## # A tibble: 5 × 9
##    mtry trees min_n .metric .estimator  mean     n std_err .config              
##   <int> <int> <int> <chr>   <chr>      <dbl> <int>   <dbl> <chr>                
## 1     1   775    20 roc_auc binary     0.840     5  0.0108 Preprocessor1_Model1…
## 2     1   325     1 roc_auc binary     0.840     5  0.0110 Preprocessor1_Model0…
## 3     1   550    20 roc_auc binary     0.840     5  0.0109 Preprocessor1_Model1…
## 4     1   325    20 roc_auc binary     0.840     5  0.0103 Preprocessor1_Model1…
## 5     1   775    10 roc_auc binary     0.839     5  0.0108 Preprocessor1_Model0…

And finally, let’s see the most important features while trying to predict status.

rf_best <- select_best(results, metric = "roc_auc")

final_rf_wf <- workflow() %>% add_recipe(rf_recipe) %>% add_model(rf_mod) %>%
  finalize_workflow(rf_best)

final_rf_fit <- final_rf_wf %>% fit(data = rf_train)

final_rf_fit %>% extract_fit_parsnip() %>% vip(num_features = 20)

We see that contract length, tenure, and monthly and total charges are some of the most important features in this model.

MARS Model

Let’s finally look at a Multivariate Adaptive Regression Spline.

set.seed(123)
mars_split <- initial_split(df, prop = .7, strata = Status)
mars_train <- training(mars_split)
mars_test <- testing(mars_split)

mars_recipe <- recipe(Status ~ ., data = mars_train)

set.seed(123)
mars_kfolds <- vfold_cv(mars_train, v = 5, strata = "Status")

mars_mod <- mars(num_terms = tune(), prod_degree = tune()) %>%
  set_mode("classification")

mars_grid <- grid_regular(num_terms(range = c(1,40)), prod_degree(), levels = 50)

mars_wf <- workflow() %>% add_recipe(mars_recipe) %>% add_model(mars_mod)

mars_results <- mars_wf %>% tune_grid(resamples = mars_kfolds, grid = mars_grid)

mars_results %>% collect_metrics() %>% filter(.metric == "roc_auc") %>%
arrange(desc(mean))
## # A tibble: 80 × 8
##    num_terms prod_degree .metric .estimator  mean     n std_err .config         
##        <int>       <int> <chr>   <chr>      <dbl> <int>   <dbl> <chr>           
##  1        20           1 roc_auc binary     0.850     5 0.00486 Preprocessor1_M…
##  2        19           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  3        18           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  4        21           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  5        22           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  6        23           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  7        24           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  8        25           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
##  9        26           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
## 10        27           1 roc_auc binary     0.849     5 0.00486 Preprocessor1_M…
## # ℹ 70 more rows

Let’s do a little tuning to to get the best hyperparameter values.

mars_best <- select_best(mars_results, metric = "roc_auc")

mars_final_wf <- workflow() %>% 
  add_model(mars_mod) %>% add_formula(Status ~ .) %>% 
  finalize_workflow(mars_best)

mars_final_wf %>% 
  fit(data = mars_train) %>%
  extract_fit_parsnip() %>%
  vip(20, type = "rss")

Looking above, we can see that tenure, total and monthly charges, and payment method are the more important features in this model when predicting customer status.

Business Analysis

Predictor Varables

Tenure, total charges, and monthly charges are by far the most important features in this set for predicting customer status. There was also a handful of features that had little to no impact of the status variable, such as whether or not the customer had dependents, the gender of the customer, and if the customer had a partner or not.

Tenure being the most important variable is pretty expected. The longer a customer is with the company, the more likely they are to stay with the company. Regork should look to retaining these customers by offering them these new incentives.

Total and monthly charges were important predictors of status as well, and these people with higher bills should be considered for the new incentives as well. This may relieve these people a little bit on their bills, as well as give them some interactivity with the company.

The new incentives should not be based on a customers personal information. There is no evidence to suggest that any personal details have an effect on whether or not a customer leaves. With little to no importance in the models, this shows that it is more random that people of certain gender, number of dependents, or if they have a partner, leave the company.