#Librarias

library(tidyverse)
library(magrittr)
library(skimr)
library(janitor)
library(tidymodels)
library(ranger)
library(xgboost)
library(vip)

#Cargar datos

data <- read_csv('Data/WA_Fn-UseC_-Telco-Customer-Churn.csv',
                 show_col_types = FALSE)

#Verificar y corregir columnas

Verificar base de datos con glimpse

data %>% 
        glimpse
## Rows: 7,043
## Columns: 21
## $ customerID       <chr> "7590-VHVEG", "5575-GNVDE", "3668-QPYBK", "7795-CFOCW…
## $ gender           <chr> "Female", "Male", "Male", "Male", "Female", "Female",…
## $ SeniorCitizen    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ Partner          <chr> "Yes", "No", "No", "No", "No", "No", "No", "No", "Yes…
## $ Dependents       <chr> "No", "No", "No", "No", "No", "No", "Yes", "No", "No"…
## $ tenure           <dbl> 1, 34, 2, 45, 2, 8, 22, 10, 28, 62, 13, 16, 58, 49, 2…
## $ PhoneService     <chr> "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", …
## $ MultipleLines    <chr> "No phone service", "No", "No", "No phone service", "…
## $ InternetService  <chr> "DSL", "DSL", "DSL", "DSL", "Fiber optic", "Fiber opt…
## $ OnlineSecurity   <chr> "No", "Yes", "Yes", "Yes", "No", "No", "No", "Yes", "…
## $ OnlineBackup     <chr> "Yes", "No", "Yes", "No", "No", "No", "Yes", "No", "N…
## $ DeviceProtection <chr> "No", "Yes", "No", "Yes", "No", "Yes", "No", "No", "Y…
## $ TechSupport      <chr> "No", "No", "No", "Yes", "No", "No", "No", "No", "Yes…
## $ StreamingTV      <chr> "No", "No", "No", "No", "No", "Yes", "Yes", "No", "Ye…
## $ StreamingMovies  <chr> "No", "No", "No", "No", "No", "Yes", "No", "No", "Yes…
## $ Contract         <chr> "Month-to-month", "One year", "Month-to-month", "One …
## $ PaperlessBilling <chr> "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes", "No", …
## $ PaymentMethod    <chr> "Electronic check", "Mailed check", "Mailed check", "…
## $ MonthlyCharges   <dbl> 29.85, 56.95, 53.85, 42.30, 70.70, 99.65, 89.10, 29.7…
## $ TotalCharges     <dbl> 29.85, 1889.50, 108.15, 1840.75, 151.65, 820.50, 1949…
## $ Churn            <chr> "No", "No", "Yes", "No", "Yes", "Yes", "No", "No", "Y…

Verificar base de datos y convertir en factor

data %>% 
        summarise_all(list(
                .n=~sum(!is.na(.)),
                .na=~sum(is.na(.)),
                .min=~min(.,na.rm = T),
                .max=~max(.,na.rm = T),
                .clase=~class(.),
                .valor_distinto=~n_distinct(.)
        )) %>% mutate(across(everything(),~as.character(.))) %>% 
        pivot_longer(everything(),
                     names_to = c("varible",".value"),
                     names_sep = c("_\\.")) %>% gt::gt()
varible n na min max clase valor_distinto
customerID 7043 0 0002-ORFBO 9995-HOTOH character 7043
gender 7043 0 Female Male character 2
SeniorCitizen 7043 0 0 1 numeric 2
Partner 7043 0 No Yes character 2
Dependents 7043 0 No Yes character 2
tenure 7043 0 0 72 numeric 73
PhoneService 7043 0 No Yes character 2
MultipleLines 7043 0 No Yes character 3
InternetService 7043 0 DSL No character 3
OnlineSecurity 7043 0 No Yes character 3
OnlineBackup 7043 0 No Yes character 3
DeviceProtection 7043 0 No Yes character 3
TechSupport 7043 0 No Yes character 3
StreamingTV 7043 0 No Yes character 3
StreamingMovies 7043 0 No Yes character 3
Contract 7043 0 Month-to-month Two year character 3
PaperlessBilling 7043 0 No Yes character 2
PaymentMethod 7043 0 Bank transfer (automatic) Mailed check character 4
MonthlyCharges 7043 0 18.25 118.75 numeric 1585
TotalCharges 7032 11 18.8 8684.8 numeric 6531
Churn 7043 0 No Yes character 2
#Convertir a factor
data %>%
  mutate( Churn = factor(Churn,
  levels= c("Yes","No"),
  labels= c("si", "no"))
  ) -> data


data %>%
  mutate(
    SeniorCitizen = factor(SeniorCitizen, 
                           levels = c(0, 1), labels = c('no', 'si')),
    gender = factor(gender,
                    levels = c('Female', 'Male'), labels = c('Mujeres', 'Hombres')),
    Partner = factor(Partner,
                     levels = c('Yes', 'No'), labels = c('si', 'No')),
    Dependents = factor(Dependents,
                        levels = c('Yes', 'No'), labels = c('si', 'No')),
    PhoneService = factor(PhoneService,
                          levels = c('Yes', 'No'), labels = c('si', 'No')),
    PaperlessBilling = factor(PaperlessBilling,
                              levels = c('Yes', 'No'), labels = c('si', 'No')),
    MultipleLines = factor(MultipleLines, 
                           levels = c('Yes', 'No', "No phone service"), labels = c('si', 'no', 'sin servicio')),
    InternetService = factor(InternetService, 
                             levels = c('DSL', 'Fiber optic', 'No'), labels = c('DSL', 'Fibra optica', 'No')),
    OnlineSecurity = factor(OnlineSecurity,
                            levels = c('No', 'Yes', 'No internet service'), labels = c('No', 'Yes', 'sin servicio')),
    OnlineBackup = factor(OnlineBackup, 
                          levels = c('Yes', 'No', 'No internet service'), labels = c('si', 'no', 'sin servicio')),
    DeviceProtection = factor(DeviceProtection, 
                              levels = c('Yes', 'No', 'No internet service'), labels = c('si', 'no', 'sin servicio')),
    TechSupport = factor(TechSupport, 
                         levels = c('Yes', 'No', 'No internet service'), labels = c('si', 'no', 'sin servicio')),
    StreamingTV = factor(StreamingTV, 
                         levels = c('Yes', 'No', 'No internet service'), labels = c('si', 'no', 'sin servicio')),
    StreamingMovies = factor(StreamingMovies, 
                             levels = c('Yes', 'No', 'No internet service'), labels = c('si', 'no', 'sin servicio')),
    Contract = factor(Contract, 
                      levels = c('Month-to-month', 'One year', 'Two year'), labels = c('mes a mes', 'un año', 'dos años')),
    PaymentMethod = factor(PaymentMethod, 
                           levels = c('Electronic check', 'Mailed check', 'Bank transfer (automatic)', 'Credit card (automatic)'),
                           labels = c('cheque electronico', 'cheque mail', 'transferencia bancaria', 'transferencia automatica'))
  ) -> data

#EDA

EDA Univariado

Todas las variables

skim(data)
Data summary
Name data
Number of rows 7043
Number of columns 21
_______________________
Column type frequency:
character 1
factor 17
numeric 3
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
customerID 0 1 10 10 0 7043 0

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
gender 0 1 FALSE 2 Hom: 3555, Muj: 3488
SeniorCitizen 0 1 FALSE 2 no: 5901, si: 1142
Partner 0 1 FALSE 2 No: 3641, si: 3402
Dependents 0 1 FALSE 2 No: 4933, si: 2110
PhoneService 0 1 FALSE 2 si: 6361, No: 682
MultipleLines 0 1 FALSE 3 no: 3390, si: 2971, sin: 682
InternetService 0 1 FALSE 3 Fib: 3096, DSL: 2421, No: 1526
OnlineSecurity 0 1 FALSE 3 No: 3498, Yes: 2019, sin: 1526
OnlineBackup 0 1 FALSE 3 no: 3088, si: 2429, sin: 1526
DeviceProtection 0 1 FALSE 3 no: 3095, si: 2422, sin: 1526
TechSupport 0 1 FALSE 3 no: 3473, si: 2044, sin: 1526
StreamingTV 0 1 FALSE 3 no: 2810, si: 2707, sin: 1526
StreamingMovies 0 1 FALSE 3 no: 2785, si: 2732, sin: 1526
Contract 0 1 FALSE 3 mes: 3875, dos: 1695, un : 1473
PaperlessBilling 0 1 FALSE 2 si: 4171, No: 2872
PaymentMethod 0 1 FALSE 4 che: 2365, che: 1612, tra: 1544, tra: 1522
Churn 0 1 FALSE 2 no: 5174, si: 1869

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
tenure 0 1 32.37 24.56 0.00 9.00 29.00 55.00 72.00 ▇▃▃▃▆
MonthlyCharges 0 1 64.76 30.09 18.25 35.50 70.35 89.85 118.75 ▇▅▆▇▅
TotalCharges 11 1 2283.30 2266.77 18.80 401.45 1397.47 3794.74 8684.80 ▇▂▂▂▁

Posibles outliers

data %>% 
  reframe( 
    tibble(
      Descrip= c('P_0', 'P_02', 'P_25', 'P_50' , 'P_75', 'P_98', 'P_100') ,
      Valor= quantile( TotalCharges, c(0, 0.2, 0.25, 0.50 ,0.75, 0.98, 1), na.rm= T)
    )
    ) %>% 
        gt::gt()
Descrip Valor
P_0 18.800
P_02 267.070
P_25 401.450
P_50 1397.475
P_75 3794.738
P_98 7721.096
P_100 8684.800
#Media por Partner
data %>% 
        group_by(Partner) %>% 
        summarise(media=mean(TotalCharges,
                          na.rm =T )) %>% 
        gt::gt()
Partner media
si 3032.272
No 1584.960
#Distribuciòn de TotalCharges por Partner
data %>% 
        ggplot(aes(TotalCharges,
                   color=Partner))+
        geom_density()+
        scale_y_continuous(labels = scales::number_format())+
        theme_dark()

Existe una diferencia importante en los registros de dinero cobrado a los clientes al considerar aquellos que tienen pareja. Al revisar el gráfico anterior, se evidencia que las personas que informaron tener pareja son las que presentan los cobros más altos. Esto se refleja en la distribución, donde se observa una densidad mayor para valores altos en comparación con las personas que declararon no tener pareja.

EDA Bivariado

Churn vs SeniorCitizen

data %>%
  group_by(SeniorCitizen, Churn) %>%
  summarise(
    N = n(),
    Porc = round(100*N/nrow(data),2)
  ) %>%
  mutate(Porc_grupo = round(100*N/sum(N),2)) -> valores_Sc
## `summarise()` has grouped output by 'SeniorCitizen'. You can override using the
## `.groups` argument.
valores_Sc %>% 
        ungroup() %>% 
        gt::gt()
SeniorCitizen Churn N Porc Porc_grupo
no si 1393 19.78 23.61
no no 4508 64.01 76.39
si si 476 6.76 41.68
si no 666 9.46 58.32
# Comentado por conflictos con paquete
# tigerstats::rowPerc(xtabs(~SeniorCitizen+Churn, data=data) )

Churn vs SeniorCitizen vs Porc_grupo

ggplot(valores_Sc, aes(x = SeniorCitizen, y = Porc_grupo, fill = Churn)) +
  geom_col(stat = "identity", position = "dodge") +
  geom_text(aes(label = Porc_grupo), vjust = 1.5,
            position = position_dodge(.9))
## Warning in geom_col(stat = "identity", position = "dodge"): Ignoring unknown
## parameters: `stat`

De los adultos mayores el 41% abandonó el servicio, mientras que de los no adultos mayores apenas un 24% abandonó el servicio. Aparentemente, un adulto mayor tiene mayor probabilidad de abandonar el servicio.

Churn vs MultipleLines

valores_Ml <- data %>%
        count(Churn,MultipleLines) %>% 
        group_by(Churn) %>% 
        mutate(n_chur=sum(n),
               porcentaje_personas=n/n_chur) %>% 
        ungroup() 

valores_Ml %>% 
        gt::gt()%>% 
        gt::fmt_percent(columns=porcentaje_personas)
Churn MultipleLines n n_chur porcentaje_personas
si si 850 1869 45.48%
si no 849 1869 45.43%
si sin servicio 170 1869 9.10%
no si 2121 5174 40.99%
no no 2541 5174 49.11%
no sin servicio 512 5174 9.90%

######Gráfico

valores_Ml %>% 
        ggplot(aes(Churn, y = porcentaje_personas, fill = MultipleLines)) +
        geom_col(position = "dodge") +
        geom_text(aes(label = scales::percent(porcentaje_personas,accuracy = 0.01)), vjust = -0.8,
                  position = position_dodge(.9))

Entre las categorías de MultipleLines no hay mayor diferencia, entre los que abandonan o no el servicio. Los porcentajes son muy parecidos.

Churn vs TechSupport

data %>%
        count(Churn,TechSupport) %>% 
        group_by(Churn) %>% 
        mutate(n_chur=sum(n),
               porcentaje_personas=n/n_chur) %>% 
        ungroup() %>% 
        ggplot(aes(Churn,
                   porcentaje_personas,
                   fill = TechSupport)) +
        geom_col(position = "dodge") +
        geom_text(aes(label = scales::percent(porcentaje_personas,
                                              accuracy = 0.01)),
                  vjust = -0.8,
                  position = position_dodge(.9)) 

Las personas que se van de la compañía en promedio el 77.37% no tienen soporte técnico y solo el 16.59% de los que se van indican tener soporte técnico.

Churn vs PaymentMethod

data %>%
        count(Churn,PaymentMethod) %>% 
        group_by(Churn) %>% 
        mutate(n_chur=sum(n),
               porcentaje_personas=n/n_chur) %>% 
        ungroup() %>% 
        ggplot(aes(Churn,
                   porcentaje_personas,
                   fill = PaymentMethod)) +
        geom_col(position = "dodge") +
        geom_text(aes(label = scales::percent(porcentaje_personas,
                                              accuracy = 0.1)),
                  vjust = -0.8,
                  position = position_dodge(.9)) 

A excepción de los que pagan con cheque electrónico los porcentajes de abandono son muy parecidos. En el caso de los que pagan con cheque electrónico un 57.3% abandona el servicio.

Churn vs Dependents

data %>% tabyl(Churn, Dependents ) -> t1

t1 %>% adorn_totals(c("row", "col")) %>% adorn_percentages("all") %>%
adorn_pct_formatting(rounding = "half up", digits = 0) %>% adorn_ns() %>%
  adorn_title("combined") %>% knitr::kable()
Churn/Dependents si No Total
si 5% (326) 22% (1,543) 27% (1,869)
no 25% (1,784) 48% (3,390) 73% (5,174)
Total 30% (2,110) 70% (4,933) 100% (7,043)
data %>% count(Churn, Dependents) %>%
  mutate(porc = n / sum(n)) %>% 
  ggplot(aes(fill=Dependents, y=porc, x=Churn)) + 
    geom_col(position="stack") +
    geom_text(aes(label=scales::percent(porc)),position = position_stack(vjust=0.5))+
  scale_y_continuous(labels = scales::percent_format()) 

De la muestra analizada alrededor del 27% corresponde a individuos que dejaron sus planes en el último mes. Además se conoce que el 4,6% de estos individuos contaban con personas dependientes.

Churn vs OnlineSecurity

data %>% tabyl(Churn,OnlineSecurity ) -> t2

t2 %>% adorn_totals(c("row", "col")) %>% adorn_percentages("all") %>%
adorn_pct_formatting(rounding = "half up", digits = 0) %>% adorn_ns()%>% adorn_title("combined")%>% knitr::kable()
Churn/OnlineSecurity No Yes sin servicio Total
si 21% (1,461) 4% (295) 2% (113) 27% (1,869)
no 29% (2,037) 24% (1,724) 20% (1,413) 73% (5,174)
Total 50% (3,498) 29% (2,019) 22% (1,526) 100% (7,043)
data %>% count(Churn, OnlineSecurity) %>%
  mutate(porc = n / sum(n)) %>% 
  ggplot(aes(fill=OnlineSecurity, y=porc, x=Churn)) + 
    geom_col(position="stack") +
    geom_text(aes(label=scales::percent(porc)),position = position_stack(vjust=0.5))+
  scale_y_continuous(labels = scales::percent_format()) 

El 20,74% de los indviduos analizados y que abandonaron el servicio no contaban con seguridad en línea. Por otro lado aquellos que no salieron del plan reflejaron una participación similar con relación al servicio de seguridad en línea.

Churn vs StreamingMovies

data %>% tabyl(Churn,StreamingMovies) -> t3

t3 %>% adorn_totals(c("row", "col")) %>% adorn_percentages("all") %>%
adorn_pct_formatting(rounding = "half up", digits = 0) %>% adorn_ns()%>% adorn_title("combined")%>% knitr::kable()
Churn/StreamingMovies si no sin servicio Total
si 12% (818) 13% (938) 2% (113) 27% (1,869)
no 27% (1,914) 26% (1,847) 20% (1,413) 73% (5,174)
Total 39% (2,732) 40% (2,785) 22% (1,526) 100% (7,043)
data %>% count(Churn, StreamingMovies) %>%
  mutate(porc = n / sum(n)) %>% 
  ggplot(aes(fill=StreamingMovies, y=porc, x=Churn)) + 
    geom_col(position="stack") +
    geom_text(aes(label=scales::percent(porc)),position = position_stack(vjust=0.5))+
  scale_y_continuous(labels = scales::percent_format()) 

El 13,32% de los indviduos analizados y que abandonaron el servicio no contaban con servicio de Streaming Movies. Sin embargo, tanto para el grupo que abandonaron o mantuvieron el servicio no se evidencia una importancia relevante para su salidad del plan.

Churn vs Total Charges

data %>%  group_by(Churn) %>% 
  summarise(n = n(),
            promedio = mean(TotalCharges,na.rm = T),
            n_missing = sum(is.na(TotalCharges)),
            desv = sd(TotalCharges, na.rm =T)) -> t4
t4 %>% 
        gt::gt()
Churn n promedio n_missing desv
si 1869 1531.796 0 1890.823
no 5174 2555.344 11 2329.457
data %>%
  ggplot(aes(x = Churn, y = TotalCharges, fill = Churn)) +
  geom_boxplot() +
  stat_summary(fun = mean, geom = "point", shape = 3, size = 3,
               color = "white", position = position_dodge(width = 0.75)) +
  labs(title = "Churn vs Total Charges",
       x = "Churn",
       y = "Total Charges") +
        theme_minimal()

En promedio el gasto total en el servicio de telecomunicaciones para los individuos que salieron del plan es inferior por usuario en alrededor de USD 1000. Para los usuarios que salieron del servicio se evidencia datos atípicos elevados que alcanzan los valores máximos de los usarios que no salieron del servicio. Cabe señalar que, para los usuarios que no abandoron el servicio su extipendio total del plan se concentra entre el rango intercuartílico. Adicionalmente, se observa que la variable gasto total cuenta con 11 valores perdidos.

EDA Multivariado

Churn vs MonthlyCharges vs PaymentMethod

ggplot(data, aes(x = Churn, y = MonthlyCharges, fill = PaymentMethod)) +
  geom_boxplot()

Los montos de pago de aquellos que abandonan el servicio son superiores a aquellos que permanecen con excepción de los que pagan por cheque electrónico.

Churn vs MonthlyCharges vs Dependents

data %>%  group_by(Churn,Dependents) %>% 
    summarise(n = n(),
            promedio = mean(MonthlyCharges,na.rm = T),
            n_missing = sum(is.na(MonthlyCharges)),
            desv = sd(MonthlyCharges, na.rm =T),
            .groups="drop")  -> t5
t5 %>% 
        gt::gt() %>% 
        gt::fmt_number(columns = c(promedio,desv),
                       decimals = 2)
Churn Dependents n promedio n_missing desv
si si 326 72.87 0 25.76
si No 1543 74.77 0 24.42
no si 1784 57.08 0 31.61
no No 3390 63.47 0 30.59
data %>%
  ggplot(aes(x = Churn, y = MonthlyCharges, fill = Dependents)) +
  geom_boxplot() +
  stat_summary(fun = mean, geom = "point", shape = 3, size = 3, color = "white", position = position_dodge(width = 0.75)) +
  labs(title = "Monthtly Charges for Dependents vs Churn",
       x = "Churn",
       y = "Monthly Charges")

La distribución del gasto mensual de aquellos individuos que no salieron del plan se concentra dentro del cuartil 1 y cuartil 3, además entre el máximo y mínimo de los que dejaron el servicio respecto de los que se quedaron no se verifica mayor diferencia. Es importante destacar que, tanto el promedio como la mediana del pago mensual es superior en los individuos que salieron del plan de los que se quedaron, no obstante no se verifica mayor diferencia respecto de contar o no con dependientes en los usuarios que abandonaron el plan.

Matriz de Correlación

data %>%  select_if(where(is.numeric)) %>% GGally::ggpairs()

Según el gráfico anterior existe una alta correlación positiva entre las variables TotalChanges y tenure, con 0.826, seguida de la correlación entre las variables TotalChages y MonthyChanges, con una correlación moderada de 0.651.

Revisión del balanceo de datos

data %>% 
  group_by(Churn) %>% 
  count(name = "frec") %>%
        ungroup() %>%
        mutate( Porc= frec/sum(frec)) %>% 
        gt::gt() %>% 
        gt::fmt_percent(columns=Porc)
Churn frec Porc
si 1869 26.54%
no 5174 73.46%
data %>%
group_by(Churn) %>%
count( name = 'frec') %>%
ungroup() %>%
mutate( Porc= frec/sum(frec)) %>%
ggplot( aes(x= Churn, y= Porc)) +
geom_segment( aes(xend= Churn, y=0, yend=Porc),
color= "steelblue", linewidth= 1) +
geom_point( size=5, color= "steelblue") +
coord_flip() +
scale_y_continuous( labels = percent_format()) +
labs(title= 'Porcentaje de Clientes que Abandonan',
y= "Porcentaje", x= "Churn") +
theme_bw()

El porcentaje de clientes que abandonan el servicio de telecomunicaciones es aproximadamente 3 a 1 en el muestra de análisis.

MODELAMIENTO

Train - Test Split

#data %>% select(-customerID) -> data

set.seed(1234) # Semilla para aleatorios
split <- data %>%
initial_split(
prop = 0.8, # Porcentaje al train
strata = Churn # Estratificación del muestreo
)

Data de entrenamiento

train <- training(split)
dim(train)
## [1] 5634   21

Data de prueba (test)

test <- testing(split)
dim(test)
## [1] 1409   21

Preprocesamiento

Receipe y Balanceo de datos

receta <- train %>%
recipe(Churn ~ . ) %>% ## Crea la receta
## Eliminar variables que no usaremos
step_rm(customerID) %>%
## Crear nuevas variables (insight desde el EDA)
# step_mutate( account_length_anio= account_length/12 )
## Imputar los datos
# step_impute_mean()
step_impute_knn(TotalCharges ) %>%
## Estandarizacion/Normalizacion de numericas
step_normalize( all_numeric(), -all_outcomes()) %>%
## Crear una categoría "otros" que agrupe a categorias pequeñas
step_other(all_nominal(), -all_outcomes() , threshold = 0.07, other = "otros") %>%
## Crear una categoría "new" para observaciones con labels "no muestreados"
step_novel(all_nominal(), -all_outcomes() , new_level = "new") %>%
## Crear variables indicadoras para cada categoría
step_dummy(all_nominal(), -all_outcomes() ) %>% # Dummy
## Eliminar automáticamente variables con alta correlacion
## para evitar la multicolinealidad xi ~ xj
# step_corr(all_numeric(), -all_outcomes(), threshold = 0.9) %>%
## Tambien podemos eliminar variables con multicolinealidad "a mano"
#step_rm(total_day_charge, total_eve_charge,
#total_night_charge, total_intl_charge) %>% # Eliminar
## Eliminar columnas con varianza cercana a cero
step_nzv(all_predictors()) %>% 
themis::step_upsample(Churn, over_ratio = 0.9, skip= TRUE, seed= 123)  

receta
## 
## ── Recipe ──────────────────────────────────────────────────────────────────────
## 
## ── Inputs
## Number of variables by role
## outcome:    1
## predictor: 20
## 
## ── Operations
## • Variables removed: customerID
## • K-nearest neighbor imputation for: TotalCharges
## • Centering and scaling for: all_numeric() and -all_outcomes()
## • Collapsing factor levels for: all_nominal() and -all_outcomes()
## • Novel factor level assignment for: all_nominal() and -all_outcomes()
## • Dummy variables from: all_nominal() and -all_outcomes()
## • Sparse, unbalanced variable filter on: all_predictors()
## • Up-sampling based on: Churn

Entrenamiento y ajuste de Hiperparámetro

set.seed(1234)
cv <- vfold_cv(train, v = 5, repeats = 1, strata = Churn)
cv 
## #  5-fold cross-validation using stratification 
## # A tibble: 5 × 2
##   splits              id   
##   <list>              <chr>
## 1 <split [4507/1127]> Fold1
## 2 <split [4507/1127]> Fold2
## 3 <split [4507/1127]> Fold3
## 4 <split [4507/1127]> Fold4
## 5 <split [4508/1126]> Fold5

Métricas

metricas <- metric_set(accuracy, sens, spec, bal_accuracy)
metricas
## A metric set, consisting of:
## - `accuracy()`, a class metric     | direction: maximize
## - `sens()`, a class metric         | direction: maximize
## - `spec()`, a class metric         | direction: maximize
## - `bal_accuracy()`, a class metric | direction: maximize

Modelamiento - Random Forest

Especificacion del modelo*

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

rf_sp
## Random Forest Model Specification (classification)
## 
## Main Arguments:
##   mtry = tune()
##   trees = tune()
##   min_n = tune()
## 
## Engine-Specific Arguments:
##   importance = impurity
## 
## Computational engine: ranger

Work Flow

rf_wflow <-
  workflow() %>%
  add_recipe(receta) %>%
  add_model(rf_sp) 
  rf_wflow
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: rand_forest()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## Random Forest Model Specification (classification)
## 
## Main Arguments:
##   mtry = tune()
##   trees = tune()
##   min_n = tune()
## 
## Engine-Specific Arguments:
##   importance = impurity
## 
## Computational engine: ranger

Afinamiento de Hiperpárametros

set.seed(123)
rf_grid <- rf_sp %>%
## preguntamos los parametros tuneables del modelo
parameters() %>%
## Vamos a definir un rango para el min_n y mtry
update(min_n= min_n( range= c(70, 170)),
mtry= mtry( range= c(4, 7))) %>%
grid_latin_hypercube(size = 10) #preguntar como se construye la malla

rf_grid %>% 
        gt::gt()
mtry trees min_n
5 70 144
6 467 91
6 1737 158
6 1124 162
7 773 80
4 1370 116
6 950 130
5 325 83
5 1904 126
5 1441 106

La data luego de aplicar la receta registra 31 variables, por tanto, se utiliza como criterio para establecer el hiperpárametro “mtry” la raíz cuadrada del número de variables analizadas siendo esta igual a 5,56. Con la finalidad de contar con un rango para la elección de variables se establece entre 4 a 7.

###Paralelización

#parallel::detectCores(logical=FALSE)

Entrenamiento de Malla de Búsqueda en la Crossvalidation

set.seed(123)
rf_tuned <- tune_grid(
        rf_wflow, ## Modelo
        resamples= cv, ## Crossvalidation
        grid = rf_grid, ## Malla de Busqueda
        metrics = metricas, ## Metricas
        control= control_grid(allow_par = T, save_pred = T) ## Paralel y Pred
)
## Warning: package 'themis' was built under R version 4.3.3
rf_tuned
## # Tuning results
## # 5-fold cross-validation using stratification 
## # A tibble: 5 × 5
##   splits              id    .metrics          .notes           .predictions
##   <list>              <chr> <list>            <list>           <list>      
## 1 <split [4507/1127]> Fold1 <tibble [40 × 7]> <tibble [0 × 3]> <tibble>    
## 2 <split [4507/1127]> Fold2 <tibble [40 × 7]> <tibble [0 × 3]> <tibble>    
## 3 <split [4507/1127]> Fold3 <tibble [40 × 7]> <tibble [0 × 3]> <tibble>    
## 4 <split [4507/1127]> Fold4 <tibble [40 × 7]> <tibble [0 × 3]> <tibble>    
## 5 <split [4508/1126]> Fold5 <tibble [40 × 7]> <tibble [0 × 3]> <tibble>

Evaluación de modelos Evaluamos que modelo resulto mejor

show_best(rf_tuned, metric = 'accuracy', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
5 325 83 accuracy binary 76.82% 5 0.009591922 Preprocessor1_Model08
4 1370 116 accuracy binary 76.82% 5 0.008914298 Preprocessor1_Model06
6 950 130 accuracy binary 76.78% 5 0.010440599 Preprocessor1_Model07
7 773 80 accuracy binary 76.78% 5 0.008703191 Preprocessor1_Model05
6 467 91 accuracy binary 76.68% 5 0.008892465 Preprocessor1_Model02
5 1904 126 accuracy binary 76.68% 5 0.010000934 Preprocessor1_Model09
5 1441 106 accuracy binary 76.62% 5 0.010255550 Preprocessor1_Model10
6 1124 162 accuracy binary 76.62% 5 0.009952380 Preprocessor1_Model04
6 1737 158 accuracy binary 76.62% 5 0.009770258 Preprocessor1_Model03
5 70 144 accuracy binary 76.41% 5 0.009384949 Preprocessor1_Model01
show_best(rf_tuned, metric = 'sens', n = 10) %>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1370 116 sens binary 74.11% 5 0.015743950 Preprocessor1_Model06
6 1737 158 sens binary 73.78% 5 0.011517843 Preprocessor1_Model03
6 1124 162 sens binary 73.78% 5 0.012361016 Preprocessor1_Model04
5 70 144 sens binary 73.71% 5 0.013192698 Preprocessor1_Model01
5 1904 126 sens binary 73.71% 5 0.013192698 Preprocessor1_Model09
6 950 130 sens binary 73.38% 5 0.012761728 Preprocessor1_Model07
5 1441 106 sens binary 73.24% 5 0.013708295 Preprocessor1_Model10
5 325 83 sens binary 72.84% 5 0.013268786 Preprocessor1_Model08
6 467 91 sens binary 72.64% 5 0.009364548 Preprocessor1_Model02
7 773 80 sens binary 72.17% 5 0.009830728 Preprocessor1_Model05
show_best(rf_tuned, metric = 'spec', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
7 773 80 spec binary 78.45% 5 0.008401188 Preprocessor1_Model05
5 325 83 spec binary 78.26% 5 0.008397753 Preprocessor1_Model08
6 467 91 spec binary 78.14% 5 0.008873514 Preprocessor1_Model02
6 950 130 spec binary 78.01% 5 0.010178449 Preprocessor1_Model07
5 1441 106 spec binary 77.85% 5 0.009223959 Preprocessor1_Model10
4 1370 116 spec binary 77.80% 5 0.007230052 Preprocessor1_Model06
5 1904 126 spec binary 77.75% 5 0.009297852 Preprocessor1_Model09
6 1124 162 spec binary 77.65% 5 0.009590338 Preprocessor1_Model04
6 1737 158 spec binary 77.65% 5 0.009384198 Preprocessor1_Model03
5 70 144 spec binary 77.39% 5 0.008428486 Preprocessor1_Model01
show_best(rf_tuned, metric = 'bal_accuracy', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1370 116 bal_accuracy binary 75.96% 5 0.010880902 Preprocessor1_Model06
5 1904 126 bal_accuracy binary 75.73% 5 0.010856210 Preprocessor1_Model09
6 1124 162 bal_accuracy binary 75.72% 5 0.010525350 Preprocessor1_Model04
6 1737 158 bal_accuracy binary 75.72% 5 0.010231820 Preprocessor1_Model03
6 950 130 bal_accuracy binary 75.70% 5 0.010955612 Preprocessor1_Model07
5 325 83 bal_accuracy binary 75.55% 5 0.010722115 Preprocessor1_Model08
5 70 144 bal_accuracy binary 75.55% 5 0.010457200 Preprocessor1_Model01
5 1441 106 bal_accuracy binary 75.54% 5 0.011282778 Preprocessor1_Model10
6 467 91 bal_accuracy binary 75.39% 5 0.008979525 Preprocessor1_Model02
7 773 80 bal_accuracy binary 75.31% 5 0.009022861 Preprocessor1_Model05

Revisando los resultados de las métricas de evaluación del modelo y considerando que es de interés para le empresa contar con la mejor estimación de los individuos que realmente abandonan el servicio se selecciona como referencia los hiperpárametros del modelo número 6 que presenta los mejores resultados en sensibilidad, para ajustar el modelo final.

Nueva Malla de Búsqueda

set.seed(123)

rf_grid_2 <- crossing(
  min_n = seq(114, 118, 2),
  mtry = c(4, 5),
  trees= seq(1270, 1570, 100)
)

rf_grid_2%>% 
        gt::gt()
min_n mtry trees
114 4 1270
114 4 1370
114 4 1470
114 4 1570
114 5 1270
114 5 1370
114 5 1470
114 5 1570
116 4 1270
116 4 1370
116 4 1470
116 4 1570
116 5 1270
116 5 1370
116 5 1470
116 5 1570
118 4 1270
118 4 1370
118 4 1470
118 4 1570
118 5 1270
118 5 1370
118 5 1470
118 5 1570

Entrenamiento de Malla de Busqueda en la Crossvalidation

set.seed(123)
rf_tuned_2 <- tune_grid(
  rf_wflow, ## Modelo
  resamples= cv, ## Crossvalidation
  grid = rf_grid_2, ## Malla de Busqueda
  metrics = metricas, ## Metricas
  control= control_grid(allow_par = T, save_pred = T) ## Paralel y Pred
)
rf_tuned_2
## # Tuning results
## # 5-fold cross-validation using stratification 
## # A tibble: 5 × 5
##   splits              id    .metrics          .notes           .predictions
##   <list>              <chr> <list>            <list>           <list>      
## 1 <split [4507/1127]> Fold1 <tibble [96 × 7]> <tibble [0 × 3]> <tibble>    
## 2 <split [4507/1127]> Fold2 <tibble [96 × 7]> <tibble [0 × 3]> <tibble>    
## 3 <split [4507/1127]> Fold3 <tibble [96 × 7]> <tibble [0 × 3]> <tibble>    
## 4 <split [4507/1127]> Fold4 <tibble [96 × 7]> <tibble [0 × 3]> <tibble>    
## 5 <split [4508/1126]> Fold5 <tibble [96 × 7]> <tibble [0 × 3]> <tibble>

Evaluamos que modelo resulto mejor de la segunda grilla de hiperpárametros

show_best(rf_tuned_2, metric = 'accuracy', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1470 116 accuracy binary 76.84% 5 0.009517598 Preprocessor1_Model11
4 1270 118 accuracy binary 76.77% 5 0.008798099 Preprocessor1_Model17
5 1270 114 accuracy binary 76.75% 5 0.009485337 Preprocessor1_Model05
4 1270 116 accuracy binary 76.75% 5 0.009196969 Preprocessor1_Model09
4 1570 118 accuracy binary 76.75% 5 0.008753040 Preprocessor1_Model20
4 1470 118 accuracy binary 76.73% 5 0.010074315 Preprocessor1_Model19
4 1570 116 accuracy binary 76.73% 5 0.009373133 Preprocessor1_Model12
5 1470 114 accuracy binary 76.71% 5 0.009529661 Preprocessor1_Model07
4 1370 118 accuracy binary 76.71% 5 0.009867331 Preprocessor1_Model18
5 1370 118 accuracy binary 76.71% 5 0.010002056 Preprocessor1_Model22
show_best(rf_tuned_2, metric = 'sens', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1570 116 sens binary 73.85% 5 0.01400695 Preprocessor1_Model12
4 1470 118 sens binary 73.78% 5 0.01633669 Preprocessor1_Model19
4 1270 116 sens binary 73.71% 5 0.01516493 Preprocessor1_Model09
4 1370 116 sens binary 73.71% 5 0.01592058 Preprocessor1_Model10
4 1270 118 sens binary 73.71% 5 0.01389470 Preprocessor1_Model17
4 1370 114 sens binary 73.58% 5 0.01374903 Preprocessor1_Model02
4 1570 114 sens binary 73.58% 5 0.01453984 Preprocessor1_Model04
5 1470 114 sens binary 73.58% 5 0.01246913 Preprocessor1_Model07
4 1470 116 sens binary 73.58% 5 0.01586427 Preprocessor1_Model11
4 1470 114 sens binary 73.51% 5 0.01396697 Preprocessor1_Model03
show_best(rf_tuned_2, metric = 'spec', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1470 116 spec binary 78.01% 5 0.007626337 Preprocessor1_Model11
5 1370 118 spec binary 77.97% 5 0.009307187 Preprocessor1_Model22
4 1370 118 spec binary 77.94% 5 0.008514508 Preprocessor1_Model18
4 1570 118 spec binary 77.94% 5 0.007638444 Preprocessor1_Model20
5 1270 114 spec binary 77.92% 5 0.008828907 Preprocessor1_Model05
5 1570 118 spec binary 77.89% 5 0.008602431 Preprocessor1_Model24
5 1470 116 spec binary 77.87% 5 0.009168637 Preprocessor1_Model15
4 1270 118 spec binary 77.87% 5 0.007378859 Preprocessor1_Model17
5 1470 114 spec binary 77.85% 5 0.008993232 Preprocessor1_Model07
4 1570 114 spec binary 77.85% 5 0.008682746 Preprocessor1_Model04
show_best(rf_tuned_2, metric = 'bal_accuracy', n = 10)%>% 
        gt::gt() %>% 
        gt::fmt_percent(columns = mean,
                        decimals = 2)
mtry trees min_n .metric .estimator mean n std_err .config
4 1570 116 bal_accuracy binary 75.81% 5 0.01071294 Preprocessor1_Model12
4 1470 116 bal_accuracy binary 75.80% 5 0.01143105 Preprocessor1_Model11
4 1270 118 bal_accuracy binary 75.79% 5 0.01029785 Preprocessor1_Model17
4 1470 118 bal_accuracy binary 75.79% 5 0.01191147 Preprocessor1_Model19
4 1270 116 bal_accuracy binary 75.78% 5 0.01100681 Preprocessor1_Model09
4 1370 116 bal_accuracy binary 75.74% 5 0.01129458 Preprocessor1_Model10
5 1270 114 bal_accuracy binary 75.71% 5 0.01034855 Preprocessor1_Model05
5 1470 114 bal_accuracy binary 75.71% 5 0.01027413 Preprocessor1_Model07
4 1570 114 bal_accuracy binary 75.71% 5 0.01129857 Preprocessor1_Model04
4 1570 118 bal_accuracy binary 75.69% 5 0.01028621 Preprocessor1_Model20

Se verifica que el modelo obtenido de la primera malla de hiperparámetros tiene mejores resultados en sensibilidad que la segunda grilla, por tanto se selecciona el mejor modelo del primer ejercicio. ### Modelo final

## Definir la mejor combinacion
rf_pars_fin <- select_best(rf_tuned, metric = 'sens')
## Finalizar (darle valores a parametros tuneables) el workflow
rf_wflow_fin <-
rf_wflow %>%
  finalize_workflow(rf_pars_fin)
rf_wflow_fin
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: rand_forest()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## Random Forest Model Specification (classification)
## 
## Main Arguments:
##   mtry = 4
##   trees = 1370
##   min_n = 116
## 
## Engine-Specific Arguments:
##   importance = impurity
## 
## Computational engine: ranger

Entrenamiento del modelo

Entrenar el modelo final

rf_fitted <- fit(rf_wflow_fin, train)
rf_fitted
## ══ Workflow [trained] ══════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: rand_forest()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## Ranger result
## 
## Call:
##  ranger::ranger(x = maybe_data_frame(x), y = y, mtry = min_cols(~4L,      x), num.trees = ~1370L, min.node.size = min_rows(~116L, x),      importance = ~"impurity", num.threads = 1, verbose = FALSE,      seed = sample.int(10^5, 1), probability = TRUE) 
## 
## Type:                             Probability estimation 
## Number of trees:                  1370 
## Sample size:                      7864 
## Number of independent variables:  30 
## Mtry:                             4 
## Target node size:                 116 
## Variable importance mode:         impurity 
## Splitrule:                        gini 
## OOB prediction error (Brier s.):  0.1478207

Modelo sin workflow

rf_model_fin <- extract_fit_parsnip(rf_fitted)

Evaluación del Modelo

Evaluación en la data de entrenamiento

train %>%
predict(rf_fitted , new_data = . ) %>%
mutate(Real= train$Churn) %>%
conf_mat(truth = Real, estimate = .pred_class ) %>%
summary
## # A tibble: 13 × 3
##    .metric              .estimator .estimate
##    <chr>                <chr>          <dbl>
##  1 accuracy             binary         0.793
##  2 kap                  binary         0.523
##  3 sens                 binary         0.791
##  4 spec                 binary         0.793
##  5 ppv                  binary         0.580
##  6 npv                  binary         0.913
##  7 mcc                  binary         0.536
##  8 j_index              binary         0.584
##  9 bal_accuracy         binary         0.792
## 10 detection_prevalence binary         0.362
## 11 precision            binary         0.580
## 12 recall               binary         0.791
## 13 f_meas               binary         0.669

Evaluación en la data de prueba

test %>%
predict(rf_fitted, new_data = . ) %>%
mutate(Real= test$Churn) %>%
  conf_mat(truth = Real, estimate = .pred_class ) %>%
summary
## # A tibble: 13 × 3
##    .metric              .estimator .estimate
##    <chr>                <chr>          <dbl>
##  1 accuracy             binary         0.779
##  2 kap                  binary         0.480
##  3 sens                 binary         0.722
##  4 spec                 binary         0.8  
##  5 ppv                  binary         0.566
##  6 npv                  binary         0.888
##  7 mcc                  binary         0.487
##  8 j_index              binary         0.522
##  9 bal_accuracy         binary         0.761
## 10 detection_prevalence binary         0.339
## 11 precision            binary         0.566
## 12 recall               binary         0.722
## 13 f_meas               binary         0.635

¿Que variables pueden estar relacionadas más con el abandono de clientes?

library(vip)
rf_model_fin %>%
  vip(geom = "point")

Modelo Boosting XGBoost*

Como parte del ejercicio de selección del mejor modelo para la determinación de la mejor estrategia para retención de clientes, se utiliza el algoritmo XGboost con la finalidad de elegir el modelo con mayor poder predictivo.

Para tal efecto, la aplicación del algoritmo XGboost inicia a partir de la receta establecida para los datos de abandono de clientes del servicio de telecomunicaciones.

Especificación del modelo

xgb_sp <- boost_tree(mtry = tune(), trees = tune(),
  loss_reduction = tune(), learn_rate= tune() ) %>%
  set_engine("xgboost") %>%
  set_mode("classification")

xgb_sp %>%
  translate()
## Boosted Tree Model Specification (classification)
## 
## Main Arguments:
##   mtry = tune()
##   trees = tune()
##   learn_rate = tune()
##   loss_reduction = tune()
## 
## Computational engine: xgboost 
## 
## Model fit template:
## parsnip::xgb_train(x = missing_arg(), y = missing_arg(), weights = missing_arg(), 
##     colsample_bynode = tune(), nrounds = tune(), eta = tune(), 
##     gamma = tune(), nthread = 1, verbose = 0)

Afinamiento de Malla de búsqueda

Previo a establecer la malla de búsqueda el algoritmo requiere la data incorporada las funciones establecidas en la receta, por tal motivo se aplica prep y bake. Posteriormente, se obtiene la malla de búsqueda

receta_prep = prep(receta, train)
finalize(mtry(), bake(receta_prep, new_data = NULL ))
## # Randomly Selected Predictors (quantitative)
## Range: [1, 31]
set.seed(123)
xgb_grid <- xgb_sp %>%
  parameters() %>%
  finalize(bake(receta_prep, new_data = NULL)) %>%
  grid_latin_hypercube(size = 20)

xgb_grid
## # A tibble: 20 × 4
##     mtry trees learn_rate loss_reduction
##    <int> <int>      <dbl>          <dbl>
##  1     8   340    0.00151       5.11e- 4
##  2     9   514    0.00628       8.18e- 2
##  3     6  1881    0.00200       6.04e- 1
##  4    28  1318    0.169         5.69e- 9
##  5    14  1595    0.0503        1.08e- 2
##  6     3    65    0.00300       1.42e-10
##  7    22  1901    0.0388        1.42e- 4
##  8    28  1031    0.00557       2.23e- 5
##  9    21  1759    0.0131        1.31e+ 1
## 10    31   861    0.00106       4.42e-10
## 11     2  1186    0.0273        5.92e- 1
## 12    12  1406    0.00880       2.44e- 9
## 13    25  1652    0.0753        6.58e- 7
## 14    17   252    0.114         1.14e- 2
## 15    20   908    0.263         3.28e+ 0
## 16    19   791    0.0137        1.75e- 7
## 17    16   430    0.0687        6.27e- 8
## 18    26  1290    0.00392       1.25e- 6
## 19     5   683    0.0235        1.86e- 3
## 20    10   173    0.184         6.01e- 6

Work Flow

xgb_wflow <-
  workflow() %>%
  add_recipe(receta) %>%
  add_model(xgb_sp)

xgb_wflow
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: boost_tree()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## Boosted Tree Model Specification (classification)
## 
## Main Arguments:
##   mtry = tune()
##   trees = tune()
##   learn_rate = tune()
##   loss_reduction = tune()
## 
## Computational engine: xgboost

Entrenamiento de Malla de Busqueda en la Crossvalidation - Xgboost

set.seed(123)

xgb_tuned <- tune_grid(
  xgb_wflow,
  resamples= cv,
  grid = xgb_grid,
  metrics = metricas,
  control= control_grid(allow_par = T, save_pred = T)
  )

xgb_tuned
## # Tuning results
## # 5-fold cross-validation using stratification 
## # A tibble: 5 × 5
##   splits              id    .metrics          .notes           .predictions
##   <list>              <chr> <list>            <list>           <list>      
## 1 <split [4507/1127]> Fold1 <tibble [80 × 8]> <tibble [0 × 3]> <tibble>    
## 2 <split [4507/1127]> Fold2 <tibble [80 × 8]> <tibble [0 × 3]> <tibble>    
## 3 <split [4507/1127]> Fold3 <tibble [80 × 8]> <tibble [0 × 3]> <tibble>    
## 4 <split [4507/1127]> Fold4 <tibble [80 × 8]> <tibble [0 × 3]> <tibble>    
## 5 <split [4508/1126]> Fold5 <tibble [80 × 8]> <tibble [0 × 3]> <tibble>

Mejor modelo

Evaluamos que modelo resulto mejor

show_best(xgb_tuned, metric = 'accuracy', n = 10)
## # A tibble: 10 × 10
##     mtry trees learn_rate loss_reduction .metric  .estimator  mean     n std_err
##    <int> <int>      <dbl>          <dbl> <chr>    <chr>      <dbl> <int>   <dbl>
##  1     5   683    0.0235         1.86e-3 accuracy binary     0.763     5 0.00639
##  2    12  1406    0.00880        2.44e-9 accuracy binary     0.761     5 0.00638
##  3    21  1759    0.0131         1.31e+1 accuracy binary     0.760     5 0.00700
##  4    19   791    0.0137         1.75e-7 accuracy binary     0.760     5 0.00534
##  5     6  1881    0.00200        6.04e-1 accuracy binary     0.759     5 0.00832
##  6    10   173    0.184          6.01e-6 accuracy binary     0.759     5 0.00563
##  7     2  1186    0.0273         5.92e-1 accuracy binary     0.758     5 0.00663
##  8     9   514    0.00628        8.18e-2 accuracy binary     0.758     5 0.00675
##  9    26  1290    0.00392        1.25e-6 accuracy binary     0.757     5 0.00721
## 10    28  1031    0.00557        2.23e-5 accuracy binary     0.756     5 0.00616
## # ℹ 1 more variable: .config <chr>
show_best(xgb_tuned, metric = 'sens', n = 10)
## # A tibble: 10 × 10
##     mtry trees learn_rate loss_reduction .metric .estimator  mean     n std_err
##    <int> <int>      <dbl>          <dbl> <chr>   <chr>      <dbl> <int>   <dbl>
##  1     3    65    0.00300       1.42e-10 sens    binary     0.766     5 0.0127 
##  2    21  1759    0.0131        1.31e+ 1 sens    binary     0.763     5 0.0113 
##  3     8   340    0.00151       5.11e- 4 sens    binary     0.761     5 0.0118 
##  4    31   861    0.00106       4.42e-10 sens    binary     0.748     5 0.0179 
##  5     9   514    0.00628       8.18e- 2 sens    binary     0.747     5 0.00979
##  6     6  1881    0.00200       6.04e- 1 sens    binary     0.745     5 0.0114 
##  7    28  1031    0.00557       2.23e- 5 sens    binary     0.740     5 0.0127 
##  8    26  1290    0.00392       1.25e- 6 sens    binary     0.740     5 0.0141 
##  9    19   791    0.0137        1.75e- 7 sens    binary     0.726     5 0.00812
## 10    12  1406    0.00880       2.44e- 9 sens    binary     0.721     5 0.00714
## # ℹ 1 more variable: .config <chr>
show_best(xgb_tuned, metric = 'spec', n = 10)
## # A tibble: 10 × 10
##     mtry trees learn_rate loss_reduction .metric .estimator  mean     n std_err
##    <int> <int>      <dbl>          <dbl> <chr>   <chr>      <dbl> <int>   <dbl>
##  1    28  1318     0.169   0.00000000569 spec    binary     0.804     5 0.00322
##  2    14  1595     0.0503  0.0108        spec    binary     0.799     5 0.00321
##  3    22  1901     0.0388  0.000142      spec    binary     0.795     5 0.00466
##  4    25  1652     0.0753  0.000000658   spec    binary     0.794     5 0.00335
##  5    10   173     0.184   0.00000601    spec    binary     0.790     5 0.00441
##  6    16   430     0.0687  0.0000000627  spec    binary     0.788     5 0.00507
##  7    20   908     0.263   3.28          spec    binary     0.783     5 0.00540
##  8     5   683     0.0235  0.00186       spec    binary     0.782     5 0.00576
##  9    17   252     0.114   0.0114        spec    binary     0.782     5 0.00320
## 10     2  1186     0.0273  0.592         spec    binary     0.776     5 0.00748
## # ℹ 1 more variable: .config <chr>
show_best(xgb_tuned, metric = 'bal_accuracy', n = 10)
## # A tibble: 10 × 10
##     mtry trees learn_rate loss_reduction .metric  .estimator  mean     n std_err
##    <int> <int>      <dbl>          <dbl> <chr>    <chr>      <dbl> <int>   <dbl>
##  1    21  1759    0.0131        1.31e+ 1 bal_acc… binary     0.761     5 0.00816
##  2     3    65    0.00300       1.42e-10 bal_acc… binary     0.757     5 0.00938
##  3     8   340    0.00151       5.11e- 4 bal_acc… binary     0.756     5 0.00934
##  4     6  1881    0.00200       6.04e- 1 bal_acc… binary     0.755     5 0.00873
##  5     9   514    0.00628       8.18e- 2 bal_acc… binary     0.754     5 0.00727
##  6    26  1290    0.00392       1.25e- 6 bal_acc… binary     0.751     5 0.00924
##  7    28  1031    0.00557       2.23e- 5 bal_acc… binary     0.751     5 0.00799
##  8    19   791    0.0137        1.75e- 7 bal_acc… binary     0.749     5 0.00622
##  9    12  1406    0.00880       2.44e- 9 bal_acc… binary     0.748     5 0.00642
## 10    31   861    0.00106       4.42e-10 bal_acc… binary     0.748     5 0.00919
## # ℹ 1 more variable: .config <chr>

Selección del modelo final

La selección de los hipepáramestros es con base al mejor modelo de sensibilidad y “bal_accuracy”, por tanto se el modelo final resulta ser el modelo 04, no se realizá un proceso búsqueda manual del mejor modelo debido a la amplitud de valores que puede tomar el learn_rate y la función de costo, así como la discrecionalidad de la amplitud de búsqueda.

xgb_pars_fin <- select_best(xgb_tuned, metric = 'sens')
xgb_wflow_fin <-
  xgb_wflow %>%
  finalize_workflow(xgb_pars_fin)

xgb_wflow_fin
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: boost_tree()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## Boosted Tree Model Specification (classification)
## 
## Main Arguments:
##   mtry = 3
##   trees = 65
##   learn_rate = 0.00299866530863753
##   loss_reduction = 1.42014018015466e-10
## 
## Computational engine: xgboost

Entrenar el modelo final

xgb_fitted <- fit(xgb_wflow_fin, train)
xgb_fitted
## ══ Workflow [trained] ══════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: boost_tree()
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 8 Recipe Steps
## 
## • step_rm()
## • step_impute_knn()
## • step_normalize()
## • step_other()
## • step_novel()
## • step_dummy()
## • step_nzv()
## • step_upsample()
## 
## ── Model ───────────────────────────────────────────────────────────────────────
## ##### xgb.Booster
## raw: 173.7 Kb 
## call:
##   xgboost::xgb.train(params = list(eta = 0.00299866530863753, max_depth = 6, 
##     gamma = 1.42014018015466e-10, colsample_bytree = 1, colsample_bynode = 0.1, 
##     min_child_weight = 1, subsample = 1), data = x$data, nrounds = 65L, 
##     watchlist = x$watchlist, verbose = 0, nthread = 1, objective = "binary:logistic")
## params (as set within xgb.train):
##   eta = "0.00299866530863753", max_depth = "6", gamma = "1.42014018015466e-10", colsample_bytree = "1", colsample_bynode = "0.1", min_child_weight = "1", subsample = "1", nthread = "1", objective = "binary:logistic", validate_parameters = "TRUE"
## xgb.attributes:
##   niter
## callbacks:
##   cb.evaluation.log()
## # of features: 30 
## niter: 65
## nfeatures : 30 
## evaluation_log:
##     iter training_logloss
##        1        0.6921809
##        2        0.6914972
## ---                      
##       64        0.6464204
##       65        0.6456987

Selección del modelo

xgb_model_fin <- pull_workflow_fit(xgb_fitted)
## Warning: `pull_workflow_fit()` was deprecated in workflows 0.2.3.
## ℹ Please use `extract_fit_parsnip()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
xgb_model_fin
## parsnip model object
## 
## ##### xgb.Booster
## raw: 173.7 Kb 
## call:
##   xgboost::xgb.train(params = list(eta = 0.00299866530863753, max_depth = 6, 
##     gamma = 1.42014018015466e-10, colsample_bytree = 1, colsample_bynode = 0.1, 
##     min_child_weight = 1, subsample = 1), data = x$data, nrounds = 65L, 
##     watchlist = x$watchlist, verbose = 0, nthread = 1, objective = "binary:logistic")
## params (as set within xgb.train):
##   eta = "0.00299866530863753", max_depth = "6", gamma = "1.42014018015466e-10", colsample_bytree = "1", colsample_bynode = "0.1", min_child_weight = "1", subsample = "1", nthread = "1", objective = "binary:logistic", validate_parameters = "TRUE"
## xgb.attributes:
##   niter
## callbacks:
##   cb.evaluation.log()
## # of features: 30 
## niter: 65
## nfeatures : 30 
## evaluation_log:
##     iter training_logloss
##        1        0.6921809
##        2        0.6914972
## ---                      
##       64        0.6464204
##       65        0.6456987

Evaluación del modelo en la data de entrenamiento y prueba

train %>%
  predict(xgb_fitted , new_data = . ) %>%
  mutate(Real= train$Churn) %>%
  conf_mat(truth = Real, estimate = .pred_class ) %>%
  summary
## # A tibble: 13 × 3
##    .metric              .estimator .estimate
##    <chr>                <chr>          <dbl>
##  1 accuracy             binary         0.763
##  2 kap                  binary         0.468
##  3 sens                 binary         0.777
##  4 spec                 binary         0.757
##  5 ppv                  binary         0.536
##  6 npv                  binary         0.904
##  7 mcc                  binary         0.485
##  8 j_index              binary         0.535
##  9 bal_accuracy         binary         0.767
## 10 detection_prevalence binary         0.384
## 11 precision            binary         0.536
## 12 recall               binary         0.777
## 13 f_meas               binary         0.635
test %>%
  predict(xgb_fitted , new_data = . ) %>%
  mutate(Real= test$Churn) %>%
  conf_mat(truth = Real, estimate = .pred_class ) %>%
  summary
## # A tibble: 13 × 3
##    .metric              .estimator .estimate
##    <chr>                <chr>          <dbl>
##  1 accuracy             binary         0.769
##  2 kap                  binary         0.470
##  3 sens                 binary         0.746
##  4 spec                 binary         0.778
##  5 ppv                  binary         0.548
##  6 npv                  binary         0.894
##  7 mcc                  binary         0.481
##  8 j_index              binary         0.524
##  9 bal_accuracy         binary         0.762
## 10 detection_prevalence binary         0.361
## 11 precision            binary         0.548
## 12 recall               binary         0.746
## 13 f_meas               binary         0.632

Importancia de las variables en el modelo

vip(xgb_model_fin)

Analizando la importancia de las variables en el modelo se verifica que el gasto mensual y total presentan una alta importancia en las variables sin embargo el gasto total es una combinación lineal del gasto mensual, por lo que se podría excluir del modelo una de las variabales para probar si incrementa su capacidad de predicción con respecto a sensibilidad y bal_accuracy.

Conclusión

Del análisis realizado se evidencia que ambos modelos (Random Forest y Xgboost ) presentan muy buena estimación con relación a sensibilidad, es decir con la capacidad de predicción de los individuos que abandonan el servicio, no obstante Xgboost presenta un resultado superior en el test de prueba (0.7459 > 0.7245), por tanto el modelo seleccionado es Xgboost.