Sub topic 14
Continuous X and Continuous Y: Correlation and Regression
This chapter covers the last cell of the test selection map: both the input (X) and the output (Y) are continuous. Examples are oven temperature against film thickness, or machine speed against cycle time. It assumes you can already do the following:
- Classify variables as continuous or discrete.
- Write a null and alternative hypothesis.
- Read a p-value against a significance level.
- Read a scatter plot.
Why this case is different
Earlier chapters compared groups: two lines, three shifts, before and after. When X is continuous there are no groups to compare. Cutting X into "low" and "high" bins throws away information and creates arbitrary boundaries. Instead, you ask whether Y changes in a consistent way as X changes, and by how much.
Two tools answer this:
- Correlation measures the strength and direction of a relationship.
- Simple regression fits a line, so you can state the size of the effect and predict Y.
Correlation
The Pearson correlation coefficient, r, ranges from -1 to +1. The sign gives the direction. The magnitude says how closely the points follow a straight line. A value near 0 means there is no linear pattern. It does not mean there is no relationship, because a U-shaped pattern can give r near 0.
The hypotheses are:
- H0: the population correlation (rho) is 0.
- Ha: rho is not 0.
The p-value answers one question: if there were truly no linear relationship, how often would a sample of this size show an r this far from 0? With 30 points, an r of about 0.36 or larger in magnitude is needed for p < 0.05. With 1,000 points, an r of about 0.06 is enough. This is why you must report r, not only p.
Pearson's r assumes a roughly linear relationship and no extreme outliers. Spearman's rank correlation is the non-parametric alternative. It correlates the ranks of the values, so it handles monotonic curves and outliers better. It is the counterpart of the rank-based tests in the previous chapter.
Simple regression
Simple regression fits the model Y = b0 + b1·X + error. The slope b1 is the estimated change in Y for a one-unit increase in X. This is usually the number the project team cares about, because it has units, such as "0.8 microns per degree".
The hypothesis test on the slope is:
- H0: the true slope is 0.
- Ha: the true slope is not 0.
For simple regression with one X, this test gives the same p-value as the Pearson test. R² equals r squared. It is the share of the variation in Y that the line accounts for. An R² of 0.25 means X explains a quarter of the variation, and three quarters comes from other sources. In Analyze, that is useful: it tells you whether X is a major driver or a minor one.
A worked example
The script simulates 40 batches. X is the curing temperature and Y is the film thickness. It computes both correlations, fits the line, builds a confidence interval for the slope, and checks the residuals.
import numpy as np
from scipy import stats
rng = np.random.default_rng(11)
temp = rng.uniform(150, 190, size=40) # X, degrees C
thickness = 20 + 0.08 * temp + rng.normal(0, 1.0, 40) # Y, microns
# Correlation
r, p_r = stats.pearsonr(temp, thickness)
rho, p_rho = stats.spearmanr(temp, thickness)
print(f"Pearson r = {r:.3f}, p = {p_r:.4g}")
print(f"Spearman rho = {rho:.3f}, p = {p_rho:.4g}")
# Simple regression
fit = stats.linregress(temp, thickness)
print(f"slope = {fit.slope:.4f}, intercept = {fit.intercept:.3f}")
print(f"R-squared = {fit.rvalue**2:.3f}, slope p = {fit.pvalue:.4g}")
# 95% confidence interval for the slope
df = len(temp) - 2
t_crit = stats.t.ppf(0.975, df)
low = fit.slope - t_crit * fit.stderr
high = fit.slope + t_crit * fit.stderr
print(f"95% CI for slope: ({low:.4f}, {high:.4f})")
# Residual checks
fitted = fit.intercept + fit.slope * temp
resid = thickness - fitted
print("Shapiro-Wilk on residuals p =", stats.shapiro(resid).pvalue)
print("Spread vs fitted (Spearman of |resid|, fitted) p =",
stats.spearmanr(fitted, np.abs(resid)).pvalue)Read the output in this order:
- Look at r or R² for the strength of the relationship.
- Look at the slope and its confidence interval for the size of the effect.
- Look at the p-value for whether the slope is distinguishable from 0.
- Look at the residual checks to decide whether you can trust the intervals and p-values.
Checking the residuals
A residual is the observed Y minus the value the line predicts. The regression p-values and intervals depend on the residuals, not on the raw X and Y. The assumptions are:
- Linearity. Residuals plotted against fitted values show no curve.
- Independence. Observations are not serially related. Data collected over time can violate this, so plot the residuals in time order.
- Constant variance. The spread of residuals is similar across the range of X. A funnel shape breaks this.
- Approximate normality. This matters most for small samples. Use a normal probability plot and the Shapiro-Wilk test, as in the normality chapter.
Statistical tests of these assumptions are weak for small samples and oversensitive for large ones. Plots are the primary check, and tests are a supplement.
Common errors
- Reading correlation as causation. Analyze data are usually observational. A strong r makes X a candidate cause. A designed experiment in Improve is what confirms it.
- Extrapolating. The line describes only the range of X you observed. A model fitted on 150 to 190 degrees says nothing reliable about 220 degrees.
- Letting one point drive the result. A single high-leverage point at an extreme X can create or hide a relationship. Plot the data, then refit without the point and compare. Do not delete it without a documented reason.
- Ignoring lurking variables. Two variables can move together because a third variable drives both, such as ambient humidity or shift.
- Equating a small p-value with a strong relationship. With n = 2,000, an r of 0.07 gives p well below 0.05 yet explains under 1% of the variation.
Practical significance
Translate the slope into the process. If the slope is 0.08 microns per degree and the specification allows 1.5 microns of variation, a 10-degree swing in temperature moves thickness by 0.8 microns, more than half the tolerance. That is a practical finding. A statistically significant slope of 0.001 microns per degree is not, however small the p-value. Report the slope, its confidence interval, R², and the plain-language consequence for the process.
Exercise
A team measures 25 batches and finds Pearson r = 0.30 between mixing time and viscosity, with p = 0.14. A colleague says "mixing time has no effect on viscosity." What is wrong with that conclusion, and what would you do next?
Show answer
A p-value of 0.14 means the data do not provide enough evidence to reject a slope of 0 at alpha = 0.05. It does not show that the slope is 0. With n = 25, the test can only detect fairly large correlations, and r = 0.30 would be of practical interest if real. Next, plot Y against X to check for non-linearity or outliers, compute the slope with its confidence interval to see whether practically important effects are still plausible, and use a power calculation to find the sample size needed to detect the smallest effect that matters. Then collect more data if the effect could matter.