
Describe Data and EDA
Zhaoxia Yu
Image from Grolemund, G., & Wickham, H. (2018). R for data science (CC BY-NC-ND 3.0).
We begin by examining one variable at a time.
The goal is to gain a general understanding of each variable by:
identifying the possible values,
understanding how the values are distributed, and
observing how the variable varies across individuals in the sample.
In summary, we are exploring the distribution of each variable.
head()tail()glimpse()Rows: 2,700
Columns: 7
$ id <chr> "S060833", "S932623", "S755478", "S852291", "S011143", "S069…
$ diagnosis <fct> 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0, 1, 2, 2, 2, 1, …
$ age <int> 74, 56, 77, 74, 75, 72, 64, 78, 73, 81, 66, 65, 66, 73, 78, …
$ educ <int> 12, 16, 18, 20, 14, 16, 16, 17, 18, 13, 16, 16, 17, 20, 13, …
$ female <fct> 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, …
$ height <dbl> 65.0, 62.0, 65.0, 62.0, 62.0, 61.8, 60.0, 69.0, 65.0, 71.0, …
$ weight <int> 233, 110, 137, 112, 127, 141, 124, 152, 131, 197, 134, 144, …
For numerical variables, we can meaningfully compute statistics such as the mean, minimum, and maximum.
age, height, and weight are numerical variables because their values are numeric and the numbers have their usual quantitative meaning.
female may be coded as 0, and 1, but these numbers are simply labels.
Such variables are categorical variables, whose values belong to a finite set of categories.
Although categorical variables are often encoded using numbers, the numeric codes do not represent quantities and arithmetic operations (e.g., averaging) are not meaningful.
Bar graphs are commonly used to visualize categorical variables.
Each bar represents a category, and its height indicates either:
for that category.
For a categorical variable, the most useful summary statistics are:
Frequency: Number of observations in each category \(n_c\).
Relative frequency: Proportion (or percentage) of observations in each category \(p_c = \frac{n_c}{n}\).
Mode: The category with the highest frequency.
Tip
For categorical variables, the mean and standard deviation are not meaningful.
\[\bar{x} = \frac{\sum x_i}{n}\]
Both patients have the same mean and same median.
However, Patient B has much greater variability in blood pressure.
Measures of center alone do not fully describe a distribution. We also need measures of spread.
Two common measures of spread are the sample variance and the sample standard deviation.
They measure how far the observations tend to be from the sample mean.
For each observation \(x_i\), compute its deviation from the mean: \(x_i-\bar{x}.\)
The sample variance and standard deviation are calculated from these deviations.
Sample variance: \[s^2 = \frac{\sum (x_i - \bar{x})^2}{n-1}\]
sample standard deviation (SD): \[s = \sqrt{s^2}\]
The (q) quantile is also called the (100q)th percentile.
Examples:
| Quantile | Percentile | Name |
|---|---|---|
| 0.25 | 25th | First quartile (Q1) |
| 0.50 | 50th | Median (Q2) |
| 0.75 | 75th | Third quartile (Q3) |
The ordered observations can be divided into four equal parts using the 25th, 50th, and 75th percentiles.
These three values are called the quartiles:
The distance between Q1 and Q3 is called the interquartile range (IQR): \(\mathrm{IQR}=Q_3-Q_1.\)
We have learned variance and standard deviation as measures of spread.
Two other measures:
Median: line inside the box
Q1 and Q3: bottom and top of the box
IQR: height of the box \(\text{IQR} = Q_3 - Q_1\)
Whiskers: extend to the most extreme observations within 1.5 × IQR of Q1 and Q3
Outliers: observations beyond the whiskers, shown as individual points
Histograms provide another useful visual representation of the distribution of a numerical variable.
A histogram displays the distribution of a numerical variable by grouping observations into intervals (called bins).
The height of each bar can represent the frequency, relative frequency \(p_c=\frac{n_c}{n}\), or the percentage \((100\times p_c)\) of observations in that interval.
In statistics, histograms are often drawn using density rather than relative frequency.
The density is the relative frequency per unit interval: \[f_c=\frac{p_c}{w_c},\]
where \(w_c\) is the width of interval \(c\).
Note that \(f_c\) could be greater than 1; it is not a probability, but the area of the bar is a probability. The total area of the histogram is 1.
The shape of a histogram shows us how the observed values spread around the location.
Symmetric around its location: when the densities are [almost] the same for any two intervals that are equally distant from the center.
Left-skewed: stretched to the left. Example: age at death.
Right-skewed: stretched to the right. Example: house income.
We will use the age variable to practice exploratory data analysis (EDA).
Our goals are to answer the following questions:
Min. 1st Qu. Median Mean 3rd Qu. Max.
21.00 64.00 72.00 70.05 78.00 100.00
From the histogram, boxplot, and summary statistics, describe the distribution of age.
Consider:
femaleWe will use the female variable to practice EDA for a categorical variable.
Our goals are to answer the following questions:
Tip: Understand the Coding
Categorical variables are often stored using numeric codes.
For the female variable:
The numbers are labels, not quantities. Always check the coding before interpreting the results.
0 1
0.4262963 0.5737037
0 1
42.6 57.4
Describe the distribution of female.
| Numerical Variable (Age) | Categorical Variable (Female) |
|---|---|
| Mean, Median | Frequency, Relative Frequency |
| Standard Deviation, IQR | Mode |
| Histogram | Bar Graph |
| Boxplot | Frequency Table |
| Variable | Summary |
|---|---|
| Age (years) | Mean ± SD (or Median [IQR]) |
| Female | n (%) |
There are packages to automate the creation of summary tables.
A Table 1 provides a useful summary of the data, but it is only the beginning of exploratory data analysis.
EDA helps us:
Remember: Good data analysis begins with understanding the data.
