Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to predict new cases using the neuralnet package

Using RGUI. I have a dataset called Data. The response variable that I'm interested in is contained in the first column of Data.

I have training sets of Data called DataTrain and DataTest.

With DataTrain I trained a neural network model (called DataNN) using the package and function neuralnet.

> DataNN = neuralnet(DataTrain[,1] ~ DataTrain[,2] + DataTrain[,3], hidden = 1,
    data = DataTrain) 

Does anyone know how to create a prediction on this model using the test set (DataTest)?

Normally (for other models) I would use predict() for this. E.g.

> DataPred = predict(DataNN, DataTest)

But when doing this for neuralnet I get:

> DataPred = predict(DataNN, DataTest)

Error in UseMethod("predict") : 
no applicable method for 'predict' applied to an object of class "nn"  

Obviously I can't run predict() on this model. Does anyone know of any alternatives?

I've checked the help for neuralnet and I found a method called prediction in the page 12 of the documentation. I don't think it's what I want at all though, or at least I don't know how to apply it to my Data.

Any help would be appreciated (if there is any solution to this at all).

like image 560
Brian Avatar asked Feb 03 '11 21:02

Brian


2 Answers

The compute method does what you are after, I copied this example from the help file and added some comments:

 # Make Some Training Data
 Var1 <- runif(50, 0, 100) 
 # create a vector of 50 random values, min 0, max 100, uniformly distributed
 sqrt.data <- data.frame(Var1, Sqrt=sqrt(Var1)) 
 # create a dataframe with two columns, with Var1 as the first column
 # and square root of Var1 as the second column

 # Train the neural net
 print(net.sqrt <- neuralnet(Sqrt~Var1,  sqrt.data, hidden=10, threshold=0.01))
 # train a neural net, try and predict the Sqrt values based on Var1 values
 # 10 hidden nodes

 # Compute or predict for test data, (1:10)^2
 compute(net.sqrt, as.data.frame((1:10)^2))$net.result
 # What the above is doing is using the neural net trained (net.sqrt), 
 # if we have a vector of 1^2, 2^2, 3^2 ... 10 ^2 (i.e. 1, 4, 9, 16, 25 ... 100), 
 # what would net.sqrt produce?

 Output:
 $net.result
             [,1]
 [1,] 1.110635110
 [2,] 1.979895765
 [3,] 3.013604598
 [4,] 3.987401275
 [5,] 5.004621316
 [6,] 5.999245742
 [7,] 6.989198741
 [8,] 8.007833571
 [9,] 9.016971015
[10,] 9.944642147
# The first row corresponds to the square root of 1, second row is square root
# of 2 and so on. . . So from that you can see that net.sqrt is actually 
# pretty close
# Note: Your results may vary since the values of Var1 is generated randomly.
like image 107
skullkey Avatar answered Sep 20 '22 13:09

skullkey


The function for prediction is prediction, not predict.

So try DataPred = prediction(DataNN, DataTest) instead of DataPred = predict(DataNN, DataTest).

like image 26
hemant kumar Avatar answered Sep 19 '22 13:09

hemant kumar