Tutorials › Introduction to Statistics › Computing Correlation for a Small Dataset

Scatterplots and Correlation · Tutorial 232 of 996

Computing Correlation for a Small Dataset

Work through the standardization and arithmetic needed to calculate Pearson’s \(r\) correctly from paired data.

Beginner 9 min read

What You'll Learn

  • Find the sample mean and sample standard deviation for each variable.
  • Standardize every observation and pair its z-scores correctly.
  • Calculate Pearson’s \(r\) from the sum of paired z-score products.
  • Explain why the divisor is \(n-1\), not \(n\), when using sample standard deviations.
  • Check the arithmetic and report the result in context.

From Paired Data to a Correlation

Pearson’s correlation summarizes the direction and strength of a linear pattern between two quantitative variables. A scatterplot shows that pattern visually; calculating \(r\) gives a single numerical summary. The calculation uses both variables together: each \(x\)-value must stay paired with the \(y\)-value recorded for the same individual.

The key idea is to express each observation as a number of sample standard deviations above or below its variable’s mean. These standardized values are called z-scores. Multiply the two z-scores for each individual, then combine those paired products using the sample adjustment described below. If an individual is above the mean on both variables, the product is positive; if one value is above its mean and the other is below, the product is negative.

Definition: For \(n\) paired observations \((x_i,y_i)\), let \(\bar{x}\) and \(\bar{y}\) be the sample means, and let \(s_x\) and \(s_y\) be the sample standard deviations. The standardized values for pair \(i\) are \(z_{x,i}=(x_i-\bar{x})/s_x\) and \(z_{y,i}=(y_i-\bar{y})/s_y\). Provided both variables have nonzero sample standard deviations, Pearson’s sample correlation is
$$ r=\frac{1}{n-1}\sum_{i=1}^{n}z_{x,i}z_{y,i}. $$

This formula assumes that the data consist of correctly matched pairs and that both variables are quantitative and vary across the observations. It calculates a descriptive statistic for the data at hand; it does not require a probability model or a normality assumption. The value of \(r\) has no units, even if the original measurements have units, because each value is standardized by its own standard deviation.

Worked Calculation

In the first example, each row represents one hypothetical participant. The measurements might be minutes of activity (\(x\)) and a score (\(y\)). First calculate the means and sample standard deviations. Then standardize and multiply within rows. Keeping the rows intact is essential: multiplying values from different individuals would not calculate their correlation.

Worked Example: Four Paired Measurements

Suppose four hypothetical participants have the following paired measurements. Calculate Pearson’s \(r\).

Participant\(x\)\(y\)
A11552.5
B8547.5
C10549.5
D9550.5

1. Find the means. The \(x\)-values sum to \(400\), and the \(y\)-values sum to \(200\), so

$$ \bar{x}=\frac{400}{4}=100, \qquad \bar{y}=\frac{200}{4}=50. $$

2. Find the sample standard deviations. The deviations from the means are \(15,-15,5,-5\) for \(x\), and \(2.5,-2.5,-0.5,0.5\) for \(y\). Therefore,

$$ s_x=\sqrt{\frac{15^2+(-15)^2+5^2+(-5)^2}{4-1}} =\sqrt{\frac{500}{3}} \approx 12.9099, $$
$$ s_y=\sqrt{\frac{2.5^2+(-2.5)^2+(-0.5)^2+0.5^2}{4-1}} =\sqrt{\frac{13}{3}} \approx 2.0817. $$

Both standard deviations are positive, so standardization is possible.

3. Standardize each value and multiply within each pair. For participant C, for example, \(z_{x,C}=5/\sqrt{500/3}\approx0.3873\) and \(z_{y,C}=-0.5/\sqrt{13/3}\approx-0.2402\). The product is negative because C’s \(x\)-value is above its mean while the \(y\)-value is below its mean. The table shows the corresponding calculations for all four participants.

Participant\(x_i-\bar{x}\)\(z_{x,i}\)\(y_i-\bar{y}\)\(z_{y,i}\)\(z_{x,i}z_{y,i}\)
A151.16192.51.20101.3954
B-15-1.1619-2.5-1.20101.3954
C50.3873-0.5-0.2402-0.0930
D-5-0.38730.50.2402-0.0930

The z-scores are rounded for display. In particular, \(-0.5/\sqrt{13/3}\approx-0.240192\), which rounds to \(-0.2402\); the corresponding positive value rounds to \(0.2402\). Use unrounded values for the final calculation:

$$ \sum_{i=1}^{4}z_{x,i}z_{y,i} \approx 2.6047, \qquad r=\frac{2.6047}{4-1}\approx 0.8682. $$

Thus, these four hypothetical participants have a strong positive linear association between the recorded \(x\)- and \(y\)-measurements. The coefficient is dimensionless; it does not mean that one variable increases by a particular number of units when the other increases.

Why Divide by \(n-1\)?

The standard deviation in each z-score is the sample standard deviation, which uses \(n-1\) in its denominator. As a result, the sum of the paired z-score products must also be divided by \(n-1\) to obtain Pearson’s sample correlation. The following derivation makes the adjustment explicit.

Derivation: Write \(S_x=\sum_{i=1}^n(x_i-\bar{x})^2\), \(S_y=\sum_{i=1}^n(y_i-\bar{y})^2\), and \(T=\sum_{i=1}^n(x_i-\bar{x})(y_i-\bar{y})\). The sample standard deviations satisfy \(s_x=\sqrt{S_x/(n-1)}\) and \(s_y=\sqrt{S_y/(n-1)}\). Therefore,
$$ \sum_{i=1}^n z_{x,i}z_{y,i} =\frac{T}{s_xs_y} =\frac{T}{\sqrt{S_xS_y}/(n-1)} =(n-1)\frac{T}{\sqrt{S_xS_y}}. $$
The final fraction is Pearson’s correlation, so \(\sum z_{x,i}z_{y,i}=(n-1)r\), and hence \(r=\sum z_{x,i}z_{y,i}/(n-1)\).

A common informal description calls correlation an “average product of z-scores.” With sample standard deviations, the precise calculation is the sum of the \(n\) products divided by \(n-1\), not their ordinary arithmetic mean (which would divide by \(n\)). Equivalently, \(r\) is \(n/(n-1)\) times that ordinary mean. Applying the stated formula avoids a small but systematic error.

Two More Arithmetic Checks

Worked Example: A Perfect Negative Linear Pattern

Consider three hypothetical paired measurements \(x=(2,4,6)\) and \(y=(6,4,2)\). Each variable has mean \(4\), and both sample standard deviations are

$$ s_x=s_y=\sqrt{\frac{(-2)^2+0^2+2^2}{3-1}}=2. $$

The paired z-scores are \((-1,0,1)\) for \(x\) and \((1,0,-1)\) for \(y\). Their paired products are \(-1,0,-1\), so

$$ r=\frac{-1+0-1}{3-1}=-1. $$

The calculation matches the data: as \(x\) increases, \(y\) decreases in an exactly straight-line pattern. Both variables vary, so the correlation is defined.

Worked Example: A Positive but Imperfect Pattern

For four hypothetical paired measurements \(x=(2,4,5,7)\) and \(y=(4,3,6,5)\), both means are \(4.5\). The deviations are \((-2.5,-0.5,0.5,2.5)\) for \(x\) and \((-0.5,-1.5,1.5,0.5)\) for \(y\). The standard deviations are

$$ s_x=\sqrt{\frac{13}{3}}\approx2.0817, \qquad s_y=\sqrt{\frac{5}{3}}\approx1.2910. $$

The products of the paired deviations are \(1.25, 0.75, 0.75,\) and \(1.25\), with sum \(4\). Since \(z_{x,i}z_{y,i}\) equals the paired deviation product divided by \(s_xs_y\), their sum is

$$ \sum_{i=1}^4 z_{x,i}z_{y,i} =\frac{4}{\sqrt{13/3}\sqrt{5/3}} =\frac{12}{\sqrt{65}} \approx1.4884. $$

Thus,

$$ r=\frac{1.4884}{4-1}\approx0.4961. $$

The positive value indicates a positive linear association in these data, but the value is below \(1\), so the pairs do not lie exactly on an increasing straight line.

Common Mistakes and Practical Checks

The most frequent computational error is dividing the sum of products by \(n\) after using sample standard deviations. That produces the ordinary average of the products, not Pearson’s sample correlation. In the first example, that incorrect step would give approximately \(2.6047/4=0.6512\), rather than the correct \(2.6047/3=0.8682\).

A second mistake is to standardize the two columns correctly but then pair their z-scores in a different order. Each product must come from the same observational unit. A third is to round each z-score too early: small rounding errors accumulate, especially with a small dataset. Keep extra digits during the calculation and round the reported correlation at the end.

Before reporting a result, check that each row is a genuine pair, both variables have variation, and the sample-size adjustment is \(n-1\). Also compare the sign of the result with the scatterplot: a positive \(r\) should reflect an overall upward linear pattern, and a negative \(r\) an overall downward linear pattern. A numerical calculation does not replace looking at the plotted data.

1
Check the pairs.
Confirm that each \(x_i\) and \(y_i\) belong to the same individual or observational unit.
2
Compute means and sample standard deviations.
Use all \(n\) observations for each mean and divide the sum of squared deviations by \(n-1\) when finding each sample variance.
3
Standardize and multiply by row.
Find \(z_{x,i}\) and \(z_{y,i}\), then calculate \(z_{x,i}z_{y,i}\) for every pair.
4
Sum and adjust.
Add the paired products and divide by \(n-1\). Keep unrounded values until the final result.
Key takeaway: Using sample standard deviations, Pearson’s correlation is the sum of the paired products of z-scores divided by \(n-1\). Preserving the pairings and using this adjustment are central to getting the calculation right.

Check Your Understanding

Try these questions before moving on.

  1. For five paired observations, what divisor is used when the sum of the paired z-score products is converted to Pearson’s \(r\)?
  2. Suppose one observation has an \(x\)-value above \(\bar{x}\) and a \(y\)-value below \(\bar{y}\). What is the sign of its paired z-score product?
  3. Why should the \(x\)- and \(y\)-values from different individuals never be mixed when making the products?
  4. A calculation gives a sum of paired z-score products of \(1.8\) for \(n=4\). Find \(r\).
  5. What happens to the calculation of \(r\) if all the observed \(x\)-values are identical? Explain.