Initial Summary

Check out the data set - likely columns to drop would be CustomerID, Surname, and we need to create a random ID. Given the broad range of ages, credit scores, balances and salaries, any model would need some kind of normalisation. We’ll do that later after first analysis.

summary(df)
##    CustomerId         Surname           CreditScore     Geography        
##  Min.   :15565701   Length:10002       Min.   :350.0   Length:10002      
##  1st Qu.:15628525   Class :character   1st Qu.:584.0   Class :character  
##  Median :15690732   Mode  :character   Median :652.0   Mode  :character  
##  Mean   :15690933                      Mean   :650.6                     
##  3rd Qu.:15753226                      3rd Qu.:718.0                     
##  Max.   :15815690                      Max.   :850.0                     
##                                                                          
##     Gender               Age            Tenure          Balance      
##  Length:10002       Min.   :18.00   Min.   : 0.000   Min.   :     0  
##  Class :character   1st Qu.:32.00   1st Qu.: 3.000   1st Qu.:     0  
##  Mode  :character   Median :37.00   Median : 5.000   Median : 97199  
##                     Mean   :38.92   Mean   : 5.012   Mean   : 76491  
##                     3rd Qu.:44.00   3rd Qu.: 7.000   3rd Qu.:127648  
##                     Max.   :92.00   Max.   :10.000   Max.   :250898  
##                     NA's   :1                                        
##  NumOfProducts    HasCrCard      IsActiveMember   EstimatedSalary    
##  Min.   :1.00   Min.   :0.0000   Min.   :0.0000   Min.   :    11.58  
##  1st Qu.:1.00   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.: 50983.75  
##  Median :1.00   Median :1.0000   Median :1.0000   Median :100185.24  
##  Mean   :1.53   Mean   :0.7055   Mean   :0.5149   Mean   :100083.33  
##  3rd Qu.:2.00   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:149383.65  
##  Max.   :4.00   Max.   :1.0000   Max.   :1.0000   Max.   :199992.48  
##                 NA's   :1        NA's   :1                           
##      Exited      
##  Min.   :0.0000  
##  1st Qu.:0.0000  
##  Median :0.0000  
##  Mean   :0.2038  
##  3rd Qu.:0.0000  
##  Max.   :1.0000  
## 

Missing Data

Checking for missing data. There seems to be a very small number of nulls or empty values.

count_missing <- function(data) {
  data %>% summarise_all(~ sum(is.na(.)))
}

missing_counts <- count_missing(df)

print(missing_counts)
## # A tibble: 1 × 13
##   CustomerId Surname Credi…¹ Geogr…² Gender   Age Tenure Balance NumOf…³ HasCr…⁴
##        <int>   <int>   <int>   <int>  <int> <int>  <int>   <int>   <int>   <int>
## 1          0       0       0       1      0     1      0       0       0       1
## # … with 3 more variables: IsActiveMember <int>, EstimatedSalary <int>,
## #   Exited <int>, and abbreviated variable names ¹​CreditScore, ²​Geography,
## #   ³​NumOfProducts, ⁴​HasCrCard

Checking % Splits

Check the total splits across the data set for different variables. There’s a better way of doing and displaying this, but didn’t want to waste time here.

table_percentage <- function(column) {
  prop.table(table(column))
}


table_percentage <- function(column) {
   prop.table(table(column)) * 100
}
 
print(table_percentage(df$Exited))
## column
##        0        1 
## 79.62408 20.37592
print(table_percentage(df$Geography))
## column
##   France  Germany    Spain 
## 50.13499 25.09749 24.76752
print(table_percentage(df$Gender))
## column
##   Female     Male 
## 45.43091 54.56909
print(table_percentage(df$HasCrCard))
## column
##        0        1 
## 29.44706 70.55294
print(table_percentage(df$IsActiveMember))
## column
##        0        1 
## 48.50515 51.49485

Boxplot of different splits for ‘range’ variables

# Select specific columns
selected_columns <- df[, names(df) %in% c('Age', 'Balance', 'CreditScore', 'EstimatedSalary')]

# Reshape the data and create boxplots
selected_columns %>%
  gather() %>%
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_boxplot() +
  theme_minimal()
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).

Boxplot Comparisons for Exited

# Select specific columns
cols <- df[, names(df) %in% c('Age', 'Balance', 'CreditScore', 'EstimatedSalary', 'Tenure', 'Exited')]

# Reshape the data and create boxplots
cols %>%
  gather(key, value, -Exited) %>%
  ggplot(aes(x = key, y = value, fill = factor(Exited))) +
  geom_boxplot() +
  scale_fill_manual(values = c("0" = "#97e4b2", "1" = "#FACBE1")) +  # Customize fill colors
  facet_wrap(~ key, scales = "free", ncol = 2) +
  theme_minimal() +
  labs(title = "Comparison of Variables Churned",
       x = "Variable",
       y = "Value")
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).

Comparing % Splits for Exited vs Not

# Function to compare percentage splits for each category where Exited is 1 vs. 0
compare_exited_percentage <- function(data, column) {
  data %>%
    group_by(Exited, {{ column }}) %>%
    summarise(count = n()) %>%
    mutate(percentage = count / sum(count))
}

# Geography
compare_geography <- compare_exited_percentage(df, Geography)
## `summarise()` has grouped output by 'Exited'. You can override using the
## `.groups` argument.
print(compare_geography)
## # A tibble: 7 × 4
## # Groups:   Exited [2]
##   Exited Geography count percentage
##    <dbl> <chr>     <int>      <dbl>
## 1      0 France     4204   0.528   
## 2      0 Germany    1695   0.213   
## 3      0 Spain      2064   0.259   
## 4      0 <NA>          1   0.000126
## 5      1 France      810   0.397   
## 6      1 Germany     815   0.400   
## 7      1 Spain       413   0.203
# Gender
compare_gender <- compare_exited_percentage(df, Gender)
## `summarise()` has grouped output by 'Exited'. You can override using the
## `.groups` argument.
print(compare_gender)
## # A tibble: 4 × 4
## # Groups:   Exited [2]
##   Exited Gender count percentage
##    <dbl> <chr>  <int>      <dbl>
## 1      0 Female  3405      0.428
## 2      0 Male    4559      0.572
## 3      1 Female  1139      0.559
## 4      1 Male     899      0.441
# Credit Card
compare_card <- compare_exited_percentage(df, HasCrCard)
## `summarise()` has grouped output by 'Exited'. You can override using the
## `.groups` argument.
print(compare_card)
## # A tibble: 5 × 4
## # Groups:   Exited [2]
##   Exited HasCrCard count percentage
##    <dbl>     <dbl> <int>      <dbl>
## 1      0         0  2332   0.293   
## 2      0         1  5631   0.707   
## 3      0        NA     1   0.000126
## 4      1         0   613   0.301   
## 5      1         1  1425   0.699

Histograms of Tenure

Difficult to see any trend on tenure here, so moving on to age later

# Create a histogram for Tenure for Exited = 1 and Exited = 0
ggplot(df, aes(x = Tenure, fill = factor(Exited))) +
  geom_histogram(bins = 30, color = "white", alpha = 0.7, position = "identity") +
  labs(title = "Tenure",
       x = "Tenure",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_wrap(~ Exited, scales = "free") +
  theme_minimal() +
  scale_x_continuous(breaks = unique(df$Tenure))

Histograms of Balance

Given the open values, this groups balance by $25k chunks. The data set has a max balance of just over 250k.

Output shows that customers with a balance of under 25K (likely 0K) or over 75k are most likely to churn, though for a model we would likely focus on the higher value customers assuming lower value balances are lower priority or insignificant. Because of this, we add a new feature of Balance75k, which will return a 1 if the balance is greater than 75k, and a 0 if less. We can add more later when trying out models.

# Define the breaks for binning (inclusive on the right)
breaks <- c(0, 25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000, Inf)

# Create a new field 'Balance_Group' by binning the 'Balance' values
df <- df %>%
  mutate(BalanceGroup = cut(Balance, breaks = breaks, labels = c("0k-25k", "25k-50k", "50k-75k", "75k-100k", "100k-125k", "125k-150k", "150k-175k", "175k-200k", "200k+"), include.lowest = TRUE, right = TRUE))


ggplot(df, aes(x = BalanceGroup, fill = factor(Exited))) +
  geom_bar(position = "dodge", color = "white") +
  labs(title = "Balance Group",
       x = "Balance_Group",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_grid(Exited ~ ., scales = "free", switch = "y") +
  theme_minimal()

df <- df %>%
  mutate(Balance75k = ifelse(Balance >= 75000, 1, 0))

Histograms of Active Products

Assumes current or end-of-life products for relevance. Potentially shows that if a customer has 2 or more products they are less likely to churn

# Create a histogram for Product for Exited = 1 and Exited = 0
ggplot(df, aes(x = NumOfProducts, fill = factor(Exited))) +
  geom_bar(bins = 30, color = "white", alpha = 0.7, position = "identity") +
  labs(title = "Products",
       x = "Number of Products",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_wrap(~ Exited, scales = "free") +
  theme_minimal() +
  scale_x_continuous(breaks = unique(df$NumOfProducts))
## Warning in geom_bar(bins = 30, color = "white", alpha = 0.7, position =
## "identity"): Ignoring unknown parameters: `bins`

Histograms of Has Credit Card

Assumes current or end-of-life value for HasCrCard. This shows a pretty even split between churned vs not, and is likely not valuable to any models

# Create a histogram for Credit Card for Exited = 1 and Exited = 0
ggplot(df, aes(x = HasCrCard, fill = factor(Exited))) +
  geom_bar(bins = 30, color = "white", alpha = 0.7, position = "identity") +
  labs(title = "Has Credit Card",
       x = "HasCrCard",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_wrap(~ Exited, scales = "free") +
  theme_minimal() +
  scale_x_continuous(breaks = unique(df$HasCrCard))
## Warning in geom_bar(bins = 30, color = "white", alpha = 0.7, position =
## "identity"): Ignoring unknown parameters: `bins`
## Warning: Removed 1 rows containing non-finite values (`stat_count()`).

Histograms of Active Members

Assumes current or end-of-life value for IsActiveMember. Shows that if they are an active member, they have more likelihood to stay as a customer - though I’d be hesitant to run with this incase products drop in the lead up to churn.

# Create a histogram for Credit Card for Exited = 1 and Exited = 0
ggplot(df, aes(x = IsActiveMember, fill = factor(Exited))) +
  geom_bar(bins = 30, color = "white", alpha = 0.7, position = "identity") +
  labs(title = "IsActiveMember",
       x = "IsActiveMember",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_wrap(~ Exited, scales = "free") +
  theme_minimal() +
  scale_x_continuous(breaks = unique(df$IsActiveMember))
## Warning in geom_bar(bins = 30, color = "white", alpha = 0.7, position =
## "identity"): Ignoring unknown parameters: `bins`
## Warning: Removed 1 rows containing non-finite values (`stat_count()`).

Grouping Salary Values

Given the open values this groups balance by $25k chunks. The data set has a max balance of just under 200k.

There doesn’t appear to be any impact from Salary on churn.

# Define the breaks for binning (inclusive on the right)
breaks <- c(0, 25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000, Inf)

# Create a new field 'Balance_Group' by binning the 'Balance' values
df <- df %>%
  mutate(SalaryGroup = cut(EstimatedSalary, breaks = breaks, labels = c("0k-25k", "25k-50k", "50k-75k", "75k-100k", "100k-125k", "125k-150k", "150k-175k", "175k-200k", "200k+"), include.lowest = TRUE, right = TRUE))


ggplot(df, aes(x = SalaryGroup, fill = factor(Exited))) +
  geom_bar(position = "dodge", color = "white") +
  labs(title = "Salary Group",
       x = "Salary  Group",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_grid(Exited ~ ., scales = "free", switch = "y") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -75, hjust = 1))

Grouping Age Values

Age Groups 35-54 have the highest rate of churn, but for clarity, we’d want to look at this in smaller, possibly 5 year buckets

# Define the breaks for binning (inclusive on the right)
breaks <- c(18, 25, 35, 45, 55, 65, 75, 85, 95, Inf)

# Create a new field 'Age_Group' by binning the 'Age' values
df <- df %>%
  mutate(AgeGroup = cut(Age, breaks = breaks, labels = c("18-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "85-94", "95+"), include.lowest = TRUE, right = FALSE))


ggplot(df, aes(x = AgeGroup, fill = factor(Exited))) +
  geom_bar(position = "dodge", color = "white") +
  labs(title = "Age Group",
       x = "Age Group",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_grid(Exited ~ ., scales = "free", switch = "y") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -75, hjust = 1))

Expanding Age Brackets

Churn expands at 35+, we can add a new binary that determines if the customers is 35+ (1) or not (0)

# Create breaks and labels
# Define age buckets
breaks <- c(18, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, Inf)

# Create a new column for age buckets
df <- df %>%
  mutate(AgeGroup5s = cut(Age, breaks = breaks, labels = c("18-24", "25-29", "30-34", "35-39", "40-44",
                                                             "45-49", "50-54", "55-59", "60-64", "65-69",
                                                             "70-74", "75-79", "80-84", "85-89", "90-95", "95+"),
                         include.lowest = TRUE, right = FALSE))



ggplot(df, aes(x = AgeGroup5s, fill = factor(Exited))) +
  geom_bar(position = "dodge", color = "white") +
  labs(title = "Age Group",
       x = "Age Group",
       y = "Frequency") +
  scale_fill_manual(values = c("#97e4b2", "#FACBE1")) +
  facet_grid(Exited ~ ., scales = "free", switch = "y") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -75, hjust = 1))

df <- df %>%
  mutate(Age35 = ifelse(Age >= 35, 1, 0))

Update the DF ready for modelling

Overall:

# Generate a random identifier for each record
df <- df %>%
  mutate(Id = stri_rand_strings(n = nrow(df), length = 10, pattern = "[A-Za-z0-9]"))

# Reorder columns to have Id as the first column, and drop irrelevant colums
df <- df %>%
  select(Id, Exited, everything()) %>% 
  select(-CustomerId, -Surname, -EstimatedSalary, -SalaryGroup, -HasCrCard, -Age, -Balance, -BalanceGroup, -SalaryGroup, -AgeGroup, - AgeGroup5s)


#define function to scale values for CreditScore between 0 and 1
scale_values <- function(x){(x-min(x))/(max(x)-min(x))}

#scale values in CreditScore column to be between 0 and 1
df$CreditScore <- scale_values(df$CreditScore)

# Encode Geography and Gender
df$Geography <- as.numeric(factor(df$Geography, levels = c("Spain", "Germany", "France")))
df$Gender <- as.numeric(factor(df$Gender, levels = c("Male", "Female")))


# Print the updated dataframe
print(df)
## # A tibble: 10,002 × 10
##    Id         Exited Credi…¹ Geogr…² Gender Tenure NumOf…³ IsAct…⁴ Balan…⁵ Age35
##    <chr>       <dbl>   <dbl>   <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl> <dbl>
##  1 lpgiMaDbNX      1   0.61        1      1      3       3       1       1     1
##  2 HnnPsBrQS4      1   0.466       3      2      3       1       1       1     1
##  3 6K6qstLSkB      1   0.626       1      2      5       1       1       1     1
##  4 QqI3WzePnR      0   1           3      2      3       1       1       1     1
##  5 3xYs17TXFq      0   0.382       3      1      9       1       1       1     1
##  6 cDE4mWEF8O      1   0.492       3      1      4       2       1       1     0
##  7 5tQJ9q0Vls      1   0.644       1      2      2       2       1       1     1
##  8 PUqkF0kwi5      1   0.712       1      2      1       1       1       1     0
##  9 FaLX91fKiZ      0   0.446       2      2      9       2       1       1     1
## 10 SPbprMkk7b      1   0.428       3      2      7       1       1       1     1
## # … with 9,992 more rows, and abbreviated variable names ¹​CreditScore,
## #   ²​Geography, ³​NumOfProducts, ⁴​IsActiveMember, ⁵​Balance75k