Sub topic 10
Quantifying Relationships with Correlation and Regression
What this chapter assumes
You should know the mean, the standard deviation, and how to read a scatter plot. You should also know what a hypothesis test and a p-value are from the earlier chapters. This chapter covers the case where both the suspected cause X and the output Y are continuous, such as oven temperature and defect rate. Hypothesis tests like the t-test compare groups. Correlation and regression ask a different question: as X changes, how does Y change, and by how much?
Why measure a relationship
In earlier chapters you turned a suspected cause into a prediction. For a continuous X, the prediction is usually of this form: "if oven temperature is a real cause of defects, the defect rate should rise as temperature rises." Correlation tells you how tightly the two variables move together. Regression tells you how much Y changes per unit of X, with a margin of error. Both are evidence about the cause. Neither proves it. Causation is the subject of the next chapter.
Correlation
The Pearson correlation coefficient, written r, measures the strength of a straight-line relationship between two variables. It ranges from -1 to +1.
- r near +1: Y rises as X rises, and the points lie close to a line.
- r near -1: Y falls as X rises, and the points lie close to a line.
- r near 0: there is no straight-line relationship. A strong curved relationship can still exist.
Squaring r gives r², the share of the variation in Y that a straight line on X accounts for. An r of 0.7 means r² is about 0.49. About half of the variation in Y is unexplained by X, so other causes are still at work.
Spearman's rank correlation, rho, replaces each value with its rank before computing the correlation. It detects any consistently rising or falling relationship, not only a straight line. It is also much less affected by outliers. Use it when the scatter plot shows a curve that always goes one way, or when a few extreme points worry you.
Testing whether r differs from zero
The usual test has the null hypothesis that the true correlation is zero. A small p-value means the observed r would be unusual if X and Y were unrelated. The same two cautions from the p-value chapter apply:
- With a large sample, a tiny and practically useless r (say 0.1) can give a very small p-value.
- With a small sample, a large r can fail to reach significance.
Report r, the sample size, and the p-value together, and judge whether the size of the relationship matters for the process.
Simple linear regression
Simple regression fits a line through the data:
Y = b0 + b1 × X + error
- b0 is the intercept, the fitted Y when X is zero. It is often outside the range you observed and has no practical meaning.
- b1 is the slope, the average change in Y for a one-unit increase in X. This is the number that matters for root cause work.
- The error term is what the line does not explain. Its spread tells you how far a single unit can differ from the line.
The line is fitted by least squares, which picks the b0 and b1 that minimize the sum of squared vertical distances from the points to the line.
For verification, the slope is the most useful output. Its hypothesis test has a null of "true slope is zero." In simple regression this test gives the same p-value as the correlation test. The slope also has a confidence interval. An interval that excludes zero supports a relationship. Its width shows how precisely you know the effect. Its location shows whether the effect is large enough to matter. A slope of 0.001 percentage points per degree might be significant and still irrelevant.
Worked example
A line makes a sealed pack. You suspect that high sealing temperature increases the defect rate. You have twelve runs, listed in time order, with the temperature in °C and the defect rate in percent.
import numpy as np
from scipy import stats
# Sealing temperature (deg C) and defect rate (%) for 12 runs, in time order
temp = np.array([180, 182, 185, 187, 190, 192, 195, 197, 200, 202, 205, 207], dtype=float)
defect_pct = np.array([2.1, 2.4, 2.2, 2.9, 3.1, 3.0, 3.6, 3.4, 4.1, 3.9, 4.6, 4.4])
n = len(temp)
# Correlation: linear (Pearson) and rank-based (Spearman)
pearson = stats.pearsonr(temp, defect_pct)
spearman = stats.spearmanr(temp, defect_pct)
print(f"Pearson r = {pearson.statistic:.3f}, p = {pearson.pvalue:.5f}")
print(f"Spearman rho = {spearman.statistic:.3f}, p = {spearman.pvalue:.5f}")
# Simple linear regression
res = stats.linregress(temp, defect_pct)
t_crit = stats.t.ppf(0.975, df=n - 2)
ci_low = res.slope - t_crit * res.stderr
ci_high = res.slope + t_crit * res.stderr
print(f"slope = {res.slope:.4f} %/degC, 95% CI [{ci_low:.4f}, {ci_high:.4f}]")
print(f"intercept = {res.intercept:.2f}, R^2 = {res.rvalue**2:.3f}, slope p = {res.pvalue:.5f}")
# Residuals: observed minus fitted
fitted = res.intercept + res.slope * temp
resid = defect_pct - fitted
print("residuals:", np.round(resid, 2))
# Durbin-Watson statistic: values near 2 suggest residuals are not serially correlated
dw = np.sum(np.diff(resid) ** 2) / np.sum(resid ** 2)
print(f"Durbin-Watson = {dw:.2f}")Read the output in this order:
- Pearson r and Spearman rho. If they disagree strongly, look at the scatter plot for outliers or a curve.
- The slope and its 95% confidence interval. The interpretation is "each extra degree is associated with an average rise in the defect rate of about the slope, within the interval." Ask whether that size matters. Compare it with the range of temperatures the process really runs at.
- R². It tells you how much of the variation temperature accounts for. A real cause can have a modest R² if other causes also act.
- The residuals and the Durbin-Watson value, covered next.
Checking the regression assumptions
The p-value and confidence interval are only trustworthy if the model fits. Check these four points, preferably with a residuals-versus-fitted plot and a residuals-in-time-order plot.
| Assumption | What to look for | What a violation looks like |
|---|---|---|
| Linearity | Residuals scatter randomly around zero | A curved pattern, such as a U-shape |
| Constant spread | The vertical band of residuals has the same width everywhere | A funnel that widens as fitted values grow |
| Independence | Residuals in time order show no runs or drifts | Long streaks above or below zero |
| Roughly normal residuals | A histogram or normal probability plot without heavy tails | Strong skew or extreme outliers |
Independence matters most for process data. Measurements taken in sequence often drift together, because tool wear or ambient conditions change slowly. That makes neighboring points correlated. Positive autocorrelation makes the standard errors look smaller than they really are, so slopes appear more significant than they are. A Durbin-Watson value well below 2 is a warning sign.
Normality matters least with a moderate sample size. Linearity and independence matter most.
Common errors
- Extrapolating. The line describes the temperatures you observed. Predicting the defect rate at 230 °C when you only saw 180 to 207 °C assumes the pattern continues, with no evidence for it.
- Letting one point drive the result. A single outlier at an extreme X value (a high-leverage point) can create or hide a correlation. Plot the data and refit without the point to see how much the slope moves. Do not delete the point silently. Find out why it is unusual and record that.
- Ignoring strata. Pooling two machines, shifts, or suppliers can produce a correlation that does not exist within either group, or hide one that does. Plot each stratum in its own color before you fit a single line.
- Restricted range. If the process only ran within a narrow band of X, r will look weak even when the effect is real. Plan wider settings for a confirmatory trial.
- Fitting a line to a curve. If the residuals show a U-shape, a straight line is the wrong model. Consider a transformation or a quadratic term, and consult a statistician if the shape matters.
- Testing many pairs. If you correlate 40 candidate Xs with Y, about two will reach p < 0.05 by chance alone. Start from the hypothesis that the fishbone and data plan identified, not from a search through everything.
- Binary outputs. If Y is defective or good for each unit, ordinary regression is the wrong tool. Aggregate to a rate per run, as above, or use logistic regression. Logistic regression is not covered here.
- Reading correlation as cause. A strong r means X and Y move together. It does not show that changing X changes Y.
Connecting the result to the root cause
Return to your original hypothesis and prediction. For the sealing example, the prediction was a positive slope with a size that matters in practice. Compare the result with it:
- The sign matches the prediction, and the confidence interval excludes zero: the data support the hypothesis.
- The interval includes zero and is wide: the data are inconclusive. You may need more runs or a wider range of X.
- The interval includes zero and is narrow, so it rules out any slope of practical size: the data argue against this X as a meaningful cause.
- The sign is opposite to the prediction: the hypothesis was wrong, or a confounder is at work.
Note the result with its numbers: r, slope, interval, R², n, the range of X, and any points you excluded and why. The next chapter explains why even a strong, well-fitted relationship still leaves open the question of whether X causes Y.
Exercise
A regression of defect rate on sealing temperature gives a slope of 0.09 %/°C with a 95% confidence interval of [0.07, 0.11] and R² = 0.35, using runs between 180 and 207 °C. A colleague says the 35% means temperature causes 35% of defects, and proposes using the line to predict the defect rate at 230 °C. What is wrong with each claim?
Show answer
R² is the share of the variation in Y that a straight line on X accounts for in this data. It is not the share of defects caused by temperature. The data are observational, so a confounder could explain part of the pattern. Predicting at 230 °C is extrapolation beyond the observed range of 180 to 207 °C, and nothing in the data shows the line continues there. The slope itself is well determined, since the interval excludes zero and is narrow.
Exercise
You correlate 30 candidate process settings with the defect rate and find that two reach p < 0.05. Why should you not treat these two as confirmed causes, and what should you do before spending effort on them?
Show answer
With 30 tests at a 5% significance level, you expect about 1.5 false positives even if no setting matters, so two significant results are consistent with chance. Check whether each setting has a plausible mechanism and matches a hypothesis from the fishbone or 5 Whys. Then plot the data by stratum, look for confounders, and test the setting in a confirmatory trial or a fresh sample collected against a stated prediction.