library(caret) library(ROCR) library(pROC) library(e1071) library(rpart) set.seed(123) Difteri<-read.table("D:/Machine Learning/dataDifteri.csv",sep=",",header=TRUE) Y<-as.factor(Difteri$Y) Difteri<-cbind(Y,Difteri[,-1]) part<-createDataPartition(Difteri$Y,p=0.80,list=FALSE) training<-Difteri[part,] testing<-Difteri[-part,] reglog<-glm(Y~.,data=training,family=binomial(link="logit")) predTr_reglog <- predict(reglog,newdata=training,type='response') predTr_reglog<- ifelse(predTr_reglog>0.5,1,0) conMat_Tr_reglog<-confusionMatrix(predTr_reglog,training$Y) predTr2_reglog <- prediction(predTr_reglog, training$Y) perfTr_reglog <- performance(predTr2_reglog, measure = "tpr", x.measure = "fpr") plot(perfTr_reglog) predTe_reglog <- predict(reglog,newdata=testing,type='response') predTe_reglog<- ifelse(predTe_reglog>0.5,0,1) conMat_Te_reglog<-confusionMatrix(predTe_reglog,testing$Y) predTe2_reglog <- prediction(predTe_reglog, testing$Y) perfTe_reglog <- performance(predTe2_reglog, measure = "tpr", x.measure = "fpr") plot(perfTe_reglog) ##############SVM Radial################# svmR<-svm(Y~.,data=training,type="C-classification",cross=10,probability=TRUE,kernel="radial") predTr_svmR<-predict(svmR,training[,-1],probability=TRUE) conMat_Tr_svmR<-confusionMatrix(predTr_svmR,training$Y) predTr2_svmR<-prediction(attr(predTr_svmR,"probabilities")[,2], training[,1] == "0") plot(performance(predTr2_svmR,"tpr","fpr")) abline(0,1,lty=2,col="red") predTe_svmR<-predict(svmR,testing[,-1],probability=TRUE) conMat_Te_svmR<-confusionMatrix(predTe_svmR,testing$Y) predTe2_svmR<-prediction(attr(predTe_svmR,"probabilities")[,2], testing[,1] == "0") plot(performance(predTe2_svmR,"tpr","fpr")) abline(0,1,lty=2,col="red") #################CART################# library(rpart) cart<-rpart(Y~.,method="class",data=training,,control = rpart.control(minsplit = 20, cp = 1e-04)) plot(cart, uniform=TRUE, main="Classification Tree for Difteri") text(cart, use.n=TRUE, all=TRUE, cex=.8) predTr_cart<-predict(cart,training,type="class") conMat_Tr_cart<-confusionMatrix(predTr_cart,training$Y) predTr_cart<-predict(cart,training,type="prob")[,2] predTr2_cart<-prediction(predTr_cart,training$Y) plot(performance(predTr2_cart,"tpr","fpr"),main="ROC curve") abline(0,1,lty=2,col="green") predTe_cart<-predict(cart,testing,type="class") conMat_Te_cart<-confusionMatrix(predTe_cart,testing$Y) predTe_cart<-predict(cart,testing,type="prob")[,2] predTe2_cart<-prediction(predTe_cart,testing$Y) plot(performance(predTe2_cart,"tpr","fpr"),main="ROC curve") abline(0,1,lty=2,col="green") ####################TRAINING DATA######################### predTr_reglog <- predict(reglog,newdata=training,type='response') predTr_reglog<- ifelse(predTr_reglog>0.5,0,1) conMat_Tr_reglog<-confusionMatrix(predTr_reglog,training$Y) predTr2_reglog <- prediction(predTr_reglog, training$Y) perfTr_reglog <- performance(predTr2_reglog, measure = "tpr", x.measure = "fpr") predTr_svmR<-predict(svmR,training[,-1],probability=TRUE) conMat_Tr_svmR<-confusionMatrix(predTr_svmR,training$Y) predTr2_svmR<-prediction(attr(predTr_svmR,"probabilities")[,2], training[,1] == "1") predTr_cart<-predict(cart,training,type="class") conMat_Tr_cart<-confusionMatrix(predTr_cart,training$Y) predTr_cart<-predict(cart,training,type="prob")[,2] predTr2_cart<-prediction(predTr_cart,training$Y) plot(perfTr_reglog,lwd=2,main="ROC Training Data",lty=1,col="yellow") abline(0,1,lty=2,col="black") plot(performance(predTr2_svmR,"tpr","fpr"),lwd=2,add=TRUE,lty=1,col="green") plot(performance(predTr2_cart,"tpr","fpr"),lwd=2,add=TRUE,lty=1,col="red") rg<-range(0,0.5,0.5,0.5,0) legend(0.5,rg[2],c("Reglog","SVM","CART"),title="Model",cex=0.8,col=c("yellow","green","red"),lwd=c(3,3,3),lty=c(1,1,1),box.lty=2,box.col="blue") AUC1<-performance(predTr2_reglog,"auc") AUC2<-performance(predTr2_svmR,"auc") AUC3<-performance(predTr2_cart,"auc") #######################TESTING DATA################### predTe_reglog <- predict(reglog,newdata=testing,type='response') predTe_reglog<- ifelse(predTe_reglog>0.5,0,1) conMat_Te_reglog<-confusionMatrix(predTe_reglog,testing$Y) predTe2_reglog <- prediction(predTe_reglog, testing$Y) perfTe_reglog <- performance(predTe2_reglog, measure = "tpr", x.measure = "fpr") plot(perfTe_reglog,main="ROC Testing Data",col="yellow") abline(0,1,lty=2,col="black") predTe_svmR<-predict(svmR,testing[,-1],probability=TRUE) conMat_Te_svmR<-confusionMatrix(predTe_svmR,testing$Y) predTe2_svmR<-prediction(attr(predTe_svmR,"probabilities")[,2], testing[,1] == "1") plot(performance(predTe2_svmR,"fpr","tpr"),add=TRUE,col="green") predTe_cart<-predict(cart,testing,type="class") conMat_Te_cart<-confusionMatrix(predTe_cart,testing$Y) predTe_cart<-predict(cart,testing,type="prob")[,2] predTe2_cart<-prediction(predTe_cart,testing$Y) plot(performance(predTe2_cart,"tpr","fpr"),add=TRUE,col="red") plot(perfTe_reglog,lwd=2,main="ROC Testing Data",lty=1,col="yellow") abline(0,1,lty=2,col="black") plot(performance(predTe2_svmR,"tpr","fpr"),lwd=2,add=TRUE,lty=1,col="green") plot(performance(predTe2_cart,"tpr","fpr"),lwd=2,add=TRUE,lty=1,col="red") rg<-range(0,0.5,0.5,0.5,0) legend(0.5,rg[2],c("Reglog","SVM","CART"),title="Model",cex=0.8,col=c("yellow","green","red"),lwd=c(3,3,3),lty=c(1,1,1),box.lty=2,box.col="blue") AUC1<-performance(predTe2_reglog,"auc") AUC2<-performance(predTe2_svmR,"auc") AUC3<-performance(predTe2_cart,"auc") Difteri<-read.table("D:/Machine Learning/dataDifteri.csv",sep=",",header=TRUE) Y<-as.factor(Difteri$Y) Difteri<-cbind(Y,Difteri[,-1]) part<-createDataPartition(Difteri$Y,p=0.80,list=FALSE) training<-Difteri