---
title: "Neural Network in R"
author: "ChenHsi Shen"
date: '2017-06-06'
output:
  html_document:
    theme: cosmo
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# Please write down your working directory below.
#knitr::opts_knit$set(root.dir = normalizePath("~/Desktop/kyper/Grupo_Bimbo/data")) 
```

***

## Neural Network Example

### Loading data and libraries.

Read all the libraryies you need.

```{r, include=FALSE}
library(neuralnet)
library(nnet)
library(caret)

Data <- iris
```

### Build labels for classification

In `iris` dataset, there are three types of flowers, which means we're now dealing with a multi-class classification.

```{r}
xor <- model.matrix(~ Species, Data)
xor[,1] <- ifelse(xor[,2] %in% 0 & xor[,3] %in% 0, 1, 0)
colnames(xor)[1] <- 'Speciessetosa'
head(xor)

Data <- cbind(xor, Data[,-5])
```

### Split data for training

```{r}
set.seed(30)
DataFinal <- Data[sample(nrow(Data)),]

Size <- floor(0.7*nrow(Data))

TrainSet <- DataFinal[1:Size, ]
TestSet <- DataFinal[c(Size+1):nrow(Data), ]
```

### Train NN model

We can us `neuralnet()` to train a NN model. Also, the `train()` function from `caret` can help us tune parameters. We can plot the result to see which set of parameters is fit our data the best.

```{r}
FormulaNN <- Speciessetosa + Speciesversicolor + Speciesvirginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

Model <- train(form=FormulaNN,     
               data=TrainSet,           
               method="neuralnet",   
               ### Parameters for layers
               tuneGrid = expand.grid(.layer1=c(1:4), .layer2=c(0:4), .layer3=c(0)),               
               ### Parameters for optmization
               learningrate = 0.01,  
               threshold = 0.01,     
               stepmax = 50000         
)

plot(Model)

```

### Bulid best model

Build model based on the result from the plot above and plot the model.

```{r}
BestModel <- neuralnet(formula = FormulaNN, 
                 data = TrainSet,
                 hidden = c(1,2),     
                 learningrate = 0.01, 
                 threshold = 0.01,   
                 stepmax = 50000       
                 
)

plot(BestModel, rep = 'best')
```

### Measure the performance

Use `compute()` to calculate the prediction of the model.

```{r}
pred <-  compute(BestModel, TestSet[,4:7])
pred <- round(pred$net.result)

Result <- ifelse(pred[,1] %in% 1, 'setosa',
                 ifelse(pred[,2] %in% 1, 'versicolor', 'virginica'))
Species <- ifelse(TestSet[,1] %in% 1, 'setosa',
                 ifelse(TestSet[,2] %in% 1, 'versicolor', 'virginica'))

table(Result, Species)
```

