DEV Community

Cover image for Linear Regression by Least Squares: Fitting the Best Straight Line to Data
NovaSolver
NovaSolver

Posted on • Originally published at novasolver.jp

Linear Regression by Least Squares: Fitting the Best Straight Line to Data

A lab notebook records the weight a spring carries and how far it stretches. The numbers never fall on a perfect line — the ruler wobbles, the spring warms up, the reading is rounded. Yet there is clearly a trend, and you want a single equation that captures it well enough to predict the stretch at a load you never tested. That is the everyday job of linear regression.

This article explains what "best fit" actually means, derives the least-squares slope and intercept, works a full numerical example by hand, and points out the mistakes that quietly corrupt regression results.

Why this calculation matters

Linear regression is the workhorse of applied data analysis. It calibrates sensors, estimates material constants from test data, projects demand from past sales, and gives the baseline that every more elaborate model is measured against. When an engineer says "the response is roughly linear over this range," a regression line is the object behind that sentence.

It matters because eyeballing a line through a scatter plot is unreliable and unrepeatable. Two people will draw two different lines, and neither can defend the choice. Least squares replaces judgment with a definite rule: out of every possible straight line, pick the one that makes the total squared vertical error as small as possible. That rule produces one answer, the same answer every time, and it comes with diagnostics — the correlation coefficient and the residuals — that tell you whether a straight line was a sensible choice at all.

The core method

A straight-line model has two unknowns, a slope and an intercept:

y = a + b*x
Enter fullscreen mode Exit fullscreen mode

For each data point, the residual is the gap between the observed y and the value the line predicts: residual = y - (a + b*x). Least squares chooses a and b to minimise the sum of those residuals squared. Squaring matters: it makes every error positive so they cannot cancel, and it penalises large misses far more than small ones.

Setting the derivatives of that sum to zero gives two clean formulas. The slope is the cross-product of the deviations divided by the spread in x:

b = sum[(x - x_mean)(y - y_mean)] / sum[(x - x_mean)^2]
Enter fullscreen mode Exit fullscreen mode

The intercept then follows from the fact that the best-fit line always passes through the centroid of the data, the point (x_mean, y_mean):

a = y_mean - b*x_mean
Enter fullscreen mode Exit fullscreen mode

Two quantities help you judge the fit afterward. The correlation coefficient r runs from -1 to +1 and measures how tightly the points hug a line. The coefficient of determination, R squared, is the fraction of the variation in y that the line explains. An R squared of 0.95 means the line accounts for 95 percent of the up-and-down movement in the data; the rest is scatter the model does not capture.

A worked example

Take four points: (1, 2), (2, 5), (3, 7), and (4, 10). The goal is the least-squares line y = a + b*x.

Step 1 — find the means.

x_mean = (1 + 2 + 3 + 4) / 4 = 2.5
y_mean = (2 + 5 + 7 + 10) / 4 = 6.0
Enter fullscreen mode Exit fullscreen mode

Step 2 — form the deviations and the cross-product sum. Subtract each mean, then multiply the paired deviations:

(1-2.5)(2-6) = (-1.5)(-4) = 6.0
(2-2.5)(5-6) = (-0.5)(-1) = 0.5
(3-2.5)(7-6) = ( 0.5)( 1) = 0.5
(4-2.5)(10-6)= ( 1.5)( 4) = 6.0

sum[(x - x_mean)(y - y_mean)] = 6 + 0.5 + 0.5 + 6 = 13
Enter fullscreen mode Exit fullscreen mode

Step 3 — sum the squared x-deviations.

(-1.5)^2 + (-0.5)^2 + (0.5)^2 + (1.5)^2
= 2.25 + 0.25 + 0.25 + 2.25 = 5.0
Enter fullscreen mode Exit fullscreen mode

Step 4 — compute the slope.

b = 13 / 5 = 2.6
Enter fullscreen mode Exit fullscreen mode

Step 5 — compute the intercept.

a = y_mean - b*x_mean = 6.0 - 2.6*2.5 = 6.0 - 6.5 = -0.5
Enter fullscreen mode Exit fullscreen mode

The best-fit line is:

y = -0.5 + 2.6x
Enter fullscreen mode Exit fullscreen mode

Read it back: each unit increase in x raises y by 2.6, and the line crosses the y-axis at -0.5. Check the centroid — at x = 2.5, the line gives -0.5 + 2.6*2.5 = 6.0, exactly y_mean, as it must. The fit is excellent here; the points sit very close to the line.

Common mistakes

Reversing x and y. Regression of y on x is not the same as regression of x on y. Least squares minimises the error in one chosen direction — vertical for y on x. Swap the roles and you get a different slope. Decide which variable you are predicting before you compute anything.

Reading slope as causation. A strong fit shows that two quantities move together, not that one drives the other. Ice cream sales and drowning rates correlate beautifully, and neither causes the other — summer does. Regression quantifies association; causation needs an experiment or a mechanism.

Letting an outlier set the line. Because errors are squared, one stray point far from the trend can pull the whole line toward itself. Always look at the scatter plot and the residuals; a single bad reading deserves investigation, not silent inclusion.

Extrapolating past the data. A line fitted between x = 1 and x = 4 says nothing reliable about x = 40. The linear relationship is an observation within the tested range, not a law that holds forever.

Trusting the slope while ignoring R squared. A line can always be computed, even through a shapeless cloud. A low R squared is the model telling you a straight line is the wrong shape — perhaps the trend is curved, or there is simply no relationship.

Try the interactive NovaSolver calculator

Working one fit by hand fixes the idea; building intuition for how a single point shifts the line is faster with a live tool. The Linear Regression Simulator on NovaSolver lets you click anywhere on the chart to add data points and watch the least-squares line, slope b, intercept a, correlation r, R squared, and RMSE update in real time. You can load sample datasets, toggle the residual segments and the 95 percent band, and drop in an outlier to see exactly how far it drags the fit.

Related calculators

Browse the full set in the math and statistics tools hub.

Closing note

Least-squares linear regression is small enough to do on paper and powerful enough to anchor most of applied statistics. Find the means, form the cross-products, divide, and you have a slope; the intercept comes free from the centroid. The harder discipline is afterward: look at the residuals, check the correlation, resist the pull of outliers, and never extrapolate beyond the data you actually have. Get the line and read its diagnostics honestly, and a scatter of measurements becomes a model you can defend.

Top comments (0)