###PART 2

library(caret)
## Warning: package 'caret' was built under R version 3.5.3
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.3
creditData1 <- read.csv("C:/Users/Priya/Downloads/creditData.csv")
credit_rand <- creditData1[order(runif(1000)), ]
creditDataScaled <- scale(credit_rand[,2:ncol(credit_rand)], center=TRUE, scale = TRUE)
C <- cor(creditDataScaled)
highlycor <- findCorrelation(C, 0.30)
creditData1$Creditability <- as.factor(creditData1$Creditability)
creditData1$Account.Balance <- as.factor(creditData1$Account.Balance)
creditData1$Payment.Status.of.Previous.Credit <- as.factor(creditData1$Payment.Status.of.Previous.Credit)
creditData1$Purpose <- as.factor(creditData1$Purpose)
creditData1$Value.Savings.Stocks <- as.factor(creditData1$Value.Savings.Stocks)
creditData1$Length.of.current.employment <- as.factor(creditData1$Length.of.current.employment)
creditData1$Instalment.per.cent <- as.factor(creditData1$Instalment.per.cent)
creditData1$Sex...Marital.Status <- as.factor(creditData1$Sex...Marital.Status)
creditData1$Guarantors <- as.factor(creditData1$Guarantors)
creditData1$Duration.in.Current.address <- as.factor(creditData1$Duration.in.Current.address)
creditData1$Most.valuable.available.asset <- as.factor(creditData1$Most.valuable.available.asset)
creditData1$Concurrent.Credits <- as.factor(creditData1$Concurrent.Credits)
creditData1$Type.of.apartment <- as.factor(creditData1$Type.of.apartment)
creditData1$No.of.Credits.at.this.Bank <- as.factor(creditData1$No.of.Credits.at.this.Bank)
creditData1$Occupation <- as.factor(creditData1$Occupation)
creditData1$No.of.dependents <- as.factor(creditData1$No.of.dependents)
creditData1$Telephone <- as.factor(creditData1$Telephone)
creditData1$Foreign.Worker <- as.factor(creditData1$Foreign.Worker)

credit_rand <- creditData1[order(runif(1000)), ]

filteredData <- credit_rand[, -(highlycor[5]+1)]
filteredTraining <- filteredData[1:750, ]
filteredTest <- filteredData[751:1000, ]
library(naivebayes)
## Warning: package 'naivebayes' was built under R version 3.5.3
## naivebayes 0.9.6 loaded
nb_model <- naive_bayes(Creditability ~ ., data=filteredTraining)
filteredTestPred <- predict(nb_model, newdata = filteredTest)
## Warning: predict.naive_bayes(): More features in the newdata are provided
## as there are probability tables in the object. Calculation is performed
## based on features to be found in the tables.
conf_nat <- table(filteredTestPred, filteredTest$Creditability)

Accuracy <- sum(diag(conf_nat))/sum(conf_nat)*100
Accuracy
## [1] 74.8

###Q3- What is the accuracy this time? Be sure to include in your results report whether or not, after all this work, the performance of your Naïve Bayes Classifier was improved.

###ANSWER - Q3: As we can see from above the accuracy has now dropped to 74.4% from previous accuracy of 80%. The earlier model performed better here.