Correlation and Linear Regression, Explained

After reading this you can measure how strongly two variables move together, fit the straight line that best predicts one from the other, and tell an honest signal from a misleading one.

What correlation and regression tell you

Correlation answers one question: as one variable rises, does the other tend to rise, fall, or drift with no pattern? The Pearson correlation coefficient r puts a number on that tendency, from -1 (a perfect downhill line) through 0 (no linear link) to +1 (a perfect uphill line).

Linear regression goes one step further. It draws the single straight line that comes closest to your points and gives you an equation, \hat{y} = a + bx, so you can predict y from x. Correlation describes the strength of the association. Regression gives you a formula to use.

Here is the hook. Suppose you record eight days of study hours and test scores and get r = 0.997. That single number says the points sit almost exactly on a line. The regression then tells you the line rises by about 4.4 score points per study hour. Both facts come from the same eight pairs.

When to use it, and when not to

Use Pearson r and the least-squares line when three things hold: the relationship looks roughly straight on a scatter plot, both variables are numeric, and no single point sits far from the crowd. If any of those fail, the numbers still compute, but they mislead.

A curved relationship is the classic trap. Take y = x^2 for x from -3 to 3. The link is perfect and deterministic, yet Pearson r = 0 because the uphill and downhill halves cancel. Correlation only sees the straight-line part.

When the data are ranks or the relationship is monotone but bent, reach for Spearman \rho instead. Spearman is just Pearson r computed on the ranks of the data, so it measures whether y consistently increases with x without demanding a straight line.

Correlation is not causation. Ice cream sales and drowning deaths both climb in summer, giving a high r, but neither causes the other. Temperature drives both. A strong r tells you two things move together, not why.

The formulas and the intuition

Pearson correlation is the average product of the two variables measured in standard-deviation units:

r = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2}\,\sqrt{\sum (y_i - \bar{y})^2}}

Here \bar{x} and \bar{y} are the means, and each sum runs over all n pairs. When a point is above average on both x and y, the product (x_i - \bar{x})(y_i - \bar{y}) is positive and pushes r up. When a point is high on one and low on the other, the product is negative and pulls r down. The denominator scales the result into the range from -1 to +1.

The regression slope reuses the same top sum, divided by the spread of x alone:

b = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}, \qquad a = \bar{y} - b\bar{x}

The slope b is the predicted change in y for a one-unit rise in x. The intercept a forces the line through the point (\bar{x}, \bar{y}). The line found this way minimizes the sum of squared vertical gaps between the points and the line, which is why it is called least squares.

Finally, R^2 is simply r^2 in simple regression. It is the fraction of the variance in y that the line explains. An R^2 of 0.99 means the line accounts for 99 percent of the up-and-down variation in y.

A worked example with the demo data

Fitting a line to eight points

Use the demo values: x = 1, 2, 3, 4, 5, 6, 7, 8 and y = 52, 55, 61, 64, 70, 72, 79, 83.

  1. Means: \bar{x} = 4.5 and \bar{y} = 67.
  2. Sum of squares of x deviations: \sum (x_i - \bar{x})^2 = 42 (the values 12.25, 6.25, 2.25, 0.25 and their mirror image).
  3. Cross-product sum: \sum (x_i - \bar{x})(y_i - \bar{y}) = 185.
  4. Slope: b = 185 / 42 = 4.405.
  5. Intercept: a = 67 - 4.405 \times 4.5 = 47.18.
  6. Correlation: with \sum (y_i - \bar{y})^2 = 817.5, r = 185 / \sqrt{42 \times 817.5} = 0.9975.
  7. Explained variance: R^2 = 0.9975^2 = 0.9950.

So the fitted line is \hat{y} = 47.18 + 4.405x. Predict day 5: 47.18 + 4.405 \times 5 = 69.2, close to the observed 70. The R^2 of 0.995 confirms almost all the variation follows the line.

The eight points hug the line, matching the R squared of 0.995.

Reading and interpreting the results

Start with the scatter plot, always. A number cannot tell you whether the shape is straight, curved, or clumped. Then read the coefficients.

Pearson r
Strength and direction of the linear link. Rough guide: 0.1 weak, 0.3 moderate, 0.5 or more strong, but always judge against your field.
Spearman rho
The same idea on ranks. If \rho is much higher than r, the relationship is monotone but bent.
Slope b
Expected change in y per unit of x, in the original units. Here 4.405 score points per study hour.
Intercept a
Predicted y when x = 0. Meaningful only if x = 0 is realistic.
p-value
The probability of seeing a correlation this large if the true correlation were zero. Small (below 0.05) suggests the link is unlikely to be pure chance.

Watch the interplay between r and sample size in the p-value. With only n = 5 points, an r of 0.7 gives a p-value near 0.19, not significant. With n = 30, that same r = 0.7 yields a p-value below 0.0001. More data makes the same correlation far more convincing.

Without JavaScript, picture this: the eight demo points give r = 0.997. Move the last point from (8, 83) down to (8, 40) and the correlation collapses to about 0.55 while the slope flips from 4.4 toward roughly -0.5. One point can rewrite the whole fit.

Common mistakes

Extrapolating past the data. The demo line fits days 1 to 8. Using it to predict day 20 gives 47.18 + 4.405 \times 20 = 135, a score above any real test. Nothing guarantees the line stays straight outside the observed range.

Trusting r without the plot. Anscombe's quartet is four datasets with the same r = 0.816 and the same fitted line, yet one is a clean line, one a curve, and one is dominated by a single outlier. Identical numbers, very different truths.

Confusing R squared with accuracy. A high R^2 means the line explains variance in your sample, not that predictions are correct on new data. Check residuals and, when you can, test on held-out points.

Fitting a line to a curve. If the residuals show a smile or frown shape, a straight line is the wrong model. Try the Polynomial Regression Calculator for a degree-2 or higher fit.

A fixed correlation of 0.7 becomes far more significant as n grows.

Related tools

Once you have predictions, judge their error with the Regression Metrics Calculator, which reports MSE, RMSE, MAE, MAPE and R^2 from actual versus predicted values. For classification problems where the target is a category rather than a number, correlation does not apply; use the Confusion Matrix Metrics Calculator instead.

Frequently asked questions

What is a good correlation coefficient?

It depends on the field. In physics you might expect r above 0.99. In social science, 0.3 can be meaningful. Judge r against typical results for your kind of data, not against a universal cutoff.

What is the difference between Pearson and Spearman?

Pearson measures straight-line association on the raw values. Spearman measures monotone association on the ranks. For the demo data, both are near 1 because the points rise almost linearly. For a bent but always-increasing relationship, Spearman stays high while Pearson drops.

Does a high R squared mean my model is correct?

No. R^2 reports how much sample variance the line explains, not whether the model form is right or whether it generalizes. A curved dataset can still yield a moderate R^2 from a straight line while being systematically wrong.

Can correlation be negative?

Yes. A value like r = -0.8 means y falls as x rises, and the link is strong. The sign is direction, the magnitude is strength.

How many data points do I need?

You can compute r from just three pairs, but small samples give unstable results and large p-values. As the chart above shows, an r of 0.7 is not significant at n = 5 but is highly significant by n = 30. Aim for at least 20 to 30 pairs when you want to trust the p-value.