#------------------ # Data Preparation #------------------ #Read datasets #Download the data from http://www.saedsayad.com/datasets/CreditData.zip train <- read.csv("Credit_train.csv") test <- read.csv("Credit_test.csv") #Rows and Cols dim(train) dim(test) #Columns name colnames(train) colnames(test) #Show head(train) head(test) #-------------------------------------------- # Artificial Neural Network - Classification #-------------------------------------------- library(caret) library(nnet) library(AUC) #remove all records with missing values train <- na.omit(train) test <- na.omit(test) #train model.ANN <- nnet(DEFAULT~., na.roughfix(train), size=10, MaxNWts=10000, maxit=10000) summary(model.ANN) #confusion matrix pc <- NULL pc <- predict(model.ANN, na.roughfix(test), type = "class") xtab <- table(pc, test$DEFAULT) caret::confusionMatrix(xtab, positive="Y") #lift chart pb <- NULL pb <- predict(model.ANN, na.roughfix(test), type="raw") pb <- as.data.frame(pb) pred.ANN <- data.frame(test$DEFAULT, pb) colnames(pred.ANN) <- c("target","score") lift.ANN <- lift(target ~ score, data = pred.ANN, cuts=10, class="Y") xyplot(lift.ANN, main="Neural Network - Lift Chart", type=c("l","g"), lwd=2 , scales=list(x=list(alternating=FALSE,tick.number = 10) ,y=list(alternating=FALSE,tick.number = 10))) #roc chart labels <- as.factor(ifelse(pred.ANN$target=="Y", 1, 0)) predictions <- pred.ANN$score auc(roc(predictions, labels), min = 0, max = 1) plot(roc(predictions, labels), min=0, max=1, type="l", main="Neural Network - ROC Chart")