library(caret) library(e1071) library(plyr) library(randomForest) library (ROCR) set.seed(100) #read the file data<-read.csv("best5SVRdata.csv") #10 fold cross validation 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 testing and training for (i in 1:k){ trainingset <- subset(data, id %in% list[-i]) testset <- subset(data, id %in% list[i]) #training mymodel <- svm(AL~., data = trainingset[,-7], kernel = "radial") capture.output(mymodel,file="mymodel.txt", append=TRUE) #testing predict.test <- predict(mymodel, testset[, -c(6:7)]) #denormalized the data testset.norm <- (testset[,-7]$AL)*6+2 predict.test.norm <- predict.test *6+2 #calculate the mse msetest[i]<- sum(((testset.norm) - predict.test.norm)^2)/nrow(testset[,-7]) temp <- as.data.frame(predict.test.norm) temp1<- as.data.frame(testset.norm) pred <- cbind(temp, temp1) names(pred) <- c("Predicted","Actual") capture.output(pred,file="prediction.txt", append=TRUE) progress.bar$step() } #msetraining 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 Random Forest',horizontal=TRUE)