Introduction

The dataset under examination represents the outcome of a marketing campaign conducted by a Portuguese banking institution and provides details of users who have subscribed or not subscribed to a term deposit.

The dataset can be found at the following link: https://archive.ics.uci.edu/ml/datasets/bank+marketing

In the first part of the case study, we will analyze some of the variables present in the dataset. Through Exploratory Data Analysis (EDA), we will understand how the dependent variable “Term Deposit” is influenced by the main independent variables in the reference dataset.

In the second part of the case study, glm models will be estimated, considering appropriate distributions belonging to the exponential family that best fit the resolution of our problem.

In this case, the main objective will be to implement the best classification model, i.e., a model that can classify with a certain degree of accuracy those who have subscribed to a term deposit. This is aimed at minimizing potential errors, particularly reducing the number of false negatives, i.e., those who have not actually subscribed to the term deposit proposed by the financial institution.

The models will be validated using unseen data, and various performance metrics considered will inform us which model best aligns with the case study’s objective.

Exploratory Data Analysis (EDA)

The dependent variable ‘Term Deposit’ is categorical. Through a bar chart, we can analyze the percentage of observations that have subscribed to a term deposit.

From the bar chart, we observe that the classes are imbalanced, with over 75% of the observations not subscribing to a term deposit.

Since the goal is to build a classifier capable of correctly classifying those who have subscribed to a term deposit, it will be necessary to take this imbalance into account.

Since the examined dataset includes some numerical variables, it is useful to identify any potential linear relationships and observe the correlation to understand the intensity of such a linear relationship if present.

As we can see from the graph, the majority of numerical variables show a low correlation.

The strength of the linear relationship between ‘previous’ and ‘pdays’ is moderate.

‘Previous’ represents the number of contacts made with the customer before the current marketing campaign, while ‘pdays’ represents the number of days elapsed since the customer was last contacted for a previous marketing campaign.

During exploratory analysis, it is useful to select some variables from the dataset to see how they impact the subscription or non-subscription to a term deposit.

For example, let’s consider the variable ‘age’ and see how it influences the dependent variable.

As easily observed, individuals of all ages can subscribe to a term deposit. However, users in the age group of thirty to forty are more likely to do so.

The same age group, however, also has the highest count among those who have not subscribed to a deposit, which is reasonable as individuals in this age group are also the most contacted.

The histogram illustrates the asymmetric distribution of user ages. In this case, a positive skewness is evident with a long tail to the right. The bank has primarily targeted a young to mid-young user base.

A crucial categorical variable is ‘housing,’ which indicates the presence of a housing loan. Let’s compare customers with housing loans to those without in relation to the target variable to analyze how the mortgage factor impacts deposit subscriptions.

From the bar chart, it is evident that the majority of bank-contacted users have a housing loan. Additionally, there is a higher count of deposit subscriptions for those without a housing loan.

In light of the obtained results, it is likely that having a housing loan influences a person’s propensity or inclination to invest in a term deposit.

Finally, before concluding the exploratory data analysis, it is also useful to examine the relationship between various independent explanatory variables present in the dataset.

For instance, let’s consider the categorical variables ‘job sector’ and ‘education,’ along with the numerical variable ‘balance.’

From the bar chart, it is apparent that the level of education influences the balance. Individuals with higher education levels have a higher average balance, and this holds true for all four job sectors. Additionally, for all three education levels, self-employed workers and those in the Administration Management sector show a higher average balance.

The bottom part shows the median balance level. The median is a robust measure less affected by the presence of outliers, in this case, very high balances.

Categorical data

Since the dependent variable is a categorical variable, we will consider Generalized Linear Models (GLM) for categorical data.

Firstly, let’s split the dataset into a training set and a test set. The training set consists of 75% of the total observations and will be used to train various models, while the remaining 25% will form the test set. The test set will be used to evaluate how well or poorly the model under consideration predicts the dependent variable ‘Term Deposit’.

Logistic Regression

Logistic regression is a Generalized Linear Model (GLM) where the random component follows a Bernoulli or Binomial distribution. The link function is the logit function.

The goal is to select one or more models containing only those explanatory variables that have a statistically significant impact on the dependent variable ‘Term Deposit.’

The predictive performance of the model will be evaluated using the unseen data from the test set.

Let’s consider the first model as a complete model containing all independent explanatory variables.

As we can see from the summary, particularly from the p-values, not all explanatory variables are statistically significant.

## 
## Call:
## glm(formula = Term.Deposit ~ . + loan:housing + duration:housing, 
##     family = binomial(link = "logit"), data = as.data.frame(train_set))
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -6.0709  -0.3904  -0.2524  -0.1547   3.1040  
## 
## Coefficients:
##                                            Estimate Std. Error z value Pr(>|z|)
## (Intercept)                              -3.1202578  0.2511528 -12.424  < 2e-16
## balance                                   0.0501368  0.0181799   2.758 0.005819
## day                                      -0.0191316  0.0224574  -0.852 0.394267
## duration                                  1.1745856  0.0277464  42.333  < 2e-16
## campaign                                 -0.3303167  0.0390300  -8.463  < 2e-16
## pdays                                    -0.0002533  0.0379100  -0.007 0.994670
## previous                                  0.0162828  0.0150152   1.084 0.278177
## educationsecondary                        0.2401718  0.0831031   2.890 0.003852
## educationtertiary                         0.3845906  0.0920496   4.178 2.94e-05
## educationunknown                          0.2565895  0.1363630   1.882 0.059882
## defaultno_defaulter                       0.2603082  0.2088163   1.247 0.212548
## housingno_housing_loan                    0.9618075  0.0555921  17.301  < 2e-16
## loanpersonal_loan                        -0.3231586  0.0990165  -3.264 0.001100
## contacttelephone                         -0.0664450  0.1021950  -0.650 0.515577
## contactunknown                           -1.0504371  0.0705308 -14.893  < 2e-16
## poutcomeother                             0.1921460  0.1084543   1.772 0.076448
## poutcomesuccess                           2.2717172  0.1010393  22.484  < 2e-16
## poutcomeunknown                          -0.3908219  0.1117474  -3.497 0.000470
## Age_rangeSeconda Fascia(40-59)           -0.1714682  0.0490256  -3.498 0.000470
## Job.Sectorblue_collar                    -0.1715101  0.0567991  -3.020 0.002531
## Job.Sectorself_employed                  -0.2019837  0.0914039  -2.210 0.027120
## Job.Sectorservices                       -0.2408625  0.0828850  -2.906 0.003661
## Job.Sectorstudent                         0.5616851  0.1124913   4.993 5.94e-07
## Marital.Statusno_married                  0.3221753  0.0472048   6.825 8.79e-12
## housingno_housing_loan:loanpersonal_loan -0.4736887  0.1428787  -3.315 0.000915
## duration:housingno_housing_loan          -0.2010039  0.0393840  -5.104 3.33e-07
##                                             
## (Intercept)                              ***
## balance                                  ** 
## day                                         
## duration                                 ***
## campaign                                 ***
## pdays                                       
## previous                                    
## educationsecondary                       ** 
## educationtertiary                        ***
## educationunknown                         .  
## defaultno_defaulter                         
## housingno_housing_loan                   ***
## loanpersonal_loan                        ** 
## contacttelephone                            
## contactunknown                           ***
## poutcomeother                            .  
## poutcomesuccess                          ***
## poutcomeunknown                          ***
## Age_rangeSeconda Fascia(40-59)           ***
## Job.Sectorblue_collar                    ** 
## Job.Sectorself_employed                  *  
## Job.Sectorservices                       ** 
## Job.Sectorstudent                        ***
## Marital.Statusno_married                 ***
## housingno_housing_loan:loanpersonal_loan ***
## duration:housingno_housing_loan          ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 20778  on 30531  degrees of freedom
## Residual deviance: 14314  on 30506  degrees of freedom
## AIC: 14366
## 
## Number of Fisher Scoring iterations: 6

We perform the Likelihood-Ratio-Test (LRT) considering the previously estimated model.

We observe that the explanatory variable ‘default’ reduces the deviance slightly.

## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: Term.Deposit
## 
## Terms added sequentially (first to last)
## 
## 
##                  Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                             30531      20778              
## balance           1     45.0     30530      20733 1.993e-11 ***
## day               1     18.8     30529      20715 1.476e-05 ***
## duration          1   3485.0     30528      17230 < 2.2e-16 ***
## campaign          1    153.4     30527      17076 < 2.2e-16 ***
## pdays             1    278.2     30526      16798 < 2.2e-16 ***
## previous          1     54.6     30525      16743 1.478e-13 ***
## education         3    231.7     30522      16512 < 2.2e-16 ***
## default           1      7.6     30521      16504 0.0057880 ** 
## housing           1    669.2     30520      15835 < 2.2e-16 ***
## loan              1    106.4     30519      15728 < 2.2e-16 ***
## contact           2    345.1     30517      15383 < 2.2e-16 ***
## poutcome          3    864.8     30514      14518 < 2.2e-16 ***
## Age_range         1     46.1     30513      14472 1.096e-11 ***
## Job.Sector        4     72.9     30509      14400 5.650e-15 ***
## Marital.Status    1     45.5     30508      14354 1.510e-11 ***
## housing:loan      1     13.7     30507      14340 0.0002162 ***
## duration:housing  1     26.0     30506      14314 3.446e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

We estimate a reduced model by excluding the explanatory variable ‘default’ and compare the full model with the reduced model using the Likelihood-Ratio-Test (LRT) for nested models.

The p-value (0.1994) is greater than the significance level \(\alpha\) set at 5%, so we do not reject the null hypothesis H0. The two models are equivalent.

## Analysis of Deviance Table
## 
## Model 1: Term.Deposit ~ balance + day + duration + campaign + pdays + 
##     previous + education + housing + loan + contact + poutcome + 
##     Age_range + Job.Sector + Marital.Status + loan:housing + 
##     duration:housing
## Model 2: Term.Deposit ~ balance + day + duration + campaign + pdays + 
##     previous + education + default + housing + loan + contact + 
##     poutcome + Age_range + Job.Sector + Marital.Status + loan:housing + 
##     duration:housing
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1     30507      14316                     
## 2     30506      14314  1   1.6468   0.1994

We perform the Likelihood-Ratio-Test (LRT) considering the previously estimated reduced model.

By adding one explanatory variable at a time, we observe a substantial reduction in deviance when we include a variable that has a significant impact on the dependent variable within the model.

## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: Term.Deposit
## 
## Terms added sequentially (first to last)
## 
## 
##                  Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                             30531      20778              
## balance           1     45.0     30530      20733 1.993e-11 ***
## day               1     18.8     30529      20715 1.476e-05 ***
## duration          1   3485.0     30528      17230 < 2.2e-16 ***
## campaign          1    153.4     30527      17076 < 2.2e-16 ***
## pdays             1    278.2     30526      16798 < 2.2e-16 ***
## previous          1     54.6     30525      16743 1.478e-13 ***
## education         3    231.7     30522      16512 < 2.2e-16 ***
## housing           1    667.8     30521      15844 < 2.2e-16 ***
## loan              1    110.4     30520      15734 < 2.2e-16 ***
## contact           2    345.8     30518      15388 < 2.2e-16 ***
## poutcome          3    867.2     30515      14521 < 2.2e-16 ***
## Age_range         1     46.2     30514      14474 1.076e-11 ***
## Job.Sector        4     73.4     30510      14401 4.420e-15 ***
## Marital.Status    1     45.1     30509      14356 1.858e-11 ***
## housing:loan      1     14.1     30508      14342  0.000176 ***
## duration:housing  1     25.9     30507      14316 3.559e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

We estimate another model in which the explanatory variables ‘day’ and ‘previous’ are excluded from the previous model.

We compare the full model with the reduced model using the Likelihood-Ratio-Test (LRT) for nested models.

The p-value (0.3211) is greater than the significance level \(\alpha\) set at 5%, so we cannot reject the Null Hypothesis H0.

The two models are equivalent. Based on the principle of parsimony, we choose the restricted model.

## Analysis of Deviance Table
## 
## Model 1: Term.Deposit ~ balance + duration + campaign + pdays + education + 
##     housing + loan + contact + poutcome + Age_range + Job.Sector + 
##     Marital.Status + loan:housing + duration:housing
## Model 2: Term.Deposit ~ balance + day + duration + campaign + pdays + 
##     previous + education + default + housing + loan + contact + 
##     poutcome + Age_range + Job.Sector + Marital.Status + loan:housing + 
##     duration:housing
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1     30509      14318                     
## 2     30506      14314  3   3.4974   0.3211

We perform the Likelihood-Ratio-Test (LRT) considering the previously estimated reduced model.

This is the model chosen during the model selection phase.

## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: Term.Deposit
## 
## Terms added sequentially (first to last)
## 
## 
##                  Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                             30531      20778              
## balance           1     45.0     30530      20733 1.993e-11 ***
## duration          1   3495.1     30529      17238 < 2.2e-16 ***
## campaign          1    160.5     30528      17078 < 2.2e-16 ***
## pdays             1    279.8     30527      16798 < 2.2e-16 ***
## education         3    238.9     30524      16559 < 2.2e-16 ***
## housing           1    670.0     30523      15889 < 2.2e-16 ***
## loan              1    108.4     30522      15781 < 2.2e-16 ***
## contact           2    356.6     30520      15424 < 2.2e-16 ***
## poutcome          3    902.1     30517      14522 < 2.2e-16 ***
## Age_range         1     46.0     30516      14476 1.180e-11 ***
## Job.Sector        4     73.6     30512      14402 3.982e-15 ***
## Marital.Status    1     45.0     30511      14358 2.014e-11 ***
## housing:loan      1     13.9     30510      14344 0.0001906 ***
## duration:housing  1     25.8     30509      14318 3.825e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Interpretation of the coefficients

Let’s consider some explanatory variables.

personal_loan = -27.87031436

no_housing_loan = 161.06431573

educationtertiary = 46.87036974

##                              (Intercept) 
##                             -94.15300894 
##                                  balance 
##                               5.25993293 
##                                 duration 
##                             223.66746539 
##                                 campaign 
##                             -28.33394006 
##                                    pdays 
##                              -0.02018816 
##                       educationsecondary 
##                              26.96186297 
##                        educationtertiary 
##                              46.87036974 
##                         educationunknown 
##                              28.51273183 
##                   housingno_housing_loan 
##                             161.06431573 
##                        loanpersonal_loan 
##                             -27.87031436 
##                         contacttelephone 
##                              -6.36602114 
##                           contactunknown 
##                             -64.96532167 
##                            poutcomeother 
##                              22.09927315 
##                          poutcomesuccess 
##                             872.26505983 
##                          poutcomeunknown 
##                             -34.03561339 
##           Age_rangeSeconda Fascia(40-59) 
##                             -15.74818470 
##                    Job.Sectorblue_collar 
##                             -15.86953232 
##                  Job.Sectorself_employed 
##                             -18.53929583 
##                       Job.Sectorservices 
##                             -21.50070698 
##                        Job.Sectorstudent 
##                              76.08442027 
##                 Marital.Statusno_married 
##                              37.72159767 
## housingno_housing_loan:loanpersonal_loan 
##                             -37.95314936 
##          duration:housingno_housing_loan 
##                             -18.13918676

Validation - Comparison of holdout CV with ten-fold CV

We apply a ten-fold cross-validation (with k = 10). In k-fold cross-validation, we divide the training set into k folds of nearly identical size. In the first step, the first fold serves as the test set, while the remaining k − 1 folds play the role of the training set. The model is trained on the k − 1 folds and subsequently tested on unseen data, represented by the first fold in this case.

Since our task is a classification problem, the accuracy rate will be used as the performance metric. The logic for the subsequent steps is the same. In the last step, the last fold becomes the test set, while the remaining k − 1 folds serve as the training set.

At the end of the process, we will have k performance metrics, each corresponding to the k steps taken. The k performance metrics are aggregated to obtain a single final metric.

In various steps of the k-fold cross-validation, observations in turn will be part of both the training data and the unseen data.

In this case, the cross-validation accuracy is 90%, meaning that we correctly classify observations into their true class 90 out of 100 times.

## Generalized Linear Model 
## 
## 30532 samples
##    15 predictor
##     2 classes: 'No_TermDeposit', 'TermDeposit' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 27478, 27479, 27479, 27479, 27479, 27478, ... 
## Resampling results:
## 
##   Accuracy  Kappa    
##   0.908555  0.3794211

Now, considering the two models selected through the holdout cross-validation and ten-fold cross-validation procedures, we proceed to predict the class membership of the observations included in the test set.

We will compare the performance metrics of the two models to see which one better fits the test set data.

The performance metrics considered are the accuracy rate and its complement to one, namely the Misclassification Rate.

Metrica Senza_CV Con_CV
Accuracy 0.9074292 0.9074292
MC Rate 0.0925708 0.0925708

Marginal effects

We use the model selected in the model selection phase to calculate marginal effects.

Let’s consider some explanatory variables that may influence the subscription to a term deposit, such as the duration of the last contact.

As one can imagine, the duration of the contact represents a factor that can play a key role in the subscription to a deposit, as the bank must encourage the customer to initiate a potential investment in just a few minutes.

The predicted probability of subscribing to a term deposit is higher for higher values of duration.

Now, let’s consider the categorical variables ‘job sector’ and ‘education.’

Students with a tertiary education level have a higher predicted probability of subscribing to a term deposit.

Now, let’s consider the categorical variables ‘housing’ and ‘marital status.’

Unmarried individuals without a housing loan have the higher predicted probability of subscribing to a term deposit.

Finally, let’s consider the categorical variables ‘housing’ and ‘loan.’

Individuals who have not requested a personal loan from the bank and do not have a housing loan have the higher predicted probability of subscribing to a term deposit.

Imbalanced Classes Issue

In the first part of the case study, we observed class imbalance through the bar chart, with a significant bias toward ‘No_TermDeposit.’ Specifically, almost 90% of observations did not subscribe to a term deposit.

When dealing with imbalanced classes, the model tends to better learn the dominant class at the expense of the minority class. Consequently, the model may struggle to make accurate predictions for examples of the minority class.

The main objective is to implement the best classification model, one that can accurately classify those who have subscribed to a term deposit.

Analyzing the confusion matrix reveals the described issue. Looking at Sensitivity, the ratio of true positives to the sum of true positives and false negatives, we obtain a measure of the model’s ability to “capture” all examples of the minority class. In this case, it is 32%.

## Confusion Matrix and Statistics
## 
##                 Reference
## Prediction       No_TermDeposit TermDeposit
##   No_TermDeposit           8881         736
##   TermDeposit               206         353
##                                          
##                Accuracy : 0.9074         
##                  95% CI : (0.9016, 0.913)
##     No Information Rate : 0.893          
##     P-Value [Acc > NIR] : 8.251e-07      
##                                          
##                   Kappa : 0.3837         
##                                          
##  Mcnemar's Test P-Value : < 2.2e-16      
##                                          
##             Sensitivity : 0.32415        
##             Specificity : 0.97733        
##          Pos Pred Value : 0.63148        
##          Neg Pred Value : 0.92347        
##              Prevalence : 0.10702        
##          Detection Rate : 0.03469        
##    Detection Prevalence : 0.05493        
##       Balanced Accuracy : 0.65074        
##                                          
##        'Positive' Class : TermDeposit    
## 

Weighted Logistic Regression

Weighted logistic regression can be useful when dealing with imbalanced classes and wanting to give more weight to the minority class.

In this case, we create a weight vector where we assign a higher weight to the minority class, i.e., ‘No_TermDeposit.’ This weight is set to 5, while for the majority class, it is set to 1.

Now, considering the weighted logistic regression trained on the training data, we proceed to predict the class membership of the observations in the test set.

The performance metrics considered are still the accuracy rate and its complement to one, the Misclassification Rate.

As we can see from the table, the accuracy rate is 87%, compared to the previous rate of 90%.

Metrica Modello_Pesato
Accuracy 0.8748035
MC Rate 0.1251965

However, in problems with imbalanced classes, accuracy may not be a sufficient metric to assess model performance. It does not consider the class distribution and can provide a distorted view of the model’s goodness, especially when one class is much more frequent than the other.

Looking at other performance metrics, such as Sensitivity, we observe how the model is able to ‘capture’ a greater number of observations that have truly subscribed to a term deposit. Previously, Sensitivity was 32%, and now it is 68%.

## Confusion Matrix and Statistics
## 
##                 Reference
## Prediction       No_TermDeposit TermDeposit
##   No_TermDeposit           8156         343
##   TermDeposit               931         746
##                                           
##                Accuracy : 0.8748          
##                  95% CI : (0.8682, 0.8812)
##     No Information Rate : 0.893           
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0.4707          
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.68503         
##             Specificity : 0.89755         
##          Pos Pred Value : 0.44484         
##          Neg Pred Value : 0.95964         
##              Prevalence : 0.10702         
##          Detection Rate : 0.07331         
##    Detection Prevalence : 0.16480         
##       Balanced Accuracy : 0.79129         
##                                           
##        'Positive' Class : TermDeposit     
## 

Following the same logic as before, we can create a grid of weights and, for each weight, train the model on the training data and evaluate accuracy and sensitivity on the test data.

From the graph, we can observe how accuracy and sensitivity on the test set vary for a given weight.