#STATISTICAL MODELLING OF BANK CHURNERS

#ABSTRACT
  # A bank manager is concerned with the number of clients leaving or being attrited. 
  #it seem that more clients are leaving and our concern is why are these clients leaving our 
  #bank. The manager wants to classify the clients that are most prawn to leave based on the 
  # data at hand, as to try and know which clients to target and what factors might influence their choice
  #to leave our bank.
  #The data contains info on 10127 clients ,both who are still with the bank,
  # and those who are no longer with the bank.

#PROBLEM STUDY
  #More and more clients are attriting/leaving our bank.
#AIM AND OBJECTIVES
  #Classify the attrited clients and find out why they are leaving 
  # or which factors influence their decision to leave.
  #Model the data and Do Machine learning as well as Predictive Analytics 
  #train the model
  #chose the model with the best Accuracy,Precision,kappa and 

#Inferential rules
  #Kappa "rule of thumb" for interpretation
   # 0.81-1.00 ALMOST PERFECT
   # 0.61-0.80 SUBSTATIAL
   # 0.41-0.60 MODERATE
   # 0.21-0.40 FAIR
   # 0.00-0.20 SLIGHT
   # <0.00 POOR

  #Accuracy- the proportion of all properly classified cases is measured by accuracy
  
  #sensitivity-the number of cases in which the positive class was actually predicted divided by the total number of cases 
  #whose observed data fall into the positive class -the prediction accuracy for those who quit
  
  #specificity-the proportion of people we said would stay relative to the overall number of people that stayed

  #prevalence-base probability of those who quit
  #we can say that based on the prevalence it's likely that _% of people are going to quit

  #balanced accuracy= (sensitivity+specificity)/2

##This will be our measures of perfomance for our models##

#MODELING
  #Step-WISE LOGISTIC REGRESSION
  #LOGISTIC REGRESSION- k-fold cross-validation
  #LASSO REGRESSION ANALYSIS-- k-fold cross validation+Variable importance analysis
  #Random Forest Modeling
  #Decision Trees--RPART
  #Ridge Regression-- k-fold cross-validation +variable importance

# we are going to deploy 5 models and pick the best 

##First we set
#Setting the working directory
setwd("C:\\Users\\Xholi\\Downloads")

#Loading the packages that will prove to useful
pacman::p_load(pacman,dplyr,GGally,ggvis,rio,
               shiny,stringr,tidyr,rmarkdown,
               httr,ggplot2,ggthemes,plotly,dummies,caret,
               psych,rpart,rpart.plot,DT,pROC,randomForest,ROCR,
               lubridate)

library(readr)
#load the data
df<-read.csv("BankChurners.csv")

#clean the data

df1<-df[,-1]
Churners<-df1[,-c(21,22)]

#change Attrited to Quit and existing to Stay
  #changing Attrition and Existing customer
Churners$Attrition_Flag[Churners$Attrition_Flag=="Existing Customer"]<-"Stay"
Churners$Attrition_Flag[Churners$Attrition_Flag=="Attrited Customer"]<-"Quit"
head(Churners)
##   Attrition_Flag Customer_Age Gender Dependent_count Education_Level
## 1           Stay           45      M               3     High School
## 2           Stay           49      F               5        Graduate
## 3           Stay           51      M               3        Graduate
## 4           Stay           40      F               4     High School
## 5           Stay           40      M               3      Uneducated
## 6           Stay           44      M               2        Graduate
##   Marital_Status Income_Category Card_Category Months_on_book
## 1        Married     $60K - $80K          Blue             39
## 2         Single  Less than $40K          Blue             44
## 3        Married    $80K - $120K          Blue             36
## 4        Unknown  Less than $40K          Blue             34
## 5        Married     $60K - $80K          Blue             21
## 6        Married     $40K - $60K          Blue             36
##   Total_Relationship_Count Months_Inactive_12_mon Contacts_Count_12_mon
## 1                        5                      1                     3
## 2                        6                      1                     2
## 3                        4                      1                     0
## 4                        3                      4                     1
## 5                        5                      1                     0
## 6                        3                      1                     2
##   Credit_Limit Total_Revolving_Bal Avg_Open_To_Buy Total_Amt_Chng_Q4_Q1
## 1        12691                 777           11914                1.335
## 2         8256                 864            7392                1.541
## 3         3418                   0            3418                2.594
## 4         3313                2517             796                1.405
## 5         4716                   0            4716                2.175
## 6         4010                1247            2763                1.376
##   Total_Trans_Amt Total_Trans_Ct Total_Ct_Chng_Q4_Q1 Avg_Utilization_Ratio
## 1            1144             42               1.625                 0.061
## 2            1291             33               3.714                 0.105
## 3            1887             20               2.333                 0.000
## 4            1171             20               2.333                 0.760
## 5             816             28               2.500                 0.000
## 6            1088             24               0.846                 0.311
#set random seed so that your results can be reproducable

set.seed(1992)

#create training and testing data 
  #we created our partioning by 70/30


intrain<-createDataPartition(Churners$Attrition_Flag,p=0.7,
                             list = F,
                             times = 1)
#creating both testing and training data sets
training <-Churners[intrain,]
testing <-Churners[-intrain,]


#Re-label values as factors 
  #re label
testing$Attrition_Flag<-as.factor(testing$Attrition_Flag)
training$Attrition_Flag<-as.factor(training$Attrition_Flag)



##MODELING
  #MODEL 1
  #STEP-WISE LOGISTIC REGRESSION-10,000 STEPS

#STEP-WISE REGRESSION
Model1 <-glm(as.factor(Attrition_Flag)~.,data=training,
             family=binomial(logit))
Model1_step <-step(Model1,
                   direction = "both",
                   steps = 10000,
                   trace = F)
predictions_step <-predict(Model1_step,testing,
                           type = "response")

#to decide the cut-off point
pred_step <-prediction(predictions_step,testing$Attrition_Flag)
plot(performance(pred_step,"tpr","fpr"),colorize=T)

auc_step<-performance(pred_step,"auc")
auc_step
## A performance instance
##   'Area under the ROC curve'
roc_step<-roc(response=testing$Attrition_Flag,predictor=predictions_step)
## Setting levels: control = Quit, case = Stay
## Setting direction: controls < cases
d<-coords(roc_step,"best","threshold",transpose=T)
d
##   threshold specificity sensitivity 
##   0.8067485   0.8299180   0.8690196
#  threshold specificity sensitivity 
#  0.8067485   0.8299180   0.8690196 


#Forming confusion matrix from the decided cut-off point
Attrition_step <-ifelse(predictions_step>=d[[1]],"Stay","Quit")
testing$Attrition_Flag<-as.factor(testing$Attrition_Flag)
Attrition_step<-as.factor(Attrition_step)
  #confusion matrix
cm_1 <-confusionMatrix(testing$Attrition_Flag,Attrition_step)
cm_1
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  405   83
##       Stay  334 2216
##                                         
##                Accuracy : 0.8627        
##                  95% CI : (0.85, 0.8748)
##     No Information Rate : 0.7567        
##     P-Value [Acc > NIR] : < 2.2e-16     
##                                         
##                   Kappa : 0.5786        
##                                         
##  Mcnemar's Test P-Value : < 2.2e-16     
##                                         
##             Sensitivity : 0.5480        
##             Specificity : 0.9639        
##          Pos Pred Value : 0.8299        
##          Neg Pred Value : 0.8690        
##              Prevalence : 0.2433        
##          Detection Rate : 0.1333        
##    Detection Prevalence : 0.1606        
##       Balanced Accuracy : 0.7560        
##                                         
##        'Positive' Class : Quit          
## 
#MODEL 1  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#          0.8627 0.5786   0.5480     0.9639    0.2433


  #MODEL 2
  #Logistic Regression-k-fold cross-validation

#Specify and train logistic regression model
  #k-folds(number of folds=10) cross validation framework

#set your control specs
ctrl_specs <-trainControl(method = "cv",
                          savePredictions = "all",
                          number = 10,
                          classProbs = T)

Model2<-train(Attrition_Flag~.,data = training,
              method="glm",
              family=binomial,
              trControl=ctrl_specs)
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading

## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading
Model2
## Generalized Linear Model 
## 
## 7089 samples
##   19 predictor
##    2 classes: 'Quit', 'Stay' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 6380, 6380, 6381, 6380, 6380, 6380, ... 
## Resampling results:
## 
##   Accuracy   Kappa    
##   0.9050648  0.6111962
# Accuracy   Kappa    
# 0.9050648  0.6111962

summary(Model2)
## 
## Call:
## NULL
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.3172   0.0640   0.1672   0.3608   2.9052  
## 
## Coefficients: (1 not defined because of singularities)
##                                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     -6.383e+00  5.721e-01 -11.155  < 2e-16 ***
## Customer_Age                     5.237e-03  9.338e-03   0.561 0.574955    
## GenderM                          8.900e-01  1.788e-01   4.978 6.43e-07 ***
## Dependent_count                 -1.356e-01  3.616e-02  -3.750 0.000177 ***
## Education_LevelDoctorate        -3.858e-01  2.502e-01  -1.542 0.123104    
## Education_LevelGraduate          3.155e-02  1.665e-01   0.189 0.849731    
## `Education_LevelHigh School`     8.696e-02  1.783e-01   0.488 0.625752    
## `Education_LevelPost-Graduate`  -4.500e-01  2.390e-01  -1.883 0.059698 .  
## Education_LevelUneducated       -1.491e-01  1.866e-01  -0.799 0.424463    
## Education_LevelUnknown          -1.522e-01  1.840e-01  -0.827 0.408276    
## Marital_StatusMarried            4.535e-01  1.848e-01   2.454 0.014132 *  
## Marital_StatusSingle            -1.590e-01  1.857e-01  -0.856 0.391930    
## Marital_StatusUnknown           -1.171e-01  2.342e-01  -0.500 0.616965    
## `Income_Category$40K - $60K`     7.341e-01  2.476e-01   2.965 0.003024 ** 
## `Income_Category$60K - $80K`     4.273e-01  2.192e-01   1.949 0.051255 .  
## `Income_Category$80K - $120K`   -3.260e-02  2.033e-01  -0.160 0.872644    
## `Income_CategoryLess than $40K`  5.465e-01  2.687e-01   2.034 0.041953 *  
## Income_CategoryUnknown           6.314e-01  2.856e-01   2.211 0.027057 *  
## Card_CategoryGold               -1.414e+00  4.360e-01  -3.244 0.001180 ** 
## Card_CategoryPlatinum           -6.289e-01  8.965e-01  -0.702 0.482970    
## Card_CategorySilver             -5.182e-01  2.339e-01  -2.215 0.026750 *  
## Months_on_book                   7.477e-03  9.287e-03   0.805 0.420746    
## Total_Relationship_Count         4.600e-01  3.266e-02  14.082  < 2e-16 ***
## Months_Inactive_12_mon          -5.164e-01  4.594e-02 -11.241  < 2e-16 ***
## Contacts_Count_12_mon           -5.589e-01  4.434e-02 -12.604  < 2e-16 ***
## Credit_Limit                     1.957e-05  8.327e-06   2.350 0.018791 *  
## Total_Revolving_Bal              9.471e-04  8.642e-05  10.959  < 2e-16 ***
## Avg_Open_To_Buy                         NA         NA      NA       NA    
## Total_Amt_Chng_Q4_Q1             3.213e-01  2.225e-01   1.444 0.148805    
## Total_Trans_Amt                 -4.805e-04  2.731e-05 -17.595  < 2e-16 ***
## Total_Trans_Ct                   1.197e-01  4.447e-03  26.908  < 2e-16 ***
## Total_Ct_Chng_Q4_Q1              2.803e+00  2.251e-01  12.456  < 2e-16 ***
## Avg_Utilization_Ratio            1.061e-02  2.948e-01   0.036 0.971290    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6249.4  on 7088  degrees of freedom
## Residual deviance: 3273.2  on 7057  degrees of freedom
## AIC: 3337.2
## 
## Number of Fisher Scoring iterations: 6
#list by variable importance
varImp(Model2)
## glm variable importance
## 
##   only 20 most important variables shown (out of 31)
## 
##                                 Overall
## Total_Trans_Ct                  100.000
## Total_Trans_Amt                  65.343
## Total_Relationship_Count         52.271
## Contacts_Count_12_mon            46.771
## Total_Ct_Chng_Q4_Q1              46.218
## Months_Inactive_12_mon           41.698
## Total_Revolving_Bal              40.650
## GenderM                          18.391
## Dependent_count                  13.821
## Card_CategoryGold                11.937
## `Income_Category$40K - $60K`     10.901
## Marital_StatusMarried             8.998
## Credit_Limit                      8.610
## Card_CategorySilver               8.109
## Income_CategoryUnknown            8.093
## `Income_CategoryLess than $40K`   7.435
## `Income_Category$60K - $80K`      7.120
## `Education_LevelPost-Graduate`    6.873
## Education_LevelDoctorate          5.604
## Total_Amt_Chng_Q4_Q1              5.239
plot(varImp(Model2))

#predict outcomes based on testing data
prediction_glm<-predict(Model2,newdata = testing)
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
## prediction from a rank-deficient fit may be misleading
#create confusion matrix
cm_2<-confusionMatrix(data = prediction_glm,testing$Attrition_Flag)
cm_2
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  289   98
##       Stay  199 2452
##                                           
##                Accuracy : 0.9022          
##                  95% CI : (0.8911, 0.9126)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6044          
##                                           
##  Mcnemar's Test P-Value : 6.53e-09        
##                                           
##             Sensitivity : 0.59221         
##             Specificity : 0.96157         
##          Pos Pred Value : 0.74677         
##          Neg Pred Value : 0.92493         
##              Prevalence : 0.16063         
##          Detection Rate : 0.09513         
##    Detection Prevalence : 0.12739         
##       Balanced Accuracy : 0.77689         
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 2  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#          0.9022  0.6044   0.59221    0.96157   0.16063


  #MODEL 3
  #LASSO Regression analysis-k-fold cross validation
#control specs will be the same as the previous model

#we have to create a vector for potential lambdas
lambda_vector <-10^seq(-5,5,length=500)
set.seed(1992)
Model3 <-train(Attrition_Flag~.,data =training,
               method="glmnet",
               tuneGrid=expand.grid(alpha=1,lambda=lambda_vector),
               trControl=ctrl_specs,
               preProcess=c("center","scale"),
               na.action = na.omit)
Model3
## glmnet 
## 
## 7089 samples
##   19 predictor
##    2 classes: 'Quit', 'Stay' 
## 
## Pre-processing: centered (32), scaled (32) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 6380, 6380, 6380, 6380, 6380, 6380, ... 
## Resampling results across tuning parameters:
## 
##   lambda        Accuracy   Kappa      
##   1.000000e-05  0.9053457  0.612617112
##   1.047225e-05  0.9053457  0.612617112
##   1.096681e-05  0.9053457  0.612617112
##   1.148472e-05  0.9053457  0.612617112
##   1.202708e-05  0.9053457  0.612617112
##   1.259506e-05  0.9053457  0.612617112
##   1.318987e-05  0.9053457  0.612617112
##   1.381276e-05  0.9053457  0.612617112
##   1.446507e-05  0.9053457  0.612617112
##   1.514819e-05  0.9053457  0.612617112
##   1.586357e-05  0.9053457  0.612617112
##   1.661273e-05  0.9053457  0.612617112
##   1.739726e-05  0.9053457  0.612617112
##   1.821885e-05  0.9053457  0.612617112
##   1.907924e-05  0.9053457  0.612617112
##   1.998026e-05  0.9053457  0.612617112
##   2.092383e-05  0.9053457  0.612617112
##   2.191197e-05  0.9053457  0.612617112
##   2.294676e-05  0.9053457  0.612617112
##   2.403043e-05  0.9053457  0.612617112
##   2.516527e-05  0.9053457  0.612617112
##   2.635371e-05  0.9053457  0.612617112
##   2.759826e-05  0.9053457  0.612617112
##   2.890160e-05  0.9053457  0.612617112
##   3.026648e-05  0.9053457  0.612617112
##   3.169582e-05  0.9053457  0.612617112
##   3.319266e-05  0.9053457  0.612617112
##   3.476019e-05  0.9053457  0.612617112
##   3.640175e-05  0.9053457  0.612617112
##   3.812083e-05  0.9053457  0.612617112
##   3.992109e-05  0.9053457  0.612617112
##   4.180637e-05  0.9053457  0.612617112
##   4.378069e-05  0.9053457  0.612617112
##   4.584824e-05  0.9053457  0.612617112
##   4.801343e-05  0.9053457  0.612617112
##   5.028087e-05  0.9053457  0.612617112
##   5.265540e-05  0.9053457  0.612617112
##   5.514206e-05  0.9053457  0.612617112
##   5.774615e-05  0.9053457  0.612617112
##   6.047322e-05  0.9053457  0.612617112
##   6.332908e-05  0.9053457  0.612617112
##   6.631981e-05  0.9053457  0.612617112
##   6.945178e-05  0.9053457  0.612617112
##   7.273165e-05  0.9053457  0.612617112
##   7.616642e-05  0.9053457  0.612617112
##   7.976339e-05  0.9053457  0.612617112
##   8.353023e-05  0.9053457  0.612617112
##   8.747496e-05  0.9053457  0.612617112
##   9.160598e-05  0.9053457  0.612617112
##   9.593209e-05  0.9053457  0.612617112
##   1.004625e-04  0.9053457  0.612617112
##   1.052069e-04  0.9053457  0.612617112
##   1.101753e-04  0.9053457  0.612617112
##   1.153783e-04  0.9053457  0.612617112
##   1.208271e-04  0.9053457  0.612617112
##   1.265332e-04  0.9053457  0.612617112
##   1.325087e-04  0.9053457  0.612617112
##   1.387665e-04  0.9053457  0.612617112
##   1.453198e-04  0.9053457  0.612617112
##   1.521825e-04  0.9053457  0.612617112
##   1.593694e-04  0.9053457  0.612617112
##   1.668956e-04  0.9052047  0.611909118
##   1.747773e-04  0.9052047  0.611909118
##   1.830312e-04  0.9053457  0.612295933
##   1.916748e-04  0.9053457  0.612295933
##   2.007267e-04  0.9054868  0.612720023
##   2.102061e-04  0.9054868  0.612720023
##   2.201331e-04  0.9053457  0.612273706
##   2.305289e-04  0.9053457  0.612273706
##   2.414157e-04  0.9050636  0.611108696
##   2.528166e-04  0.9049228  0.610115840
##   2.647559e-04  0.9046405  0.608973451
##   2.772591e-04  0.9044995  0.608269082
##   2.903527e-04  0.9044995  0.608269082
##   3.040646e-04  0.9043584  0.607844991
##   3.184242e-04  0.9044995  0.608239273
##   3.334618e-04  0.9043584  0.607529358
##   3.492096e-04  0.9043584  0.607529358
##   3.657011e-04  0.9042174  0.606810367
##   3.829714e-04  0.9042174  0.606810367
##   4.010573e-04  0.9042174  0.606810367
##   4.199973e-04  0.9042174  0.606810367
##   4.398317e-04  0.9040763  0.606039844
##   4.606029e-04  0.9042174  0.606462183
##   4.823549e-04  0.9042174  0.606462183
##   5.051342e-04  0.9040763  0.606019465
##   5.289893e-04  0.9040763  0.606019465
##   5.539709e-04  0.9040763  0.606019465
##   5.801323e-04  0.9040763  0.606019465
##   6.075292e-04  0.9043584  0.606806748
##   6.362198e-04  0.9044997  0.607214200
##   6.662655e-04  0.9046407  0.607638290
##   6.977300e-04  0.9046407  0.607638290
##   7.306804e-04  0.9046407  0.607638290
##   7.651869e-04  0.9046407  0.607638290
##   8.013230e-04  0.9047817  0.608065773
##   8.391656e-04  0.9049228  0.608466275
##   8.787954e-04  0.9050638  0.609170644
##   9.202967e-04  0.9050638  0.609170644
##   9.637579e-04  0.9049228  0.608466275
##   1.009272e-03  0.9047817  0.607715641
##   1.056935e-03  0.9047817  0.607715641
##   1.106848e-03  0.9047817  0.607715641
##   1.159120e-03  0.9049228  0.607779528
##   1.213859e-03  0.9053461  0.609307012
##   1.271184e-03  0.9053461  0.608962475
##   1.331216e-03  0.9054872  0.609342763
##   1.394083e-03  0.9053461  0.608280815
##   1.459919e-03  0.9054872  0.608725291
##   1.528864e-03  0.9059105  0.609975413
##   1.601064e-03  0.9057694  0.609282441
##   1.676675e-03  0.9056284  0.608499105
##   1.755856e-03  0.9054874  0.607693867
##   1.838777e-03  0.9053463  0.606928226
##   1.925614e-03  0.9054874  0.607024829
##   2.016551e-03  0.9057694  0.608102921
##   2.111783e-03  0.9054872  0.606922246
##   2.211512e-03  0.9053459  0.605964841
##   2.315951e-03  0.9050638  0.604141937
##   2.425323e-03  0.9044997  0.601193585
##   2.539859e-03  0.9044997  0.601193585
##   2.659804e-03  0.9040765  0.598416260
##   2.785414e-03  0.9039355  0.597660107
##   2.916956e-03  0.9039357  0.597640234
##   3.054710e-03  0.9033715  0.594679985
##   3.198969e-03  0.9037946  0.596268923
##   3.350041e-03  0.9036536  0.595547719
##   3.508247e-03  0.9037946  0.595625699
##   3.673925e-03  0.9032305  0.592901572
##   3.847427e-03  0.9026661  0.589991247
##   4.029122e-03  0.9025250  0.588707928
##   4.219398e-03  0.9022430  0.587141220
##   4.418660e-03  0.9018198  0.584500858
##   4.627332e-03  0.9019611  0.584559897
##   4.845859e-03  0.9015379  0.581626956
##   5.074705e-03  0.9016792  0.580746912
##   5.314359e-03  0.9012560  0.577863491
##   5.565331e-03  0.9011150  0.577089397
##   5.828155e-03  0.9008325  0.574559616
##   6.103390e-03  0.9012558  0.575108327
##   6.391624e-03  0.9013969  0.574836493
##   6.693470e-03  0.9009740  0.571983477
##   7.009570e-03  0.9006919  0.569274205
##   7.340598e-03  0.8998454  0.564235987
##   7.687260e-03  0.8985760  0.557828729
##   8.050292e-03  0.8982939  0.556213182
##   8.430468e-03  0.8981529  0.554206918
##   8.828599e-03  0.8981531  0.553530385
##   9.245531e-03  0.8980120  0.551624759
##   9.682153e-03  0.8973066  0.547625521
##   1.013939e-02  0.8968835  0.544715436
##   1.061823e-02  0.8966014  0.542095963
##   1.111968e-02  0.8964606  0.540163978
##   1.164481e-02  0.8964606  0.538902770
##   1.219473e-02  0.8960376  0.535248928
##   1.277063e-02  0.8953324  0.529593817
##   1.337373e-02  0.8956145  0.528545441
##   1.400531e-02  0.8947680  0.522388372
##   1.466671e-02  0.8946270  0.519430827
##   1.535935e-02  0.8947680  0.518597210
##   1.608469e-02  0.8936395  0.511604179
##   1.684430e-02  0.8932162  0.507791200
##   1.763977e-02  0.8925109  0.502184615
##   1.847281e-02  0.8919468  0.497112019
##   1.934520e-02  0.8911007  0.490903221
##   2.025878e-02  0.8901130  0.483194991
##   2.121550e-02  0.8887026  0.472126051
##   2.221741e-02  0.8877153  0.462271915
##   2.326663e-02  0.8867275  0.454951723
##   2.436540e-02  0.8868684  0.452953795
##   2.551606e-02  0.8864453  0.446683129
##   2.672106e-02  0.8854576  0.438662387
##   2.798297e-02  0.8843290  0.430487162
##   2.930447e-02  0.8833415  0.421369222
##   3.068838e-02  0.8805198  0.397753297
##   3.213764e-02  0.8786863  0.380512636
##   3.365535e-02  0.8761471  0.360568396
##   3.524473e-02  0.8741723  0.341626105
##   3.690917e-02  0.8700810  0.307176943
##   3.865221e-02  0.8685287  0.287867441
##   4.047757e-02  0.8647201  0.255105715
##   4.238913e-02  0.8624630  0.232199156
##   4.439097e-02  0.8593599  0.203791341
##   4.648734e-02  0.8542817  0.153206329
##   4.868271e-02  0.8507552  0.118511972
##   5.098176e-02  0.8486394  0.098366885
##   5.338938e-02  0.8465233  0.077866009
##   5.591071e-02  0.8448306  0.060349585
##   5.855110e-02  0.8434198  0.045223314
##   6.131619e-02  0.8420089  0.027581235
##   6.421186e-02  0.8408804  0.016062760
##   6.724427e-02  0.8394699  0.001478518
##   7.041990e-02  0.8393287  0.000000000
##   7.374549e-02  0.8393287  0.000000000
##   7.722814e-02  0.8393287  0.000000000
##   8.087525e-02  0.8393287  0.000000000
##   8.469460e-02  0.8393287  0.000000000
##   8.869432e-02  0.8393287  0.000000000
##   9.288292e-02  0.8393287  0.000000000
##   9.726934e-02  0.8393287  0.000000000
##   1.018629e-01  0.8393287  0.000000000
##   1.066734e-01  0.8393287  0.000000000
##   1.117111e-01  0.8393287  0.000000000
##   1.169866e-01  0.8393287  0.000000000
##   1.225114e-01  0.8393287  0.000000000
##   1.282970e-01  0.8393287  0.000000000
##   1.343558e-01  0.8393287  0.000000000
##   1.407008e-01  0.8393287  0.000000000
##   1.473454e-01  0.8393287  0.000000000
##   1.543038e-01  0.8393287  0.000000000
##   1.615909e-01  0.8393287  0.000000000
##   1.692220e-01  0.8393287  0.000000000
##   1.772136e-01  0.8393287  0.000000000
##   1.855825e-01  0.8393287  0.000000000
##   1.943467e-01  0.8393287  0.000000000
##   2.035248e-01  0.8393287  0.000000000
##   2.131362e-01  0.8393287  0.000000000
##   2.232016e-01  0.8393287  0.000000000
##   2.337424e-01  0.8393287  0.000000000
##   2.447809e-01  0.8393287  0.000000000
##   2.563407e-01  0.8393287  0.000000000
##   2.684465e-01  0.8393287  0.000000000
##   2.811239e-01  0.8393287  0.000000000
##   2.944000e-01  0.8393287  0.000000000
##   3.083031e-01  0.8393287  0.000000000
##   3.228628e-01  0.8393287  0.000000000
##   3.381101e-01  0.8393287  0.000000000
##   3.540774e-01  0.8393287  0.000000000
##   3.707988e-01  0.8393287  0.000000000
##   3.883098e-01  0.8393287  0.000000000
##   4.066478e-01  0.8393287  0.000000000
##   4.258518e-01  0.8393287  0.000000000
##   4.459628e-01  0.8393287  0.000000000
##   4.670234e-01  0.8393287  0.000000000
##   4.890787e-01  0.8393287  0.000000000
##   5.121755e-01  0.8393287  0.000000000
##   5.363631e-01  0.8393287  0.000000000
##   5.616930e-01  0.8393287  0.000000000
##   5.882190e-01  0.8393287  0.000000000
##   6.159978e-01  0.8393287  0.000000000
##   6.450884e-01  0.8393287  0.000000000
##   6.755528e-01  0.8393287  0.000000000
##   7.074559e-01  0.8393287  0.000000000
##   7.408657e-01  0.8393287  0.000000000
##   7.758532e-01  0.8393287  0.000000000
##   8.124930e-01  0.8393287  0.000000000
##   8.508632e-01  0.8393287  0.000000000
##   8.910453e-01  0.8393287  0.000000000
##   9.331251e-01  0.8393287  0.000000000
##   9.771921e-01  0.8393287  0.000000000
##   1.023340e+00  0.8393287  0.000000000
##   1.071668e+00  0.8393287  0.000000000
##   1.122277e+00  0.8393287  0.000000000
##   1.175277e+00  0.8393287  0.000000000
##   1.230780e+00  0.8393287  0.000000000
##   1.288904e+00  0.8393287  0.000000000
##   1.349772e+00  0.8393287  0.000000000
##   1.413516e+00  0.8393287  0.000000000
##   1.480269e+00  0.8393287  0.000000000
##   1.550175e+00  0.8393287  0.000000000
##   1.623382e+00  0.8393287  0.000000000
##   1.700047e+00  0.8393287  0.000000000
##   1.780332e+00  0.8393287  0.000000000
##   1.864409e+00  0.8393287  0.000000000
##   1.952456e+00  0.8393287  0.000000000
##   2.044661e+00  0.8393287  0.000000000
##   2.141220e+00  0.8393287  0.000000000
##   2.242340e+00  0.8393287  0.000000000
##   2.348235e+00  0.8393287  0.000000000
##   2.459130e+00  0.8393287  0.000000000
##   2.575263e+00  0.8393287  0.000000000
##   2.696881e+00  0.8393287  0.000000000
##   2.824241e+00  0.8393287  0.000000000
##   2.957617e+00  0.8393287  0.000000000
##   3.097291e+00  0.8393287  0.000000000
##   3.243561e+00  0.8393287  0.000000000
##   3.396739e+00  0.8393287  0.000000000
##   3.557150e+00  0.8393287  0.000000000
##   3.725137e+00  0.8393287  0.000000000
##   3.901058e+00  0.8393287  0.000000000
##   4.085286e+00  0.8393287  0.000000000
##   4.278214e+00  0.8393287  0.000000000
##   4.480254e+00  0.8393287  0.000000000
##   4.691835e+00  0.8393287  0.000000000
##   4.913407e+00  0.8393287  0.000000000
##   5.145444e+00  0.8393287  0.000000000
##   5.388438e+00  0.8393287  0.000000000
##   5.642908e+00  0.8393287  0.000000000
##   5.909396e+00  0.8393287  0.000000000
##   6.188468e+00  0.8393287  0.000000000
##   6.480720e+00  0.8393287  0.000000000
##   6.786773e+00  0.8393287  0.000000000
##   7.107280e+00  0.8393287  0.000000000
##   7.442922e+00  0.8393287  0.000000000
##   7.794416e+00  0.8393287  0.000000000
##   8.162509e+00  0.8393287  0.000000000
##   8.547985e+00  0.8393287  0.000000000
##   8.951665e+00  0.8393287  0.000000000
##   9.374409e+00  0.8393287  0.000000000
##   9.817117e+00  0.8393287  0.000000000
##   1.028073e+01  0.8393287  0.000000000
##   1.076624e+01  0.8393287  0.000000000
##   1.127468e+01  0.8393287  0.000000000
##   1.180713e+01  0.8393287  0.000000000
##   1.236472e+01  0.8393287  0.000000000
##   1.294865e+01  0.8393287  0.000000000
##   1.356015e+01  0.8393287  0.000000000
##   1.420053e+01  0.8393287  0.000000000
##   1.487115e+01  0.8393287  0.000000000
##   1.557345e+01  0.8393287  0.000000000
##   1.630891e+01  0.8393287  0.000000000
##   1.707910e+01  0.8393287  0.000000000
##   1.788566e+01  0.8393287  0.000000000
##   1.873032e+01  0.8393287  0.000000000
##   1.961486e+01  0.8393287  0.000000000
##   2.054117e+01  0.8393287  0.000000000
##   2.151123e+01  0.8393287  0.000000000
##   2.252711e+01  0.8393287  0.000000000
##   2.359095e+01  0.8393287  0.000000000
##   2.470504e+01  0.8393287  0.000000000
##   2.587174e+01  0.8393287  0.000000000
##   2.709354e+01  0.8393287  0.000000000
##   2.837304e+01  0.8393287  0.000000000
##   2.971296e+01  0.8393287  0.000000000
##   3.111616e+01  0.8393287  0.000000000
##   3.258562e+01  0.8393287  0.000000000
##   3.412449e+01  0.8393287  0.000000000
##   3.573602e+01  0.8393287  0.000000000
##   3.742366e+01  0.8393287  0.000000000
##   3.919100e+01  0.8393287  0.000000000
##   4.104181e+01  0.8393287  0.000000000
##   4.298001e+01  0.8393287  0.000000000
##   4.500975e+01  0.8393287  0.000000000
##   4.713535e+01  0.8393287  0.000000000
##   4.936132e+01  0.8393287  0.000000000
##   5.169242e+01  0.8393287  0.000000000
##   5.413360e+01  0.8393287  0.000000000
##   5.669007e+01  0.8393287  0.000000000
##   5.936727e+01  0.8393287  0.000000000
##   6.217090e+01  0.8393287  0.000000000
##   6.510694e+01  0.8393287  0.000000000
##   6.818162e+01  0.8393287  0.000000000
##   7.140151e+01  0.8393287  0.000000000
##   7.477346e+01  0.8393287  0.000000000
##   7.830465e+01  0.8393287  0.000000000
##   8.200261e+01  0.8393287  0.000000000
##   8.587519e+01  0.8393287  0.000000000
##   8.993067e+01  0.8393287  0.000000000
##   9.417766e+01  0.8393287  0.000000000
##   9.862522e+01  0.8393287  0.000000000
##   1.032828e+02  0.8393287  0.000000000
##   1.081604e+02  0.8393287  0.000000000
##   1.132683e+02  0.8393287  0.000000000
##   1.186174e+02  0.8393287  0.000000000
##   1.242191e+02  0.8393287  0.000000000
##   1.300854e+02  0.8393287  0.000000000
##   1.362287e+02  0.8393287  0.000000000
##   1.426621e+02  0.8393287  0.000000000
##   1.493993e+02  0.8393287  0.000000000
##   1.564548e+02  0.8393287  0.000000000
##   1.638434e+02  0.8393287  0.000000000
##   1.715809e+02  0.8393287  0.000000000
##   1.796838e+02  0.8393287  0.000000000
##   1.881694e+02  0.8393287  0.000000000
##   1.970558e+02  0.8393287  0.000000000
##   2.063618e+02  0.8393287  0.000000000
##   2.161073e+02  0.8393287  0.000000000
##   2.263130e+02  0.8393287  0.000000000
##   2.370006e+02  0.8393287  0.000000000
##   2.481930e+02  0.8393287  0.000000000
##   2.599140e+02  0.8393287  0.000000000
##   2.721885e+02  0.8393287  0.000000000
##   2.850426e+02  0.8393287  0.000000000
##   2.985038e+02  0.8393287  0.000000000
##   3.126007e+02  0.8393287  0.000000000
##   3.273634e+02  0.8393287  0.000000000
##   3.428231e+02  0.8393287  0.000000000
##   3.590130e+02  0.8393287  0.000000000
##   3.759675e+02  0.8393287  0.000000000
##   3.937226e+02  0.8393287  0.000000000
##   4.123163e+02  0.8393287  0.000000000
##   4.317880e+02  0.8393287  0.000000000
##   4.521792e+02  0.8393287  0.000000000
##   4.735335e+02  0.8393287  0.000000000
##   4.958962e+02  0.8393287  0.000000000
##   5.193150e+02  0.8393287  0.000000000
##   5.438397e+02  0.8393287  0.000000000
##   5.695227e+02  0.8393287  0.000000000
##   5.964185e+02  0.8393287  0.000000000
##   6.245845e+02  0.8393287  0.000000000
##   6.540806e+02  0.8393287  0.000000000
##   6.849697e+02  0.8393287  0.000000000
##   7.173175e+02  0.8393287  0.000000000
##   7.511929e+02  0.8393287  0.000000000
##   7.866682e+02  0.8393287  0.000000000
##   8.238187e+02  0.8393287  0.000000000
##   8.627237e+02  0.8393287  0.000000000
##   9.034660e+02  0.8393287  0.000000000
##   9.461324e+02  0.8393287  0.000000000
##   9.908137e+02  0.8393287  0.000000000
##   1.037605e+03  0.8393287  0.000000000
##   1.086606e+03  0.8393287  0.000000000
##   1.137921e+03  0.8393287  0.000000000
##   1.191660e+03  0.8393287  0.000000000
##   1.247936e+03  0.8393287  0.000000000
##   1.306870e+03  0.8393287  0.000000000
##   1.368587e+03  0.8393287  0.000000000
##   1.433219e+03  0.8393287  0.000000000
##   1.500903e+03  0.8393287  0.000000000
##   1.571784e+03  0.8393287  0.000000000
##   1.646012e+03  0.8393287  0.000000000
##   1.723745e+03  0.8393287  0.000000000
##   1.805149e+03  0.8393287  0.000000000
##   1.890397e+03  0.8393287  0.000000000
##   1.979672e+03  0.8393287  0.000000000
##   2.073162e+03  0.8393287  0.000000000
##   2.171068e+03  0.8393287  0.000000000
##   2.273597e+03  0.8393287  0.000000000
##   2.380968e+03  0.8393287  0.000000000
##   2.493409e+03  0.8393287  0.000000000
##   2.611161e+03  0.8393287  0.000000000
##   2.734474e+03  0.8393287  0.000000000
##   2.863610e+03  0.8393287  0.000000000
##   2.998844e+03  0.8393287  0.000000000
##   3.140465e+03  0.8393287  0.000000000
##   3.288774e+03  0.8393287  0.000000000
##   3.444087e+03  0.8393287  0.000000000
##   3.606735e+03  0.8393287  0.000000000
##   3.777064e+03  0.8393287  0.000000000
##   3.955436e+03  0.8393287  0.000000000
##   4.142232e+03  0.8393287  0.000000000
##   4.337850e+03  0.8393287  0.000000000
##   4.542706e+03  0.8393287  0.000000000
##   4.757236e+03  0.8393287  0.000000000
##   4.981898e+03  0.8393287  0.000000000
##   5.217169e+03  0.8393287  0.000000000
##   5.463550e+03  0.8393287  0.000000000
##   5.721568e+03  0.8393287  0.000000000
##   5.991770e+03  0.8393287  0.000000000
##   6.274732e+03  0.8393287  0.000000000
##   6.571058e+03  0.8393287  0.000000000
##   6.881377e+03  0.8393287  0.000000000
##   7.206351e+03  0.8393287  0.000000000
##   7.546673e+03  0.8393287  0.000000000
##   7.903066e+03  0.8393287  0.000000000
##   8.276289e+03  0.8393287  0.000000000
##   8.667139e+03  0.8393287  0.000000000
##   9.076446e+03  0.8393287  0.000000000
##   9.505083e+03  0.8393287  0.000000000
##   9.953962e+03  0.8393287  0.000000000
##   1.042404e+04  0.8393287  0.000000000
##   1.091632e+04  0.8393287  0.000000000
##   1.143184e+04  0.8393287  0.000000000
##   1.197171e+04  0.8393287  0.000000000
##   1.253708e+04  0.8393287  0.000000000
##   1.312915e+04  0.8393287  0.000000000
##   1.374917e+04  0.8393287  0.000000000
##   1.439848e+04  0.8393287  0.000000000
##   1.507845e+04  0.8393287  0.000000000
##   1.579053e+04  0.8393287  0.000000000
##   1.653624e+04  0.8393287  0.000000000
##   1.731717e+04  0.8393287  0.000000000
##   1.813498e+04  0.8393287  0.000000000
##   1.899141e+04  0.8393287  0.000000000
##   1.988828e+04  0.8393287  0.000000000
##   2.082751e+04  0.8393287  0.000000000
##   2.181109e+04  0.8393287  0.000000000
##   2.284112e+04  0.8393287  0.000000000
##   2.391980e+04  0.8393287  0.000000000
##   2.504942e+04  0.8393287  0.000000000
##   2.623238e+04  0.8393287  0.000000000
##   2.747121e+04  0.8393287  0.000000000
##   2.876854e+04  0.8393287  0.000000000
##   3.012714e+04  0.8393287  0.000000000
##   3.154990e+04  0.8393287  0.000000000
##   3.303985e+04  0.8393287  0.000000000
##   3.460016e+04  0.8393287  0.000000000
##   3.623416e+04  0.8393287  0.000000000
##   3.794533e+04  0.8393287  0.000000000
##   3.973730e+04  0.8393287  0.000000000
##   4.161391e+04  0.8393287  0.000000000
##   4.357913e+04  0.8393287  0.000000000
##   4.563716e+04  0.8393287  0.000000000
##   4.779239e+04  0.8393287  0.000000000
##   5.004939e+04  0.8393287  0.000000000
##   5.241298e+04  0.8393287  0.000000000
##   5.488820e+04  0.8393287  0.000000000
##   5.748030e+04  0.8393287  0.000000000
##   6.019482e+04  0.8393287  0.000000000
##   6.303753e+04  0.8393287  0.000000000
##   6.601449e+04  0.8393287  0.000000000
##   6.913204e+04  0.8393287  0.000000000
##   7.239681e+04  0.8393287  0.000000000
##   7.581576e+04  0.8393287  0.000000000
##   7.939618e+04  0.8393287  0.000000000
##   8.314568e+04  0.8393287  0.000000000
##   8.707225e+04  0.8393287  0.000000000
##   9.118425e+04  0.8393287  0.000000000
##   9.549045e+04  0.8393287  0.000000000
##   1.000000e+05  0.8393287  0.000000000
## 
## Tuning parameter 'alpha' was held constant at a value of 1
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 1 and lambda = 0.001528864.
#get the best tuning parameters
Model3$bestTune
##     alpha      lambda
## 110     1 0.001528864
# alpha       lambda
#   1   0.001528864

#just the best lamda
Model3$bestTune$lambda
## [1] 0.001528864
#0.001528864

#LASSO Regression model coefficients(Parameter Estimates)
round(coef(Model3$finalModel,Model3$bestTune$lambda),3)
## 33 x 1 sparse Matrix of class "dgCMatrix"
##                                    1
## (Intercept)                    2.888
## Customer_Age                   0.013
## GenderM                        0.300
## Dependent_count               -0.149
## Education_LevelDoctorate      -0.045
## Education_LevelGraduate        0.024
## Education_LevelHigh School     0.037
## Education_LevelPost-Graduate  -0.061
## Education_LevelUneducated     -0.011
## Education_LevelUnknown        -0.016
## Marital_StatusMarried          0.228
## Marital_StatusSingle          -0.028
## Marital_StatusUnknown          .    
## Income_Category$40K - $60K     0.077
## Income_Category$60K - $80K     0.034
## Income_Category$80K - $120K   -0.059
## Income_CategoryLess than $40K  .    
## Income_CategoryUnknown         0.017
## Card_CategoryGold             -0.098
## Card_CategoryPlatinum          .    
## Card_CategorySilver           -0.048
## Months_on_book                 0.054
## Total_Relationship_Count       0.681
## Months_Inactive_12_mon        -0.473
## Contacts_Count_12_mon         -0.564
## Credit_Limit                   0.054
## Total_Revolving_Bal            0.730
## Avg_Open_To_Buy                .    
## Total_Amt_Chng_Q4_Q1           0.051
## Total_Trans_Amt               -1.435
## Total_Trans_Ct                 2.555
## Total_Ct_Chng_Q4_Q1            0.634
## Avg_Utilization_Ratio          .
#Plot log(lambda) & RMSE
plot(log(Model3$results$lambda),Model3$results$RMSE,
     xlab="Log(lambda)",
     ylab="RMSE",
     xlim=c(0,100))

log(Model3$bestTune$lambda)
## [1] -6.483231
#variable importance
varImp(Model3)
## glmnet variable importance
## 
##   only 20 most important variables shown (out of 32)
## 
##                              Overall
## Total_Trans_Ct               100.000
## Total_Trans_Amt               56.142
## Total_Revolving_Bal           28.581
## Total_Relationship_Count      26.661
## Total_Ct_Chng_Q4_Q1           24.794
## Contacts_Count_12_mon         22.059
## Months_Inactive_12_mon        18.516
## GenderM                       11.725
## Marital_StatusMarried          8.930
## Dependent_count                5.836
## Card_CategoryGold              3.829
## Income_Category$40K - $60K     3.017
## Education_LevelPost-Graduate   2.391
## Income_Category$80K - $120K    2.325
## Credit_Limit                   2.133
## Months_on_book                 2.101
## Total_Amt_Chng_Q4_Q1           1.996
## Card_CategorySilver            1.885
## Education_LevelDoctorate       1.753
## Education_LevelHigh School     1.445
#plot variable importance
ggplot(varImp(Model3))+
  labs(title = "Model 3 Variable importance Rank")

#forming confussion matrix
predictions_Lasso<-predict(Model3,newdata = testing)
cm_3<-confusionMatrix(data=predictions_Lasso,testing$Attrition_Flag)
cm_3
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  279   90
##       Stay  209 2460
##                                           
##                Accuracy : 0.9016          
##                  95% CI : (0.8904, 0.9119)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5951          
##                                           
##  Mcnemar's Test P-Value : 8.847e-12       
##                                           
##             Sensitivity : 0.57172         
##             Specificity : 0.96471         
##          Pos Pred Value : 0.75610         
##          Neg Pred Value : 0.92169         
##              Prevalence : 0.16063         
##          Detection Rate : 0.09184         
##    Detection Prevalence : 0.12146         
##       Balanced Accuracy : 0.76821         
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 3  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#          0.9016   0.5951   0.57172   0.96471   0.16063


  #MODEL 4
    #4.a  with 2 repeats and 3 folds
    #4.b with 3 repeats and 5 folds
#RANDOM FOREST MODELLING-RPART

#4.a
rf_ctr_specs1 <-trainControl(method = "repeatedcv",
                            repeats = 2,
                            number=3,
                            search = "random")
Model4.a <-train(Attrition_Flag~.,data=training,
               method="rf",
               trControl=rf_ctr_specs1)
plot(varImp(Model4.a,scale=F),main="Model 4.a RandomForest-3 FOLDS-2-REPEATED CV")

#Forming confusion matrix
predictions_rf1<-predict(Model4.a,newdata=testing)
cm_4.a<-confusionMatrix(predictions_rf1,testing$Attrition_Flag)
cm_4.a
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  423   24
##       Stay   65 2526
##                                           
##                Accuracy : 0.9707          
##                  95% CI : (0.9641, 0.9764)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.8875          
##                                           
##  Mcnemar's Test P-Value : 2.235e-05       
##                                           
##             Sensitivity : 0.8668          
##             Specificity : 0.9906          
##          Pos Pred Value : 0.9463          
##          Neg Pred Value : 0.9749          
##              Prevalence : 0.1606          
##          Detection Rate : 0.1392          
##    Detection Prevalence : 0.1471          
##       Balanced Accuracy : 0.9287          
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 4.a  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#          0.9707  0.8875   0.8668    0.9906   0.1606

#4.b
rf_ctr_specs <-trainControl(method = "repeatedcv",
                            repeats = 3,
                            number=5,
                            search = "random")
Model4.b <-train(Attrition_Flag~.,data=training,
               method="rf",
               trControl=rf_ctr_specs)#THIS WILL TAKE A BIT LONGER THAN 4.A SINCE THE NUMBER OF FOLDS IS BIG AND IT REPEATS 3 TIMES
plot(varImp(Model4.b,scale=F),main="Model 4b RandomForest-5 FOLDS-3-REAPEATED CV")

#Forming confusion matrix
predictions_rf<-predict(Model4.b,newdata=testing)
cm_4.b<-confusionMatrix(predictions_rf,testing$Attrition_Flag)
cm_4.b
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  418   20
##       Stay   70 2530
##                                           
##                Accuracy : 0.9704          
##                  95% CI : (0.9637, 0.9761)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.8854          
##                                           
##  Mcnemar's Test P-Value : 2.404e-07       
##                                           
##             Sensitivity : 0.8566          
##             Specificity : 0.9922          
##          Pos Pred Value : 0.9543          
##          Neg Pred Value : 0.9731          
##              Prevalence : 0.1606          
##          Detection Rate : 0.1376          
##    Detection Prevalence : 0.1442          
##       Balanced Accuracy : 0.9244          
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 4.b  ACCURACY  KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#             0.9704  0.8854   0.8566   0.9922   0.1606



  #MODEL 5
  #DECISION TREES-RPART

Model5 <-rpart(Attrition_Flag~.,data = training,method="class")
predictions_rpart<-predict(Model5,testing,type="class")
rpart.plot(Model5,
           type = 1,
           extra=100,
           branch.lty=3,
           box.palette = "RdYlGn",
           tweak = 1.6,)

cm_5<-confusionMatrix(predictions_rpart,testing$Attrition_Flag)
cm_5
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  406  111
##       Stay   82 2439
##                                           
##                Accuracy : 0.9365          
##                  95% CI : (0.9272, 0.9449)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2e-16         
##                                           
##                   Kappa : 0.7699          
##                                           
##  Mcnemar's Test P-Value : 0.04385         
##                                           
##             Sensitivity : 0.8320          
##             Specificity : 0.9565          
##          Pos Pred Value : 0.7853          
##          Neg Pred Value : 0.9675          
##              Prevalence : 0.1606          
##          Detection Rate : 0.1336          
##    Detection Prevalence : 0.1702          
##       Balanced Accuracy : 0.8942          
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 5    ACCURACY    KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#             0.9365   0.7699   0.8320       0.9565   0.1606


  #MODEL 6
  #RIDGE REGRESSION -K-FOLD CROSS-VALIDATION FRAMEWORK

#control specs will be different from that of LASSO  model
#difference being alpha, in the case of ridge regression alpha=0 not 1
rdg_ctrl_specs <-trainControl(method = "cv",
                          savePredictions = "all",
                          number = 10,
                          classProbs = T)
#we have to create a vector for potential lambdas
lambda_vector <-10^seq(-5,5,length=500)
Model6 <-train(Attrition_Flag~.,data =training,
               method="glmnet",
               tuneGrid=expand.grid(alpha=0,lambda=lambda_vector),
               trControl=rdg_ctrl_specs,
               preProcess=c("center","scale"),
               na.action = na.omit)
Model6
## glmnet 
## 
## 7089 samples
##   19 predictor
##    2 classes: 'Quit', 'Stay' 
## 
## Pre-processing: centered (32), scaled (32) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 6380, 6380, 6380, 6380, 6380, 6380, ... 
## Resampling results across tuning parameters:
## 
##   lambda        Accuracy   Kappa      
##   1.000000e-05  0.8985738  0.550924140
##   1.047225e-05  0.8985738  0.550924140
##   1.096681e-05  0.8985738  0.550924140
##   1.148472e-05  0.8985738  0.550924140
##   1.202708e-05  0.8985738  0.550924140
##   1.259506e-05  0.8985738  0.550924140
##   1.318987e-05  0.8985738  0.550924140
##   1.381276e-05  0.8985738  0.550924140
##   1.446507e-05  0.8985738  0.550924140
##   1.514819e-05  0.8985738  0.550924140
##   1.586357e-05  0.8985738  0.550924140
##   1.661273e-05  0.8985738  0.550924140
##   1.739726e-05  0.8985738  0.550924140
##   1.821885e-05  0.8985738  0.550924140
##   1.907924e-05  0.8985738  0.550924140
##   1.998026e-05  0.8985738  0.550924140
##   2.092383e-05  0.8985738  0.550924140
##   2.191197e-05  0.8985738  0.550924140
##   2.294676e-05  0.8985738  0.550924140
##   2.403043e-05  0.8985738  0.550924140
##   2.516527e-05  0.8985738  0.550924140
##   2.635371e-05  0.8985738  0.550924140
##   2.759826e-05  0.8985738  0.550924140
##   2.890160e-05  0.8985738  0.550924140
##   3.026648e-05  0.8985738  0.550924140
##   3.169582e-05  0.8985738  0.550924140
##   3.319266e-05  0.8985738  0.550924140
##   3.476019e-05  0.8985738  0.550924140
##   3.640175e-05  0.8985738  0.550924140
##   3.812083e-05  0.8985738  0.550924140
##   3.992109e-05  0.8985738  0.550924140
##   4.180637e-05  0.8985738  0.550924140
##   4.378069e-05  0.8985738  0.550924140
##   4.584824e-05  0.8985738  0.550924140
##   4.801343e-05  0.8985738  0.550924140
##   5.028087e-05  0.8985738  0.550924140
##   5.265540e-05  0.8985738  0.550924140
##   5.514206e-05  0.8985738  0.550924140
##   5.774615e-05  0.8985738  0.550924140
##   6.047322e-05  0.8985738  0.550924140
##   6.332908e-05  0.8985738  0.550924140
##   6.631981e-05  0.8985738  0.550924140
##   6.945178e-05  0.8985738  0.550924140
##   7.273165e-05  0.8985738  0.550924140
##   7.616642e-05  0.8985738  0.550924140
##   7.976339e-05  0.8985738  0.550924140
##   8.353023e-05  0.8985738  0.550924140
##   8.747496e-05  0.8985738  0.550924140
##   9.160598e-05  0.8985738  0.550924140
##   9.593209e-05  0.8985738  0.550924140
##   1.004625e-04  0.8985738  0.550924140
##   1.052069e-04  0.8985738  0.550924140
##   1.101753e-04  0.8985738  0.550924140
##   1.153783e-04  0.8985738  0.550924140
##   1.208271e-04  0.8985738  0.550924140
##   1.265332e-04  0.8985738  0.550924140
##   1.325087e-04  0.8985738  0.550924140
##   1.387665e-04  0.8985738  0.550924140
##   1.453198e-04  0.8985738  0.550924140
##   1.521825e-04  0.8985738  0.550924140
##   1.593694e-04  0.8985738  0.550924140
##   1.668956e-04  0.8985738  0.550924140
##   1.747773e-04  0.8985738  0.550924140
##   1.830312e-04  0.8985738  0.550924140
##   1.916748e-04  0.8985738  0.550924140
##   2.007267e-04  0.8985738  0.550924140
##   2.102061e-04  0.8985738  0.550924140
##   2.201331e-04  0.8985738  0.550924140
##   2.305289e-04  0.8985738  0.550924140
##   2.414157e-04  0.8985738  0.550924140
##   2.528166e-04  0.8985738  0.550924140
##   2.647559e-04  0.8985738  0.550924140
##   2.772591e-04  0.8985738  0.550924140
##   2.903527e-04  0.8985738  0.550924140
##   3.040646e-04  0.8985738  0.550924140
##   3.184242e-04  0.8985738  0.550924140
##   3.334618e-04  0.8985738  0.550924140
##   3.492096e-04  0.8985738  0.550924140
##   3.657011e-04  0.8985738  0.550924140
##   3.829714e-04  0.8985738  0.550924140
##   4.010573e-04  0.8985738  0.550924140
##   4.199973e-04  0.8985738  0.550924140
##   4.398317e-04  0.8985738  0.550924140
##   4.606029e-04  0.8985738  0.550924140
##   4.823549e-04  0.8985738  0.550924140
##   5.051342e-04  0.8985738  0.550924140
##   5.289893e-04  0.8985738  0.550924140
##   5.539709e-04  0.8985738  0.550924140
##   5.801323e-04  0.8985738  0.550924140
##   6.075292e-04  0.8985738  0.550924140
##   6.362198e-04  0.8985738  0.550924140
##   6.662655e-04  0.8985738  0.550924140
##   6.977300e-04  0.8985738  0.550924140
##   7.306804e-04  0.8985738  0.550924140
##   7.651869e-04  0.8985738  0.550924140
##   8.013230e-04  0.8985738  0.550924140
##   8.391656e-04  0.8985738  0.550924140
##   8.787954e-04  0.8985738  0.550924140
##   9.202967e-04  0.8985738  0.550924140
##   9.637579e-04  0.8985738  0.550924140
##   1.009272e-03  0.8985738  0.550924140
##   1.056935e-03  0.8985738  0.550924140
##   1.106848e-03  0.8985738  0.550924140
##   1.159120e-03  0.8985738  0.550924140
##   1.213859e-03  0.8985738  0.550924140
##   1.271184e-03  0.8985738  0.550924140
##   1.331216e-03  0.8985738  0.550924140
##   1.394083e-03  0.8985738  0.550924140
##   1.459919e-03  0.8985738  0.550924140
##   1.528864e-03  0.8985738  0.550924140
##   1.601064e-03  0.8985738  0.550924140
##   1.676675e-03  0.8985738  0.550924140
##   1.755856e-03  0.8985738  0.550924140
##   1.838777e-03  0.8985738  0.550924140
##   1.925614e-03  0.8985738  0.550924140
##   2.016551e-03  0.8985738  0.550924140
##   2.111783e-03  0.8985738  0.550924140
##   2.211512e-03  0.8985738  0.550924140
##   2.315951e-03  0.8985738  0.550924140
##   2.425323e-03  0.8985738  0.550924140
##   2.539859e-03  0.8985738  0.550924140
##   2.659804e-03  0.8985738  0.550924140
##   2.785414e-03  0.8985738  0.550924140
##   2.916956e-03  0.8985738  0.550924140
##   3.054710e-03  0.8985738  0.550924140
##   3.198969e-03  0.8985738  0.550924140
##   3.350041e-03  0.8985738  0.550924140
##   3.508247e-03  0.8985738  0.550924140
##   3.673925e-03  0.8985738  0.550924140
##   3.847427e-03  0.8985738  0.550924140
##   4.029122e-03  0.8985738  0.550924140
##   4.219398e-03  0.8985738  0.550924140
##   4.418660e-03  0.8985738  0.550924140
##   4.627332e-03  0.8985738  0.550924140
##   4.845859e-03  0.8985738  0.550924140
##   5.074705e-03  0.8985738  0.550924140
##   5.314359e-03  0.8985738  0.550924140
##   5.565331e-03  0.8985738  0.550924140
##   5.828155e-03  0.8985738  0.550924140
##   6.103390e-03  0.8985738  0.550924140
##   6.391624e-03  0.8985738  0.550924140
##   6.693470e-03  0.8985738  0.550924140
##   7.009570e-03  0.8985738  0.550924140
##   7.340598e-03  0.8985738  0.550924140
##   7.687260e-03  0.8985738  0.550924140
##   8.050292e-03  0.8985738  0.550924140
##   8.430468e-03  0.8985738  0.550924140
##   8.828599e-03  0.8985738  0.550924140
##   9.245531e-03  0.8985738  0.550924140
##   9.682153e-03  0.8985738  0.550924140
##   1.013939e-02  0.8985738  0.550924140
##   1.061823e-02  0.8985738  0.550924140
##   1.111968e-02  0.8985738  0.550924140
##   1.164481e-02  0.8985738  0.550924140
##   1.219473e-02  0.8985738  0.550924140
##   1.277063e-02  0.8985738  0.550924140
##   1.337373e-02  0.8985738  0.550924140
##   1.400531e-02  0.8985738  0.550924140
##   1.466671e-02  0.8982917  0.548609130
##   1.535935e-02  0.8980096  0.546138482
##   1.608469e-02  0.8974453  0.541708539
##   1.684430e-02  0.8971632  0.539278126
##   1.763977e-02  0.8973044  0.538861671
##   1.847281e-02  0.8965992  0.533986543
##   1.934520e-02  0.8965994  0.533586644
##   2.025878e-02  0.8964584  0.531839802
##   2.121550e-02  0.8963175  0.530196091
##   2.221741e-02  0.8960356  0.527633082
##   2.326663e-02  0.8957535  0.525071782
##   2.436540e-02  0.8960358  0.525941438
##   2.551606e-02  0.8958950  0.524664614
##   2.672106e-02  0.8957539  0.522869512
##   2.798297e-02  0.8958950  0.522547911
##   2.930447e-02  0.8956129  0.520375991
##   3.068838e-02  0.8954719  0.518558576
##   3.213764e-02  0.8956127  0.517408767
##   3.365535e-02  0.8958952  0.517027666
##   3.524473e-02  0.8950487  0.511471036
##   3.690917e-02  0.8946256  0.507555440
##   3.865221e-02  0.8930737  0.497342932
##   4.047757e-02  0.8932150  0.495994284
##   4.238913e-02  0.8929333  0.493263732
##   4.439097e-02  0.8922281  0.486903593
##   4.648734e-02  0.8916637  0.482849933
##   4.868271e-02  0.8912403  0.478125974
##   5.098176e-02  0.8905347  0.472714107
##   5.338938e-02  0.8896885  0.466143888
##   5.591071e-02  0.8892653  0.462368159
##   5.855110e-02  0.8889831  0.459022062
##   6.131619e-02  0.8882776  0.453322328
##   6.421186e-02  0.8864435  0.440455905
##   6.724427e-02  0.8854556  0.431410559
##   7.041990e-02  0.8853145  0.427783900
##   7.374549e-02  0.8844679  0.420724250
##   7.722814e-02  0.8836218  0.412395100
##   8.087525e-02  0.8827753  0.404023547
##   8.469460e-02  0.8817880  0.395885647
##   8.869432e-02  0.8817880  0.394081002
##   9.288292e-02  0.8799539  0.378462813
##   9.726934e-02  0.8788255  0.367619535
##   1.018629e-01  0.8771328  0.354303809
##   1.066734e-01  0.8764278  0.347550810
##   1.117111e-01  0.8743119  0.329971671
##   1.169866e-01  0.8731838  0.319181932
##   1.225114e-01  0.8712092  0.303000168
##   1.282970e-01  0.8686698  0.281575884
##   1.343558e-01  0.8672591  0.268751324
##   1.407008e-01  0.8662718  0.257555454
##   1.473454e-01  0.8654256  0.249808094
##   1.543038e-01  0.8631689  0.230899038
##   1.615909e-01  0.8613353  0.213346724
##   1.692220e-01  0.8589372  0.192379124
##   1.772136e-01  0.8572444  0.177410596
##   1.855825e-01  0.8554107  0.160816843
##   1.943467e-01  0.8532950  0.141363811
##   2.035248e-01  0.8520254  0.128511577
##   2.131362e-01  0.8511793  0.119593128
##   2.232016e-01  0.8489219  0.097052191
##   2.337424e-01  0.8469464  0.077985353
##   2.447809e-01  0.8460998  0.068653003
##   2.563407e-01  0.8448302  0.056085590
##   2.684465e-01  0.8437018  0.044764789
##   2.811239e-01  0.8429966  0.037667051
##   2.944000e-01  0.8421504  0.029072784
##   3.083031e-01  0.8413039  0.020379846
##   3.228628e-01  0.8405983  0.013152797
##   3.381101e-01  0.8403160  0.010227905
##   3.540774e-01  0.8401749  0.008781532
##   3.707988e-01  0.8400339  0.007317943
##   3.883098e-01  0.8400339  0.007317943
##   4.066478e-01  0.8398929  0.005854354
##   4.258518e-01  0.8398929  0.005854354
##   4.459628e-01  0.8397518  0.004390766
##   4.670234e-01  0.8393287  0.000000000
##   4.890787e-01  0.8393287  0.000000000
##   5.121755e-01  0.8393287  0.000000000
##   5.363631e-01  0.8393287  0.000000000
##   5.616930e-01  0.8393287  0.000000000
##   5.882190e-01  0.8393287  0.000000000
##   6.159978e-01  0.8393287  0.000000000
##   6.450884e-01  0.8393287  0.000000000
##   6.755528e-01  0.8393287  0.000000000
##   7.074559e-01  0.8393287  0.000000000
##   7.408657e-01  0.8393287  0.000000000
##   7.758532e-01  0.8393287  0.000000000
##   8.124930e-01  0.8393287  0.000000000
##   8.508632e-01  0.8393287  0.000000000
##   8.910453e-01  0.8393287  0.000000000
##   9.331251e-01  0.8393287  0.000000000
##   9.771921e-01  0.8393287  0.000000000
##   1.023340e+00  0.8393287  0.000000000
##   1.071668e+00  0.8393287  0.000000000
##   1.122277e+00  0.8393287  0.000000000
##   1.175277e+00  0.8393287  0.000000000
##   1.230780e+00  0.8393287  0.000000000
##   1.288904e+00  0.8393287  0.000000000
##   1.349772e+00  0.8393287  0.000000000
##   1.413516e+00  0.8393287  0.000000000
##   1.480269e+00  0.8393287  0.000000000
##   1.550175e+00  0.8393287  0.000000000
##   1.623382e+00  0.8393287  0.000000000
##   1.700047e+00  0.8393287  0.000000000
##   1.780332e+00  0.8393287  0.000000000
##   1.864409e+00  0.8393287  0.000000000
##   1.952456e+00  0.8393287  0.000000000
##   2.044661e+00  0.8393287  0.000000000
##   2.141220e+00  0.8393287  0.000000000
##   2.242340e+00  0.8393287  0.000000000
##   2.348235e+00  0.8393287  0.000000000
##   2.459130e+00  0.8393287  0.000000000
##   2.575263e+00  0.8393287  0.000000000
##   2.696881e+00  0.8393287  0.000000000
##   2.824241e+00  0.8393287  0.000000000
##   2.957617e+00  0.8393287  0.000000000
##   3.097291e+00  0.8393287  0.000000000
##   3.243561e+00  0.8393287  0.000000000
##   3.396739e+00  0.8393287  0.000000000
##   3.557150e+00  0.8393287  0.000000000
##   3.725137e+00  0.8393287  0.000000000
##   3.901058e+00  0.8393287  0.000000000
##   4.085286e+00  0.8393287  0.000000000
##   4.278214e+00  0.8393287  0.000000000
##   4.480254e+00  0.8393287  0.000000000
##   4.691835e+00  0.8393287  0.000000000
##   4.913407e+00  0.8393287  0.000000000
##   5.145444e+00  0.8393287  0.000000000
##   5.388438e+00  0.8393287  0.000000000
##   5.642908e+00  0.8393287  0.000000000
##   5.909396e+00  0.8393287  0.000000000
##   6.188468e+00  0.8393287  0.000000000
##   6.480720e+00  0.8393287  0.000000000
##   6.786773e+00  0.8393287  0.000000000
##   7.107280e+00  0.8393287  0.000000000
##   7.442922e+00  0.8393287  0.000000000
##   7.794416e+00  0.8393287  0.000000000
##   8.162509e+00  0.8393287  0.000000000
##   8.547985e+00  0.8393287  0.000000000
##   8.951665e+00  0.8393287  0.000000000
##   9.374409e+00  0.8393287  0.000000000
##   9.817117e+00  0.8393287  0.000000000
##   1.028073e+01  0.8393287  0.000000000
##   1.076624e+01  0.8393287  0.000000000
##   1.127468e+01  0.8393287  0.000000000
##   1.180713e+01  0.8393287  0.000000000
##   1.236472e+01  0.8393287  0.000000000
##   1.294865e+01  0.8393287  0.000000000
##   1.356015e+01  0.8393287  0.000000000
##   1.420053e+01  0.8393287  0.000000000
##   1.487115e+01  0.8393287  0.000000000
##   1.557345e+01  0.8393287  0.000000000
##   1.630891e+01  0.8393287  0.000000000
##   1.707910e+01  0.8393287  0.000000000
##   1.788566e+01  0.8393287  0.000000000
##   1.873032e+01  0.8393287  0.000000000
##   1.961486e+01  0.8393287  0.000000000
##   2.054117e+01  0.8393287  0.000000000
##   2.151123e+01  0.8393287  0.000000000
##   2.252711e+01  0.8393287  0.000000000
##   2.359095e+01  0.8393287  0.000000000
##   2.470504e+01  0.8393287  0.000000000
##   2.587174e+01  0.8393287  0.000000000
##   2.709354e+01  0.8393287  0.000000000
##   2.837304e+01  0.8393287  0.000000000
##   2.971296e+01  0.8393287  0.000000000
##   3.111616e+01  0.8393287  0.000000000
##   3.258562e+01  0.8393287  0.000000000
##   3.412449e+01  0.8393287  0.000000000
##   3.573602e+01  0.8393287  0.000000000
##   3.742366e+01  0.8393287  0.000000000
##   3.919100e+01  0.8393287  0.000000000
##   4.104181e+01  0.8393287  0.000000000
##   4.298001e+01  0.8393287  0.000000000
##   4.500975e+01  0.8393287  0.000000000
##   4.713535e+01  0.8393287  0.000000000
##   4.936132e+01  0.8393287  0.000000000
##   5.169242e+01  0.8393287  0.000000000
##   5.413360e+01  0.8393287  0.000000000
##   5.669007e+01  0.8393287  0.000000000
##   5.936727e+01  0.8393287  0.000000000
##   6.217090e+01  0.8393287  0.000000000
##   6.510694e+01  0.8393287  0.000000000
##   6.818162e+01  0.8393287  0.000000000
##   7.140151e+01  0.8393287  0.000000000
##   7.477346e+01  0.8393287  0.000000000
##   7.830465e+01  0.8393287  0.000000000
##   8.200261e+01  0.8393287  0.000000000
##   8.587519e+01  0.8393287  0.000000000
##   8.993067e+01  0.8393287  0.000000000
##   9.417766e+01  0.8393287  0.000000000
##   9.862522e+01  0.8393287  0.000000000
##   1.032828e+02  0.8393287  0.000000000
##   1.081604e+02  0.8393287  0.000000000
##   1.132683e+02  0.8393287  0.000000000
##   1.186174e+02  0.8393287  0.000000000
##   1.242191e+02  0.8393287  0.000000000
##   1.300854e+02  0.8393287  0.000000000
##   1.362287e+02  0.8393287  0.000000000
##   1.426621e+02  0.8393287  0.000000000
##   1.493993e+02  0.8393287  0.000000000
##   1.564548e+02  0.8393287  0.000000000
##   1.638434e+02  0.8393287  0.000000000
##   1.715809e+02  0.8393287  0.000000000
##   1.796838e+02  0.8393287  0.000000000
##   1.881694e+02  0.8393287  0.000000000
##   1.970558e+02  0.8393287  0.000000000
##   2.063618e+02  0.8393287  0.000000000
##   2.161073e+02  0.8393287  0.000000000
##   2.263130e+02  0.8393287  0.000000000
##   2.370006e+02  0.8393287  0.000000000
##   2.481930e+02  0.8393287  0.000000000
##   2.599140e+02  0.8393287  0.000000000
##   2.721885e+02  0.8393287  0.000000000
##   2.850426e+02  0.8393287  0.000000000
##   2.985038e+02  0.8393287  0.000000000
##   3.126007e+02  0.8393287  0.000000000
##   3.273634e+02  0.8393287  0.000000000
##   3.428231e+02  0.8393287  0.000000000
##   3.590130e+02  0.8393287  0.000000000
##   3.759675e+02  0.8393287  0.000000000
##   3.937226e+02  0.8393287  0.000000000
##   4.123163e+02  0.8393287  0.000000000
##   4.317880e+02  0.8393287  0.000000000
##   4.521792e+02  0.8393287  0.000000000
##   4.735335e+02  0.8393287  0.000000000
##   4.958962e+02  0.8393287  0.000000000
##   5.193150e+02  0.8393287  0.000000000
##   5.438397e+02  0.8393287  0.000000000
##   5.695227e+02  0.8393287  0.000000000
##   5.964185e+02  0.8393287  0.000000000
##   6.245845e+02  0.8393287  0.000000000
##   6.540806e+02  0.8393287  0.000000000
##   6.849697e+02  0.8393287  0.000000000
##   7.173175e+02  0.8393287  0.000000000
##   7.511929e+02  0.8393287  0.000000000
##   7.866682e+02  0.8393287  0.000000000
##   8.238187e+02  0.8393287  0.000000000
##   8.627237e+02  0.8393287  0.000000000
##   9.034660e+02  0.8393287  0.000000000
##   9.461324e+02  0.8393287  0.000000000
##   9.908137e+02  0.8393287  0.000000000
##   1.037605e+03  0.8393287  0.000000000
##   1.086606e+03  0.8393287  0.000000000
##   1.137921e+03  0.8393287  0.000000000
##   1.191660e+03  0.8393287  0.000000000
##   1.247936e+03  0.8393287  0.000000000
##   1.306870e+03  0.8393287  0.000000000
##   1.368587e+03  0.8393287  0.000000000
##   1.433219e+03  0.8393287  0.000000000
##   1.500903e+03  0.8393287  0.000000000
##   1.571784e+03  0.8393287  0.000000000
##   1.646012e+03  0.8393287  0.000000000
##   1.723745e+03  0.8393287  0.000000000
##   1.805149e+03  0.8393287  0.000000000
##   1.890397e+03  0.8393287  0.000000000
##   1.979672e+03  0.8393287  0.000000000
##   2.073162e+03  0.8393287  0.000000000
##   2.171068e+03  0.8393287  0.000000000
##   2.273597e+03  0.8393287  0.000000000
##   2.380968e+03  0.8393287  0.000000000
##   2.493409e+03  0.8393287  0.000000000
##   2.611161e+03  0.8393287  0.000000000
##   2.734474e+03  0.8393287  0.000000000
##   2.863610e+03  0.8393287  0.000000000
##   2.998844e+03  0.8393287  0.000000000
##   3.140465e+03  0.8393287  0.000000000
##   3.288774e+03  0.8393287  0.000000000
##   3.444087e+03  0.8393287  0.000000000
##   3.606735e+03  0.8393287  0.000000000
##   3.777064e+03  0.8393287  0.000000000
##   3.955436e+03  0.8393287  0.000000000
##   4.142232e+03  0.8393287  0.000000000
##   4.337850e+03  0.8393287  0.000000000
##   4.542706e+03  0.8393287  0.000000000
##   4.757236e+03  0.8393287  0.000000000
##   4.981898e+03  0.8393287  0.000000000
##   5.217169e+03  0.8393287  0.000000000
##   5.463550e+03  0.8393287  0.000000000
##   5.721568e+03  0.8393287  0.000000000
##   5.991770e+03  0.8393287  0.000000000
##   6.274732e+03  0.8393287  0.000000000
##   6.571058e+03  0.8393287  0.000000000
##   6.881377e+03  0.8393287  0.000000000
##   7.206351e+03  0.8393287  0.000000000
##   7.546673e+03  0.8393287  0.000000000
##   7.903066e+03  0.8393287  0.000000000
##   8.276289e+03  0.8393287  0.000000000
##   8.667139e+03  0.8393287  0.000000000
##   9.076446e+03  0.8393287  0.000000000
##   9.505083e+03  0.8393287  0.000000000
##   9.953962e+03  0.8393287  0.000000000
##   1.042404e+04  0.8393287  0.000000000
##   1.091632e+04  0.8393287  0.000000000
##   1.143184e+04  0.8393287  0.000000000
##   1.197171e+04  0.8393287  0.000000000
##   1.253708e+04  0.8393287  0.000000000
##   1.312915e+04  0.8393287  0.000000000
##   1.374917e+04  0.8393287  0.000000000
##   1.439848e+04  0.8393287  0.000000000
##   1.507845e+04  0.8393287  0.000000000
##   1.579053e+04  0.8393287  0.000000000
##   1.653624e+04  0.8393287  0.000000000
##   1.731717e+04  0.8393287  0.000000000
##   1.813498e+04  0.8393287  0.000000000
##   1.899141e+04  0.8393287  0.000000000
##   1.988828e+04  0.8393287  0.000000000
##   2.082751e+04  0.8393287  0.000000000
##   2.181109e+04  0.8393287  0.000000000
##   2.284112e+04  0.8393287  0.000000000
##   2.391980e+04  0.8393287  0.000000000
##   2.504942e+04  0.8393287  0.000000000
##   2.623238e+04  0.8393287  0.000000000
##   2.747121e+04  0.8393287  0.000000000
##   2.876854e+04  0.8393287  0.000000000
##   3.012714e+04  0.8393287  0.000000000
##   3.154990e+04  0.8393287  0.000000000
##   3.303985e+04  0.8393287  0.000000000
##   3.460016e+04  0.8393287  0.000000000
##   3.623416e+04  0.8393287  0.000000000
##   3.794533e+04  0.8393287  0.000000000
##   3.973730e+04  0.8393287  0.000000000
##   4.161391e+04  0.8393287  0.000000000
##   4.357913e+04  0.8393287  0.000000000
##   4.563716e+04  0.8393287  0.000000000
##   4.779239e+04  0.8393287  0.000000000
##   5.004939e+04  0.8393287  0.000000000
##   5.241298e+04  0.8393287  0.000000000
##   5.488820e+04  0.8393287  0.000000000
##   5.748030e+04  0.8393287  0.000000000
##   6.019482e+04  0.8393287  0.000000000
##   6.303753e+04  0.8393287  0.000000000
##   6.601449e+04  0.8393287  0.000000000
##   6.913204e+04  0.8393287  0.000000000
##   7.239681e+04  0.8393287  0.000000000
##   7.581576e+04  0.8393287  0.000000000
##   7.939618e+04  0.8393287  0.000000000
##   8.314568e+04  0.8393287  0.000000000
##   8.707225e+04  0.8393287  0.000000000
##   9.118425e+04  0.8393287  0.000000000
##   9.549045e+04  0.8393287  0.000000000
##   1.000000e+05  0.8393287  0.000000000
## 
## Tuning parameter 'alpha' was held constant at a value of 0
## Accuracy was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0 and lambda = 0.01400531.
#get the best tuning parameters
Model6$bestTune
##     alpha     lambda
## 158     0 0.01400531
# alpha     lambda
#   0      0.01400531

#just the best lamda
Model6$bestTune$lambda
## [1] 0.01400531
#0.01400531

#LASSO Regression model coefficients(Parameter Estimates)
round(coef(Model6$finalModel,Model6$bestTune$lambda),3)
## 33 x 1 sparse Matrix of class "dgCMatrix"
##                                    1
## (Intercept)                    2.411
## Customer_Age                   0.002
## GenderM                        0.199
## Dependent_count               -0.105
## Education_LevelDoctorate      -0.058
## Education_LevelGraduate        0.014
## Education_LevelHigh School     0.024
## Education_LevelPost-Graduate  -0.064
## Education_LevelUneducated     -0.034
## Education_LevelUnknown        -0.034
## Marital_StatusMarried          0.104
## Marital_StatusSingle          -0.071
## Marital_StatusUnknown         -0.041
## Income_Category$40K - $60K     0.069
## Income_Category$60K - $80K     0.050
## Income_Category$80K - $120K   -0.048
## Income_CategoryLess than $40K -0.006
## Income_CategoryUnknown         0.032
## Card_CategoryGold             -0.100
## Card_CategoryPlatinum         -0.020
## Card_CategorySilver           -0.077
## Months_on_book                 0.052
## Total_Relationship_Count       0.566
## Months_Inactive_12_mon        -0.372
## Contacts_Count_12_mon         -0.454
## Credit_Limit                   0.082
## Total_Revolving_Bal            0.505
## Avg_Open_To_Buy                0.037
## Total_Amt_Chng_Q4_Q1           0.060
## Total_Trans_Amt               -0.432
## Total_Trans_Ct                 1.394
## Total_Ct_Chng_Q4_Q1            0.547
## Avg_Utilization_Ratio          0.150
#Plot log(lambda) & RMSE
plot(log(Model6$results$lambda),Model6$results$RMSE,
     xlab="Log(lambda)",
     ylab="RMSE",
     xlim=c(0,100),
     main="Model 6 ridge regression")

log(Model6$bestTune$lambda)
## [1] -4.268319
#variable importance
varImp(Model6)
## glmnet variable importance
## 
##   only 20 most important variables shown (out of 32)
## 
##                              Overall
## Total_Trans_Ct               100.000
## Total_Relationship_Count      40.485
## Total_Ct_Chng_Q4_Q1           39.118
## Total_Revolving_Bal           36.136
## Contacts_Count_12_mon         32.423
## Total_Trans_Amt               30.850
## Months_Inactive_12_mon        26.578
## GenderM                       14.105
## Avg_Utilization_Ratio         10.574
## Dependent_count                7.395
## Marital_StatusMarried          7.290
## Card_CategoryGold              6.991
## Credit_Limit                   5.748
## Card_CategorySilver            5.337
## Marital_StatusSingle           4.912
## Income_Category$40K - $60K     4.811
## Education_LevelPost-Graduate   4.395
## Total_Amt_Chng_Q4_Q1           4.141
## Education_LevelDoctorate       4.004
## Months_on_book                 3.531
#plot variable importance
ggplot(varImp(Model3))+
  labs(title = "Model 6 Variable importance Rank")

#forming confusion matrix
predictions_Ridge<-predict(Model6,newdata = testing)
cm_6<-confusionMatrix(data=predictions_Ridge,testing$Attrition_Flag)
cm_6
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Quit Stay
##       Quit  242   55
##       Stay  246 2495
##                                           
##                Accuracy : 0.9009          
##                  95% CI : (0.8897, 0.9113)
##     No Information Rate : 0.8394          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5635          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.49590         
##             Specificity : 0.97843         
##          Pos Pred Value : 0.81481         
##          Neg Pred Value : 0.91025         
##              Prevalence : 0.16063         
##          Detection Rate : 0.07966         
##    Detection Prevalence : 0.09776         
##       Balanced Accuracy : 0.73717         
##                                           
##        'Positive' Class : Quit            
## 
#MODEL 6  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE
#          0.9009   0.5635  0.49590   0.97843   0.16063

#CONCLUSION ON MODELS

#we have trained 6 Models using 6 different types of Regression analysis and
#Supervised Machine learning to try and get the best model that will predict or classify 
#bank attritions as accurate as possibly and as well as parsimonias 

#this are the performance measures for all six Models

  #MODEL 1  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE    #STEPWISE REGRESSION
  #          0.8627 0.5786   0.5480     0.9639    0.2433
  #MODEL 2  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE    #LOGISTIC REG 10-FOLDS
  #          0.9022  0.6044   0.59221    0.96157   0.16063
  #MODEL 3  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE    #LASSO REG 10-FOLDS
  #          0.9016   0.5951   0.57172   0.96471   0.16063
  #MODEL 4.a  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE  #RANDOMFOREST 3-folds,2repeats
  #          0.971  0.8879   0.8586    0.9925   0.1606
  #MODEL 4.b  ACCURACY  KAPPA  SENSITIVITY SPECIFICITY PREVALANCE #RANDOMFOREST 5-folds,3repeats
  #             0.9691  0.8813   0.8627   0.9894   0.1606
  #MODEL 5    ACCURACY    KAPPA  SENSITIVITY SPECIFICITY PREVALANCE #DECISION TREES-RPART
  #             0.9365   0.7699   0.8320       0.9565   0.1606
  #MODEL 6  ACCURACY KAPPA  SENSITIVITY SPECIFICITY PREVALANCE    #RIDGE REG 10-FOLDS
  #          0.9013   0.5654  0.49795   0.97843   0.16063

#The best model so far is Model 4.a RandomForest Modeling with 3-folds and 2 repeats,
#followed by Model 4.b RandomForest with 5-folds and 3 repeats
    #in general RandomForest is our Model of choice
      #RandomForest Model Performance matrix
        #MODEL 4.a  ACCURACY   KAPPA  SENSITIVITY SPECIFICITY PREVALANCE  #RANDOMFOREST 3-folds,2repeats
        #               0.9707  0.8879   0.8586      0.9925      0.1606
#Accuracy= 97.07% this measures the proportion of all properly classified cases
#Kappa=88.79% which is almost perfect
#Sensitivity=85.86% this measures the prediction accuracy of those who quit
#Specificity=99.25% this measures the proportion of people we said would stay relative to the overall number of people that stayed
#Prevalence=16.06% this is the base probability of those who quit
#Balanced Accuracy=92.56% this is measured by (sensitivity+specificity)/2