Prediction on Neural Network in R

mina picture mina · May 19, 2016 · Viewed 9.4k times · Source

I want to get the accuracy or the RMSE of the Prediction result of a neural network. I started using a Confusion Matrix, but as indicated by previous answers, the Confusion Matrix gives valid results for non Continuous variables.

Is there any way I can get the accuracy or the error rate of a Neural Network Prediction??

As an example here is the code I've got until now:

library(nnet)
library(caret)
library(e1071)
data(rock)
newformula <- perm ~ area + peri + shape
y <- rock[, "perm"]
x <- rock[!colnames(rock)%in% "perm"]
original <- datacol(rock,"perm")

nnclas_model <- nnet(newformula, data = rock, size = 4, decay = 0.0001, maxit = 500)    
nnclas_prediction <- predict(nnclas_model, x)
nnclas_tab <- table(nnclas_prediction, y)
rmse <- sqrt(mean((original - nnclas_prediction)^2))

Does anyone know how can I make this work? or how can I get the Accuracy or the of the Neural Network Prediction? Any help will be deeply appreciated.

Answer

Jason picture Jason · May 19, 2016

As mentioned in the comments, confusion matrices are for classification problems. If you meant to classify perm according to its levels, then the following code should work for you.

library(nnet)
library(caret)
library(e1071)
data(rock)
rock$perm <- as.factor(rock$perm)
nnclas_model <- nnet(perm ~ area + peri + shape, data = rock, 
                     size = 4, decay = 0.0001, maxit = 500)
x <- rock[, 1:3]
y <- rock[, 4]
yhat <- predict(nnclas_model, x, type = 'class')
confusionMatrix(as.factor(yhat), y)

If you mean to treat perm as continuous, the confusion matrix doesn't make any sense. You should think in terms of mean-squared error instead.