Sameer Mathur
Default Data from ISLR Package using caret Package
---
library(ISLR)
# reading inbuilt data as data frame
default.df <- as.data.frame(Default)
# attach data frame
attach(default.df)
# dimension of the data frame
dim(default.df)
[1] 10000 4
# structure of the data table
str(default.df)
'data.frame': 10000 obs. of 4 variables:
$ default: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
$ student: Factor w/ 2 levels "No","Yes": 1 2 1 1 1 2 1 2 1 1 ...
$ balance: num 730 817 1074 529 786 ...
$ income : num 44362 12106 31767 35704 38463 ...
# descriptive statistics of the dataframe
library(psych)
describe(default.df)[, c(1:5)]
vars n mean sd median
default* 1 10000 1.03 0.18 1.00
student* 2 10000 1.29 0.46 1.00
balance 3 10000 835.37 483.71 823.64
income 4 10000 33516.98 13336.64 34552.64
library(caret)
# data partition
set.seed(2341)
trainIndex <- createDataPartition(default.df$default, p = 0.80, list = FALSE)
# 80% training data
trainData.df <- default.df[trainIndex, ]
table(trainData.df$default)
No Yes
7734 267
# 20% testing data
testData.df <- default.df[-trainIndex, ]
table(testData.df$default)
No Yes
1933 66
# control parameters
objControl <- trainControl(method = "boot",
number = 2,
returnResamp = 'none',
summaryFunction = twoClassSummary,
classProbs = TRUE,
savePredictions = TRUE)
# model building using caret package
set.seed(766)
caretLogitModel <- train(trainData.df[, 2:4],
trainData.df[, 1],
method = 'glmStepAIC',
trControl = objControl,
metric = "ROC",
verbose = FALSE)
# summary of the model
summary(caretLogitModel)
Call:
NULL
Deviance Residuals:
Min 1Q Median 3Q Max
-2.4914 -0.1374 -0.0521 -0.0185 3.7862
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.100e+01 4.260e-01 -25.832 < 2e-16 ***
studentYes -7.034e-01 1.655e-01 -4.249 2.15e-05 ***
balance 5.881e-03 2.662e-04 22.092 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 2340.6 on 8000 degrees of freedom
Residual deviance: 1227.7 on 7998 degrees of freedom
AIC: 1233.7
Number of Fisher Scoring iterations: 8
# predicted probabilities
predTestProb <- predict(caretLogitModel, testData.df, type = "prob")
# plot of probabilities
plot(predTestProb[,2],
main = "Scatterplot of Probabilities of Default (test data)",
xlab = "Customer ID", ylab = "Predicted Probability of Default")
# prediction of default = {no, yes} on test data
predClass <- predict(object = caretLogitModel, testData.df[, 2:4], type = 'raw')
table(predClass)
predClass
No Yes
1976 23
# confusion matrix
confusionMatrix(predClass, testData.df$default, positive = "Yes")
Confusion Matrix and Statistics
Reference
Prediction No Yes
No 1925 51
Yes 8 15
Accuracy : 0.9705
95% CI : (0.9621, 0.9775)
No Information Rate : 0.967
P-Value [Acc > NIR] : 0.2098
Kappa : 0.3256
Mcnemar's Test P-Value : 4.553e-08
Sensitivity : 0.227273
Specificity : 0.995861
Pos Pred Value : 0.652174
Neg Pred Value : 0.974190
Prevalence : 0.033017
Detection Rate : 0.007504
Detection Prevalence : 0.011506
Balanced Accuracy : 0.611567
'Positive' Class : Yes
library(ROCR)
#Every classifier evaluation using ROCR starts with creating a prediction object. This function is used to transform the input data into a standardized format.
PredictObject <- prediction(predTestProb[2], testData.df$default)
# All kinds of predictor evaluations are performed using the performance function
PerformObject <- performance(PredictObject, "tpr","fpr")
# Plot the ROC Curve for Credit Card Default
plot(PerformObject,
main = "ROC Curve for CC Default",
col = 2,
lwd = 2)
abline(a = 0,b = 1,lwd = 2,lty = 3,col = "black")
library(ROCR)
#Every classifier evaluation using ROCR starts with creating a prediction object. This function is used to transform the input data into a standardized format.
PredictObject <- prediction(predTestProb[2], testData.df$default)
# All kinds of predictor evaluations are performed using the performance function
PerformObject2 <- performance(PredictObject, "sens","spec")
# Plot the ROC Curve for Credit Card Default
plot(PerformObject2,
main = "Sensitivity - Specificity Plot for CC Default",
col = "blue",
lwd = 2)
abline(a = 1, b = -1, lwd = 2, lty = 3, col = "black")