require(RSNNS)
library(neuralnet)
set.seed(2016)
attribute<-as.data.frame(sample(seq(-2,2,length=50),50,replace=F),ncol=1)
response<-attribute^2
data<-cbind(attribute,response)
colnames(data)<-c("attribute","response")
head(data,10)
attribute response
1 -1.2653061 1.60099958
2 -1.4285714 2.04081633
3 1.2653061 1.60099958
4 -1.5102041 2.28071637
5 -0.2857143 0.08163265
6 -1.5918367 2.53394419
7 0.2040816 0.04164931
8 1.1020408 1.21449396
9 -2.0000000 4.00000000
10 -1.8367347 3.37359434
fit<-neuralnet(response~attribute,data=data,hidden=c(3,3),threshold = 0.01)
testdata<-as.matrix(sample(seq(-2,2,length=10),10,replace=F),ncol=1)
pred<-compute(fit,testdata)
result<-cbind(testdata,pred$net.result,testdata^2)
colnames(result)<-c("Attribute","Prediction","Actual")
round(result,4)
Attribute Prediction Actual
[1,] -1.5556 2.4213 2.4198
[2,] -0.2222 0.0364 0.0494
[3,] -1.1111 1.2254 1.2346
[4,] 1.1111 1.2013 1.2346
[5,] 0.6667 0.4395 0.4444
[6,] 1.5556 2.4521 2.4198
[7,] -0.6667 0.4554 0.4444
[8,] 0.2222 0.0785 0.0494
[9,] 2.0000 3.9317 4.0000
[10,] -2.0000 3.9675 4.0000
require(Metrics)
data("Boston",package="MASS")
data<-Boston
keeps<-c("crim","indus","nox","rm","age","dis","tax","ptratio","lstat","medv")
data<-data[keeps]
apply(data,2,function(x) sum(is.na(x)))
crim indus nox rm age dis tax ptratio lstat
0 0 0 0 0 0 0 0 0
medv
0