keepcalculator

06 Numbers and Powers

Least Squares Regression Line: Formula and Example

Card No. 06·0113 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

To calculate the least squares regression line, use the paired data values to find the slope and intercept of the line that minimizes the sum of squared vertical differences between observed and predicted values. Enter the x-values and y-values below; the calculator returns the slope, intercept, and regression equation.

Formula

The least squares regression line predicts y from x using the form ŷ = a + bx. Here, x is the explanatory-variable value, y is an observed response, ŷ is the predicted response, b is the slope, and a is the y-intercept. The units of b are y-units per x-unit; a and ŷ use the same units as y.

For n paired observations, calculate the slope and intercept as follows:

  • b = [Σ(xᵢ − x̄)(yᵢ − ȳ)] / [Σ(xᵢ − x̄)²]
  • a = ȳ − bx̄

In these formulas, x̄ and ȳ are the respective means, Σ means to sum across all n pairs, and i identifies an individual pair. An equivalent slope formula is b = [Σxᵢyᵢ − (Σxᵢ)(Σyᵢ)/n] / [Σxᵢ² − (Σxᵢ)²/n]. The calculator uses this equivalent form. Its correlation output, r, summarizes the direction and strength of the linear association; it is not needed to calculate the line.

Worked Example

Using the default data, the paired observations are (1, 2), (2, 3), (3, 5), (4, 4), and (5, 6). There are five pairs. Their sums are Σx = 15, Σy = 20, Σx² = 55, Σy² = 90, and Σxy = 69. The means are x̄ = 3 and ȳ = 4.

  1. Calculate the slope: b = [69 − (15 × 20)/5] / [55 − 15²/5] = (69 − 60)/(55 − 45) = 9/10 = 0.9.
  2. Calculate the intercept: a = 4 − (0.9 × 3) = 1.3.
  3. Write the regression equation: ŷ = 1.3 + 0.9x.
  4. For the optional correlation output, r = (69 − 60) / √[(55 − 45)(90 − 20²/5)] = 9/√50 ≈ 1.2728? This value would exceed the possible correlation range, so check the calculation: Σy² for the data is 90, giving 90 − 400/5 = 10; the denominator is √(10 × 10) = 10. Thus r = 9/10 = 0.9000.

For instance, at x = 6 the line predicts ŷ = 1.3 + 0.9(6) = 6.7. This is a prediction from the fitted line, not a promise that a future observed y-value will equal 6.7.

What the Least Squares Line Minimizes

For each data point, the residual is the observed value minus the value predicted by the line: eᵢ = yᵢ − ŷᵢ. Least squares chooses a and b to make the sum of squared residuals, Σeᵢ², as small as possible. Squaring prevents positive and negative errors from canceling and gives larger errors more influence than smaller ones.

This is ordinary least squares regression of y on x. It minimizes vertical distances in y, not the shortest geometric distances from points to the line. Consequently, changing which variable is treated as x can produce a different regression line. The method assumes the relationship is being summarized as a straight line; it does not establish that x causes y.

When to Use and How to Read the Result

Use the line when paired numerical measurements show a reasonably linear pattern and you want a simple summary or a prediction within the observed x-range. The slope describes the average change in predicted y for a one-unit increase in x. A slope of 0.9 means predicted y rises by 0.9 y-units per x-unit. The intercept is the predicted y when x equals zero, though it may have little practical meaning if zero is outside the data’s relevant range.

Inspect a scatterplot before relying on the equation. A line can be misleading when the data curve, form separate clusters, or contain influential outliers. Also check residuals: a systematic curve or changing spread suggests the straight-line model may not describe the data well. The correlation coefficient r is between −1 and 1 when both variables vary. A value near 1 indicates a strong positive linear association, near −1 a strong negative one, and near 0 little linear association; zero correlation does not rule out a nonlinear relationship.

Common Mistakes and Limitations

  • Pairing values incorrectly: Each x must stay matched with its corresponding y. Sorting one list independently changes the data.
  • Reversing the variables: The line predicting y from x is generally not the same as the line predicting x from y.
  • Extrapolating: Predictions far beyond the observed x-values can be unreliable because the relationship may change outside the measured range.
  • Rounding too early: Keep full precision while calculating the slope and intercept, then round the final coefficients to a useful number of digits.
  • Ignoring unusual points: A single extreme x-value can strongly affect the fitted slope. Investigate such points rather than deleting them automatically.

The calculation also requires at least two paired observations and variation in x. If every x-value is identical, the slope denominator is zero, so there is no unique least squares slope. Missing or nonnumeric entries must be resolved before fitting the line. The equation describes the data’s fitted linear trend; it does not account automatically for measurement error, omitted variables, or a causal mechanism.

Frequently Asked Questions

How do you calculate a least squares regression line by hand?
Find the means of x and y, calculate b = Σ(xᵢ − x̄)(yᵢ − ȳ)/Σ(xᵢ − x̄)², then calculate a = ȳ − bx̄. Write the result as ŷ = a + bx.

What does least squares mean?
It means the chosen line minimizes the sum of squared vertical residuals between observed y-values and the y-values predicted by the line.

Does the regression line pass through the mean point?
Yes, when the usual least squares line includes an intercept, it passes through (x̄, ȳ), the point made from the two sample means.

Is the least squares line the same as the line of best fit?
Usually, “line of best fit” refers to the least squares regression line, but the phrase can be used informally for other fitting methods. Check how the line was calculated.

Can I use the line to predict beyond my data?
You can calculate an extrapolated value, but it may be unreliable. The fitted relationship is best interpreted within the range of observed x-values unless there is sound reason to expect it continues.

Related

Formula used
list x | X values | 1, 2, 3, 4, 5
list y | Y values | 2, 3, 5, 4, 6
out b | Slope | - | (sum(x*y) - sum(x)*sum(y)/count(x)) / (sum(x^2) - sum(x)^2/count(x)) | 4
out a | Y-intercept | - | mean(y) - b*mean(x) | 4
out r | Correlation coefficient | - | (sum(x*y) - sum(x)*sum(y)/count(x)) / sqrt((sum(x^2) - sum(x)^2/count(x))*(sum(y^2) - sum(y)^2/count(y))) | 4

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →