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:
No |
CustomerWillTip |
Service |
Ambience |
Food |
TipOrNo |
1 |
1 |
4 |
4 |
5 |
Tip |
2 |
1 |
6 |
4 |
4 |
Tip |
3 |
1 |
5 |
2 |
4 |
Tip |
4 |
1 |
6 |
5 |
5 |
Tip |
5 |
1 |
6 |
3 |
4 |
Tip |
6 |
1 |
3 |
4 |
5 |
Tip |
7 |
1 |
5 |
5 |
5 |
Tip |
8 |
1 |
5 |
4 |
4 |
Tip |
9 |
1 |
7 |
6 |
4 |
Tip |
10 |
1 |
7 |
6 |
4 |
Tip |
11 |
1 |
6 |
7 |
2 |
Tip |
12 |
1 |
5 |
6 |
4 |
Tip |
13 |
1 |
7 |
3 |
3 |
Tip |
14 |
1 |
5 |
1 |
4 |
Tip |
15 |
1 |
7 |
5 |
5 |
Tip |
16 |
0 |
3 |
1 |
3 |
No-tip |
17 |
0 |
4 |
6 |
2 |
No-tip |
18 |
0 |
2 |
5 |
2 |
No-tip |
19 |
0 |
5 |
2 |
4 |
No-tip |
20 |
0 |
4 |
1 |
3 |
No-tip |
21 |
0 |
3 |
3 |
4 |
No-tip |
22 |
0 |
3 |
4 |
5 |
No-tip |
23 |
0 |
3 |
6 |
3 |
No-tip |
24 |
0 |
4 |
4 |
2 |
No-tip |
25 |
0 |
6 |
3 |
6 |
No-tip |
26 |
0 |
3 |
6 |
3 |
No-tip |
27 |
0 |
4 |
3 |
2 |
No-tip |
28 |
0 |
3 |
5 |
2 |
No-tip |
29 |
0 |
5 |
5 |
3 |
No-tip |
30 |
0 |
1 |
3 |
2 |
No-tip |
Â
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)
########################################################################