# Summarize and Explore the Data

## Introduction

The document introduces the SmartEDA package and how it can help you to build exploratory data analysis.

SmartEDA includes multiple custom functions to perform initial exploratory analysis on any input data describing the structure and the relationships present in the data. The generated output can be obtained in both summary and graphical form. The graphical form or charts can also be exported as reports.

सर्वस्य लोचनं शास्त्रं
Science is the only eye

अनेकसंशयोच्छेदि, परोक्षार्थस्य दर्शक|
सर्वस्य लोचनं शास्त्रं, यस्य नास्त्यन्ध एव सः ||

It blasts many doubts, foresees what is not obvious |
Science is the eye of everyone, one who hasnt got it, is like a blind ||

SmartEDA package helps you to construct a good base of data understanding. The capabilities and functionalities are listed below

1. SmartEDA package will make you capable of applying different types of EDA without having to
• remember the different R package names
• write lengthy R scripts
• manual effort to prepare the EDA report
2. No need to categorize the variables into Character, Numeric, Factor etc. SmartEDA functions automatically categorize all the features into the right data type (Character, Numeric, Factor etc.) based on the input data.

3. ggplot2 functions are used for graphical presentation of data

4. Rmarkdown and knitr functions were used for build HTML reports

To summarize, SmartEDA package helps in getting the complete exploratory data analysis just by running the function instead of writing lengthy r code.

## Data

In this vignette, we will be using a simulated data set containing sales of child car seats at 400 different stores.

Data Source ISLR package.

Install the package “ISLR” to get the example data set.

install.packages("ISLR")
library("ISLR")
install.packages("SmartEDA")
library("SmartEDA")
## Load sample dataset from ISLR pacakge
Carseats= ISLR::Carseats

### Overview of the data

Understanding the dimensions of the dataset, variable names, overall missing summary and data types of each variables

# Overview of the data - Type = 1
ExpData(data=Carseats,type=1)

# Structure of the data - Type = 2
ExpData(data=Carseats,type=2)
• Overview of the data
Descriptions Obs
Sample size (Nrow) 400
No. of Variables (Ncol) 11
No. of Numeric Variables 8
No. of Factor Variables 3
No. of Text Variables 0
No. of Logical Variables 0
No. of Date Variables 0
No. of Zero variance Variables (Uniform) 0
%. of Variables having complete cases 100% (11)
%. of Variables having <50% missing cases 0% (0)
%. of Variables having >50% missing cases 0% (0)
%. of Variables having >90% missing cases 0% (0)
• Structure of the data
S.no Variable Name Variable Type % of Missing No. of Unique values
1 Sales numeric 0 336
2 CompPrice numeric 0 73
3 Income numeric 0 98
5 Population numeric 0 275
6 Price numeric 0 101
7 ShelveLoc* factor 0 3
8 Age numeric 0 56
9 Education numeric 0 9
10 Urban* factor 0 2
11 US* factor 0 2

## Exploratory data analysis (EDA)

This function shows the EDA output for 3 different cases

1. Target variable is not defined
2. Target variable is continuous
3. Target variable is categorical

### Example for case 1: Target variable is not defined

#### 1.1 Summary of numerical variables

Summary of all numerical variables

ExpNumStat(Carseats,by="A",gp=NULL,Qnt=seq(0,1,0.1),MesofShape=2,Outlier=TRUE,round=2,Nlim=10)

#### 1.2 Distributions of numerical variables

Graphical representation of all numeric features

• Density plot (Univariate)
# Note: Variable excluded (if unique value of variable which is less than or eaual to 10 [nlim=10])
plot1 <- ExpNumViz(Carseats,target=NULL,nlim=10,Page=c(2,2),sample=8,theme=)
plot1[[1]]

#### 1.3. Summary of categorical variables

• frequency for all categorical independent variables
ExpCTable(Carseats,Target=NULL,margin=1,clim=10,nlim=NULL,round=2,bin=NULL,per=T)
Variable Valid Frequency Percent CumPercent
ShelveLoc Good 85 21.25 45.25
ShelveLoc Medium 219 54.75 100.00
ShelveLoc TOTAL 400 NA NA
Urban No 118 29.50 29.50
Urban Yes 282 70.50 100.00
Urban TOTAL 400 NA NA
US No 142 35.50 35.50
US Yes 258 64.50 100.00
US TOTAL 400 NA NA

NA is Not Applicable

#### 1.4. Distributions of categorical variables

• Bar plots for all categorical variables
plot2 <- ExpCatViz(Carseats,target=NULL,col ="slateblue4",clim=10,margin=2,Page = c(2,1),sample=4)
plot2[[1]]

### Example for case 2: Target variable is continuous

#### 2.1. Target variable

Summary of continuous dependent variable

1. Variable name - Price
2. Variable description - Price company charges for car seats at each site
summary(Carseats[,"Price"])
##      Price
##  Min.   : 24.0
##  1st Qu.:100.0
##  Median :117.0
##  Mean   :115.8
##  3rd Qu.:131.0
##  Max.   :191.0

#### 2.2 Summary of numerical variables

Summary statistics when dependent variable is continuous Price.

ExpNumStat(Carseats,by="A",gp="Price",Qnt=seq(0,1,0.1),MesofShape=1,Outlier=TRUE,round=2)

If Target variable is continuous, summary statistics will add the correlation column (Correlation between Target variable vs all independet variables)

#### 2.3 Distributions of numerical variables

Graphical representation of all numeric variables

• Scatter plot (Bivariate) - with continuous dependent variable

Scatter plot between all numeric variables and target variable Price. This plot help to examine how well a target variable is correlated with dependent variables.

Dependent variable is Price (continuous).

#Note: sample=8 means randomly selected 8 scatter plots
#Note: nlim=4 means included numeric variable with unique value is more than 4
plot3 <- ExpNumViz(Carseats,target="Price",nlim=4,scatter=FALSE,fname=NULL,col="green",Page=c(2,2),sample=8)
plot3[[1]]

• Scatter plot (Bivariate) - between all the numerical variables
#Note: sample=8 means randomly selected 8 scatter plots
#Note: nlim=4 means included numeric variable with unique value is more than 4
plot31 <- ExpNumViz(Carseats,target="US",nlim=4,scatter=TRUE,fname=NULL,Page=c(2,1),sample=4)
plot31[[1]]

#### 2.4. Summary of categorical variables

Summary of categorical variables

• frequency for all categorical independent variables by descretized Price
##bin=4, descretized 4 categories based on quantiles
ExpCTable(Carseats,Target="Price",margin=1,clim=10,nlim=NULL,round=2,bin=4,per=F)

### Example for case 3: Target variable is categorical

#### 3.1. Summary of categorical dependent variable

1. Variable name - Urban
2. Variable description - Whether the store is in an urban or rural location
Urban Frequency Descriptions
No 118 Store location
Yes 282 Store location

#### 3.2 Summary of numerical variables

Summary of all numeric variables

ExpNumStat(Carseats,by="GA",gp="Urban",Qnt=seq(0,1,0.1),MesofShape=2,Outlier=TRUE,round=2)

#### 3.3 Distributions of Numerical variables

• Box plots for all numerical variables vs categorical dependent variable - Bivariate comparision only with categories

Boxplot for all the numeric attributes by each category of Urban

plot4 <- ExpNumViz(Carseats,target="Urban",type=1,nlim=NULL,fname=NULL,col=c("darkgreen","springgreen3","springgreen1"),Page=c(2,2),sample=8)
plot4[[1]]

#### 3.4 Summary of categorical variables

Cross tabulation with target variable

• Custom tables between all categorical independent variables and target variable Urban
ExpCTable(Carseats,Target="Urban",margin=1,clim=10,nlim=NULL,round=2,bin=NULL,per=F)
VARIABLE CATEGORY Urban:No Urban:Yes TOTAL
ShelveLoc Good 28 57 85
ShelveLoc Medium 68 151 219
ShelveLoc TOTAL 118 282 400
US No 46 96 142
US Yes 72 186 258
US TOTAL 118 282 400

Information Value

ExpCatStat(Carseats,Target="Urban",result = "IV",clim=10,nlim=5,bins=10,Pclass="Yes",plot=FALSE,top=20,Round=2)
Variable Class Out_1 Out_0 TOTAL Per_1 Per_0 Odds WOE IV Ref_1 Ref_0
ShelveLoc.1 Bad 74 22 96 0.26 0.19 0.85 0.31 0.02 Yes No
ShelveLoc.2 Good 57 28 85 0.20 0.24 0.81 -0.19 0.01 Yes No
ShelveLoc.3 Medium 151 68 219 0.54 0.58 1.55 -0.07 0.00 Yes No
US.1 No 96 46 142 0.34 0.39 1.24 -0.14 0.01 Yes No
US.2 Yes 186 72 258 0.66 0.61 0.81 0.08 0.00 Yes No
Sales.1 [0,4.11] 29 11 40 0.10 0.09 1.29 0.10 0.00 Yes No
Sales.2 (4.11,5.05] 29 11 40 0.10 0.09 0.75 0.10 0.00 Yes No
Sales.3 (5.05,5.86] 26 14 40 0.09 0.12 0.75 -0.29 0.01 Yes No
Sales.4 (5.86,6.59] 30 10 40 0.11 0.08 0.59 0.32 0.01 Yes No
Sales.5 (6.59,7.49] 32 9 41 0.11 0.08 1.44 0.32 0.01 Yes No
Sales.6 (7.49,8.07] 30 9 39 0.11 0.08 1.55 0.32 0.01 Yes No
Sales.7 (8.07,8.8] 24 16 40 0.09 0.14 1.29 -0.45 0.02 Yes No
Sales.8 (8.8,9.71] 26 14 40 0.09 0.12 0.75 -0.29 0.01 Yes No
Sales.9 (9.71,11.3] 26 14 40 0.09 0.12 1.11 -0.29 0.01 Yes No
Sales.10 (11.3,16.3] 30 10 40 0.11 0.08 1.11 0.32 0.01 Yes No

Statistical test

et4 <- ExpCatStat(Carseats,Target="Urban",result = "Stat",clim=10,nlim=5,bins=10,Pclass="Yes",plot=FALSE,top=20,Round=2)
Variable Target Unique Chi-squared p-value df IV Value Cramers V Degree of Association Predictive Power
ShelveLoc Urban 3 2.738 0.254 2 0.03 0.08 Very Weak Not Predictive
US Urban 2 0.684 0.408 1 0.01 0.04 Very Weak Not Predictive
Sales Urban 10 6.696 0.669 9 0.09 0.13 Weak Somewhat Predictive
CompPrice Urban 10 4.543 0.872 9 0.03 0.11 Weak Not Predictive
Income Urban 10 8.428 0.492 9 0.08 0.15 Weak Not Predictive
Advertising Urban 7 5.565 0.474 6 0.06 0.12 Weak Not Predictive
Population Urban 10 10.560 0.307 9 0.14 0.16 Weak Somewhat Predictive
Price Urban 10 11.143 0.266 9 0.14 0.17 Weak Somewhat Predictive
Age Urban 10 8.414 0.493 9 0.08 0.15 Weak Not Predictive
Education Urban 8 5.122 0.645 7 0.05 0.11 Weak Not Predictive

Variable importance based on Information value

varimp <- ExpCatStat(Carseats,Target="Urban",result = "Stat",clim=10,nlim=5,bins=10,Pclass="Yes",plot=TRUE,top=10,Round=2)

#### 3.5. Distributions of categorical variables

Stacked bar plot with vertical or horizontal bars for all categorical variables

plot5 <- ExpCatViz(Carseats,target="Urban",fname=NULL,clim=5,col=c("slateblue4","slateblue1"),margin=2,Page = c(2,1),sample=2)
plot5[[1]]

## Quantile-quantile plot for numeric variables

Function definition:

ExpOutQQ (data,nlim=NULL,fname=NULL,Page=NULL,sample=NULL)
data    : Input dataframe or data.table
nlim    : numeric variable limit
fname   : output file name (Output will be in PDF format)
Page    : output pattern. if Page=c(3,2), It will generate 6 plots with 3 rows and 2 columns
sample  : random number of plots

Carseats data from ISLR package:

options(width = 150)
CData = ISLR::Carseats
qqp <- ExpOutQQ(CData,nlim=10,fname=NULL,Page=c(2,2),sample=4)
qqp[[1]]

## Parallel Co-ordinate plots

Function definition:

ExpParcoord (data,Group=NULL,Stsize=NULL,Nvar=NULL,Cvar=NULL,scale=NULL)
data    : Input dataframe or data.table
Group   : stratification variables
Stsize  : vector of startum sample sizes
Nvar    : vector of numerice variables, default it will consider all the numeric variable from data
Cvar    : vector of categorical variables, default it will consider all the categorical variable
scale   : scale the variables in the parallel coordinate plot[Default normailized with minimum of the variable is zero and maximum of the variable is one]

### Defualt ExpParcoord funciton

ExpParcoord(CData,Group=NULL,Stsize=NULL,Nvar=c("Price","Income","Advertising","Population","Age","Education"))

### With Stratified rows and selected columns only

ExpParcoord(CData,Group="ShelveLoc",Stsize=c(10,15,20),Nvar=c("Price","Income"),Cvar=c("Urban","US"))

### Without stratification

ExpParcoord(CData,Group="ShelveLoc",Nvar=c("Price","Income"),Cvar=c("Urban","US"),scale=NULL)

### Scale change

std: univariately, subtract mean and divide by standard deviation

ExpParcoord(CData,Group="US",Nvar=c("Price","Income"),Cvar=c("ShelveLoc"),scale="std")

### Selected numeric variables

ExpParcoord(CData,Group="ShelveLoc",Stsize=c(10,15,20),Nvar=c("Price","Income","Advertising","Population","Age","Education"))

### Selected categorical variables

ExpParcoord(CData,Group="US",Stsize=c(15,50),Cvar=c("ShelveLoc","Urban"))

## Customized Summary Statistics

Used ‘data.table’ package functions

Function definition:

ExpCustomStat(data,Cvar=NULL,Nvar=NULL,stat=NULL,gpby=TRUE,filt=NULL,dcast=FALSE)

ExpCustomStat examples

ExpCustomStat(Carseats,Cvar="Urban",Nvar=c("Age","Price"),stat=c("mean","count"),gpby=TRUE,dcast=F)
Urban Attribute mean count
Yes Age 53.62057 282
No Age 52.61017 118
Yes Price 116.51418 282
No Price 114.07627 118
ExpCustomStat(Carseats,Cvar="Urban",Nvar=c("Age","Price"),stat=c("mean","count"),gpby=TRUE,dcast=T)
Attribute mean_No mean_Yes count_No count_Yes
Age 52.61017 53.62057 118 282
Price 114.07627 116.51418 118 282
ExpCustomStat(Carseats,Cvar=c("Urban","ShelveLoc"),Nvar=c("Age","Price","Advertising","Sales"),stat=c("mean"),gpby=FALSE,dcast=T)
Attribute ShelveLoc_Bad ShelveLoc_Good ShelveLoc_Medium Urban_No Urban_Yes
Advertising 6.218750 7.352941 6.538813 6.203390 6.815603
Age 52.052083 52.611765 54.155251 52.610169 53.620567
Price 114.270833 117.882353 115.652968 114.076271 116.514184
Sales 5.522917 10.214000 7.306575 7.563559 7.468191