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 Ripley
William 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 taken
weights: (Case) weights for each example; if missing, defaults to 1
subset: An index vector specifying the cases to be used in the training sample
na.action: A function to specify the action to be taken if NAs are found
contrasts: 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset