We have already seen how a neural network can be used to solve classification problems by attempting to group data based on its attributes. However, what if we wish to solve a regression problem using a neural network? i.e. one where the dependent variable is an interval one and can take on a wide range of values?
Solving regression Problems using neuralnet (R)
Let us go back to the cars dataset that you may remember from the post on decision trees. In this example, we wish to analyse the impact of the explanatory variables age, gender, miles, debt, and income on the dependent variable car sales.
Data Normalization
Again, we normalize our data and split into training and test data:
#MAX-MIN NORMALIZATION
> normalize <- function(x) {
> return ((x - min(x)) / (max(x) - min(x)))
> }
> maxmindf <- as.data.frame(lapply(fullData, normalize))
#TRAINING AND TEST DATA
> trainset <- maxmindf[1:770, ]
> testset <- maxmindf[771:963, ]
Neural Network Output
We then run our neural network and generate our parameters:
#4. NEURAL NETWORK
> library(neuralnet)
> nn <- neuralnet(CarSales ~ Age + Gender + Miles + Debt + Income, data=trainset, hidden=c(5,2), linear.output=TRUE, threshold=0.01)
> nn$result.matrix
1
error 3.838155246342
reached.threshold 0.009360951351
steps 8224.000000000000
Intercept.to.1layhid1 0.937500936998
Age.to.1layhid1 -0.800654053498
Gender.to.1layhid1 -0.144016578936
Miles.to.1layhid1 -1.955550671581
Debt.to.1layhid1 8.368282531034
Income.to.1layhid1 -5.550028425561
Intercept.to.1layhid2 -1.079527900260
Age.to.1layhid2 1.765403246043
Gender.to.1layhid2 -0.342171955590
Miles.to.1layhid2 0.168803247516
Debt.to.1layhid2 -2.949328287925
Income.to.1layhid2 -1.480559116175
Intercept.to.1layhid3 -3.027407768422
Age.to.1layhid3 1.637501765244
Gender.to.1layhid3 0.308918925087
Miles.to.1layhid3 3.814461487263
Debt.to.1layhid3 -4.423748541949
Income.to.1layhid3 3.120186391242
Intercept.to.1layhid4 4.422190550873
Age.to.1layhid4 -44.475517352523
Gender.to.1layhid4 0.954985580452
Miles.to.1layhid4 -13.343442869129
Debt.to.1layhid4 -7.094851326004
Income.to.1layhid4 3.219041342502
Intercept.to.1layhid5 0.943811362656
Age.to.1layhid5 -3.300126678880
Gender.to.1layhid5 1.268421830522
Miles.to.1layhid5 -1.089520903836
Debt.to.1layhid5 -6.637958813321
Income.to.1layhid5 0.618176150024
Intercept.to.2layhid1 3.215194463029
1layhid.1.to.2layhid1 -2.839823192854
1layhid.2.to.2layhid1 15.781559349565
1layhid.3.to.2layhid1 -2.581206119967
1layhid.4.to.2layhid1 -49.056574723122
1layhid.5.to.2layhid1 -4.510432644775
Intercept.to.2layhid2 0.454654461103
1layhid.1.to.2layhid2 1.385115316129
1layhid.2.to.2layhid2 -3.806958594125
1layhid.3.to.2layhid2 1.892986684335
1layhid.4.to.2layhid2 -20.967842326468
1layhid.5.to.2layhid2 -2.061789420151
Intercept.to.CarSales 0.085018416868
2layhid.1.to.CarSales -0.352833732331
2layhid.2.to.CarSales 1.145956189664
> plot(nn)
> temp_test <- subset(testset, select = c("Age", "Gender", "Miles", "Debt", "Income"))
> head(temp_test)
Age Gender Miles Debt
771 0.4390243902 0 0.13793103448 0.13545256818
772 0.2926829268 0 0.12643678161 0.75887569015
773 0.9756097561 1 0.18390804598 0.14160950310
774 0.8780487805 0 0.42528735632 0.03853103564
775 0.1463414634 1 0.22988505747 0.03316044839
776 0.7560975610 1 0.03448275862 0.00000000000
Income
771 0.4407685881
772 0.6131996658
773 0.6375939850
774 0.7188805347
775 0.5309106099
776 0.3994987469
> nn.results <- compute(nn, temp_test)
Generated Neural Network
Here is what our neural network looks like in visual format:
Model Validation
Then, we validate (or test the accuracy of our model) by comparing the estimated car sales yielded from the neural network to the actual sales achieved in the test output:
> results <- data.frame(actual = testset$CarSales, prediction = nn.results$net.result)
> results
actual prediction
771 0.593148915925 0.33765347521
772 0.843335825460 0.82100952412
773 0.339189832121 0.47498530411
774 0.563005505335 0.48417563618
775 0.208183239312 0.20896164698
776 0.018962821994 0.09635509155
777 0.061714130361 0.15554534538
778 0.028852035615 0.08570868938
779 0.138109155169 0.08505691074
780 0.125127438320 0.08501843705
781 0.309862026779 0.21485767927
782 0.520729966696 0.37274847221
783 0.127676204717 0.10413566417
784 0.110718412288 0.08501848345
785 0.145891388568 0.08501841744
...
945 0.084177258207 0.53466064852
946 0.090600149528 0.18246953938
947 0.164208523075 0.08502062965
948 0.058859511996 0.09563511503
949 0.730272548087 0.71867150493
950 0.165160062530 0.14779867248
951 0.428838442194 0.26548108964
952 0.068272955889 0.13442924592
953 0.484843335825 0.19513735567
954 0.185108407531 0.31067468816
955 0.419017195677 0.33915799895
956 0.183103377965 0.16221112242
957 0.293278053422 0.30944903199
958 0.596819139536 0.55853917872
959 0.037144022293 0.08501841959
960 0.144158227418 0.08501841749
961 0.417012166112 0.36212001802
962 0.951913273975 0.81943124529
963 0.190443825189 0.23352416819
Accuracy
In the below code, we are then converting the data back to its original format, and yielding an accuracy of 74% on a mean absolute deviation basis (i.e. the average deviation between estimated and actual car sales stands at a mean of 26%):
> predicted=results$prediction * abs(diff(range(CarSales))) + min(CarSales) > actual=results$actual * abs(diff(range(CarSales))) + min(CarSales) > comparison=data.frame(predicted,actual) > deviation=((actual-predicted)/actual) > comparison=data.frame(predicted,actual,deviation) > accuracy=1-abs(mean(deviation)) > accuracy [1] 0.7443109186
Get my e-book bundle: Finance and Regression Analysis: Python and R
Learn how to build regression models in Python and R
Get two e-books covering regression analysis in both Python and R. This introductory course provides a range of tutorials in regression analysis and finance, such as:
- Constructing linear and logistic regression models in Python and R and testing for violations
- Tutorials on important libraries such as car, forecast, statsmodels, sklearn, and more
- Generating time series forecasts with ARIMA models
- Conducting financial analysis with quantmod and quandl
Upon subscription, you will also gain access to the datasets for the tutorials on my website, as well as optional data science newsletters with my latest tutorials and promotional content.
