1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| normalize <- function(x) { return ((x-min(x))/(max(x)-min(x))) }
neufun_f <- function() { datatr <- read.csv("/home/lm/projects/RProjects/train_f.csv", head = FALSE) datate <- read.csv("/home/lm/projects/RProjects/test_f.csv", head = FALSE) datatr.target <- datatr[,1] datate.target <- datate[,1] datatr[,1] <- ifelse(datatr[,1]=='Y',1,0) datate[,1] <- ifelse(datate[,1]=='Y',1,0) datatr[2:96] <- lapply(datatr[2:96],normalize) datate[2:96] <- lapply(datate[2:96],normalize)
library(neuralnet) set.seed(5) formula <- as.formula(paste("V1 ~",paste(setdiff(names(datatr), "V1"),collapse = "+"),sep="")) neu.model <- neuralnet(formula=formula,data=datatr,threshold=1e-8,hidden=c(80,50,25,15), stepmax=1e+08,err.fct="ce",linear.output=FALSE,lifesign = "full", algorithm = 'rprop+') neu.com <- neuralnet::compute(neu.model,datate[,2:96]) neu.predicted <- ifelse(neu.com$net.result>0.5,"Y","N")
neu.table <- table(datate.target,neu.predicted,dnn=c("True values","Predicted values")) library(caret) neu.cfMatrix <- caret::confusionMatrix(neu.table,positive="Y") neu.cfMatrix
neu.TP <- neu.table[2,2] neu.FN <- neu.table[2,1] neu.FP <- neu.table[1,2] neu.TN <- neu.table[1,1]
e.ACC <- (neu.TP+neu.TN)/(neu.TP+neu.TN+neu.FP+neu.FN) e.SN <- neu.TP/(neu.TP+neu.FN) e.SP <- neu.TN/(neu.TN+neu.FP) e.PPV <- neu.TP/(neu.TP+neu.FP) e.FRP <- neu.FP/(neu.FP+neu.TN) pe <- ((neu.TP+neu.FN)*(neu.TP+neu.FP)+(neu.FP+neu.TN)*(neu.FN+neu.TN))/(neu.TP+neu.FN+neu.FP+neu.TN)^2 e.Kappa <- (e.ACC-pe)/(1-pe)
library(ROCR) neu.pred <- ROCR::prediction(predictions = neu.com$net.result, datate[,1]) neu.perf <- ROCR::performance(neu.pred, measure="tpr",x.measure = "fpr") plot(neu.perf,col="blue",main="ROC Plot",xlab="False Positive Rate(1-Specificity)",ylab="True Positive Rate(Sensitivity)") neu.auc <- ROCR::performance(neu.pred,"auc")@y.values[[1]] neu.auc legend(x=0,legend=c(paste("AUC=",neu.auc)),xjust = 1,yjust=-0.2) }
|