How to use the Linear Regression
- Enter the independent variable values as X and the dependent values as Y, keeping the pairs in order.
- Read the fitted equation in y = mx + b form.
- Enter a value in the prediction box to get the model's estimate of Y at that X.
- Check R² to judge how much of the variation the line accounts for.
- Use the residual standard error as a typical prediction miss, in the units of Y.
How the calculation works
Ordinary least squares picks the slope and intercept that minimise the sum of squared vertical distances from the points to the line. That criterion has a closed-form solution, so no iteration is needed, and under the standard assumptions it is the minimum-variance unbiased linear estimator. It also means the fitted line always passes through the point of means, which is a useful sanity check.
The slope is the interesting parameter: it is the average change in Y for a one-unit change in X. The intercept is the fitted value at X = 0, which is meaningful only when zero is inside or near the observed range. Extrapolating a house-price-versus-size line to zero square feet produces a number, and that number describes nothing.
Least squares squares the residuals, which makes it as sensitive to outliers as the mean is. A single mistyped observation can visibly tilt the line, and it will do so silently — R² may barely move. The residual standard error gives the typical size of a miss and belongs next to any prediction; a point forecast quoted without it implies a precision the model does not have.
slope m = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)²; intercept b = ȳ − m·x̄; R² = r²; SE = √(Σ residual² / (n − 2))Source: Legendre, Nouvelles méthodes pour la détermination des orbites des comètes (1805); Gauss, Theoria Motus (1809); NIST/SEMATECH e-Handbook, section 4.1.4.
Worked example
Eight weeks of study hours 1–8 against exam scores 52, 57, 64, 66, 74, 78, 83, 90.
- x̄ = 4.5, ȳ = 70.5.
- Σ(x − x̄)(y − ȳ) = 178.0 and Σ(x − x̄)² = 42.
- Slope = 178 ÷ 42 = 4.238 points per hour.
- Intercept = 70.5 − 4.238 × 4.5 = 51.43.
- Predicting at x = 10: 51.43 + 4.238 × 10 = 93.8.
y = 4.238x + 51.43 with R² near 0.99. Note that x = 10 sits outside the observed 1–8 range, so that prediction is an extrapolation.
Frequently asked questions
What is a good R²?+
It depends entirely on the field. Physics experiments routinely exceed 0.99; social-science models are often useful at 0.3.
Can I predict outside the range of my data?+
Arithmetically yes, honestly no. The linear relationship is only evidenced within the observed range; beyond it you are assuming, not measuring.
Which variable goes on X?+
The one you treat as the cause, the input, or the thing you can set. Swapping X and Y gives a different line, not the inverse of the same one.
What does the residual standard error mean?+
It is roughly the typical distance between an actual value and the line, in the units of Y — a practical measure of prediction accuracy.
Last reviewed September 1, 2026. We review this page whenever the underlying formula, tax year, published rate or standard changes.