Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. Figure 1: Spearman correlation heat map with correlation coefficient and significance levels based on the mtcars data set.

In a recent paper we included data from a survey we conducted. During the publication process, one of the reviewers asked for a more in depth statistical analysis of the data set. He (she?) explicitly expressed a special interest in correlating the variables of the survey in order to spot any interesting correlations. This posed a number of problems:

1. The data set has 35 variables. This translates into a huge correlation matrix.

2. There are many ways to correlate variables. In my field the Pearson product-moment correlation coefficient (Pearson’s r) and the Spearman’s rank correlation coefficient (Spearman’s rho) are the most common ones. Which one should I choose and why?

3. Correlations by themselves are not very useful. You, most likely, want to have at least some information about the statistical significance of the correlation.

### The Data Set

For the sake of demonstration we will use the mtcars data set, provided by the stats package. The data set includes 11 variables with 32 observations, hence rendering the task a bit more manageable.

Table 1: The first five (of 32) observations of the mtcars data set.
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.06160.01103.902.62016.460144
Mazda RX4 Wag21.06160.01103.902.87517.020144
Datsun 71022.84108.0933.852.32018.611141
Hornet 4 Drive21.46258.01103.083.21519.441031
Table 2: Description of the variable labels of the mtcars data set.
axisdescription
mpgMiles/(US) gallon
cylNumber of cylinders
dispDisplacement (cu.in.)
hpGross horsepower
dratRear axle ratio
wtWeight (lb/1000)
qsec1/4 mile time
vsV/S
amTransmission (0 = automatic, 1 = manual)
gearNumber of forward gears
carbNumber of carburetors

### Choosing a Suitable Correlation Algorithm

As most papers in my field use either Pearson’s r or Spearman’s rho I began my research there. As it turns out, there are two types of algorithms: parametric and non-parametric. Parametric algorithms depend on some assumptions about the data set. In case of Pearson’s r you need to know about the distribution, scaling and dependency of the variables of the data set.

Spearman’s rho, on the other hand, is a non-parametric algorithm. It compares two ranked variables of either ordinal, interval or ratio type and describes how well they can be described using a monotonic function. No further assumptions on the data set are necessary which is handy if you do not know too much about the distribution of your data.

### Let’s Get Started!

R, a statistical computing programming language, is being used for the statistical analysis with some additional libraries: ggplot2 for plotting, Hmisc to create a correlation matrix, reshape2 to meld the data frame as well as stats to provide the mtcars data set.

``````library(ggplot2)
library(reshape2)
library(Hmisc)
library(stats)``````

The first thing is to calculate the correlation of the individual variables to another. As we have our data already in a data frame, merely a call of the rcorr function of the Hmisc library is necessary. This creates a new list with two entries: ”r” the correlation coefficients and ”P” the significance levels.

rcorr Computes a matrix of Pearson’s r or Spearman’s rho rank correlation coefficients for all possible pairs of columns of a matrix. Missing values are deleted in pairs rather than deleting all rows of x having any missing variables. Ranks are computed using efficient algorithms, using midranks for ties.
— R Documentation / rcorr {Hmisc}

``````d <- mtcars
cormatrix = rcorr(as.matrix(d), type='spearman')``````
Table 3: Resulting correlation matrix – correlation coefficients (cormatrix\$r)
mpgcyldisphpdratwtqsecvsamgearcarb
mpg1.00-0.91-0.91-0.890.65-0.890.470.710.560.54-0.66
cyl-0.911.000.930.90-0.680.86-0.57-0.81-0.52-0.560.58
disp-0.910.931.000.85-0.680.90-0.46-0.72-0.62-0.590.54
hp-0.890.900.851.00-0.520.77-0.67-0.75-0.36-0.330.73
drat0.65-0.68-0.68-0.521.00-0.750.090.450.690.74-0.13
wt-0.890.860.900.77-0.751.00-0.23-0.59-0.74-0.680.50
qsec0.47-0.57-0.46-0.670.09-0.231.000.79-0.20-0.15-0.66
vs0.71-0.81-0.72-0.750.45-0.590.791.000.170.28-0.63
am0.56-0.52-0.62-0.360.69-0.74-0.200.171.000.81-0.06
gear0.54-0.56-0.59-0.330.74-0.68-0.150.280.811.000.11
carb-0.660.580.540.73-0.130.50-0.66-0.63-0.060.111.00
Table 4: Resulting correlation matrix – significance levels (cormatrix\$P)
mpgcyldisphpdratwtqsecvsamgearcarb
mpgNA0.000.000.000.000.000.010.000.000.000.00
cyl0.00NA0.000.000.000.000.000.000.000.000.00
disp0.000.00NA0.000.000.000.010.000.000.000.00
hp0.000.000.00NA0.000.000.000.000.040.060.00
drat0.000.000.000.00NA0.000.620.010.000.000.49
wt0.000.000.000.000.00NA0.210.000.000.000.00
qsec0.010.000.010.000.620.21NA0.000.260.420.00
vs0.000.000.000.000.010.000.00NA0.360.120.00
am0.000.000.000.040.000.000.260.36NA0.000.73
gear0.000.000.000.060.000.000.420.120.00NA0.53
carb0.000.000.000.000.490.000.000.000.730.53NA

The correlation coefficients can be plotted using a heat map representation. ggplot2 provides the geom_tile geometric object for this purpose. In order to plot the correlation matrix we need to meld the data frame first.

``````cordata = melt(cormatrix\$r)
ggplot(cordata, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() + xlab("") + ylab("")`````` Figure 2: Spearman correlation heat map based on the mtcars data set. No information about significance levels is included

Figure 2 shows the resulting heat map, plotted by ggplot2. However, some key data is missing. There is no information about the significance levels nor does the plot include the numeric values of Spearman’s rho. In order to efficiently use the little space per tile, I wrote a little function ("abbreviateSTR") which abbreviates the values. The resulting strings are stored in a label column. This column can then be plotted onto the corresponding tile. To further enhance the distinction between significant (P > 0.05) and insignificant correlations, a red, semi transparent, ”X” is being overlaid onto insignificant tiles. The resulting plot is shown in figure 1.

### Conclusion

By using the non-parametric Spearman algorithm it is possible to create a preliminary overview of the correlations within a data set. This information can be plotted utilizing a heat map representation in conjunction with significance levels as well as numeric values of Spearman’s rho. The resulting plot is a comprehensible representation of the data set, which allows quick identification of significant correlations.

Looking at the heat map, one could get the impression that the gross horsepower has a strong negative correlation (r ≈ -0.89, P < 0.01) with the gasoline consumption. Who would have thought?

### Complete R Script

``````#!/usr/bin/env Rscript

library(ggplot2)
library(reshape2)
library(Hmisc)
library(stats)

abbreviateSTR <- function(value, prefix){  # format string more concisely
lst = c()
for (item in value) {
if (is.nan(item) || is.na(item)) { # if item is NaN return empty string
lst <- c(lst, '')
next
}
item <- round(item, 2) # round to two digits
if (item == 0) { # if rounding results in 0 clarify
item = '<.01'
}
item <- as.character(item)
item <- sub("(^)+", "", item)    # remove leading 0: 0.05 -> .05
item <- sub("(^-)+", "-", item)  # remove leading -0: -0.05 -> -.05
lst <- c(lst, paste(prefix, item, sep = ""))
}
return(lst)
}

d <- mtcars

cormatrix = rcorr(as.matrix(d), type='spearman')
cordata = melt(cormatrix\$r)
cordata\$labelr = abbreviateSTR(melt(cormatrix\$r)\$value, 'r')
cordata\$labelP = abbreviateSTR(melt(cormatrix\$P)\$value, 'P')
cordata\$label = paste(cordata\$labelr, "n",
cordata\$labelP, sep = "")
cordata\$strike = ""
cordata\$strike[cormatrix\$P > 0.05] = "X"

txtsize <- par('din') / 2
ggplot(cordata, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
theme(axis.text.x = element_text(angle=90, hjust=TRUE)) +
xlab("") + ylab("") +
geom_text(label=cordata\$label, size=txtsize) +
geom_text(label=cordata\$strike, size=txtsize * 4, color="red", alpha=0.4)``````