How to predict new cases using neuralnet

Using RGUI. I have a dataset called Data. The response variable of interest to me is contained in the first column of Data .

I have Data training sets called DataTrain and DataTest .

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

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

Does anyone know how to create a forecast for this model using a test suite ( DataTest )?

Normally (for other models) I would use predict() for this. For example.

 > DataPred = predict(DataNN, DataTest) 

But when you do 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 cannot run predict() on this model. Does anyone know any alternatives?

I checked the help for neuralnet and I found a method called prediction on page 12 of the documentation . I donโ€™t think that this is generally what I want, or at least I donโ€™t know how to apply it to my Data .

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

+7
source share
4 answers

The method computes does what you need, 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, (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 (ie 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. 
+21
source

Function for predicting prediction , not predict .

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

+2
source

The answer is calculated (nn, test)

+1
source

You must use the neuralnet version to predict ie

 DataPred <- compute(DataNN, DataTest) 

If you use dplyr for any kind of manipulation, you need to specifically declare the library, and then the function name, for example,

 DataPred <- neuralnet::compute(DataNN, DataTest) 

BTW never use the equal sign when assigning values โ€‹โ€‹to variables, unfortunately, to bad practice.

+1
source

All Articles