I am attempting to create a neural network model using R that estimates the sine function for a given x variable, i.e. sin(x). Dataset has 5000 observations with x iterating by 0.05 each time:
However, when I estimate using the model, my error is quite high at 219.2 and my prediction remains fairly constant at a value of 0.48 or thereabouts, while the actual value deviates sharply between 0 and 1:
The steps I am taking to estimate the model are as follows:
1) I am scaling my sin and x variables using max-min normalization:
sinscaled=(sin-min(sin))/(max(sin)-min(sin))
xscaled=(x-min(x))/(max(x)-min(x))
2) I am then fitting my training and test data:
trainset <- maxmindf[1:3500, ]
testset <- maxmindf[3501:5000, ]
3) The neural network is fitted as below, and the resulting output tested:
#Neural Network
library(neuralnet)
sinscalednet <- neuralnet(sinscaled ~ xscaled, data=trainset,
hidden=c(2,1), linear.output=FALSE, threshold=0.01)
sinscalednet$result.matrix
plot(sinscalednet)
Note that while I am using 2 hidden layers in this instance, the error is remaining consistent even when the number of hidden layers are being manipulated:
4) Resulting output is tested and accuracy obtained:
#Test the resulting output
temp_test <- subset(testset, select = c("xscaled"))
head(temp_test)
sinscalednet.results <- compute(sinscalednet, temp_test)
#Accuracy
results <- data.frame(actual = testset$sin, prediction =
sinscalednet.results$net.result)
results
roundedresults<-sapply(results,round,digits=0)
roundedresultsdf=data.frame(roundedresults)
attach(roundedresultsdf)
table(actual,prediction)
Would be grateful for any advice on steps I need to take to improve this model. I am experimenting by trying to modify the hidden layers, but the error is remaining consistently high.