STAT-151 · Lesson 10 of 12 · 24 min

Correlation and simple linear regression

Much of science is about relationships between quantitative variables: how enzyme activity depends on temperature, how tree height relates to diameter, how instrument response varies with concentration. Correlation measures the strength of a linear association between two variables. Regression goes further, describing how one variable changes with another by fitting a line that can be used for prediction. This lesson covers both, with an emphasis on calibration curves, which every laboratory scientist uses, and on the checks that keep regression honest.

Scatterplots first

Every analysis of two quantitative variables should begin with a scatterplot, with the explanatory variable (the one that might explain or predict) on the horizontal axis and the response on the vertical axis. Look for the direction of the association (positive or negative), its form (linear or curved), its strength (how tightly the points cluster), and any unusual points. In 1973 the statistician Francis Anscombe published four small data sets with identical means, variances, correlations and regression lines, but completely different scatterplots: one linear, one curved, one with an outlier, and one in which a single point created the whole relationship. Summary statistics alone can hide everything that matters.

Four scatterplots of Anscombe’s quartet, each with the same fitted line y = 3 + 0.5x. The first shows a linear scatter, the second a smooth curve, the third a tight line with one outlier, and the fourth a vertical column of points plus one far point that determines the slope.
Anscombe’s quartet: four data sets with the same correlation (0.82) and regression line

The correlation coefficient

The Pearson correlation coefficient r measures the strength and direction of a linear relationship. It is the average product of the standardized values of x and y:

\[r = \frac{1}{n-1}\sum \Bigl(\frac{x_i - \bar{x}}{s_x}\Bigr)\Bigl(\frac{y_i - \bar{y}}{s_y}\Bigr)\]

r lies between −1 and +1. Values near ±1 indicate points close to a straight line; values near 0 indicate no linear relationship. r has no units and does not change if either variable is rescaled, so it is the same whether temperature is in degrees Celsius or Fahrenheit. It is symmetric: the correlation of x with y equals that of y with x.

Three warnings apply. First, r measures only *linear* association: a perfect U-shaped relationship can have r = 0. Second, r is sensitive to outliers, as Anscombe’s third and fourth data sets show. Third, and most important, correlation does not imply causation. Two variables may be correlated because one causes the other, because both are caused by a third (a confounder), or by coincidence. Ice cream sales and drownings are correlated because both rise in hot weather.

The test of H₀: ρ = 0, where ρ is the population correlation, uses t = r√(n − 2)/√(1 − r²) with n − 2 degrees of freedom. When data are skewed or contain outliers, the Spearman rank correlation, which is Pearson’s r applied to the ranks, measures any monotonic association and is more robust.

Pearson and Spearman correlations for spruce diameter and height
import numpy as np
from scipy import stats
rng = np.random.default_rng(1522)

# Invented: diameter at breast height (cm) and height (m) for 25 white spruce
dbh = np.round(rng.uniform(10, 50, 25), 1)
height = np.round(4 + 0.55*dbh - 0.004*dbh**2 + rng.normal(0, 1.5, 25), 1)

pr = stats.pearsonr(dbh, height)
sr = stats.spearmanr(dbh, height)
print(f"Pearson r = {pr.statistic:.3f}, p = {pr.pvalue:.2g}")
ci = pr.confidence_interval(0.95)
print(f"95% CI for rho: {ci.low:.3f} to {ci.high:.3f}")
print(f"Spearman rho = {sr.statistic:.3f}")
Output
Pearson r = 0.868, p = 1.9e-08
95% CI for rho: 0.720 to 0.941
Spearman rho = 0.814

The least-squares regression line

A regression line ŷ = b₀ + b₁x describes how the mean response changes with x. The least-squares line is the one that minimizes the sum of squared vertical distances from the points to the line. These vertical distances, observed minus predicted, are the residuals. The slope and intercept have simple formulas:

\[b_1 = r\,\frac{s_y}{s_x}, \qquad b_0 = \bar{y} - b_1\bar{x}\]

The slope b₁ is the estimated change in the mean of y for a one-unit increase in x, in the units of y per unit of x. The intercept b₀ is the predicted value at x = 0, which may be meaningless if x = 0 lies outside the data. The line always passes through (x̄, ȳ). Unlike correlation, regression is not symmetric: regressing y on x gives a different line from regressing x on y, because each minimizes errors in a different direction.

The coefficient of determination, R², equals r² in simple regression. It is the proportion of the variation in y explained by the linear relationship with x. An R² of 0.81 means that 81 per cent of the variance in y is accounted for by x, and 19 per cent remains in the residuals.

Inference for the slope

The regression model assumes that, for each x, the responses are normally distributed around a mean β₀ + β₁x with constant standard deviation σ, and that observations are independent.

\[y_i = \beta_0 + \beta_1 x_i + \varepsilon_i, \qquad \varepsilon_i \sim N(0, \sigma^2)\]

σ is estimated by the residual standard error s = √(Σ residuals²/(n − 2)). The standard error of the slope is s/(s_x√(n − 1)), and the t statistic b₁/SE(b₁) with n − 2 degrees of freedom tests H₀: β₁ = 0, no linear relationship. A confidence interval for the slope, b₁ ± t* SE(b₁), is usually more useful than the test, because it shows how steep the relationship might be.

Calibration curves

Instrumental analysis in chemistry, biochemistry and environmental science relies on calibration: measuring the instrument response for standards of known concentration, fitting a regression line, and then using the line in reverse to estimate the concentration of unknown samples from their responses. This inverse prediction has its own uncertainty, which depends on the scatter about the line, the number of standards, the number of replicate readings of the unknown, and how far the unknown lies from the centre of the standards.

A calibration curve and inverse prediction of an unknown concentration
import numpy as np
from scipy import stats

# Invented calibration: absorbance at 540 nm for nitrite standards (mg/L)
conc = np.array([0.0, 0.0, 0.2, 0.2, 0.4, 0.4, 0.6, 0.6, 0.8, 0.8, 1.0, 1.0])
absb = np.array([0.004, 0.009, 0.181, 0.173, 0.352, 0.361, 0.538, 0.527,
                 0.712, 0.704, 0.889, 0.871])
fit = stats.linregress(conc, absb)
n = conc.size
resid = absb - (fit.intercept + fit.slope*conc)
s = np.sqrt(np.sum(resid**2)/(n - 2))
tstar = stats.t.ppf(0.975, n - 2)
print(f"slope = {fit.slope:.4f} AU per mg/L (SE {fit.stderr:.4f}), intercept = {fit.intercept:.4f}")
print(f"R^2 = {fit.rvalue**2:.5f}, residual SE = {s:.4f} AU")
print(f"95% CI for slope: {fit.slope - tstar*fit.stderr:.4f} to {fit.slope + tstar*fit.stderr:.4f}")

# Inverse prediction for an unknown read in triplicate
y0 = np.array([0.455, 0.462, 0.449]); m = y0.size
x0 = (y0.mean() - fit.intercept)/fit.slope
sxx = np.sum((conc - conc.mean())**2)
se_x0 = (s/fit.slope)*np.sqrt(1/m + 1/n + (y0.mean() - absb.mean())**2/(fit.slope**2*sxx))
print(f"unknown: {x0:.3f} mg/L, 95% CI {x0 - tstar*se_x0:.3f} to {x0 + tstar*se_x0:.3f} mg/L")
Output
slope = 0.8766 AU per mg/L (SE 0.0052), intercept = 0.0051
R^2 = 0.99964, residual SE = 0.0062 AU
95% CI for slope: 0.8650 to 0.8883
unknown: 0.514 mg/L, 95% CI 0.503 to 0.524 mg/L

A very high R², such as 0.999, is typical of a good calibration, but R² alone is not proof of linearity: a slightly curved response can still give R² above 0.99. Analysts should inspect the residuals, which should scatter randomly around zero, and should not extrapolate beyond the highest standard, where detector saturation often causes curvature. The limit of detection, the smallest concentration that can be reliably distinguished from zero, is commonly estimated as about three times the standard deviation of blank responses divided by the slope.

Confidence and prediction intervals

Regression supports two different kinds of interval at a given x. A confidence interval for the mean response estimates where the average y lies for all units with that x. A prediction interval estimates where a single new observation will fall. The prediction interval is always wider, because it includes the scatter of individual points around the line as well as the uncertainty in the line itself. Both are narrowest at x̄ and widen towards the ends of the data.

\[\hat{y} \pm t^{*} s\sqrt{\frac{1}{n} + \frac{(x_0-\bar{x})^2}{S_{xx}}} \ \text{(mean)}, \qquad \hat{y} \pm t^{*} s\sqrt{1 + \frac{1}{n} + \frac{(x_0-\bar{x})^2}{S_{xx}}} \ \text{(new value)}\]
A scatterplot with a fitted regression line, a narrow shaded confidence band for the mean response that is narrowest near the centre of the data, and a wider dashed prediction band that contains most of the points.
Confidence band for the mean response (shaded) and prediction band for new observations (dashed)

Residual plots and model checking

The assumptions of linearity, constant variance, normality and independence are checked mainly through the residuals. A residual plot graphs residuals against fitted values or against x. A random, even band around zero supports the model. A curved pattern means the relationship is not linear. A funnel shape, with residuals spreading out as fitted values increase, means the variance is not constant, a common pattern for concentrations and counts. A normal Q–Q plot of the residuals checks normality, which matters mainly for small samples and for prediction intervals. Independence is a matter of design, but a plot of residuals in time order can reveal drift in an instrument.

Transformations

Many biological and physical relationships are multiplicative rather than additive, and become linear after taking logarithms. Body mass and metabolic rate across species follow a power law, B = aM^b, which becomes log B = log a + b log M; the slope b of the log–log regression estimates the scaling exponent, famously close to 3/4 across many groups of organisms. Exponential growth or decay, such as bacterial counts or radioactive activity, becomes linear on a log scale of the response. Transformations can also stabilize variance and make residuals more normal.

A log–log regression recovers the metabolic scaling exponent (invented data)
import numpy as np
from scipy import stats
rng = np.random.default_rng(1523)

# Invented: body mass (kg) and basal metabolic rate (W) for 20 mammal species
mass = np.round(10**rng.uniform(-1.5, 3, 20), 3)
bmr = np.round(3.4*mass**0.75 * np.exp(rng.normal(0, 0.15, 20)), 2)

raw = stats.linregress(mass, bmr)
log = stats.linregress(np.log10(mass), np.log10(bmr))
print(f"raw scale:     R^2 = {raw.rvalue**2:.3f}")
print(f"log-log scale: slope = {log.slope:.3f} (SE {log.stderr:.3f}), R^2 = {log.rvalue**2:.3f}")
print(f"estimated a = {10**log.intercept:.2f} W per kg^b")
Output
raw scale:     R^2 = 0.984
log-log scale: slope = 0.776 (SE 0.010), R^2 = 0.997
estimated a = 3.05 W per kg^b

The raw-scale R² looks impressive, but it is misleading: with masses spanning more than four orders of magnitude, the few largest species dominate the fit, and the residuals fan out as mass increases. On the log–log scale the points are evenly spread, the residuals have constant variance, and the slope of about 0.78 is close to the exponent of 0.75 used to generate the data. The standard error of 0.010 is probably optimistic for real species, because related species are not independent observations, a problem that comparative biologists address with phylogenetic methods.

Regression pitfalls

Extrapolation, predicting outside the range of x in the data, is risky because the relationship may change. Influential points, especially those with extreme x values, can pull the line towards themselves; it is good practice to fit the model with and without them and report both if the conclusions differ. Regression to the mean occurs whenever two measurements are imperfectly correlated: subjects selected for extreme first measurements will, on average, be less extreme the second time, even with no intervention. Francis Galton, who coined the term regression, observed it in the heights of parents and their adult children. It is a frequent source of spurious treatment effects in studies that enrol subjects because of extreme baseline values without a control group. Finally, a regression on observational data describes association, not the effect of changing x, unless confounders are accounted for, a task that lesson 11 begins.

Common mistakes

  • Mistaker = 0 means there is no relationship between the variables.

    Insteadr = 0 means no linear relationship; a strong curved relationship can have r near zero.

  • MistakeA high correlation shows that x causes y.

    InsteadCorrelation can arise from confounding or coincidence; causal claims need experimental design or careful causal analysis.

  • MistakeR² = 0.99 proves a calibration curve is linear.

    InsteadSlight curvature can still give a very high R²; check the residual plot.

  • MistakeUse a confidence interval for the mean to predict a single new observation.

    InsteadUse the wider prediction interval for an individual value.

  • MistakePredict y for x values well beyond the data.

    InsteadExtrapolation assumes the relationship continues unchanged, which often fails.

  • MistakeInterpret improvement in patients selected for extreme baseline values as a treatment effect.

    InsteadRegression to the mean alone produces such improvement; a control group is needed.

Summary and key terms

  • Always plot the data first; summary statistics can hide curvature, outliers and influential points.
  • Pearson’s r measures linear association between −1 and +1; Spearman’s ρ measures monotonic association and is more robust.
  • The least-squares line minimizes squared residuals; its slope is the change in mean y per unit x.
  • R² is the proportion of variation in y explained by the regression.
  • Inference for the slope uses t with n − 2 degrees of freedom and assumes linearity, constant variance, normal errors and independence.
  • Calibration curves are used in reverse to estimate unknown concentrations, with uncertainty from inverse prediction.
  • Prediction intervals for new observations are wider than confidence intervals for the mean; residual plots and transformations help ensure the model fits.
explanatory variable
The variable used to explain or predict changes in the response; plotted on the horizontal axis.
response variable
The outcome variable whose changes are being studied.
Pearson correlation
A measure of the strength and direction of a linear relationship.
Spearman correlation
A rank-based measure of monotonic association.
least squares
The method that chooses the line minimizing the sum of squared residuals.
residual
The difference between an observed response and the value predicted by the model.
coefficient of determination
R², the proportion of variance in the response explained by the model.
calibration curve
A regression of instrument response on known standard concentrations, used to estimate unknowns.
prediction interval
An interval likely to contain a single new observation at a given x.
regression to the mean
The tendency of extreme measurements to be followed by less extreme ones when correlation is imperfect.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. If r = 0.6, s_x = 2 and s_y = 5, find the slope of the regression of y on x.

    Show answer

    b₁ = 0.6 × 5/2 = 1.5.

  2. If x̄ = 10, ȳ = 30 and b₁ = 1.5, find the intercept.

    Show answer

    b₀ = 30 − 15 = 15.

  3. What proportion of variation is explained when r = −0.8?

    Show answer

    R² = 0.64, or 64 per cent.

  4. Test H₀: ρ = 0 for r = 0.45 with n = 20.

    Show answer

    t = 0.45√18/√(1 − 0.2025) ≈ 2.14, df = 18, p ≈ 0.046.

  5. A calibration line is absorbance = 0.012 + 0.875 × concentration. Estimate the concentration for absorbance 0.450.

    Show answer

    (0.450 − 0.012)/0.875 ≈ 0.50 mg/L.

  6. Why is a prediction interval wider than a confidence interval for the mean?

    Show answer

    It adds the variation of individual observations around the line.

  7. A residual plot shows a funnel shape. What does this indicate, and what might help?

    Show answer

    Non-constant variance; a log transformation of the response or weighted regression may help.

  8. A log–log regression of metabolic rate on mass has slope 0.72. Interpret it.

    Show answer

    Metabolic rate scales roughly as mass to the power 0.72; doubling mass multiplies rate by about 2^0.72 ≈ 1.65.

  9. Students with the lowest scores on a first test improve on a second test with no teaching. Why?

    Show answer

    Regression to the mean: part of their low first score was bad luck that is unlikely to recur.

  10. Give a reason not to extrapolate a calibration curve above the highest standard.

    Show answer

    The detector may saturate, making the response non-linear beyond the calibrated range.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz