Category:ANNs

Package neuralnet
To build a neural net, with hidden layers (can be a vector by specifying number of nodes in each layer): net <- neuralnet(formula, data,  hidden = 1, threshold = 1, learningrate = 1) Here the formula can be compiled using the below code: formula <- as.formula(paste('class_name', paste(names(data)[-n], collapse='+'), sep='~')) where class_name is the class attribute, and n is the number of remaining (n-1)attributes except the class attribute in the data.

The function returns a net object where the net$result can be used to find the RMSE. output <- matrix(unlist(my_net$net.result), ncol = 1, byrow = TRUE) -- (to make a matrix for the predicted values) To calculate the RMSE for the the actual and predicted values: RMSE(output, data$class)

Backpropogation
There are totally four packages available in R for neural networks.

Packages are nnet, neuralnet, RSNNS, caret and functions are '''mlp, neuralnet, nnet, train. '''

The function nnet can be used only for 1 hidden layer, and don’t have much option to vary, whereas other functions are good for many hidden layers.

The packages which are good to be used are neuralnet and''' RSNNS. '''

The syntax for RSNNS: mlp (inputsTrain, targetsTrain, size=c(3), learnFuncParams=c(0.3), linOut = FALSE, maxit=100, inputsTest=inputsTest,targetsTest=targetsTest). where inputsTrain= training dataset of predictors, targetsTrain= response variable, size gives the option to mention hidden nodes and layers, learnFuncParams gives the learning rate, linOut gives the option to classify as regression or classification problem, maxit= number of iterations to run the model, inputsTest is the predictor testing dataset and the targetstest is for testing the response variable.

plotIterativeError to plot the error rate for every iteration.

Genetic Algorithms
In addition to the backpropogation libraries, there is a library ANN for creating and training artificial neural networks with genetic algorithms. It is limited to learning the weights for a fixed network structure.

Installation
Installation is a bit harder than most packages since it got removed from CRAN. Instead, download it from here. Then run the following code to install it: install.packages("/path/to/Downloads/ANN_0.1.4.tar.gz", repos=NULL)

Usage
To get a demo usage: demo(ANN) To get help: ?ANNGA To learn a model: model <- ANNGA(     input, output,      design=c(48, 28, 7),      population = 500,      mutation = 0.3,      crossover = 0.7,      maxW = 25,      minW = -25,      maxGen = 10, # Default 1000      error = 0.05,      printBestChromosone=TRUE,  ) To predict: prediction <- predict(model, input)$predict