Measures of association for metric variables
In statistics, we are often interested in examining the relationship between two metric variables (e.g., height and weight). There are various measures for quantifying this dependence. The best-known are the Bravais–Pearson correlation coefficient and the Spearman rank correlation coefficient. In this blog post, I’ll show you how these measures work, when to use them, and how to calculate them in R.
Bravais–Pearson correlation coefficient
The Bravais–Pearson correlation coefficient $r$ measures the strength and direction of the linear relationship between two metric variables. It indicates how well the data points can be described by a straight line.
The formula is:
$r = \frac{\sum (x_i – \bar{x})(y_i – \bar{y})}{\sqrt{\sum (x_i – \bar{x})^2 \sum (y_i – \bar{y})^2}}$
Interpretation:
- $r = 1$: Perfect positive linear relationship (when $X$ increases, $Y$ also increases).
- $r = -1$: Perfect negative linear relationship (when $X$ increases, $Y$ decreases).
- $r = 0$: No linear relationship.
The closer $r$ is to 1 or -1, the stronger the linear relationship between the variables.
Example in R
Let’s look at an example: You want to examine the relationship between the height (in cm) and weight (in kg) of 10 people.
# Enter data
height <- c(160, 170, 175, 180, 185, 165, 190, 175, 168, 182)
weight <- c(55, 70, 68, 85, 90, 60, 95, 75, 63, 80)
# Calculate the Pearson correlation coefficient
cor(height, weight)
This code gives you the value of the Pearson correlation coefficient, showing you how strong the linear relationship between height and weight is.
Spearman rank correlation coefficient
The Spearman rank correlation coefficient $r_{SP}$ measures the strength and direction of a monotonic relationship between two variables, based on the ranks of the data points rather than their actual values. This coefficient is more robust to outliers and is also useful when the relationship between the variables is not linear.
The formula is:
$r_{SP} = 1 – \frac{6 \sum d_i^2}{n(n^2 – 1)}$
where $d_i$ is the difference between the ranks of the two variables, and $n$ is the number of observations.
Example in R
Suppose you want to examine the relationship between the ranks of corresponding height and weight data. Here is the R code:
# Calculate the Spearman correlation coefficient
cor(groesse, gewicht, method = "spearman")
This code returns the Spearman rank correlation coefficient, which describes the monotonic relationship between the two variables.
Visualizing the relationship
You can also visualize the relationship between two variables to get a better sense of it. A scatterplot is a common tool for determining whether a linear relationship exists.
# Create a scatterplot
plot(groesse, gewicht, main = "Relationship between height and weight",
xlab = "Height (cm)", ylab = "Weight (kg)", pch = 19)
This scatterplot shows you the distribution of the data points. If the points roughly follow a line, this indicates a strong linear relationship.
Conclusion
Measures of association such as the Bravais–Pearson correlation coefficient and Spearman’s rank correlation coefficient help you quantify the degree of dependence between two metric variables. Pearson’s correlation is particularly suitable for linear relationships, whereas Spearman’s correlation can also be used for monotonic relationships. You can calculate both measures in R with just a few lines of code and additionally support your analysis visually with a scatter plot.
Feel free to experiment with your own data to see how these measures work and what insights you can gain from them!
