library(plyr) library(MASS) library(neuralnet) library (ROCR) set.seed(100) #read normalized data data <- read.csv ( "best5ANNdata.csv") #10 fold cross validated MSE k = 10 data$id <- sample(1:k, nrow(data), replace = TRUE) list <- 1:k progress.bar <- create_progress_bar("text") progress.bar$init(k) msetraining<- NULL msetest<- NULL #dividing the dataset into training and testing for (i in 1:k){ trainingset <- subset(data, id %in% list[-i]) testset <- subset(data, id %in% c(i)) #fit the net n <- names(trainingset[,-7]) f <- as.formula(paste("AL ~", paste(n[!n %in% "AL"], collapse = " + "))) nn <- neuralnet(f,data=trainingset[,-7],hidden=9,rep=20,linear.output=F) A <- nn$result.matrix capture.output(A,file="nnResult.txt", append=TRUE) #testing the model pr.nn <- compute(nn,testset[, -c(6:7)]) pr.nn <- pr.nn$net.result*6+ 2 test.cv.r <- (testset[,-7]$AL)*6+2 msetest[i] <- sqrt(sum((test.cv.r - pr.nn)^2)/nrow(testset[,-7])) #capture prediction temp <- as.data.frame(pr.nn) temp1<- as.data.frame(test.cv.r) pred <- cbind(temp, temp1) names(pred) <- c("Predicted","Actual") capture.output(pred,file="prediction.txt", append=TRUE) progress.bar$step() } plot(nn) msetest cat("Result\n",file="result.txt", append=TRUE) cat("\nTesting Error\n",file="result.txt", append=TRUE) capture.output(msetest,file="result.txt", append=TRUE) cat("Average MSE\n",file="result.txt", append=TRUE) capture.output(mean(msetest),file="result.txt", append=TRUE) cat("Average RMSE\n",file="result.txt", append=TRUE) capture.output(sqrt(mean(msetest)),file="result.txt", append=TRUE) #plot the results as a boxplot boxplot(msetest,xlab='MSE CV',col='cyan', border='blue',names='CV error (MSE)', main='CV error (MSE) for Neural Network',horizontal=TRUE)