##Data Preparation

CreditCardData_1_ <- read_csv("Downloads/CreditCardData (1).csv")
## Rows: 10127 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (6): Attrition_Flag, Gender, Education_Level, Marital_Status, Income_Ca...
## dbl (15): CLIENTNUM, Customer_Age, Dependent_count, Months_on_book, Total_Re...
## 
## ℹ 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.
Credit <- CreditCardData_1_ %>% 
  mutate(random = runif(10127) , 
         Attrition_Flag = as.factor(Attrition_Flag)) %>% 
  select(-CLIENTNUM)

train <- Credit %>% 
  filter(random < 0.7) %>%  
  select(-random)

val <- Credit %>% 
  filter(random >= 0.7) %>% 
  select(-random)

##Random Forest Model

rf <- randomForest(Attrition_Flag ~ . , 
                   type = 'classification' ,
                   data = train ,
                   importance = TRUE)
rf_summary <- summary(Credit)  

##Prediction and Financial Impact

val$predicted_rf <- predict(rf, newdata = val)

average_annual_fee_forgiveness <- 100
average_customer_value <- 2300
success_rate <- 0.30

retained_customers <- sum(val$predicted_rf == "Existing Customer")

total_annual_fee_forgiveness <- retained_customers * average_annual_fee_forgiveness
total_revenue_increase <- retained_customers * average_customer_value

CrossTable(val$predicted_rf, val$Attrition_Flag)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  3085 
## 
##  
##                   | val$Attrition_Flag 
##  val$predicted_rf | Attrited Customer | Existing Customer |         Row Total | 
## ------------------|-------------------|-------------------|-------------------|
## Attrited Customer |               403 |                26 |               429 | 
##                   |          1576.911 |           308.659 |                   | 
##                   |             0.939 |             0.061 |             0.139 | 
##                   |             0.798 |             0.010 |                   | 
##                   |             0.131 |             0.008 |                   | 
## ------------------|-------------------|-------------------|-------------------|
## Existing Customer |               102 |              2554 |              2656 | 
##                   |           254.704 |            49.855 |                   | 
##                   |             0.038 |             0.962 |             0.861 | 
##                   |             0.202 |             0.990 |                   | 
##                   |             0.033 |             0.828 |                   | 
## ------------------|-------------------|-------------------|-------------------|
##      Column Total |               505 |              2580 |              3085 | 
##                   |             0.164 |             0.836 |                   | 
## ------------------|-------------------|-------------------|-------------------|
## 
## 

##Variable Importance

model <- randomForest(Attrition_Flag ~ ., data = train)

varImpPlot(model, main = "Variable Importance for Predicting Attrition_Flag")