# Computing a neural network with R package neuralnet

In computer programming, a neural network is an information processing system consisting of a group of interconnected processing units. The concept is based on the setup of the human brain which consists of a network of interconnected neurons. Setting up a system for computing using this type of network is a multi-step process that produces amazing results.

### Description

In R programming the neuralnet package is the key that opens the door to the processing power of a neural network. When setting up such a complicated system making an error is quite likely, so you should expect to get an error message or two in the process. This is particularly the case when making sure that your data frame columns are set up properly. Because the interconnectivity of the information processing units is based on that of the human brain they are also referred to as neurons. These interconnections allow the system to engage in parallel processing of information, which allows it to carry out tasks not possible with linear computations.

### Explanation

A neural network is a form of matrix multiplication designed to produce a prediction of a column present in the learning dataframe, but absent in the dataframes being processed. This vector can be accessed the same way you access individual columns of dataframes. The compute function sets different weights to various neuro connections within the network so as to give priority to some pathways over others. The complexity of these neural networks depends heavily upon the number of neurons in the network. As a result, these networks can range from quite simple, consisting of only a couple neurons, to incredibly complex networks consisting of thousands. This makes neural networks an incredibly useful and flexible tool.

### Example Code

Here is a coding example of setting up a neural network with the neuralnet package. It is a fairly simple example but there are several steps.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(5)
> y = rnorm(5)
> z = rnorm(5)
> t = rnorm(5)
> q = c(“a”, “b”, “c”,”d”, “e”)
> df1 = data.frame(x,y,z,t,q)
> df1
x y z t q
1 -0.91734106 0.7682810 -0.7319868 -1.00387169 a
2 0.52681601 -0.4412403 1.0016417 0.16944456 b
3 -0.01850743 -0.7540242 -2.3497954 0.75384873 c
4 -1.19162064 -1.4588538 -0.8917279 -0.71057444 d
5 -1.70362892 0.5963499 1.1955633 0.08821673 e

In this code, we create the dataframe that will be used in training the neural network.

> t = as.numeric(Sys.time())
> set.seed(t)
> y = rnorm(5)
> z = rnorm(5)
> t = rnorm(5)
> q = c(“a”, “b”, “c”,”d”, “e”)
> df2 = data.frame(y,z,t,q)
> df2
y z t q
1 -0.91734106 0.7682810 -0.7319868 a
2 0.52681601 -0.4412403 1.0016417 b
3 -0.01850743 -0.7540242 -2.3497954 c
4 -1.19162064 -1.4588538 -0.8917279 d
5 -1.70362892 0.5963499 1.1955633 e

In this code, we create the dataframe that will be used in testing the model.

> library(neuralnet)
> matrix.df1 = model.matrix(
+ ~ x + y + z + t + q,
+ data=df1,
+ )
> matrix.df1
(Intercept) x y z t qb qc qd qe
1 1 -0.91734106 0.7682810 -0.7319868 -1.00387169 0 0 0 0
2 1 0.52681601 -0.4412403 1.0016417 0.16944456 1 0 0 0
3 1 -0.01850743 -0.7540242 -2.3497954 0.75384873 0 1 0 0
4 1 -1.19162064 -1.4588538 -0.8917279 -0.71057444 0 0 1 0
5 1 -1.70362892 0.5963499 1.1955633 0.08821673 0 0 0 1
attr(,”assign”)
 0 1 2 3 4 5 5 5 5
attr(,”contrasts”)
attr(,”contrasts”)\$q
 “contr.treatment”

In this code, we create the model matrix that will be used in training the neural network. Comparing it to the first dataframe (df1) will help you to understand how this works.

> matrix.df2 = model.matrix(
+ ~ y + z + t + q,
+ data=df2,
+ )
> matrix.df2
(Intercept) y z t qb qc qd qe
1 1 -0.91734106 0.7682810 -0.7319868 0 0 0 0
2 1 0.52681601 -0.4412403 1.0016417 1 0 0 0
3 1 -0.01850743 -0.7540242 -2.3497954 0 1 0 0
4 1 -1.19162064 -1.4588538 -0.8917279 0 0 1 0
5 1 -1.70362892 0.5963499 1.1955633 0 0 0 1
attr(,”assign”)
 0 1 2 3 4 4 4 4
attr(,”contrasts”)
attr(,”contrasts”)\$q
 “contr.treatment”

In this code, we create the model matrix that will be used in testing the model. Comparing it to the second dataframe (df2) will help you to understand how this works.

> nn = neuralnet(
+ x ~ y + z + t + qb + qc + qe, data = matrix.df1, hidden=7, threshold=0.1
+ )

In this code, we create the neural network. Note the relationship between the formula part of this function in the column names in the model matrixes. Furthermore, the hidden argument defines the number of neurons in the hidden layer of the neural net.

> nr = compute(
+ nn, matrix.df2
+ )
> nr

In this code, we run the calculations of the neural net.

> m = as.vector(nr\$net.result)
> m
 -1.4655609 0.3545776 -0.2753003 -1.1674584 -1.0434077

In this code, we convert the final results of the calculations into a vector that we can use for other purposes.

### Application

There are many applications to using neural networks. They include facial recognition systems, autopilots for aircraft and spacecraft, aerospace development, National Defense, stock market predictions, and much more. They are handy tools for doing complicated data processing tasks.

Scroll to top