The R-Value — Measuring Fit Quality
A model is only useful if the data actually has a relationship — the r-value tells you how strong it is.
The r-value ranges from -1 to 1. Values near ±1 mean strong relationships; near 0 means no useful pattern.
EXERCISE
1What Is the R-Value (Correlation Coefficient)?
The r-value is a single number between -1 and 1 that tells you how tightly your data follows a straight line.
Think of the r-value as a relationship report card. An r of 1.0 means x and y move in perfect lockstep. An r of -1.0 means they move in perfectly opposite directions. An r of 0 means there is no linear relationship at all — they are strangers.
The r-value scale:
| R-Value | Strength | Meaning |
|---|---|---|
| 1.0 | Perfect positive | As x goes up, y goes up exactly |
| 0.7 to 0.9 | Strong positive | Clear upward trend |
| 0.4 to 0.7 | Moderate positive | Noticeable but noisy |
| 0.0 to 0.4 | Weak/none | No useful linear pattern |
| -0.7 to -1.0 | Strong negative | Clear downward trend |
from scipy import stats
# Strong negative relationship (cars: older = slower)
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y)
print(f"R-value: {round(r, 4)}")
# Output: R-value: -0.7586
# Interpretation: moderately strong NEGATIVE relationship
Pro Tip: The sign tells you the direction (positive or negative). The magnitude tells you the strength. An r of -0.9 is a STRONGER relationship than r of +0.5, even though -0.9 is a negative number. Always look at the absolute value for strength.
EXERCISE
2Good Fit vs Bad Fit — Recognising When Regression Fails
A beautiful regression line through random noise is still useless — the r-value is your reality check.
Fitting a line through random data is like drawing a "trend" in coin flips — the line exists, but it predicts nothing. The r-value protects you from this trap.
Good fit — strong relationship:
from scipy import stats
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y)
print(f"R-value: {round(r, 4)}")
# Output: R-value: -0.7586
# |r| = 0.76 → usable for predictions
Bad fit — no relationship:
from scipy import stats
x = [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y = [21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]
slope, intercept, r, p, std_err = stats.linregress(x, y)
print(f"R-value: {round(r, 4)}")
# Output: R-value: 0.0130
# |r| = 0.013 → essentially ZERO relationship
# Linear regression is USELESS here
The decision rule:
| |r| Value | Verdict | Action | |-----------|---------|--------| | > 0.7 | Strong | Use the model confidently | | 0.4 – 0.7 | Moderate | Use with caution | | < 0.4 | Weak | Do NOT use |
Pro Tip: Always check r BEFORE making predictions. A model with r = 0.01 will still happily give you a prediction — it just has no reason to be right. The model does not know it is bad; it is your job to check.
EXERCISE
3R-Squared — The Percentage Version of R
R-squared tells you what percentage of the variation in y is explained by x — making it easier to communicate quality to non-technical people.
If r is a grade letter (A, B, C), r-squared is the percentage score (95%, 80%, 65%). Both measure the same thing, but r² is easier to explain to your boss.
The relationship:
R² = r × r
from scipy import stats
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y)
r_squared = r ** 2
print(f"R: {round(r, 4)}")
# Output: R: -0.7586
print(f"R²: {round(r_squared, 4)}")
# Output: R²: 0.5755
# 57.55% of the variation in speed is explained by age
Using sklearn''s r2_score:
import numpy
from sklearn.metrics import r2_score
x = numpy.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = numpy.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
model = numpy.poly1d(numpy.polyfit(x, y, 1))
print(f"R² (sklearn): {round(r2_score(y, model(x)), 4)}")
# Output: R² (sklearn): 0.5755
How to interpret R²:
| R² | Plain English |
|---|---|
| 0.95 | "Age explains 95% of speed variation" |
| 0.58 | "Age explains 58% — other factors matter too" |
| 0.01 | "Age explains almost nothing about speed" |
Pro Tip: R² always falls between 0 and 1 (unlike r which ranges from -1 to 1). This makes R² easier to compare across models: a model with R² = 0.85 beats one with R² = 0.60, regardless of the sign of the underlying relationship. In ML, R² is the standard way to report regression model quality.
