-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the neural-network-modeling wiki! title: "neural networks" author: "Rizwan Ali" date: "July 12, 2018”
#packages and libraries
install.packages("neuralnet")
install.packages("e1071")
library("e1071")
library(neuralnet)
data<-read.csv(choose.files()) apply(data,2,function(x)sum(is.na(x))) good<-complete.cases(data) data<-data[good,]
apply(data,2,function(x)sum(is.na(x)))
index <- sample(1:nrow(data),round(0.75*nrow(data)))
train <- data[index,] test <- data[-index,]
maxs <- apply(data, 2, max) mins <- apply(data, 2, min) scaled <- as.data.frame(scale(data, center = mins, scale = maxs - mins))
train_ <- scaled[index,] test_ <- scaled[-index,]
#making formula
n <- names(train_) f <- as.formula(paste("y ~", paste(n[!n %in% "y"], collapse = " + "))) #applying neural network model and plotting it.
nn <- neuralnet(f,data=train_,hidden=c(4),linear.output=T) plot(nn)
#computing and descaling the values.
pr.nn <- compute(nn,test_[,2:6]) pr.nn_ <- pr.nn$net.result*(max(data$y)-min(data$y))+min(data$y) test.r <- (test_$y)*(max(data$y)-min(data$y))+min(data$y) #checking the mean square error.
MSE.nn <- sum((test.r - pr.nn_)^2)/nrow(test_) print(paste(MSE.nn)) "14.5235812267609"
#fitting the regression line.
plot(test.r,pr.nn_,col='blue',main='real vs predicted',pch=1,cex=1,type="p",xlab = "actual",ylab = "predicted") abline(0,1,col="black")
actual_pred<-data.frame(pred<-pr.nn$net.result,actual<-test_$y) cor(actual_pred)
pred....pr.nn.net.result actual....test_.y
pred....pr.nn.net.result 1.0000000000 0.9990796284 actual....test_.y 0.9990796284 1.0000000000
rsq<-function(x) 1-(x/nrow(train_)-1)*var(train_$y) rsq(.0338) 1.10924576