This is a classification problem with three inputs and one categorical output. We will address the problem with the following code:
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:
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.