Neural Network R codes in Power BI Part2



In the last post, I have explained the main concepts behind the neural network,

In this post I will show how to apply neural network in a scenario in R and how to see the results and hidden layers in a plot.

For this post I got some great example from [1].


Concert has been use in many different structure such as bridge, apartment, roadways and so on.

For the safety the strength of the concrete is matter. the concrete strength depends on the material that have been used to create it such as :Cement, Slag, Ash, water, and so forth.


imaging that we have a dataset as below, which shows the ingredient of concrete.


we are going to predict the concrete strength using neural network. neural network can be used for predict a value or class, or it can be used for predicting multiple items. In this example, we are going to predict a value, that is concrete strength.

I have loaded the data in power bi first, and in “Query Editor” I am going to write some R codes. First we need to do some data transformations. As you can see in the below picture number 2,3 and 4,data is not in a same scale, we need to do some data normalization before applying any machine learning. I am going to write a code for that (Already explained the normalization in post KNN). So to write some R codes, I just click on the R transformation component (number 5).


I have used the below codes to normalized the dataset.

The same as any predictive model first we should provide some set of data for training and the other for testing as below.

Next, I am going to call a package for Neural network that has been used a lot, name as “neuralnet”. There are other packages for this purpose. I first install it using intall.packages command in my Rstudio.

This package has a function name (neuralnet) that create a model.

next, I am going to run the model against the training dataset for all 8 attributes as below

finally I create an output data frame dataset to show the result in Power BI

The output has been shown in the below picture. column 9 (strength) shows the real concrete strength, while the column 10 (pred) shows the prediction from neural net.


I created a custom column to see the differences between predicted and the orinal value

as you see in below picture:


the differences are not that much, and if you just go to transform (number 1 in the below picture) and then choose the Average (number 2 and 3)


you will see the difference is not that much in average=-0.0044

which shows the prediction is good


However, you may interested to see the plot in visualization and see the hidden nodes and other information like weights.

so I am going to report area and just copy and paste the code I run for the neural network

just I add plot to show the model

the output will be like :


as you can see in the above picture, in number 1, I just copy and past the code (I change the dataset name). then I simply use the plot (number 2) to show the neural network.

you see the net work structure in visualization section. The number 3 show the input that we consider to predict the strength of the concrete. number 5 is a intercept or biased number. number 4 is the strength weight. and in number 6 at the bottom of the page you will the error and how many steps has gone to reach. this is a very simple network, just one hidden node, lets add some hidden node to this model just by changing the code  as below :

The only changes is to add parameter “Hidden” to the neural net function (number 1).  then just run the code and you will see another network that has 5 different hidden node. and if you look at the error, you will see it decrease so much! so always having some more hidden layer can be helpful but not that much, I could not find a rule to identify number of hidden node, but if you have any hidden node then we have the problem of over fitting see related post about over fitting




[1]Machine Learning with R,Brett Lantz, Packt Publishing,2015



Leila Etaati
Dr. Leila Etaati is Principal Data Scientist, BI Consultant, and Speaker. She has over 10 years’ experience working with databases and software systems. She was involved in many large-scale projects for big sized companies. Leila has PhD of Information System department, University of Auckland, MS and BS in computer science. Leila is Microsoft Data Platform MVP.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">