Continuous columns in the data set are - Age, Balance, Estimated Salary, Credit Score, Tenure, No. Of Products.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ 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
#Loading data
data <- read_delim("../Churn_Modelling.csv", delim = ",")
## Rows: 10002 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.
Here, Balance Per Year is the new variable that is being created which accounts the average balance during the customer’s tenure.
data$AvgBalancePerYear <- data$Balance / data$Tenure
Let, the response variable be Average Balance Per Year and the explanatory variable be Credit Score. It would be reasonable to assume, them being a response-explanatory pair of variables, as often individuals with larger account balances (Which essentially means larger Avg. Balance per year) have a higher credit score.
ggplot(data, aes_string(x = "CreditScore", y = "AvgBalancePerYear")) +
geom_point() +
geom_smooth() +
labs(x = "CreditScore", y = "AvgBalancePerYear",
title = "CreditScore vs AvgBalancePerYear")
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 413 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 137 rows containing missing values (`geom_point()`).
Contrary to the assumption, there seems to be no relation among these two variables, the confidence intervals (blue line) also indicates that the, distribution is primarily random.
cleaned_data <- na.omit(data[, c("CreditScore", "AvgBalancePerYear")])
correlation_coefficient <- cor(cleaned_data$CreditScore, cleaned_data$AvgBalancePerYear, method = "spearman")
correlation_coefficient
## [1] 0.009734162
As expected from the visualization there is a very small correlation (very close to zero) among these variables.
Financial Stability, is measured using the Balance and Estimated Salary columns
data$FinancialStability <- data$Balance / data$EstimatedSalary
For this instance let Financial Stability be the response variable, while Age being the explanatory variable. Often the assumption is that at a higher age often individuals are much financially stable, while at the initial stages it would be reasonable to assume a random pattern.
ggplot(data, aes_string(x = "Age", y = "FinancialStability")) +
geom_point() +
geom_smooth() +
labs(x = "Age", y = "FinancialStability",
title = "Age vs FinancialStability")
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
Similar to earlier, this visualization indicates that there is very low (close to zero) correlation among these variables.
cleaned_data <- na.omit(data[, c("Age", "FinancialStability")])
correlation_coefficient <- cor(cleaned_data$Age, cleaned_data$FinancialStability, method = "spearman")
correlation_coefficient
## [1] 0.03595683
As anticipated the Spearman’s correlation coefficient is very low.
Average Total No. of Products is defined as the product of Tenure and No. of Products. It indicates the the total number of products that the customer had been using over their tenure in the bank.
data$TotalNumProducts <- data$Tenure * data$NumOfProducts
Response Variable - Average Total No. of Products and explanatory variable - Balance. It would be reasonable to make this assumption since, as the individual balance reduces the number of products they use such as credit cards etc. reduces.
ggplot(data, aes_string(x = "Balance", y = "TotalNumProducts")) +
geom_point() +
geom_smooth() +
labs(x = "Balance", y = "TotalNumProducts",
title = "Balance Salary vs Total No. of Products")
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Due to the discontinuous nature of the number of products column the visualization appears to disperse. Similar to earlier the visualization suggests a low value correlation. But this time it appears to have a slight decreasing value, which would mean a low negative correlation coefficient.
cleaned_data <- na.omit(data[, c("Balance", "TotalNumProducts")])
correlation_coefficient <- cor(cleaned_data$Balance, cleaned_data$TotalNumProducts, method = "spearman")
correlation_coefficient
## [1] -0.1551233
As anticipated the variables have a decreasing relation, with a small negative correlation coefficient.