Our objective is to develop an accurate predictive model that anticipates customer turnover within Regork’s telecommunications services, enabling proactive measures for customer retention and loyalty enhancement.
Given the substantial costs associated with acquiring new customers, it is imperative to identify and mitigate turnover risk among existing clientele. By leveraging advanced data analysis techniques, we aim to harness insights from customer data to predict turnover behavior effectively.
PACKAGES USED:
tidymodels: Provides a framework for modeling and machine learning with tidy principles in R.
tidyverse: A collection of packages for data manipulation, visualization, and programming using tidy data principles.
baguette: Implements algorithms for Bayesian model averaging and predictive model comparison.
vip: Computes variable importance measures for predictive models.
pdp: Generates partial dependence plots for exploring the relationship between predictors and the response variable.
kernlab: Offers kernel-based machine learning methods such as Support Vector Machines (SVM) and kernel PCA.
earth: Performs multivariate adaptive regression splines (MARS) for flexible regression modeling.
ranger: Implements fast and efficient random forest algorithms for classification and regression tasks.
yardstick: Provides tools for evaluating models in the tidymodels framework.
library(tidymodels)
library(tidyverse)
library(baguette)
library(vip)
library(pdp)
library(kernlab)
library(earth)
library(ranger)
library(yardstick)
library(carat)
Importing and Mutating Data:
retention <- read_csv(file ="customer_retention.csv")
retention <- retention %>%
mutate(Status = as.factor(Status))
retention <- drop_na(retention)
retention %>% is.na() %>% sum()
## [1] 0
# split data
set.seed(123)
split <- initial_split(retention, prop = 0.7, strata = Status)
ret_train <- training(split)
ret_test <- testing(split)
retention %>%
ggplot( aes(Tenure)) +
geom_histogram(fill = "blue") +
facet_wrap(~Status)
From this graph we can see that a majority of people who left were newer
customers with short tenures.
retention %>%
filter(Status == "Left") %>%
ggplot(aes(InternetService)) +
geom_bar(fill = "green") +
facet_grid(~Status)
From this graph we can see a majority of the customers who left used
fiber optic internet service.
# create model recipe
model_recipe <- recipe(Status ~ ., data = ret_train)
# create resampling procedure
set.seed(123)
kfold <- vfold_cv(ret_train, v = 5)
dt_mod <- decision_tree(mode = "classification") %>%
set_engine("rpart")
# fit model workflow
dt_fit <- workflow() %>%
add_recipe(model_recipe) %>%
add_model(dt_mod) %>%
fit(data = ret_train)
# train model
dt_results <- fit_resamples(dt_mod, model_recipe, kfold)
# model results
collect_metrics(dt_results)
dt_mod <- decision_tree(
mode = "classification",
cost_complexity = tune(),
tree_depth = tune(),
min_n = tune()
) %>%
set_engine("rpart")
# create the hyperparameter grid
dt_hyper_grid <- grid_regular(
cost_complexity(),
tree_depth(),
min_n(),
levels = 5
)
# train our model across the hyper parameter grid
set.seed(123)
dt_results <- tune_grid(dt_mod, model_recipe, resamples = kfold, grid = dt_hyper_grid)
# get best results
show_best(dt_results, metric = "roc_auc", n = 5)
# create random forest model object and use the ranger package for the engine
rf_mod <- rand_forest(mode = "classification") %>%
set_engine("ranger")
# create model recipe
model_recipe <- recipe(Status ~ ., data = ret_train)
# train model
rf_results <- fit_resamples(rf_mod, model_recipe, kfold)
# model results
collect_metrics(rf_results)
# create random forest model object with tuning option
rf_mod <- rand_forest(
mode = "classification",
trees = tune(),
mtry = tune(),
min_n = tune()
) %>%
set_engine("ranger", importance = "impurity")
# create the hyperparameter grid
rf_hyper_grid <- grid_regular(
trees(range = c(20, 300)),
mtry(range = c(2, 19)),
min_n(range = c(1, 20)),
levels = 5
)
# train our model across the hyper parameter grid
set.seed(123)
rf_results <- tune_grid(rf_mod, model_recipe, resamples = kfold, grid = rf_hyper_grid)
# model results
show_best(rf_results, metric = "roc_auc")
rf_best_hyperparameters <- select_best(rf_results, metric = "roc_auc")
final_rf_wf <- workflow() %>%
add_recipe(model_recipe) %>%
add_model(rf_mod) %>%
finalize_workflow(rf_best_hyperparameters)
rf_final_fit <- final_rf_wf %>%
fit(data = ret_train)
rf_final_fit %>%
extract_fit_parsnip() %>%
vip(num_features = 20)
lr_mod <- logistic_reg()
set.seed(123) kfolds <- vfold_cv(ret_train, v = 5, strata = Status)
log_results <- lr_mod %>% fit_resamples(Status ~ ., kfolds) %>% collect_metrics()
The analysis conducted on Regork’s telecommunications service platform reveals key insights into factors influencing customer churn. Among the top predictors identified, tenure emerges as the most significant, suggesting that fostering long-term customer relationships should be a strategic priority. Encouraging customers to opt for longer contracts and maintaining competitive pricing aligns with these findings.
The conclusion drawn underscores the potential profitability of Regork’s new platform and underscores the importance of leveraging data-driven insights to mitigate customer churn. Continual refinement of the models based on ongoing feedback and additional data will further bolster their predictive accuracy and utility in guiding strategic decisions for the organization.