The correlation coefficient · Statistics & Data Science… | StemPy Lab
Course 5.1·Lesson 2 of 3
The correlation coefficient
The correlation coefficient r puts a number on a straight-line pattern. It is always between −1 and 1: the sign gives the direction and the size gives the strength. This lesson works out r by hand for five points, explains why the formula works, and checks it with Python.
about 30 min1 simulation1 practice question1 Python program6 questions to check yourself
This lesson is free to read. Enroll free to keep your place and take the tests.
Lesson 2 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
work out r from the deviations, or from the z-scores, of a small data set;
explain why points at the upper right and lower left make r positive;
read the sign and the size of r, and know that r has no unit.
Five pots of beans
A made-up science club grows beans in five pots. Each pot gets a different amount of fertiliser, and two weeks later the club measures how much each plant has grown.
Pot
Fertiliser x (g)
Growth y (cm)
1
1
4
2
2
6
3
3
5
4
4
9
5
5
11
The scatter plot rises to the right. How strong is the pattern? Words like "fairly strong" are not precise. We want a number.
Above and below the means
The means are xˉ=3g and yˉ=7cm. Draw a vertical line at xˉ and a horizontal line at yˉ. They cut the plot into four parts.
Now look at each pot. Find its deviationsx−xˉ and y−yˉ, and multiply them.
Pot
x−xˉ (g)
y−yˉ (cm)
Product
1
−2
−3
6
2
−1
−1
1
3
0
−2
0
4
1
2
2
5
2
4
8
A pot below both means (lower left) gives minus times minus, a positive product. A pot above both means (upper right) also gives a positive product. A pot at the upper left or the lower right gives a negative product. So the sum of the products is large and positive when most points agree with a rising line.
The sums have names: Sxy=6+1+0+2+8=17. In the same way, Sxx=∑(x−xˉ)2=10 and Syy=∑(y−yˉ)2=34.
The correlation coefficient
The sum Sxy still depends on the units: grams and centimetres. To remove them, divide by the matching sizes of the spreads:
r=SxxSyySxy
For the beans, r=10×3417=34017=0.922.
There is a second way to see the same number. Turn each value into a z-score, its distance from the mean in standard deviations. Use the sample standard deviations, which divide by n−1: here sx=1.581g and sy=2.915cm. Then
r=n−11∑zxzy
This is the average product of the z-scores, with n−1 in place of n. If you use the population standard deviations (dividing by n), divide the sum by n instead. Both routes give the same r, as long as you keep to one of them.
What the number says
r is always between −1 and 1.
The sign gives the direction: positive for a rising pattern, negative for a falling one.
The size gives the strength of the straight-line pattern. r=1 or r=−1 means every point lies exactly on a line. r near 0 means no straight-line pattern.
A rough guide, used in many books: above 0.8 in size is strong, 0.5 to 0.8 is moderate, and below 0.5 is weak. It is only a guide.
r has no unit, because the units cancel. It stays the same if you measure in kilograms or pounds, in degrees Celsius or Fahrenheit.
For the typing survey of the last lesson, r=0.980: a strong positive pattern. For the eight weather stations, r=−0.991: a strong negative one. The Maths course on Correlation and Regression works with the same r in the style of exam papers.
Your turn: new numbers
Try it in Python
The program works out a z-score for every value, multiplies the pairs, and divides the sum by n−1. The last line asks the statistics module for the same number.
The usual mistakes
Dividing by n with sample standard deviations. Keep to n−1 everywhere, or to n everywhere.
Giving r a unit.r=0.92, not 0.92 cm per gram.
Reading r=0.9 as "90% of the points lie on the line".r is not a percentage of anything.
Calling a negative r weak.r=−0.95 is a strong pattern. The sign is the direction only.
Remember these
r=SxxSyySxy, or the sum of zxzy divided by n−1 (sample standard deviations).
−1≤r≤1. The sign is the direction and the size is the strength.
r has no unit and does not change when either variable is measured in a new unit.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Which is stronger, r=0.6 or r=−0.85?
Three points have deviations (x−xˉ,y−yˉ) of (−1,−2), (0,1) and (1,1). Find Sxy, Sxx, Syy and r.
A point sits at the upper left of the two mean lines. Is its product (x−xˉ)(y−yˉ) positive or negative?
The heights of some made-up plants are measured in centimetres, and r=0.74 with their age in days. The heights are written again in millimetres. What is r now?
Why can r never be bigger than 1?
References
OpenStax, Introductory Statistics, Chapter 12 (Linear Regression and Correlation), the section on the correlation coefficient r.
Enroll free in Scatter Plots and Correlation
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.