0

I'm trying to do simple neural network modelling, but the NNet result gives me poor result. It is simply ' output = 0.5 x input ' model that I want nnet model to learn, but the prediction shows all '1' as a result. What is wrong?

library(neuralnet)
traininginput <- as.data.frame(runif(50,min=1,max=100))
trainingoutput <- traininginput/2

trainingdata<-cbind(traininginput,trainingoutput)
colnames(trainingdata)<-c("Input","Output")

net.sqrt2 <- nnet(trainingdata$Output~trainingdata$Input,size=0,skip=T, linout=T)


Testdata<-as.data.frame(1:50)
net.result2<-predict(net.sqrt2, Testdata)

cleanoutput2 <- cbind(Testdata,Testdata/2,as.data.frame(net.result2))
colnames(cleanoutput2)<-c("Input2","Expected Output2","Neural Net Output2")
print(cleanoutput2)
Rbeginner
  • 3
  • 1

1 Answers1

1
library(nnet)
traininginput <- as.data.frame(runif(50,min=1,max=100))
trainingoutput <- traininginput/2

trainingdata<-cbind(traininginput,trainingoutput)
colnames(trainingdata)<-c("Input","Output")

net.sqrt2 <- nnet(Output~Input, data=trainingdata, size=0,skip=T, linout=T)


Testdata<-data.frame(Input=1:50)
net.result2<-predict(net.sqrt2, newdata = Testdata, type="raw")

cleanoutput2 <- cbind(Testdata,Testdata/2,as.data.frame(net.result2))
colnames(cleanoutput2)<-c("Input2","Expected Output2","Neural Net Output2")
print(cleanoutput2)

You're using predict and the formula in nnet wrong. Predict expects newdata which needs to be a data.frame with a column of the inputs to your model (i.e. in this case a column called Input). The formula in nnet is not to be built by literal calls on the data. It's symbolic, so it should be the names of the columns in your data. Additionally, the package you are using is not neuralnet but nnet.

stanekam
  • 3,906
  • 2
  • 22
  • 34
  • Thank you @iShouldUseAName. It works! I have two additional questions ; 1) When I use 'net.result2<-predict(net.sqrt2, Input=Testdata)' Instead of 'net.result2<-predict(net.sqrt2, newdata = Testdata, type="raw")', it gives me wrong prediction result,although the code seem to run ok. What causes this? 2) What the type="raw" mean? – Rbeginner May 31 '15 at 10:33
  • @WonShin It returns the fitted value of `net.sqrt2` i.e. the fitted values from the training data. If you don't know what "raw" is vs "class" for `type`, you have no business using `nnet`. "raw" outputs actual computed values and treats the model as regression whereas "class" treats the model as classification and gives the predicted class. If the answer works for you, you should select it as an answer and/or upvote. – stanekam May 31 '15 at 10:49