library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Adjuntando el paquete: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(ISLR)
library(dplyr)
library(readr)
library(ggplot2)
library(ggcorrplot)
library(plotly)
library(ggcorrplot)
library(MASS) # Criterio de Información de Akaike (AIC)
## 
## Adjuntando el paquete: 'MASS'
## 
## The following object is masked from 'package:plotly':
## 
##     select
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(nortest) # prueba de Anderson-Darling
library(lmtest) # prueba Breusch-Pagan
## Cargando paquete requerido: zoo
## 
## Adjuntando el paquete: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Cargando paquete requerido: carData
## 
## Adjuntando el paquete: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some

Modelo de regresion - Dataset Churn

Se carga la informacion del dataset para el analisis.

# Cargar el dataset
dsChurn <- read_csv("churn.csv")
## Rows: 10000 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Surname, Geography, Gender
## dbl (11): RowNumber, CustomerId, CreditScore, Age, Tenure, Balance, NumOfPro...
## 
## ℹ 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.
head(dsChurn)
## # A tibble: 6 × 14
##   RowNumber CustomerId Surname CreditScore Geography Gender   Age Tenure Balance
##       <dbl>      <dbl> <chr>         <dbl> <chr>     <chr>  <dbl>  <dbl>   <dbl>
## 1         1   15634602 Hargra…         619 France    Female    42      2      0 
## 2         2   15647311 Hill            608 Spain     Female    41      1  83808.
## 3         3   15619304 Onio            502 France    Female    42      8 159661.
## 4         4   15701354 Boni            699 France    Female    39      1      0 
## 5         5   15737888 Mitche…         850 Spain     Female    43      2 125511.
## 6         6   15574012 Chu             645 Spain     Male      44      8 113756.
## # ℹ 5 more variables: NumOfProducts <dbl>, HasCrCard <dbl>,
## #   IsActiveMember <dbl>, EstimatedSalary <dbl>, Exited <dbl>

El dataset churn esta compuesto por los siguientes tipos de variables.

Cualitativas:

-Surname. -Geography. -Gender. -CustomerId. -RowNumber.

Cuantitativas:

-CreditScore. -Age. -Tenure. -Balance. -NumOfProducts. -HasCrCard. -IsActiveMember. -EstimatedSalary. -Exited.

A partir de las variables cuantitativas, se elabora la matriz de correlacion para verificar que relacion existe entre cada una de ellas y elaborar un modelo predictivo utilizando regresion logistica, con el objetivo de validar que probabilidad existe de que un cliente activo se retire del banco. En este caso, no se considerara la variable Exited como parte del modelo y por ello se excluye en conjunto con las variables cualitativas.

Matriz de correlación

# Remove the Customer Value column
reduced_data <- subset(dsChurn, select = - c(Exited,RowNumber,CustomerId,Surname,Geography,Gender))

reduced_data
## # A tibble: 10,000 × 8
##    CreditScore   Age Tenure Balance NumOfProducts HasCrCard IsActiveMember
##          <dbl> <dbl>  <dbl>   <dbl>         <dbl>     <dbl>          <dbl>
##  1         619    42      2      0              1         1              1
##  2         608    41      1  83808.             1         0              1
##  3         502    42      8 159661.             3         1              0
##  4         699    39      1      0              2         0              0
##  5         850    43      2 125511.             1         1              1
##  6         645    44      8 113756.             2         1              0
##  7         822    50      7      0              2         1              1
##  8         376    29      4 115047.             4         1              0
##  9         501    44      4 142051.             2         0              1
## 10         684    27      2 134604.             1         1              1
## # ℹ 9,990 more rows
## # ℹ 1 more variable: EstimatedSalary <dbl>
# Compute correlation at 2 decimal places
corr_matrix = round(cor(reduced_data), 2)

# Compute and show the  result
ggcorrplot(corr_matrix, hc.order = TRUE, type = "lower",
          lab = TRUE,lab_size = 2)

En la matriz de correlacion, no se observa una correlacion positiva ni negativa entre las variables cuantitativas que componen el dataset, razon por la cual se descarta elaborar un modelo predictivo a partir de cualquier metodo de regresion existente (lineal o logistico).