Part 1: Develop the Model & Assess Predictor Significance
Step 1: Install and load required libraries
#install.packages("readxl")
#install.packages("Hmisc")
#install.packages("pscl")
#if(!require(pROC)) install.packages("pROC")
library(readxl) #allows us to import file in excel
## Warning: package 'readxl' was built under R version 4.3.3
library(Hmisc) #allows us to call the correlation function
## Warning: package 'Hmisc' was built under R version 4.3.3
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
library(pscl) #allows us to call the pseudo R-sqaure package to evaluate our model
## Warning: package 'pscl' was built under R version 4.3.3
## Classes and Methods for R originally developed in the
## Political Science Computational Laboratory
## Department of Political Science
## Stanford University (2002-2015),
## by and under the direction of Simon Jackman.
## hurdle and zeroinfl functions by Achim Zeileis.
library(pROC) #allows us to run the area under the curve (AUC) pacakage to get the plot and AUC score
## Warning: package 'pROC' was built under R version 4.3.3
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
Step 2: Import the data
Sim_df <- read_excel(file.choose())
Step 3: Descriptive Statistics and Data visualization
summary(Sim_df)
## Customer Spending Card Coupon
## Min. : 1.00 Min. :1.058 Min. :0.0 Min. :0.0
## 1st Qu.: 25.75 1st Qu.:2.059 1st Qu.:0.0 1st Qu.:0.0
## Median : 50.50 Median :2.805 Median :0.5 Median :0.0
## Mean : 50.50 Mean :3.334 Mean :0.5 Mean :0.4
## 3rd Qu.: 75.25 3rd Qu.:4.468 3rd Qu.:1.0 3rd Qu.:1.0
## Max. :100.00 Max. :7.076 Max. :1.0 Max. :1.0
sapply(Sim_df, sd) #Standard Deviation using the sd function
## Customer Spending Card Coupon
## 29.0114920 1.7412979 0.5025189 0.4923660
xtabs(~Coupon + Card, data = Sim_df) #Cross tabulation of coupon and card
## Card
## Coupon 0 1
## 0 36 24
## 1 14 26
Interpretaion:
1. 36 customers did not use a coupon and did not have a card.
2. 24 customers did not use a coupon but had a card.
3. 14 customers used a coupon but did not have a card.
4. 26 customers used a coupon and had a card.
Step 4: Building model
sim_logit = glm(Coupon ~ Card + Spending, data = Sim_df, family = binomial)
summary(sim_logit)
##
## Call:
## glm(formula = Coupon ~ Card + Spending, family = binomial, data = Sim_df)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.1464 0.5772 -3.718 0.000201 ***
## Card 1.0987 0.4447 2.471 0.013483 *
## Spending 0.3416 0.1287 2.655 0.007928 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 134.60 on 99 degrees of freedom
## Residual deviance: 120.97 on 97 degrees of freedom
## AIC: 126.97
##
## Number of Fisher Scoring iterations: 4
Interpretation: They are all significant at alpha 0.05. (p-value<0.05)
Step 5: Interpreting the Logistic Regression Equation using odds
ratio only
exp(coef(sim_logit))
## (Intercept) Card Spending
## 0.1169074 3.0003587 1.4072585
Interpretation: Customers with a card are approximately 3 times more likely to use a coupon compared to those without a card and for every one-unit increase in the "Spending" variable the odds of using a coupon increase by a factor of approximately 1.41.