Relationship Between Two Categorical Variables – Mosaic Plot
We can use a mosaic plot to see how values of two categorical variables are related to each other. Figure 2.6 shows a mosaic plot for fuel.type
and aspiration
of the auto_spec
data set, which is drawn by the following R
codes.
Figure 2.6 Mosaic plot for fuel type and aspiration.
mosaicplot(fuel.type ~ aspiration, data = auto.spec.df,
xlab = "Fuel Type", ylab = "Aspiration",
color = c("green", "blue"),
main = "Mosaic Plot")
In a mosaic plot, the height of a bar represents the percentage for each value of the variable in the vertical axis given a fixed value of the variable in the horizontal axis. For example, in Figure 2.6 the height of the bar corresponding to turbo aspiration is much higher when the fuel type is diesel than when it is gas, which means a higher percentage of diesel cars use turbo aspiration, while a lower percentage of gasoline cars use turbo aspiration. The width of a bar in a mosaic plot corresponds to the frequency , or the number of observations, for each value of the variable in the horizontal axis. For example, from Figure 2.6, the bars for gas fuel type is much wider than those for diesel fuel type, indicating that a much larger number of cars are gasoline cars in the data set.
2.1.3 Plots for More than Two Variables
It is very difficult to plot more than two variables in a two dimensional plot. This section introduces commonly used plots that show some aspects of how multiple variables are related to each other. In Chapter 4, we will study another technique called principal component analysis, which can also serve as a useful tool to visualize high dimensional data in a low dimensional space.
We have seen that a scatter plot can effectively show the relationship between two numerical variables. By adding color coding to the points on a scatter plot of two numerical variables, we are able to study their relationship with a third variable. Typically, the third variable is a categorical variable, with each category represented by a different color. The color coded scatter plot is very useful in visualizing how some numerical variables can be used to predict a categorical variable. For the auto_spec
data, we can use a color coded scatter plot to show how fuel.type
is related to two of the numerical variables horsepower
and peak.rpm
. The color coded scatter plot is shown in Figure 2.7, which is created by the following R
codes.
Figure 2.7 Scatter plot color coded by fuel type.
oldpar <- par(xpd = TRUE) plot(auto.spec.df$peak.rpm ~ auto.spec.df$horsepower,
xlab = "Horsepower", ylab = "Peak RPM",
col = ifelse(auto.spec.df$fuel.type == "gas",
"black", "gray")) legend("topleft", inset = c(0, -0.2),
legend = c("gas", "diesel"),
col = c(“black”, "gray"), pch = 1, cex = 0.8) par(oldpar)
Although there is no clear relationship between the peak RPM and horsepower of a car from the scatter plot in Figure 2.7, it is obvious from the color coded plot that diesel cars tend to have low peak RPM and low horsepower.
Scatter Plot Matrix and Heatmap
The pairwise relationship of multiple numerical variables can be visualized simultaneously by using a matrix of scatter plots. The following R
codes plot the scatter plot matrix for five of the numerical variables in the auto_spec
data set: wheel.base
, height
, curb.weight
, city.mpg
, and highway.mpg
. The column indices of the five variables are 8, 11, 12, 22, and 23, respectively.
var.idx <- c(8, 11, 12, 22, 23) plot(auto.spec.df[, var.idx])
From the scatter plot matrix shown in Figure 2.8, there are different types of relationship among the variables. For example, there is a strong linear relationship between city.mpg
and highway.mpg
. Besides these two variables, wheel.base
, height
, and curb.weight
are positively related to each other. And the curb.weight
is negatively related to both city.mpg
and highway.mpg
.
Figure 2.8 Scatter plot matrix for five numerical variables.
For a large number of numerical variables, it is difficult to visualize all pairwise scatter plots as in the scatter plot matrix. In this case, we can use a heatmap for pairwise correlations of the variables to quickly show the strength of the relationship. The heatmap uses different shades of colors to represent the values of the correlations so that the spots or regions of strong positive or negative relationship can be quickly detected. Detailed discussion of correlation is provided in Section 2.2. We draw the heatmap of correlations for all numerical variables in the auto_spec
data set using the following R
codes.
library(gplots) var.idx <-c(8:12, 15, 17:23) data.nomiss <- na.omit(auto.spec.df[, var.idx]) heatmap.2(cor(data.nomiss), Rowv = FALSE, Colv = FALSE, dendrogram = “none”, cellnote = round(cor(data.nomiss),2), notecol = “black”, key = FALSE, trace = ’none’, margins=c(10,10))
In the above R
codes, we use the heatmap.2()
function from the gplots
package to draw the heatmap. We first remove the observations with missing values using the na.omit()
function. Then the heatmap is drawn for the pairwise correlations calculated by cor()
. In the heatmap of all numerical variables, as shown in Figure 2.9, a lighter color indicates a strong positive (linear) relationship between the variables and a darker color indicates a strong negative (linear) relationship. The correlation values are shown within each cell of the heatmap table. The diagonal cells have the lightest color because any variable has the strongest relationship to itself. From the heatmap in Figure 2.9, we can also see that the two MPG variables ( city.mpg
and highway.mpg
) have strong negative relationships with many of the other numerical variables in the data set.
Figure 2.9 Heatmap of correlation for all numerical variables.
Data visualization is an effective and intuitive representation of the qualitative features of the data. Key characteristics of data can also be quantitatively summarized by numerical statistics. This section introduces common summary statistics for univariate and multivariate data.
2.2.1 Sample Mean, Variance, and Covariance
Sample Mean – Measure of Location
A sample mean or sample average provides a measure of location, or central tendency, of a variable in a data set. Consider a univariate data set, which is a data set with a single variable, that consists of a random sample of n observations x 1, x 2,…, xn . The sample mean is simply the ordinary arithmetic average
Читать дальше