As data scientists at Regork, our job is to view data related to customer retention related to Regork’s new telecommunication service. Understanding different customer behaviors is needed to be successful in this new industry. These models we have built, using the “customer_retention.csv” file, help visualize what demographics of customers have the best retention. Using these models can help the marketing team easily view helpful trends with customers. After viewing these, we built machine learning models, including a bag model, random forest model and a mars model to understand which variables in our data impacted customers the most. Throughout our research we did an analysis on our models to find what is best to retain customers.
library(tidymodels)
library(tidyverse)
library(baguette)
library(vip)
library(pdp)
library(here)
library(kernlab)
library(ggplot2)
library(ranger)
library(earth)
df <- read_csv('~/BANA4080/Data/customer_retention.csv')
Rows: 6999 Columns: 20
── Column specification ──────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (16): Gender, Partner, Dependents, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBac...
dbl (4): SeniorCitizen, Tenure, MonthlyCharges, TotalCharges
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
df <- mutate(df, Status = factor(Status))
df <- na.omit(df)
ggplot(df, aes(Tenure)) +
geom_bar(color='blue') +
facet_wrap(~MultipleLines) +
ggtitle('Customer Tenure and if they have Multiple Lines')+
labs(x = 'Tenure Length', y = '')
As shown in the graph the more households that have multiple lines tend to have a longer tenure at the company.
ggplot(df, aes(MonthlyCharges)) +
geom_histogram(color='red') +
facet_wrap(~Status)+
ggtitle("Monthly Charge and Status")+
labs(x= 'Monthly Charge', y ='Total customers')
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
NA
NA
NA
Monthly Charge and Status: Most of Regork’s current customers have a monthly charge of around $25. The majority of the customers that left have a monthly charge of around $75.
ggplot(df, aes(x = Contract, fill = Status)) +
geom_bar(position = "fill") +
labs(title = "Customer Status by Contract Type",
x = "Contract Type",
y = "Proportion of Customers (%)") +
scale_y_continuous(labels = percent_format()) +
scale_fill_manual(values = c("Current" = "red", "Left" = "blue"))
Customer Status by Contract type: Here you can see customers with a Month-to-Month contract type are much more likely to leave then those with the one and two year contract types. As the contract type gets longer the retention rate goes up.
set.seed(123)
bag_split <- initial_split(df, prop = .7, strata = "Status")
bag_train <- training(log_split)
bag_test <- testing(log_split)
bag_recipe <- recipe(
Status ~.,
data = bag_train
)
tree_mod <- bag_tree() %>%
set_engine('rpart', times = 5) %>%
set_mode('classification')
set.seed(123)
kfold <- vfold_cv(bag_train, v = 5)
bag_results <- fit_resamples(tree_mod, bag_recipe, kfold)
collect_metrics(bag_results)
NA
NA
bag_mod <- bag_tree() %>%
set_engine("rpart", times = tune()) %>%
set_mode("classification")
# create the hyperparameter grid
bag_hyper_grid <- expand.grid(times = c(5, 25, 50, 100, 200, 300))
# train our model across the hyper parameter grid
set.seed(123)
bag_results2 <- tune_grid(bag_mod, bag_recipe, resamples = kfold, grid = bag_hyper_grid)
show_best(bag_results2, metric = "roc_auc")
#END Bag
#Start RF Model
rf_mod <- rand_forest(mode = 'classification')%>%
set_engine('ranger')
rf_results <- fit_resamples(rf_mod,bag_recipe, kfold)
collect_metrics(rf_results)
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(
mtry(c(2,50)),
min_n(c(1,20)),
trees(c(1,250)),
levels = 5
)
set.seed(123)
rf_results <- tune_grid(rf_mod, bag_recipe, resamples = kfold, grid = rf_hyper_grid)
→ A | warning: 26 columns were requested but there were 19 predictors in the data. 19 will be used.
There were issues with some computations A: x1
→ B | warning: 38 columns were requested but there were 19 predictors in the data. 19 will be used.
There were issues with some computations A: x1
There were issues with some computations A: x1 B: x1
→ C | warning: 50 columns were requested but there were 19 predictors in the data. 19 will be used.
There were issues with some computations A: x1 B: x1
There were issues with some computations A: x1 B: x1 C: x1
There were issues with some computations A: x2 B: x1 C: x1
There were issues with some computations A: x2 B: x2 C: x1
There were issues with some computations A: x2 B: x2 C: x2
There were issues with some computations A: x3 B: x2 C: x2
There were issues with some computations A: x3 B: x3 C: x2
There were issues with some computations A: x3 B: x3 C: x3
There were issues with some computations A: x4 B: x3 C: x3
There were issues with some computations A: x4 B: x4 C: x3
There were issues with some computations A: x4 B: x4 C: x4
There were issues with some computations A: x5 B: x4 C: x4
There were issues with some computations A: x5 B: x5 C: x4
There were issues with some computations A: x5 B: x5 C: x5
There were issues with some computations A: x6 B: x5 C: x5
There were issues with some computations A: x6 B: x6 C: x5
There were issues with some computations A: x6 B: x6 C: x6
There were issues with some computations A: x7 B: x6 C: x6
There were issues with some computations A: x7 B: x7 C: x6
There were issues with some computations A: x7 B: x7 C: x7
There were issues with some computations A: x8 B: x7 C: x7
There were issues with some computations A: x8 B: x8 C: x7
There were issues with some computations A: x8 B: x8 C: x8
There were issues with some computations A: x9 B: x8 C: x8
There were issues with some computations A: x9 B: x9 C: x8
There were issues with some computations A: x9 B: x9 C: x9
There were issues with some computations A: x10 B: x9 C: x9
There were issues with some computations A: x10 B: x10 C: x9
There were issues with some computations A: x10 B: x10 C: x10
There were issues with some computations A: x11 B: x10 C: x10
There were issues with some computations A: x11 B: x11 C: x10
There were issues with some computations A: x11 B: x11 C: x11
There were issues with some computations A: x12 B: x11 C: x11
There were issues with some computations A: x12 B: x12 C: x11
There were issues with some computations A: x12 B: x12 C: x12
There were issues with some computations A: x13 B: x12 C: x12
There were issues with some computations A: x13 B: x13 C: x12
There were issues with some computations A: x13 B: x13 C: x13
There were issues with some computations A: x14 B: x13 C: x13
There were issues with some computations A: x14 B: x14 C: x13
There were issues with some computations A: x14 B: x14 C: x14
There were issues with some computations A: x15 B: x14 C: x14
There were issues with some computations A: x15 B: x15 C: x14
There were issues with some computations A: x15 B: x15 C: x15
There were issues with some computations A: x16 B: x15 C: x15
There were issues with some computations A: x16 B: x16 C: x15
There were issues with some computations A: x16 B: x16 C: x16
There were issues with some computations A: x17 B: x16 C: x16
There were issues with some computations A: x17 B: x17 C: x16
There were issues with some computations A: x17 B: x17 C: x17
There were issues with some computations A: x18 B: x17 C: x17
There were issues with some computations A: x18 B: x18 C: x17
There were issues with some computations A: x18 B: x18 C: x18
There were issues with some computations A: x19 B: x18 C: x18
There were issues with some computations A: x19 B: x19 C: x18
There were issues with some computations A: x19 B: x19 C: x19
There were issues with some computations A: x20 B: x19 C: x19
There were issues with some computations A: x20 B: x20 C: x19
There were issues with some computations A: x20 B: x20 C: x20
There were issues with some computations A: x21 B: x20 C: x20
There were issues with some computations A: x21 B: x21 C: x20
There were issues with some computations A: x21 B: x21 C: x21
There were issues with some computations A: x22 B: x21 C: x21
There were issues with some computations A: x22 B: x22 C: x21
There were issues with some computations A: x22 B: x22 C: x22
There were issues with some computations A: x23 B: x22 C: x22
There were issues with some computations A: x23 B: x23 C: x22
There were issues with some computations A: x23 B: x23 C: x23
There were issues with some computations A: x24 B: x23 C: x23
There were issues with some computations A: x24 B: x24 C: x23
There were issues with some computations A: x24 B: x24 C: x24
There were issues with some computations A: x25 B: x24 C: x24
There were issues with some computations A: x25 B: x25 C: x24
There were issues with some computations A: x25 B: x25 C: x25
There were issues with some computations A: x26 B: x25 C: x25
There were issues with some computations A: x26 B: x26 C: x25
There were issues with some computations A: x26 B: x26 C: x26
There were issues with some computations A: x27 B: x26 C: x26
There were issues with some computations A: x27 B: x27 C: x26
There were issues with some computations A: x27 B: x27 C: x27
There were issues with some computations A: x28 B: x27 C: x27
There were issues with some computations A: x28 B: x28 C: x27
There were issues with some computations A: x28 B: x28 C: x28
There were issues with some computations A: x29 B: x28 C: x28
There were issues with some computations A: x29 B: x29 C: x28
There were issues with some computations A: x29 B: x29 C: x29
There were issues with some computations A: x30 B: x29 C: x29
There were issues with some computations A: x30 B: x30 C: x29
There were issues with some computations A: x30 B: x30 C: x30
There were issues with some computations A: x31 B: x30 C: x30
There were issues with some computations A: x31 B: x31 C: x30
There were issues with some computations A: x31 B: x31 C: x31
There were issues with some computations A: x32 B: x31 C: x31
There were issues with some computations A: x32 B: x32 C: x31
There were issues with some computations A: x32 B: x32 C: x32
There were issues with some computations A: x33 B: x32 C: x32
There were issues with some computations A: x33 B: x33 C: x32
There were issues with some computations A: x33 B: x33 C: x33
There were issues with some computations A: x34 B: x33 C: x33
There were issues with some computations A: x34 B: x34 C: x33
There were issues with some computations A: x34 B: x34 C: x34
There were issues with some computations A: x35 B: x34 C: x34
There were issues with some computations A: x35 B: x35 C: x34
There were issues with some computations A: x35 B: x35 C: x35
There were issues with some computations A: x36 B: x35 C: x35
There were issues with some computations A: x36 B: x36 C: x35
There were issues with some computations A: x36 B: x36 C: x36
There were issues with some computations A: x37 B: x36 C: x36
There were issues with some computations A: x37 B: x37 C: x36
There were issues with some computations A: x37 B: x37 C: x37
There were issues with some computations A: x38 B: x37 C: x37
There were issues with some computations A: x38 B: x38 C: x37
There were issues with some computations A: x38 B: x38 C: x38
There were issues with some computations A: x39 B: x38 C: x38
There were issues with some computations A: x39 B: x39 C: x38
There were issues with some computations A: x39 B: x39 C: x39
There were issues with some computations A: x40 B: x39 C: x39
There were issues with some computations A: x40 B: x40 C: x39
There were issues with some computations A: x40 B: x40 C: x40
There were issues with some computations A: x41 B: x40 C: x40
There were issues with some computations A: x41 B: x41 C: x40
There were issues with some computations A: x41 B: x41 C: x41
There were issues with some computations A: x42 B: x41 C: x41
There were issues with some computations A: x42 B: x42 C: x41
There were issues with some computations A: x42 B: x42 C: x42
There were issues with some computations A: x43 B: x42 C: x42
There were issues with some computations A: x43 B: x43 C: x42
There were issues with some computations A: x43 B: x43 C: x43
There were issues with some computations A: x44 B: x43 C: x43
There were issues with some computations A: x44 B: x44 C: x43
There were issues with some computations A: x44 B: x44 C: x44
There were issues with some computations A: x45 B: x44 C: x44
There were issues with some computations A: x45 B: x45 C: x44
There were issues with some computations A: x45 B: x45 C: x45
There were issues with some computations A: x46 B: x45 C: x45
There were issues with some computations A: x46 B: x46 C: x45
There were issues with some computations A: x46 B: x46 C: x46
There were issues with some computations A: x47 B: x46 C: x46
There were issues with some computations A: x47 B: x47 C: x46
There were issues with some computations A: x47 B: x47 C: x47
There were issues with some computations A: x48 B: x47 C: x47
There were issues with some computations A: x48 B: x48 C: x47
There were issues with some computations A: x48 B: x48 C: x48
There were issues with some computations A: x49 B: x48 C: x48
There were issues with some computations A: x49 B: x49 C: x48
There were issues with some computations A: x49 B: x49 C: x49
There were issues with some computations A: x50 B: x49 C: x49
There were issues with some computations A: x50 B: x50 C: x49
There were issues with some computations A: x50 B: x50 C: x50
There were issues with some computations A: x51 B: x50 C: x50
There were issues with some computations A: x51 B: x51 C: x50
There were issues with some computations A: x51 B: x51 C: x51
There were issues with some computations A: x52 B: x51 C: x51
There were issues with some computations A: x52 B: x52 C: x51
There were issues with some computations A: x52 B: x52 C: x52
There were issues with some computations A: x53 B: x52 C: x52
There were issues with some computations A: x53 B: x53 C: x52
There were issues with some computations A: x53 B: x53 C: x53
There were issues with some computations A: x54 B: x53 C: x53
There were issues with some computations A: x54 B: x54 C: x53
There were issues with some computations A: x54 B: x54 C: x54
There were issues with some computations A: x55 B: x54 C: x54
There were issues with some computations A: x55 B: x55 C: x54
There were issues with some computations A: x55 B: x55 C: x55
There were issues with some computations A: x56 B: x55 C: x55
There were issues with some computations A: x56 B: x56 C: x55
There were issues with some computations A: x56 B: x56 C: x56
There were issues with some computations A: x57 B: x56 C: x56
There were issues with some computations A: x57 B: x57 C: x56
There were issues with some computations A: x57 B: x57 C: x57
There were issues with some computations A: x58 B: x57 C: x57
There were issues with some computations A: x58 B: x58 C: x57
There were issues with some computations A: x58 B: x58 C: x58
There were issues with some computations A: x59 B: x58 C: x58
There were issues with some computations A: x59 B: x59 C: x58
There were issues with some computations A: x59 B: x59 C: x59
There were issues with some computations A: x60 B: x59 C: x59
There were issues with some computations A: x60 B: x60 C: x59
There were issues with some computations A: x60 B: x60 C: x60
There were issues with some computations A: x61 B: x60 C: x60
There were issues with some computations A: x61 B: x61 C: x60
There were issues with some computations A: x61 B: x61 C: x61
There were issues with some computations A: x62 B: x61 C: x61
There were issues with some computations A: x62 B: x62 C: x61
There were issues with some computations A: x62 B: x62 C: x62
There were issues with some computations A: x63 B: x62 C: x62
There were issues with some computations A: x63 B: x63 C: x62
There were issues with some computations A: x63 B: x63 C: x63
There were issues with some computations A: x64 B: x63 C: x63
There were issues with some computations A: x64 B: x64 C: x63
There were issues with some computations A: x64 B: x64 C: x64
There were issues with some computations A: x65 B: x64 C: x64
There were issues with some computations A: x65 B: x65 C: x64
There were issues with some computations A: x65 B: x65 C: x65
There were issues with some computations A: x66 B: x65 C: x65
There were issues with some computations A: x66 B: x66 C: x65
There were issues with some computations A: x66 B: x66 C: x66
There were issues with some computations A: x67 B: x66 C: x66
There were issues with some computations A: x67 B: x67 C: x66
There were issues with some computations A: x67 B: x67 C: x67
There were issues with some computations A: x68 B: x67 C: x67
There were issues with some computations A: x68 B: x68 C: x67
There were issues with some computations A: x68 B: x68 C: x68
There were issues with some computations A: x69 B: x68 C: x68
There were issues with some computations A: x69 B: x69 C: x68
There were issues with some computations A: x69 B: x69 C: x69
There were issues with some computations A: x70 B: x69 C: x69
There were issues with some computations A: x70 B: x70 C: x69
There were issues with some computations A: x70 B: x70 C: x70
There were issues with some computations A: x71 B: x70 C: x70
There were issues with some computations A: x71 B: x71 C: x70
There were issues with some computations A: x71 B: x71 C: x71
There were issues with some computations A: x72 B: x71 C: x71
There were issues with some computations A: x72 B: x72 C: x71
There were issues with some computations A: x72 B: x72 C: x72
There were issues with some computations A: x73 B: x72 C: x72
There were issues with some computations A: x73 B: x73 C: x72
There were issues with some computations A: x73 B: x73 C: x73
There were issues with some computations A: x74 B: x73 C: x73
There were issues with some computations A: x74 B: x74 C: x73
There were issues with some computations A: x74 B: x74 C: x74
There were issues with some computations A: x75 B: x74 C: x74
There were issues with some computations A: x75 B: x75 C: x74
There were issues with some computations A: x75 B: x75 C: x75
There were issues with some computations A: x76 B: x75 C: x75
There were issues with some computations A: x76 B: x76 C: x75
There were issues with some computations A: x76 B: x76 C: x76
There were issues with some computations A: x77 B: x76 C: x76
There were issues with some computations A: x77 B: x77 C: x76
There were issues with some computations A: x77 B: x77 C: x77
There were issues with some computations A: x78 B: x77 C: x77
There were issues with some computations A: x78 B: x78 C: x77
There were issues with some computations A: x78 B: x78 C: x78
There were issues with some computations A: x79 B: x78 C: x78
There were issues with some computations A: x79 B: x79 C: x78
There were issues with some computations A: x79 B: x79 C: x79
There were issues with some computations A: x80 B: x79 C: x79
There were issues with some computations A: x80 B: x80 C: x79
There were issues with some computations A: x80 B: x80 C: x80
There were issues with some computations A: x81 B: x80 C: x80
There were issues with some computations A: x81 B: x81 C: x80
There were issues with some computations A: x81 B: x81 C: x81
There were issues with some computations A: x82 B: x81 C: x81
There were issues with some computations A: x82 B: x82 C: x81
There were issues with some computations A: x82 B: x82 C: x82
There were issues with some computations A: x83 B: x82 C: x82
There were issues with some computations A: x83 B: x83 C: x82
There were issues with some computations A: x83 B: x83 C: x83
There were issues with some computations A: x84 B: x83 C: x83
There were issues with some computations A: x84 B: x84 C: x83
There were issues with some computations A: x84 B: x84 C: x84
There were issues with some computations A: x85 B: x84 C: x84
There were issues with some computations A: x85 B: x85 C: x84
There were issues with some computations A: x85 B: x85 C: x85
There were issues with some computations A: x86 B: x85 C: x85
There were issues with some computations A: x86 B: x86 C: x85
There were issues with some computations A: x86 B: x86 C: x86
There were issues with some computations A: x87 B: x86 C: x86
There were issues with some computations A: x87 B: x87 C: x86
There were issues with some computations A: x87 B: x87 C: x87
There were issues with some computations A: x88 B: x87 C: x87
There were issues with some computations A: x88 B: x88 C: x87
There were issues with some computations A: x88 B: x88 C: x88
There were issues with some computations A: x89 B: x88 C: x88
There were issues with some computations A: x89 B: x89 C: x88
There were issues with some computations A: x89 B: x89 C: x89
There were issues with some computations A: x90 B: x89 C: x89
There were issues with some computations A: x90 B: x90 C: x89
There were issues with some computations A: x90 B: x90 C: x90
There were issues with some computations A: x91 B: x90 C: x90
There were issues with some computations A: x91 B: x91 C: x90
There were issues with some computations A: x91 B: x91 C: x91
There were issues with some computations A: x92 B: x91 C: x91
There were issues with some computations A: x92 B: x92 C: x91
There were issues with some computations A: x92 B: x92 C: x92
There were issues with some computations A: x93 B: x92 C: x92
There were issues with some computations A: x93 B: x93 C: x92
There were issues with some computations A: x93 B: x93 C: x93
There were issues with some computations A: x94 B: x93 C: x93
There were issues with some computations A: x94 B: x94 C: x93
There were issues with some computations A: x94 B: x94 C: x94
There were issues with some computations A: x95 B: x94 C: x94
There were issues with some computations A: x95 B: x95 C: x94
There were issues with some computations A: x95 B: x95 C: x95
There were issues with some computations A: x96 B: x95 C: x95
There were issues with some computations A: x96 B: x96 C: x95
There were issues with some computations A: x96 B: x96 C: x96
There were issues with some computations A: x97 B: x96 C: x96
There were issues with some computations A: x97 B: x97 C: x96
There were issues with some computations A: x97 B: x97 C: x97
There were issues with some computations A: x98 B: x97 C: x97
There were issues with some computations A: x98 B: x98 C: x97
There were issues with some computations A: x98 B: x98 C: x98
There were issues with some computations A: x99 B: x98 C: x98
There were issues with some computations A: x99 B: x99 C: x98
There were issues with some computations A: x99 B: x99 C: x99
There were issues with some computations A: x100 B: x99 C: x99
There were issues with some computations A: x100 B: x100 C: x99
There were issues with some computations A: x100 B: x100 C: x100
There were issues with some computations A: x101 B: x100 C: x100
There were issues with some computations A: x101 B: x101 C: x100
There were issues with some computations A: x101 B: x101 C: x101
There were issues with some computations A: x102 B: x101 C: x101
There were issues with some computations A: x102 B: x102 C: x101
There were issues with some computations A: x102 B: x102 C: x102
There were issues with some computations A: x103 B: x102 C: x102
There were issues with some computations A: x103 B: x103 C: x102
There were issues with some computations A: x103 B: x103 C: x103
There were issues with some computations A: x104 B: x103 C: x103
There were issues with some computations A: x104 B: x104 C: x103
There were issues with some computations A: x104 B: x104 C: x104
There were issues with some computations A: x105 B: x104 C: x104
There were issues with some computations A: x105 B: x105 C: x104
There were issues with some computations A: x105 B: x105 C: x105
There were issues with some computations A: x106 B: x105 C: x105
There were issues with some computations A: x106 B: x106 C: x105
There were issues with some computations A: x106 B: x106 C: x106
There were issues with some computations A: x107 B: x106 C: x106
There were issues with some computations A: x107 B: x107 C: x106
There were issues with some computations A: x107 B: x107 C: x107
There were issues with some computations A: x108 B: x107 C: x107
There were issues with some computations A: x108 B: x108 C: x107
There were issues with some computations A: x108 B: x108 C: x108
There were issues with some computations A: x109 B: x108 C: x108
There were issues with some computations A: x109 B: x109 C: x108
There were issues with some computations A: x109 B: x109 C: x109
There were issues with some computations A: x110 B: x109 C: x109
There were issues with some computations A: x110 B: x110 C: x109
There were issues with some computations A: x110 B: x110 C: x110
There were issues with some computations A: x111 B: x110 C: x110
There were issues with some computations A: x111 B: x111 C: x110
There were issues with some computations A: x111 B: x111 C: x111
There were issues with some computations A: x112 B: x111 C: x111
There were issues with some computations A: x112 B: x112 C: x111
There were issues with some computations A: x112 B: x112 C: x112
There were issues with some computations A: x113 B: x112 C: x112
There were issues with some computations A: x113 B: x113 C: x112
There were issues with some computations A: x113 B: x113 C: x113
There were issues with some computations A: x114 B: x113 C: x113
There were issues with some computations A: x114 B: x114 C: x113
There were issues with some computations A: x114 B: x114 C: x114
There were issues with some computations A: x115 B: x114 C: x114
There were issues with some computations A: x115 B: x115 C: x114
There were issues with some computations A: x115 B: x115 C: x115
There were issues with some computations A: x116 B: x115 C: x115
There were issues with some computations A: x116 B: x116 C: x115
There were issues with some computations A: x116 B: x116 C: x116
There were issues with some computations A: x117 B: x116 C: x116
There were issues with some computations A: x117 B: x117 C: x116
There were issues with some computations A: x117 B: x117 C: x117
There were issues with some computations A: x118 B: x117 C: x117
There were issues with some computations A: x118 B: x118 C: x117
There were issues with some computations A: x118 B: x118 C: x118
There were issues with some computations A: x119 B: x118 C: x118
There were issues with some computations A: x119 B: x119 C: x118
There were issues with some computations A: x119 B: x119 C: x119
There were issues with some computations A: x120 B: x119 C: x119
There were issues with some computations A: x120 B: x120 C: x119
There were issues with some computations A: x120 B: x120 C: x120
There were issues with some computations A: x121 B: x120 C: x120
There were issues with some computations A: x121 B: x121 C: x120
There were issues with some computations A: x121 B: x121 C: x121
There were issues with some computations A: x122 B: x121 C: x121
There were issues with some computations A: x122 B: x122 C: x121
There were issues with some computations A: x122 B: x122 C: x122
There were issues with some computations A: x123 B: x122 C: x122
There were issues with some computations A: x123 B: x123 C: x122
There were issues with some computations A: x123 B: x123 C: x123
There were issues with some computations A: x124 B: x123 C: x123
There were issues with some computations A: x124 B: x124 C: x123
There were issues with some computations A: x124 B: x124 C: x124
There were issues with some computations A: x125 B: x124 C: x124
There were issues with some computations A: x125 B: x125 C: x124
There were issues with some computations A: x125 B: x125 C: x125
There were issues with some computations A: x125 B: x125 C: x125
# model results
show_best(rf_results, metric = "roc_auc")
NA
rf_best_hyperparameters <- select_best(rf_results, metric = "roc_auc")
# create final workflow object
final_rf_wf <- workflow() %>%
add_recipe(bag_recipe) %>%
add_model(rf_mod) %>%
finalize_workflow(rf_best_hyperparameters)
# fit final workflow object
rf_final_fit <- final_rf_wf %>%
fit(data = bag_train)
# plot feature importance
rf_final_fit %>%
extract_fit_parsnip() %>%
vip(num_features = 20)
#End RF
In the random forest model you can see that tenure it the most important feature in this model.
#Start Mars
mars_mod <- mars(
mode='classification',
num_terms = tune(),
prod_degree = tune())
hyper_grid <- grid_regular(
num_terms(range = c(10, 50)),
prod_degree(),
levels = 10
)
results <- tune_grid(mars_mod, bag_recipe, resamples = kfold, grid = hyper_grid)
show_best(results, metric = "roc_auc")
NA
As show above the MARS model is the best at predicting.
best_hyperparameters <- select_best(results, metric = "roc_auc")
final_wf <- workflow() %>%
add_model(mars_mod) %>%
add_formula(Status ~ .) %>%
finalize_workflow(best_hyperparameters)
# plot top 20 influential variables
final_wf %>%
fit(data = bag_train) %>%
extract_fit_parsnip() %>%
vip(20)
#End Mars
confus_matrix <- logistic_reg() %>%
fit(Status ~ ., data = bag_train)
confus_matrix %>% predict(bag_test) %>%
bind_cols(bag_test %>% select(Status)) %>%
conf_mat(truth = Status, estimate = .pred_class)
Truth
Prediction Current Left
Current 1362 225
Left 178 332
head(predictions)
unique(predictions$.pred_class)
predictions <- final_wf %>%
last_fit(bag_split) %>%
collect_predictions()
at_risk_customers <- predictions %>%
filter(.pred_class == "Left")
print(at_risk_customers)
colnames(at_risk_customers)
[1] "Gender" "SeniorCitizen" "Partner" "Dependents" "Tenure"
[6] "PhoneService" "MultipleLines" "InternetService" "OnlineSecurity" "OnlineBackup"
[11] "DeviceProtection" "TechSupport" "StreamingTV" "StreamingMovies" "Contract"
[16] "PaperlessBilling" "PaymentMethod" "MonthlyCharges" "TotalCharges" "Status...20"
[21] ".pred_class" ".pred_Current" ".pred_Left" "id" ".row"
[26] "Status...26" ".config"
test_predictions <- bind_cols(bag_test, predictions)
New names:
• `Status` -> `Status...20`
• `Status` -> `Status...26`
at_risk_customers <- test_predictions %>%
filter(.pred_class == "Left")
predicted_loss <- at_risk_customers %>%
summarize(total_loss = sum(MonthlyCharges, na.rm = TRUE))
print(predicted_loss)
NA
NA
NA
Looking at Tenure which is our most influential variable shows the importance of targeting customers who have been with the company the longest. Strategies for keeping this retention could include offering perks and promotions when customers have been with Regork for a certain amount of time. This will build the amount of loyal customers Regork has.
Another very influential factor is Contract Length. Looking at our models it is easy to see customers with longer contracts have greater retention rates then those with shorter contracts. Having ads and promotions targeted to those with shorter contracts to switch to longer contracts may be helpful in keeping these customers.
Both Total and Monthly Charges were found to be influential variables. This showed that customers who spend the most money have the most faith in Regork. It is important to us as a company to keep these loyal customers, as they are responsible for a great amount of Regork’s profits. A good way to do this would be to offer them promotions for their loyalty.
Using an Electronic Check as your payment method was also an influential aspect. This could be because of the increase in core customers trusting and having access to this technology. This is another influential demographic to focus effort on.
If nothing is done we could be looking at a loss of $37,228.65 per month meaning a yearly loss of $446,743.80 This would be a fairly large blow to the company that would only continue to snowball. So Overall we propose providing a 20% discount to customers that are at risk and also provide incentives to month to month contract customers to attempt to lock them into a longer term deal.
Limitations we face are that we only have some variables. We are lacking family statistics that could potentially show how having multiple people in a household could lead to streaming services being more relevant as well as how charges reflect the size. Other limitations faced could be timing aspects as the only timing aspect present is tenure, but our prediction could be strengthened by customers who have left over a longer time period.