Implementation using nnet() library

Reza Hartono
4 min readNov 10, 2020

--

To improve our practice with the nnet library, we look at another example. This time we will use the data collected at a restaurant through customer interviews. The customers were asked to give a score to the following aspects: service, ambience, and food. They were also asked whether they would leave the tip on the basis of these scores. In this case, the number of inputs is 2 and the output is a categorical value (Tip=1 and No-tip=0).

The input file to be used is shown in the following table:

This is a classification problem with three inputs and one categorical output. We will address the problem with the following code:

######################################################################## 
##Chapter 1 - Introduction to Neural Networks - using R ################
###Simple R program to build, train and test neural networks ###########
### Classification based on 3 inputs and 1 categorical output ##########
########################################################################
###Choose the libraries to use
library(NeuralNetTools)
library(nnet)
###Set working directory for the training data
setwd("C:/R")
getwd()
###Read the input file
mydata=read.csv('RestaurantTips.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)
##Train the model based on output from input
model=nnet(CustomerWillTip~Service+Ambience+Food,
data=mydata,
size =5,
rang=0.1,
decay=5e-2,
maxit=5000)
print(model)
plotnet(model)
garson(model)
########################################################################

Let us go through the code line-by-line

To understand all the steps in the code just proposed, we will look at them in detail. First, the code snippet will be shown, and the explanation will follow.

library(NeuralNetTools)
library(nnet)

This includes the libraries NeuralNetTools and nnet() for our program.

###Set working directory for the training data
setwd("C:/R")
getwd()
###Read the input file
mydata=read.csv('RestaurantTips.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)

This sets the working directory and reads the input CSV file.

##Train the model based on output from input
model=nnet(CustomerWillTip~Service+Ambience+Food,
data=mydata,
size =5,
rang=0.1,
decay=5e-2,
maxit=5000)
print(model)

This calls the nnet() function with the arguments passed. The output is as follows. nnet() processes the forward and backpropagation until convergence:

> model=nnet(CustomerWillTip~Service+Ambience+Food,data=mydata, size =5, rang=0.1, decay=5e-2, maxit=5000)
# weights: 26
initial value 7.571002
iter 10 value 5.927044
iter 20 value 5.267425
iter 30 value 5.238099
iter 40 value 5.217199
iter 50 value 5.216688
final value 5.216665
converged

A brief description of the nnet package, extracted from the official documentation, is shown in the following table:

nnet-package: Feed-forward neural networks and multinomial log-linear models

Description:

Software for feed-forward neural networks with a single hidden layer, and for multinomial log-linear models.

Details:

Package: nnet Type: Package Version: 7.3-12 Date: 2016-02-02 License: GPL-2 | GPL-3

Author(s):

Brian RipleyWilliam Venables

Usage:

nnet(formula, data, weights,subset, na.action, contrasts = NULL)

Meaning of the arguments:

Formula: A formula of the form class ~ x1 + x2 + ...data: Dataframe from which variables specified in formula are preferentially to be takenweights: (Case) weights for each example; if missing, defaults to 1subset: An index vector specifying the cases to be used in the training samplena.action: A function to specify the action to be taken if NAs are foundcontrasts: A list of contrasts to be used for some or all of the factors appearing as variables in the model formula

After giving a brief glimpse into the package documentation, let’s review the remaining lines of the proposed in the following code sample:

print(model)

This command prints the details of the net() as follows:

> print(model)
a 3-5-1 network with 26 weights
inputs: Service Ambience Food
output(s): CustomerWillTip
options were - decay=0.05

To plot the model, use the following command:

plotnet(model)

The plot of the model is as follows; there are five nodes in the single hidden layer:

Using NeuralNetTools, it's possible to obtain the relative importance of input variables in neural networks using garson algorithm:

garson(model)

This command prints the various input parameters and their importance to the output prediction, as shown in the following figure:

From the chart obtained from the application of the Garson algorithm, it is possible to note that, in the decision to give the tip, the service received by the customers has the greater influence.

We have seen two neural network libraries in R and used them in simple examples. We would deep dive with several practical use cases throughout this book.

--

--