A neural network is a computational system that creates predictions based on existing data. Let us train and test a neural network using the neuralnet library in R.
A neural network consists of:
Our goal is to develop a neural network to determine if a stock pays a dividend or not.
As such, we are using the neural network to solve a classification problem. By classification, I mean ones where we are using a neural network to classify data by categories.
In our dataset, we assign a value of 1 to a stock that pays a dividend. We assign a value of 0 to a stock that does not pay a dividend.
Our independent variables are as follows:
Let’s take a look at the steps we will follow in constructing this model.
One of the most important procedures when forming a neural network is data normalization. This involves adjusting the data to a common scale so as to accurately compare predicted and actual values. Failure to normalize the data will typically result in the prediction value remaining the same across all observations, regardless of the input values.
We can do this in two ways in R:
We implement both techniques below but choose to use the max-min normalization technique. Please see this link for further details on how to use the normalization function.
#Scaled Normalization scaleddataWe have now scaled our new dataset and saved it into a data frame titled maxmindf:
We base our training data (trainset) on 80% of the observations. The test data (testset) is based on the remaining 20% of observations.
#Training and Test Data trainsetTraining a Neural Network Model using neuralnet
We now load the neuralnet library into R.
Observe that we are:
Deciding on the number of hidden layers in a neural network is not an exact science. In fact, there are instances where accuracy will likely be higher without any hidden layers. Therefore, trial and error plays a significant role in this process.
One possibility is to compare how the accuracy of the predictions change as we modify the number of hidden layers.
I found that using a (2,1) configuration ultimately yielded 92.5% classification accuracy for this example.
#Neural Network library(neuralnet) nnOur neural network looks like this:
We now generate the error of the neural network model, along with the weights between the inputs, hidden layers, and outputs:
> nn$result.matrix 1 error 2.027188266758 reached.threshold 0.009190064608 steps 750.000000000000 Intercept.to.1layhid1 3.287965374794 fcfps.to.1layhid1 -1.723307330428 earnings_growth.to.1layhid1 -0.076629853467 de.to.1layhid1 1.243670462201 mcap.to.1layhid1 -3.520369700429 current_ratio.to.1layhid1 -3.068677865885 Intercept.to.1layhid2 3.618803162161 fcfps.to.1layhid2 1.109150492946 earnings_growth.to.1layhid2 -11.588713924832 de.to.1layhid2 -1.526458929898 mcap.to.1layhid2 -3.769192938001 current_ratio.to.1layhid2 -4.547481937028 Intercept.to.2layhid1 2.991704593713 1layhid.1.to.2layhid1 -7.372717428050 1layhid.2.to.2layhid1 -22.367528820159 Intercept.to.dividend -5.673537382132 2layhid.1.to.dividend 17.963989719804Testing The Accuracy Of The Model
As already mentioned, our neural network has been created using the training data. We then compare this to the test data to gauge the accuracy of the neural network forecast.
In the below:
- The "subset" function is used to eliminate the dependent variable from the test data
- The "compute" function then creates the prediction variable
- A "results" variable then compares the predicted data with the actual data
- A confusion matrix is then created with the table function to compare the number of true/false positives and negatives
#Test the resulting output temp_testThe predicted results are compared to the actual results:
> results actual prediction 161 0 0.003457573932 162 1 0.999946522139 163 0 0.006824520245 164 0 0.004010802517 165 0 0.003887702302 166 1 0.999874153644 167 1 0.999980366384 168 1 0.998146780599 169 0 0.003492978628 170 1 0.537350561975 171 0 0.004603236937 172 1 0.999939975488 173 1 0.999828510307 174 0 0.015485658976 175 0 0.003549498180 176 0 0.005980944621 177 1 0.030221943252 178 1 0.999984214195 179 1 0.999208836940 180 0 0.004112814595 181 1 0.999959074201 182 1 0.999968776118 183 1 0.999919457006 184 0 0.005201629773 185 1 0.999781888469 186 1 0.997984179158 187 1 0.954069175017 188 0 0.003532946404 189 1 0.226749902056 190 1 0.983333352988 191 0 0.003899637063 192 0 0.005530617432 193 1 0.926567574613 194 1 0.999921050854 195 0 0.007256878789 196 1 0.017199765149 197 1 0.999987719505 198 0 0.005474975456 199 0 0.003427332586 200 1 0.999985252611Confusion Matrix
Then, we create a confusion matrix to compare the number of true/false positives and negatives:
> table(actual,prediction) prediction actual 0 1 0 17 0 1 3 20A confusion matrix is used to determine the number of true and false positives generated by our predictions. The model generates 17 true negatives (0's), 20 true positives (1's), while there are 3 false positives.
Ultimately, we yield an 92.5% (37/40) accuracy rate in determining whether a stock pays a dividend or not.
Conclusion
In this tutorial, you have learned how to use a neural network to solve classification problems.
Specifically, you saw how we can:
If you have any questions, please leave them in the comments below. Many thanks for your time!
We have already seen how time series models such as ARIMA can be used to make time series forecasts. While…
Panel data, along with cross-sectional and time series data, are the main data types that we encounter when working with…
It is often the case that a dataset contains significant outliers - or observations that are significantly out of range…
The purpose of a variance-covariance matrix is to illustrate the variance of a particular variable (diagonals) while covariance illustrates the…
A sentiment analysis is a useful way of gauging group opinion on a certain topic at a particular point in…
A sentiment analysis of Germany and Sweden just before the World Cup match on the 23rd June reveals a very…