This dataset is taken from Kaggle[“https://www.kaggle.com/dalpozz/creditcardfraud”]. The datasets contains transactions made by credit cards in September 2013 by european cardholders. This dataset presents transactions that occurred in two days, where we have 492 frauds out of 284,807 transactions. The dataset is highly unbalanced, the positive class (frauds) account for 0.172% of all transactions.

It contains only numerical input variables which are the result of a PCA transformation. Unfortunately, due to confidentiality issues, we do not have original features and more background information about the data. Features V1, V2, … V28 are the principal components obtained with PCA, the only features which have not been transformed with PCA are ‘Time’ and ‘Amount’. Feature ‘Time’ contains the seconds elapsed between each transaction and the first transaction in the dataset. The feature ‘Amount’ is the transaction Amount, this feature can be used for example-dependant cost-senstive learning. Feature ‘Class’ is the response variable and it takes value 1 in case of fraud and 0 otherwise.

Loading Libraries

library(randomForest)   #For applying Random Forest
library(e1071)          #For SVM
library(rpart)          #For tree models
library(rpart.plot)     #for plotting tree
library(caTools)        #For calculating AUC
library(readr)          #Fore reading data
library(caret)          

Reading Data

data <- read.csv("creditcard.csv")
sum(is.na(data))
## [1] 0
data$Class <- factor(data$Class)
table(data$Class)
## 
##      0      1 
## 284315    492

Splitting to training and test dataset

set.seed(100)
train_id <- sample(1:nrow(data), nrow(data)*0.75)
train <- data[train_id,]
test <- data[-train_id,]

table(train$Class)
## 
##      0      1 
## 213214    391
mean(as.integer(train$Class) - 1)
## [1] 0.001830481

we have .18% of customers with fradulent transactions. Or sau pur baseline accuracy is 99.81%

logistic Regression

glm.model <- glm(Class ~ ., data = train, family = "binomial")
glm.prob <- predict(glm.model, newdata = test, type = "response")

glm.pred <- ifelse(glm.prob > 0.5, 1, 0)
table(test$Class, glm.pred)
##    glm.pred
##         0     1
##   0 71089    12
##   1    42    59
mean(test$Class == glm.pred)
## [1] 0.9992416

Descition Tree Model

tree.model <- rpart(Class ~ ., data = train, method = "class", minbucket = 20)
prp(tree.model) 

tree.predict <- predict(tree.model, test, type = "class")
confusionMatrix(test$Class, tree.predict)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction     0     1
##          0 71084    17
##          1    32    69
##                                           
##                Accuracy : 0.9993          
##                  95% CI : (0.9991, 0.9995)
##     No Information Rate : 0.9988          
##     P-Value [Acc > NIR] : 1.012e-05       
##                                           
##                   Kappa : 0.7376          
##  Mcnemar's Test P-Value : 0.0455          
##                                           
##             Sensitivity : 0.9996          
##             Specificity : 0.8023          
##          Pos Pred Value : 0.9998          
##          Neg Pred Value : 0.6832          
##              Prevalence : 0.9988          
##          Detection Rate : 0.9983          
##    Detection Prevalence : 0.9986          
##       Balanced Accuracy : 0.9009          
##                                           
##        'Positive' Class : 0               
## 
mean(test$Class == tree.predict)
## [1] 0.9993118

SVM Model

svm.model <- svm(Class ~ ., data = train[1:10000,], kernel = "radial", cost = 1, gamma = 0.1)
svm.predict <- predict(svm.model, test)
confusionMatrix(test$Class, svm.predict)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction     0     1
##          0 71101     0
##          1    98     3
##                                           
##                Accuracy : 0.9986          
##                  95% CI : (0.9983, 0.9989)
##     No Information Rate : 1               
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0.0576          
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.9986          
##             Specificity : 1.0000          
##          Pos Pred Value : 1.0000          
##          Neg Pred Value : 0.0297          
##              Prevalence : 1.0000          
##          Detection Rate : 0.9986          
##    Detection Prevalence : 0.9986          
##       Balanced Accuracy : 0.9993          
##                                           
##        'Positive' Class : 0               
## 
mean(test$Class == svm.predict)
## [1] 0.9986236

Random Forest

set.seed(10)
rf.model <- randomForest(Class ~ ., data = train[1:10000,],ntree = 2000, nodesize = 20)
rf.predict <- predict(rf.model, newdata = test)
confusionMatrix(test$Class, rf.predict)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction     0     1
##          0 71085    16
##          1    32    69
##                                           
##                Accuracy : 0.9993          
##                  95% CI : (0.9991, 0.9995)
##     No Information Rate : 0.9988          
##     P-Value [Acc > NIR] : 8.843e-06       
##                                           
##                   Kappa : 0.7416          
##  Mcnemar's Test P-Value : 0.03038         
##                                           
##             Sensitivity : 0.9996          
##             Specificity : 0.8118          
##          Pos Pred Value : 0.9998          
##          Neg Pred Value : 0.6832          
##              Prevalence : 0.9988          
##          Detection Rate : 0.9984          
##    Detection Prevalence : 0.9986          
##       Balanced Accuracy : 0.9057          
##                                           
##        'Positive' Class : 0               
## 
mean(test$Class == rf.predict)
## [1] 0.9993259
varImpPlot(rf.model)