Sunday, February 28, 2016

Neural Network (neuralnet) code


library(neuralnet)

length(names(train))
n <- names(train)
formula <- as.formula(paste("TARGET ~", paste(n[!n %in% c('TARGET','INDEX')], collapse = " + ")))


model.nn = neuralnet(formula, data=train, hidden=4,
                     threshold=0.05, rep=1,
                     linear.output=TRUE, err.fct='sse', act.fct='logistic',
                     algorithm='rprop+',
                     lifesign = 'minimal', lifesign.step=10000, stepmax=1e6)


model.nn$result.matrix

plot(model.nn)
prediction(model.nn); ?prediction
P_nn <- compute(model.nn, train[,3:24])
length(P_nn[[2]])

par(mfrow=c(1,2))
hist(P_nn[[2]])
plot(density(P_nn[[2]]))

error = abs(train$TARGET - P_nn[[2]])
plot(density(error))
mean(error) #nn pred on ctree data 0.99497

print(model.nn)

## save model
save(model.nn, file = "model_nn_raw_ctree_input.rda")

## load the model
load("model_nn_raw_ctree_input.rda")
 ## predict for the new `x`s in `newdf`
predict(model.nn, newdata = newdf)




## http://www.r-bloggers.com/fitting-a-neural-network-in-r-neuralnet-package/
## http://www.r-bloggers.com/using-neural-networks-for-credit-scoring-a-simple-example/
## https://journal.r-project.org/archive/2010-1/RJournal_2010-1_Guenther+Fritsch.pdf

No comments:

Post a Comment