generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 77
/
automate_eda_with_dataexplorer.Rmd
171 lines (109 loc) · 5.91 KB
/
automate_eda_with_dataexplorer.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# automate eda with dataexplorer
Jing You
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# library("devtools")
# devtools::install_github("boxuancui/DataExplorer")
```
```{r}
library(alr4)
library(DataExplorer)
```
## Overview
* This section shows how to use package DataExplorer to automate Exploratory Data Analysis (EDA) and create Data Report.
## DataExplorer
* DataExplorer attempts to automate exploratory data analysis (EDA) process and offer one-click report generation to show and visualize basics about a data set. It is a user-friendly and efficient tool for first-step analysis with visualization that can avoid time consuming manual coding.
## Installation
```{r}
#devtools::install_github("boxuancui/DataExplorer")
#library(DataExplorer)
```
* The dataset "sleep1" will be used for the following demonstration, which has both discrete and continuous variables.
```{r,message=FALSE}
data(sleep1)
```
## Exploratory data analysis (EDA)
* DataExplorer is a great tool for exploratory data analysis that it can handle most visualization plots without the need to manipulate data formats and data types. Every visualization plots can be done in one line function call instead of using different packages and functions.
### Overall Information
* There are many plots available in DataExplorer for preliminary data analysis to help us better understand the data set. For example,
* plot_str function can be used to visualizing the basic structure about the data set with names and types specified. We can see the sleep dataset has 62 observtations, 10 variables (7 discrete and 3 continuous)
```{r}
## View basic description for the data
plot_str(sleep1,type="diagonal")
```
* plot_intro function can be used to describe the basic info about the data set including number of rows/cols/data type/missing values. The plot show the percentage of the data types and the comple rows. It also specify if there are column where all values are missing.
```{r}
## View basic description for the data
plot_intro(sleep1,title="Introduction of the sleep data")
```
* plot_missing function can further let us take a peek into the profile of the missing values. Specifically which variables has missing values and what is the corresponding proportion.
```{r}
## View data profie for the missing data
plot_missing(sleep1, title="Missing Data Profile")
```
### Distribution
* DataExplorer provides bar chart, histogram, density plot, scatterplot and boxplot, etc for the exploration of the data distribution. Those function call on the whole dataset would only plot on the according discrete/continuous variables. Thus we do not need to specify the columns for plotting. Other than that, the arguments for the plot functions are mostly consistent with those in the ggplot functions that makes it easy to use.
* For example, the plot_bar function plots the distribution only for the discrete variables.
```{r}
##`Life` distribution of all discrete variables
plot_bar(sleep1, with = "Life",title="Bar distribution in response to Life span")
```
* The plot_histogram function plots the distribution only for the continuous variables.
```{r}
plot_histogram(sleep1,title= "Histogram Distribution of sleep data")
```
* The plot_qq function plots the qq-plot only for the continuous variables.
````{r,message=FALSE}
##View quantile-quantile plot of all continuous variables by feature `Life`
plot_qq(sleep1,by='Life',title="QQ-plot in response to Life span")
```
### Correlation Analysis
* DataExplorer provides correlation heatmap plot for all non-missing features. The heatmap can also be set to plot for continuous or discrete variables only.
````{r}
plot_correlation(na.omit(sleep1),title="Correlation heatmap of the sleep data")
```
````{r}
plot_correlation(na.omit(sleep1),type = "c",,title="Correlation heatmap of the sleep data, continuous only")
```
* For the discrete variable, the heatmap automatically done one hot encoding that provides further insights.
````{r}
plot_correlation(na.omit(sleep1),type = "d",,title="Correlation heatmap of the sleep data,discrete only")
```
## Feature Engineering
* Feature engineering is often needed in the data analysis process to transform data into better representative features. DataExplorer provides mutiple functions for feature engineering including missing value filling, sparse categories grouping, one hot encoding and feature transformation.
* The set_missing function can fill both the discrete and continuous variable with designated values in one line of code.
### Missing value
````{r}
final_sleep <- set_missing(sleep1, list(0L, "unknown"))
plot_missing(final_sleep)
```
* One hot encoding allows the categorical data to be more expressive. It can be done by the dummify function.
### Dummy variable
````{r}
plot_str(
list(
"original" = final_sleep,
"dummified" = dummify(final_sleep)
)
)
```
## Data Report
* All the summary statistic and visualization plots of the data set can be organized into a data report in 1 step. The report automatically generates most visualization plots above. It is indeed a rough data profile but very useful for initial analysis and user-friendly for beginners
````{r}
#create_report(final_sleep)
````
![Data report sample](resources/automate_eda_with_dataexplorer/Unknown.png)
* This function is very powerful that it provides user the ability to configure based on the needs. Each section and the arguments can be rendered, reponse variable can also be added. For example, we can add boxplot and scatterplot to the report, set number of sampled row in the qq-plot, and set response variable to Life.
````{r}
config <-configure_report(
add_plot_boxplot = TRUE,
add_plot_scatterplot = TRUE,
"plot_qq" = list(sampled_rows = 1000L)
)
#create_report(final_sleep,y ="Life",config=config)
````
## External Resources
https://boxuancui.github.io/DataExplorer/index.html : DataExplorer Github Page
https://rpubs.com/mark_sch7/DataExplorerPackage : package reflection