The Problem
Pearson’s sample correlation is a measure of the relationship between two numeric variables. It is used to summarize the bivariate relationship. To illustrate the calculations, let us use the following example.
A professor would like to investigate the relationship between student score on the midterm and the final exam. To do this, the professor collected the exam scores from n = 7 students. Using the data below, what is that correlation?
Here is the data. Note that there are two measurements on each unit (student). We are interested in the relationship between those two measurements.
Table of the bivariate data generated
Student | Midterm | Final |
Identifier | Score | Score |
1 | 1 | 34 |
2 | 54 | 68 |
3 | 99 | 14 |
4 | 1 | 78 |
5 | 31 | 85 |
6 | 83 | 94 |
7 | 55 | 20 |
Calculate the correlation of this sample.
Your Answer
You got the correct answer of -0.1991. Congratulations!
Unfortunately, your answer was not correct. Either try again or click on “Show Solution” below to see how to obtain the correct answer.
Assistance
Hide Solution
$$ \begin{align}
r &= \frac{1}{n-1}\ \sum_{i=1}^{n}\ \left(\frac{x_i - \bar{x}}{s_x}\right)\left(\frac{y_i - \bar{y}}{s_y}\right) \\[3em]
&= \frac{1}{7-1}\ \sum_{i=1}^{7}\ \left(\frac{x_i - 46.2857}{37.853729}\right)\left(\frac{y_i - 56.1429}{32.804616}\right) \\[1em]
&= \frac{1}{6}\ \left[ \left(\frac{x_1 - 46.2857}{37.853729}\right)\left(\frac{y_1 - 56.1429}{32.804616}\right) \right. \\
& \qquad + \left(\frac{x_2 - 46.2857}{37.853729}\right)\left(\frac{y_2 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{x_3 - 46.2857}{37.853729}\right)\left(\frac{y_3 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{x_4 - 46.2857}{37.853729}\right)\left(\frac{y_4 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{x_5 - 46.2857}{37.853729}\right)\left(\frac{y_5 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{x_6 - 46.2857}{37.853729}\right)\left(\frac{y_6 - 56.1429}{32.804616}\right) \\
& \qquad + \left. \left(\frac{x_7 - 46.2857}{37.853729}\right)\left(\frac{y_7 - 56.1429}{32.804616}\right) \right] \\[1em]
&= \frac{1}{6}\ \left[ \left(\frac{1 - 46.2857}{37.853729}\right)\left(\frac{34 - 56.1429}{32.804616}\right) \right. \\
& \qquad + \left(\frac{54 - 46.2857}{37.853729}\right)\left(\frac{68 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{99 - 46.2857}{37.853729}\right)\left(\frac{14 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{1 - 46.2857}{37.853729}\right)\left(\frac{78 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{31 - 46.2857}{37.853729}\right)\left(\frac{85 - 56.1429}{32.804616}\right) \\
& \qquad + \left(\frac{83 - 46.2857}{37.853729}\right)\left(\frac{94 - 56.1429}{32.804616}\right) \\
& \qquad + \left. \left(\frac{55 - 46.2857}{37.853729}\right)\left(\frac{20 - 56.1429}{32.804616}\right) \right] \\[1em]
&= \frac{1}{6}\ \left[ \left(\frac{-45.2857}{37.853729}\right)\left(\frac{-22.1429}{32.804616}\right) \right. \\
& \qquad + \left(\frac{7.7143}{37.853729}\right)\left(\frac{11.8571}{32.804616}\right) \\
& \qquad + \left(\frac{52.7143}{37.853729}\right)\left(\frac{-42.1429}{32.804616}\right) \\
& \qquad + \left(\frac{-45.2857}{37.853729}\right)\left(\frac{21.8571}{32.804616}\right) \\
& \qquad + \left(\frac{-15.2857}{37.853729}\right)\left(\frac{28.8571}{32.804616}\right) \\
& \qquad + \left(\frac{36.7143}{37.853729}\right)\left(\frac{37.8571}{32.804616}\right) \\
& \qquad + \left. \left(\frac{8.7143}{37.853729}\right)\left(\frac{-36.1429}{32.804616}\right) \right] \\[1em]
&= \frac{1}{6}\ \Big[\ \left(-1.196334\right)\left(-0.674992\right) \Big. \\
& \qquad + \left(0.203792\right)\left(0.361447\right) \\
& \qquad + \left(1.392578\right)\left(-1.284662\right) \\
& \qquad + \left(-1.196334\right)\left(0.666283\right) \\
& \qquad + \left(-0.40381\right)\left(0.879667\right) \\
& \qquad + \left(0.969899\right)\left(1.154019\right) \\
& \qquad + \Big. \left(0.230209\right)\left(-1.101761\right)\ \Big] \\[1em]
&= \frac{1}{6}\ \Big[\ \left(0.807516\right) \Big. \\
& \qquad + \left(0.07366\right) \\
& \qquad + \left(-1.788993\right) \\
& \qquad + \left(-0.797097\right) \\
& \qquad + \left(-0.355218\right) \\
& \qquad + \left(1.119281\right) \\
& \qquad + \Big. \left(-0.253636\right)\ \Big] \\[1em]
&= \frac{1}{6}\ \Big[\ -1.1945\ \Big] \\[1em]
\end{align}
$$
And so, the correlation between the midterm and final examination scores in this sample is r = -0.1991.
As this value is negative, it indicates that those who scored better on the midterm scored worse on the final.
This relationship, however, is weak.
Note, too, that we cannot (at this point) draw any conclusion about the relationship between these two variables in the population. This is only a measure of that relationship in this sample.
Hide the R Code
Copy and paste the following code into your R script window, then run it from there.
xvals = c(1, 54, 99, 1, 31, 83, 55)
yvals = c(34, 68, 14, 78, 85, 94, 20)
cor(xvals,yvals)
In the R output, the sample correlation is the number output by the script.
Hide the Excel Code
Copy and paste the following code into your Excel spreadsheet window, making sure your cursor is in A1
when you paste.
Copy and paste the following code into your Excel spreadsheet window, making sure the value x-vals
ends up in A1
after pasting.
How to calculate the expected value in Excel.
x-vals | y-vals | | correlation |
1 |
34 |
r: |
=CORREL(A:A,B:B) |
54 |
68 |
|
|
99 |
14 |
|
|
1 |
78 |
|
|
31 |
85 |
|
|
83 |
94 |
|
|
55 |
20 |
|
|
Make sure that you begin pasting in cell A1
.