Automated Dashboard for Classification Neural Network in R

January 16, 2019
By

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)

    Categories

    1. Programming

    Tags

    1. Data Visualisation
    2. Flexdashboard
    3. Neural Networks
    4. R Programming

    In this article, you learn how to make Automated Dashboard for Classification Neural Network in R. First you need to install the `rmarkdown` package into your R library. Assuming that you installed the `rmarkdown`, next you create a new `rmarkdown` script in R.

    After this you type the following code in order to create a dashboard with rmarkdown and flexdashboard:

    ---
    title: "Dashboard Classification Neural Network in R"
    author: "Kristian Larsen"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
        vertical_layout: scroll
    ---
    
    ```{r setup, include=FALSE}
    # Data management packages
    library(tidyverse)
    library(neuralnet)
    library(GGally)
    library(flexdashboard)
    library(plotly)
    
    # Read dataset
    url <- 'http://archive.ics.uci.edu/ml/machine-learning-databases//haberman/haberman.data'
    
    Hab_Data %
      na.omit() %>%
      mutate(Survival = ifelse(Survival == 2, 0, 1),
             Survival = factor(Survival))
    scale01 <- function(x){
      (x - min(x)) / (max(x) - min(x))
    }
    
    Hab_Data %
      mutate(Age = scale01(Age), 
             Operation_Year = scale01(Operation_Year), 
             Number_Pos_Nodes = scale01(Number_Pos_Nodes), 
             Survival = as.numeric(Survival)-1)
    Hab_Data %
      mutate(Survival = as.integer(Survival) - 1, 
             Survival = ifelse(Survival == 1, TRUE, FALSE))
    ```
    
    Row {data-width=350}
    -----------------------------------------------------------------------
    
    ### Chart A - Scattorplot matrix
    ```{r}
    ggpairs(Hab_Data, title = "Scatterplot Matrix of the Features of the Haberman's Survival Data Set")
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Chart B - 1st Classification ANN
    ```{r}
    set.seed(123)
    Hab_NN1 <- neuralnet(Survival ~ Age + Operation_Year + Number_Pos_Nodes, 
                         data = Hab_Data, 
                         linear.output = FALSE, 
                         err.fct = 'ce', 
                         likelihood = TRUE)
    plot(Hab_NN1, rep = 'best')
    ```
    
    Row {data-width=650}
    -----------------------------------------------------------------------
    
    ### Chart C - Classification Hyperparameters
    ```{r}
    Hab_NN1_Train_Error <- Hab_NN1$result.matrix[1,1]
    #paste("CE Error: ", round(Hab_NN1_Train_Error, 3)) 
    Hab_NN1_AIC <- Hab_NN1$result.matrix[4,1]
    #paste("AIC: ", round(Hab_NN1_AIC,3))
    Hab_NN2_BIC <- Hab_NN1$result.matrix[5,1]
    #paste("BIC: ", round(Hab_NN2_BIC, 3))
    set.seed(123)
    # 2-Hidden Layers, Layer-1 2-neurons, Layer-2, 1-neuron
    Hab_NN2 <- neuralnet(Survival ~ Age + Operation_Year + Number_Pos_Nodes, 
                         data = Hab_Data, 
                         linear.output = FALSE, 
                         err.fct = 'ce', 
                         likelihood = 
                           TRUE, hidden = c(2,1))
    
    # 2-Hidden Layers, Layer-1 2-neurons, Layer-2, 2-neurons
    set.seed(123)
    Hab_NN3 <- Hab_NN2 <- neuralnet(Survival ~ Age + Operation_Year + Number_Pos_Nodes, 
                                    data = Hab_Data, 
                                    linear.output = FALSE, 
                                    err.fct = 'ce', 
                                    likelihood = TRUE, 
                                    hidden = c(2,2))
    
    # 2-Hidden Layers, Layer-1 1-neuron, Layer-2, 2-neuron
    set.seed(123)
    Hab_NN4 <- Hab_NN2 <- neuralnet(Survival ~ Age + Operation_Year + Number_Pos_Nodes, 
                                    data = Hab_Data, 
                                    linear.output = FALSE, 
                                    err.fct = 'ce', 
                                    likelihood = TRUE, 
                                    hidden = c(1,2))
    
    # Bar plot of results
    Class_NN_ICs %
      ggplot(aes(Network, Value, fill = Metric)) +
      geom_col(position = 'dodge')  +
      ggtitle("AIC, BIC, and Cross-Entropy Error of the Classification ANNs", "Note: ce Error displayed is 100 times its true value")
    ggplotly(p = ggplot2::last_plot())
    ```
    

    Screenshot:

    The result of the above coding are published with RPubs here.

    References

    1. Using flexdashboard in R

    Related Post

    1. Automated Dashboard for Credit Modelling with Decision trees and Random forests in R
    2. How to Achieve Parallel Processing in Python Programming
    3. Recommender System for Christmas in Python
    4. Automated Dashboard visualizations with Time series visualizations in R
    5. Automated Dashboard visualizations with distribution in R

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

    R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.

    Search R-bloggers


    Sponsors

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)