Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Applied Supervised Learning with R

You're reading from   Applied Supervised Learning with R Use machine learning libraries of R to build models that solve business problems and predict future trends

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781838556334
Length 502 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Jojo Moolayil Jojo Moolayil
Author Profile Icon Jojo Moolayil
Jojo Moolayil
Karthik Ramasubramanian Karthik Ramasubramanian
Author Profile Icon Karthik Ramasubramanian
Karthik Ramasubramanian
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Applied Supervised Learning with R
Preface
1. R for Advanced Analytics FREE CHAPTER 2. Exploratory Analysis of Data 3. Introduction to Supervised Learning 4. Regression 5. Classification 6. Feature Selection and Dimensionality Reduction 7. Model Improvements 8. Model Deployment 9. Capstone Project - Based on Research Papers Appendix

Chapter 2: Exploratory Analysis of Data


Activity 4: Plotting Multiple Density Plots and Boxplots

  1. First, load the necessary libraries and packages in the RStudio:

    library(ggplot2)
    library(cowplot)
  2. Read the bank-additional-full.csv dataset in a DataFrame named df:

    df <- read.csv("bank-additional-full.csv",sep=';')
  3. Define the plot_grid_numeric function for density plot:

    plot_grid_numeric <- function(df,list_of_variables,ncols=2){
      plt_matrix<-list()
      i<-1
      for(column in list_of_variables){
        plt_matrix[[i]]<-ggplot(data=df,aes_string(x=column)) + 
          geom_density(fill="red",alpha =0.5)  +
          ggtitle(paste("Density Plot for variable:",column)) + theme_bw()
        i<-i+1
      }
      plot_grid(plotlist=plt_matrix,ncol=2)
    }
  4. Plot the density plot for the campaign, pdays, previous, and emp.var.rate variables:

    plot_grid_numeric(df,c("campaign","pdays","previous","emp.var.rate"),2)

    The output is as follows:

    Figure 2.27: Density plot for the campaign, pdays, previous, and emp.var.rate variables

    Observe that the interpretations we obtained using the histogram are visibly true in density plot as well. Hence, this serves as another alternative plot for looking at the same trend.

  5. Repeat step 4 for boxplot:

    plot_grid_numeric <- function(df,list_of_variables,ncols=2){
      plt_matrix<-list()
      i<-1
      for(column in list_of_variables){
        plt_matrix[[i]]<-ggplot(data=df,aes_string(y=column)) + 
          geom_boxplot(outlier.colour="black") +
          ggtitle(paste("Boxplot for variable:",column)) + theme_bw()
        i<-i+1
      }
      plot_grid(plotlist=plt_matrix,ncol=2)
    }
    plot_grid_numeric(df,c("campaign","pdays","previous","emp.var.rate"),2)

    The output is as follows:

    Figure 2.28: Boxplot for the campaign, pdays, previous, and emp.var.rate variables

Now, let's explore the last four numeric variable of the dataset, that is, nr.employed, euribor3m, cons.conf.index, and duration, and see whether we could derive some meaningful insights.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime