myformula <- close ~ High+Low+Open
nn_close <- neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)
nn_close$net.result[[1]]
Hi, given the above lines. I have constructed a neural network model which I will use to attempt to predict the close price. Please could someone explain to me what the nn_close$net.result[[1]]
line does? I have checked the CRAN documentation but this is still not clear to me.
The results from compute()$net.result
only contain one level and this gives the probability of each sample to be a given species (in this example). In other words, the sum of the rows is (roughly) equal to 1. In the following example, I have used this information to predict the species in the validation subset of the data, and these are compared with their true values using table
:
# install.packages("neuralnet")
library(neuralnet)
# adapted iris
data(iris)
iris2 <- iris
iris2$setosa <- c(iris2$Species == 'setosa')
iris2$versicolor <- c(iris2$Species == 'versicolor')
iris2$virginica <- c(iris2$Species == 'virginica')
# iris2$Species <- NULL
# training and validation subsets
train.samples <- sample(nrow(iris), nrow(iris)*0.5)
train <- iris2[train.samples,]
valid <- iris2[-train.samples,]
# fit model
inet <- neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, train, hidden=3, lifesign="full")
# prediction
pred <- compute(inet, valid[,1:4])
head(pred$net.result) # only one level (probability of each category)
predspp <- factor(c("setosa" , "versicolor", "virginica"))[apply(pred$net.result, MARGIN=1, FUN=which.max)]
table(predspp, valid$Species)
# predspp setosa versicolor virginica
# setosa 19 0 0
# versicolor 0 24 4
# virginica 0 2 26
In my case, all setosa samples were correctly predicted. There were 2 and 4 false predictions for versicolor and virginica, respectively. Generally, the prediction was correct for 92% of the validation samples (69/75 * 100).
You will understand this better with an example (I modified it a bit but I got it from here):
itrain <- iris[sample(1:150, 50),]
itrain$setosa <- c(itrain$Species == 'setosa')
itrain$versicolor <- c(itrain$Species == 'versicolor')
itrain$virginica <- c(itrain$Species == 'virginica')
itrain$Species <- NULL
inet <- neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, itrain, hidden=3, lifesign="full")
#make a prediction on the training set and then compare to
#inet$net.result[[1]]
predict <- compute(inet, itrain[1:4])
Now take a look at the results:
head(predict$net.result)
# [,1] [,2] [,3]
#80 0.0167232688257 0.995316738272 -0.011840391533
#112 -0.0008289388986 -0.006814451178 1.007637170495
#17 1.0028534166840 0.004240124926 -0.007115290101
#104 -0.0002256650283 -0.031771967776 1.031855488316
#149 0.0019424886784 0.007205356060 0.990892583485
#82 -0.0061699713404 0.957656929739 0.048564910023
head(inet$net.result[[1]])
# [,1] [,2] [,3]
#80 0.0167232688257 0.995316738272 -0.011840391533
#112 -0.0008289388986 -0.006814451178 1.007637170495
#17 1.0028534166840 0.004240124926 -0.007115290101
#104 -0.0002256650283 -0.031771967776 1.031855488316
#149 0.0019424886784 0.007205356060 0.990892583485
#82 -0.0061699713404 0.957656929739 0.048564910023
I used compute
to make a prediction on the training set using the neural network model.
As you can see inet$net.result[[1]]
and predict$net.result
are the same. So, inet$net.result[[1]]
is just a prediction on the data set that you used to train the model (it is the same as fitted.values of the lm
model).
As per @sebastianmm 's really useful comment there is a reason why net.result
is a list. Basically, neuralnet
has a parameter rep
, which makes it possible to train multiple models in one call. When rep
is larger than 1, net.result
will be larger than 1 (as will other components (weights
, result.matrix
, start.weights
)).