Skip to content
Statculator
Correlation & Regression

Line of Best Fit Calculator

Free line of best fit calculator. Enter x and y data to get the least-squares equation y = mx + b, slope, intercept, r², residuals and a graph, with steps.

Free, no sign-up Runs in your browserBy Hira Noor, BS MathematicsUpdated

Data points (x, y)
#xyRemove row
1
2
3
4
5
6
7
8
9
Tip: paste rows straight from a spreadsheet.

Each row is one point on the scatter plot. Paste two columns from a spreadsheet if you have many.

Optional. Reads a y value off the line for this x.

The line is found by least squares: it makes the sum of the squared vertical distances from the points to the line as small as possible.

Results

Line of best fit
y = -2.1236x + 28.1107
Slope, m
-2.1236
Predicted y at x = 5.5
16.431
Within the data range

Close fit: r = -0.9904

Downward trend. The line accounts for 98.09% of the variation in y (r² = 0.9809).

y-intercept, b
28.1107
Correlation, r
-0.9904
r²
0.9809
Number of points, n
9
Mean point (x̄, ȳ)
(4.2222, 19.1444)
Σx and Σy
38, 172.3
Σxy
626.5
Σx²
208
SSE, sum of squared residuals
4.1824
SST, total sum of squares
218.6422
SSR, explained sum of squares
214.4598
RMSE, √(SSE / n)
0.6817
Standard error of the estimate, √(SSE / (n − 2))
0.773

Interpretation: Each 1-unit increase in x lowers y by about 2.1236, on average. At x = 0 the line gives y = 28.1107, but x = 0 is outside your data (1 to 8), so treat the intercept as where the line crosses the axis rather than a reliable prediction.

Scatter plot of 10 points with fitted line10152025y12345678x(1, 26.5)(2, 23.4)(2, 24.8)(3, 21)(4, 19.6)(5, 16.4)(6, 15.9)(7, 12.8)(8, 11.9)(5.5, 16.431)y = -2.124x + 28.111
Highlighted point: the prediction at x = 5.5, which sits on the line.

Hand-calculation table

#xyxyx²y on the lineResidual (y − line)
1126.526.5125.98710.5129
2223.446.8423.8636-0.4636
3224.849.6423.86360.9364
432163921.74-0.74
5419.678.41619.6164-0.0164
6516.4822517.4928-1.0928
7615.995.43615.36920.5308
8712.889.64913.2456-0.4456
9811.995.26411.1220.778
Σ38172.3626.5208172.30

Residuals always add to zero for the least-squares line: points above it balance points below it.

Step-by-step solution

  1. 1

    Total the x, y, xy and x² columns

    The hand-calculation table below the results lists every product.

    n = 9
    Σx = 38, Σy = 172.3
    Σxy = 626.5, Σx² = 208
  2. 2

    Calculate the slope m

    m = [nΣxy − (Σx)(Σy)] / [nΣx² − (Σx)²]
    m = [9 × 626.5 − 38 × 172.3] / [9 × 208 − (38)²]
    m = -908.9 / 428 = -2.1236
  3. 3

    Calculate the y-intercept b

    b = (Σy − mΣx) / n
    b = (172.3 − (-2.1236)(38)) / 9 = 28.1107
  4. 4

    Write the equation

    The line passes through the mean point (x̄, ȳ) = (4.2222, 19.1444), which is a quick check on the arithmetic.

    y = -2.1236x + 28.1107
    Check: -2.1236 × 4.2222 + 28.1107 = 19.1444 = ȳ
  5. 5

    Measure how well the line fits with r

    r = [nΣxy − (Σx)(Σy)] / √{[nΣx² − (Σx)²][nΣy² − (Σy)²]}
    r = -908.9 / √(428 × 1967.78) = -0.9904
    r² = 0.9809
  6. 6

    Check the fit with SSE, SST and SSR

    SSE is what the line leaves unexplained; SST is the total variation in y. Their difference, SSR, is the part the line explains.

    SSE = Σ(y − line)² = 4.1824
    SST = Σ(y − ȳ)² = 218.6422, SSR = SST − SSE = 214.4598
    r² = SSR / SST = 0.9809, RMSE = √(SSE / n) = 0.6817
  7. 7

    Use the line to predict y at x = 5.5

    y = -2.1236 × 5.5 + 28.1107 = 16.431

What is a Line of Best Fit Calculator?

A line of best fit (also called a best-fit line, trend line or least-squares line) is the straight line y = mx + b that passes as close as possible to all the points on a scatter plot. This line of best fit calculator finds it by the least-squares method, which makes the sum of the squared vertical gaps between the points and the line as small as possible. Enter your (x, y) data to get the equation, the slope and y-intercept in plain words, r and r², the residuals, a graph of the line, and a prediction for any x.

What the Line of Best Fit Calculator does

Give the calculator a set of (x, y) points and it returns the equation of the line of best fit in slope-intercept form, the slope and y-intercept with a plain-language meaning, the correlation r and r² to show how tightly the points follow the line, and a scatter plot with the line drawn in, so it also works as a line of best fit graph maker or trendline calculator. The hand-calculation table lists x, y, xy and x² with their totals plus every fitted value and residual, and the results include SSE, SST, SSR and the RMSE, so you can reproduce and check the result with a basic calculator, as many algebra and statistics courses require.

This page keeps things simple: one equation, a fit check and a quick prediction. If you need standard errors, p-values, an ANOVA table or prediction intervals, use the Linear Regression Calculator, which fits the same line with full inference. To plot the points first, use the Scatter Plot Maker; for the strength of the relationship on its own, the Correlation Coefficient Calculator. For data that bend, you need a curve of best fit instead: try the Quadratic Regression Calculator or the Exponential Regression Calculator.

How to use the Line of Best Fit Calculator

  1. Type each point into Data points (x, y) or paste two columns from a spreadsheet. Use Add point for more rows.
  2. Optionally enter a value in Predict y at x = to read a y value off the line.
  3. Read the equation y = mx + b, then the plain-words meaning of the slope and intercept under the results.
  4. Follow the step-by-step solution to see the sums Σx, Σy, Σxy and Σx², how they give m and b, and how SSE and r² measure the fit.

How to calculate the line of best fit by hand

To find the line of best fit equation without software, work from a table of column totals. These are the same steps the calculator shows in its solution, using the used-car data from the worked example below.

  1. Make a table with columns x, y, xy and x², and total each column: n = 9, Σx = 38, Σy = 172.3, Σxy = 626.5, Σx² = 208.
  2. Compute the slope: m = [nΣxy − (Σx)(Σy)] / [nΣx² − (Σx)²] = −908.9 / 428 = −2.1236.
  3. Compute the y-intercept: b = (Σy − mΣx) / n = 28.1107.
  4. Write the line as y = mx + b: y = −2.1236x + 28.1107.
  5. Check it passes through the mean point (x̄, ȳ) = (4.2222, 19.1444), and that the residuals y − line add to zero.
  6. Judge the fit with r or r², or with SSE (the smaller the better for the same data). Here r = −0.9904 and r² = 0.9809.

Line of best fit formula

These are the standard least-squares formulas (the best fitting line formula) written with column totals, which is the easiest form to use by hand.

Slope
m = [ nΣxy − (Σx)(Σy) ] / [ nΣx² − (Σx)² ]
y-intercept
b = (Σy − mΣx) / n = ȳ − m·x̄
Equation of the line of best fit
y = mx + b
What least squares minimizes
SSE = Σ(y − (mx + b))²
Formula symbols
SymbolMeaning
nnumber of points
Σx, Σysum of the x values, sum of the y values
Σxysum of each x multiplied by its own y
Σx²sum of the squared x values
x̄, ȳmean of x and mean of y; the line always passes through (x̄, ȳ)
SSEsum of squared residuals, the quantity the line makes as small as possible

Statistics books often write the same line as ŷ = b₀ + b₁x, where b₁ is the slope (m here) and b₀ is the intercept (b here).

The formulas follow standard references: Penn State STAT 462, lesson 2.2: What is the “Best Fitting Line”?, the NIST/SEMATECH e-Handbook of Statistical Methods, §4.4.3.1: Least Squares and OpenStax Introductory Statistics 2e, §12.3: The Regression Equation.

Worked example

Worked example: age and price of used cars

Problem. Nine used cars of the same model have ages x (years) of 1, 2, 2, 3, 4, 5, 6, 7, 8 and prices y ($1,000s) of 26.5, 23.4, 24.8, 21.0, 19.6, 16.4, 15.9, 12.8, 11.9. Find the line of best fit and estimate the price of a 5½-year-old car.

  1. 01Column totals: n = 9, Σx = 38, Σy = 172.3, Σxy = 626.5, Σx² = 208.
  2. 02Slope: m = (9 × 626.5 − 38 × 172.3) / (9 × 208 − 38²) = (5638.5 − 6547.4) / (1872 − 1444) = −908.9 / 428 = −2.1236.
  3. 03Intercept: b = (172.3 − (−2.1236)(38)) / 9 = (172.3 + 80.6967) / 9 = 28.1107.
  4. 04Equation: y = −2.1236x + 28.1107. Check: at x̄ = 4.2222 the line gives 19.1444 = ȳ.
  5. 05Fit: r = −908.9 / √(428 × 1967.78) = −0.9904, so r² = 0.9809. SSE = 4.1824 out of SST = 218.6422, and the largest residual is −1.09 (the 5-year-old car, about $1,090 below the line).
  6. 06Prediction at x = 5.5: y = −2.1236 × 5.5 + 28.1107 = 16.4310.

Answer. Each extra year of age knocks about $2,124 off the price, and the line puts the price of a new car (x = 0) near $28,100. A 5½-year-old car is predicted to sell for about $16,400, and the line misses the actual prices by about $680 on average (RMSE).

SSE, SST and SSR: how well does the line fit?

SSE (sum of squared errors, or residual sum of squares) is Σ(y − line)², the variation the line leaves unexplained. Least squares picks the one line that makes SSE as small as possible. SST (total sum of squares) is Σ(y − ȳ)², the total variation of y around its mean. SSR (regression sum of squares) is the part the line explains, and for a least-squares line SST = SSR + SSE.

Dividing gives r² = SSR / SST = 1 − SSE / SST, the share of the variation in y the line accounts for. The RMSE = √(SSE / n) is the typical size of a residual in the units of y; regression software often reports the standard error of the estimate √(SSE / (n − 2)) instead, which is slightly larger.

QuantityFormulaUsed-car example
SSEΣ(y − ŷ)²4.1824
SSTΣ(y − ȳ)²218.6422
SSRSST − SSE214.4598
r²SSR / SST0.9809
RMSE√(SSE / n)0.6817 ($682)
Standard error of the estimate√(SSE / (n − 2))0.7730 ($773)

How to calculate b₀ and b₁ in linear regression

Statistics books write the same line as ŷ = b₀ + b₁x. The slope b₁ is m on this page and the intercept b₀ is b. In deviation form, b₁ = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² = Sxy / Sxx and b₀ = ȳ − b₁x̄. Both forms give identical numbers: for the car data, Sxy = −100.9889 and Sxx = 47.5556, so b₁ = −2.1236 and b₀ = 19.1444 − (−2.1236)(4.2222) = 28.1107.

The column-totals form is quicker with a basic calculator; the deviation form is less prone to rounding error when the x values are large. You can also get the slope from the correlation: b₁ = r × (s_y / s_x).

Drawing a best-fit line by eye vs least squares

A best fit straight line drawn by eye with a ruler is a reasonable sketch, but two people will draw two different lines. Least squares gives one definite answer: it is the only line that minimizes Σ(y − line)², and it always passes through the mean point (x̄, ȳ) with residuals that sum to zero.

A quick by-eye check is still useful: about half the points should sit above the least-squares line and half below, with no long run of points on one side. A run like that suggests the data curve and a straight line is the wrong shape.

Choosing the line of best fit on a multiple-choice question

Tests often show a scatter plot and ask which equation is the line of best fit. You can usually eliminate wrong options without computing anything:

  • Sign of the slope: points rising left to right need a positive m; falling points need a negative m.
  • Size of the slope: pick two points on the drawn trend, say x = 0 and x = 10, and check that the rise over run is about right.
  • Intercept: extend the trend to x = 0 and compare with b in each option.
  • Mean point: the correct line passes through (x̄, ȳ). If you can find the two means, plug them into each option.
  • Balance: roughly as many points should sit above the line as below it.

Line of best fit calculator vs linear regression calculator

Both find the same least-squares line. Use this page when you need the equation, a graph and the working for homework, a lab report or a quick forecast. Use the Linear Regression Calculator when you need to test whether the slope is significantly different from zero, report standard errors and p-values, or put confidence and prediction intervals around a predicted value.

Common use cases

  • Homework and exams: checking a hand-calculated slope and intercept, with every sum shown.
  • Science labs: fitting a straight line to measurements, such as extension against load, and reading off the gradient.
  • Quick forecasts: estimating a value between measured points, like the price of a car of a given age.
  • Presentations: adding a trend line to a scatter plot and stating the trend in one sentence.

Assumptions and limitations

  • Straight lines only: if the points curve, the best straight line can still be a poor description. Look at the plot and the residual column.
  • Interpolation is safer than extrapolation: predictions inside the range of your x values are far more trustworthy than predictions beyond it.
  • Outliers pull the line: because gaps are squared, one far-off point can tilt the line noticeably.
  • No uncertainty shown: this page gives the line itself, not confidence or prediction intervals. Use the Linear Regression Calculator for those.
  • y on x only: the line minimizes vertical gaps, so it predicts y from x. Regressing x on y gives a different line unless r = ±1.
  • Association, not cause: a clear trend line does not show that x causes y.

Frequently asked questions

How do you calculate the line of best fit?

Make a table with columns x, y, xy and x², and total each column. Compute the slope m = [nΣxy − (Σx)(Σy)] / [nΣx² − (Σx)²], then the intercept b = (Σy − mΣx) / n. Write the line as y = mx + b and check that it passes through the mean point (x̄, ȳ).

What is the equation for the line of best fit?

The line of best fit is written in slope-intercept form, y = mx + b, where m is the slope and b is the y-intercept found by least squares. In statistics notation the same line is ŷ = b₀ + b₁x.

What is SSE, SST and SSR?

SSE is the sum of squared residuals, Σ(y − ŷ)², the variation the line leaves unexplained. SST is the total sum of squares, Σ(y − ȳ)², the total variation in y. SSR is the explained part, SST − SSE. Their ratio SSR / SST is r², the share of variation the line explains.

How do you calculate b0 and b1 in linear regression?

The slope is b₁ = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)², and the intercept is b₀ = ȳ − b₁x̄. These are the same numbers as m and b from the column-totals formulas on this page.

Does the line of best fit have to go through the origin?

No. The least-squares line goes through the mean point (x̄, ȳ), not through (0, 0). It passes through the origin only if the intercept happens to be zero. Forcing it through the origin is a different model and usually fits worse unless theory says y must be 0 when x is 0.

Does the line of best fit have to pass through any of the data points?

No. The line balances all the points, so it may not touch any of them. What is guaranteed is that the positive and negative vertical gaps (residuals) add up to zero and the line passes through the point made of the two means.

Is a line of best fit the same as a regression line?

Yes, when it is found by least squares. The least-squares line of best fit and the simple linear regression line of y on x are the same line; regression output just adds statistics such as standard errors, p-values and intervals around it.

What is the difference between a line of best fit and a curve of best fit?

A line of best fit is straight, y = mx + b. A curve of best fit follows a bend in the data, such as a parabola y = ax² + bx + c or an exponential y = abˣ. If the residuals of the straight line form a curved pattern, fit a curve instead.

How do I get the line of best fit in Excel or Google Sheets?

Use =SLOPE(y_range, x_range) for m and =INTERCEPT(y_range, x_range) for b, or add a linear trendline to a scatter chart and tick “Display equation on chart”. All of these use least squares, so they match this calculator.

Cite this page

Statculator. (2026, November 14). Line of Best Fit Calculator. Retrieved November 14, 2026, from https://statculator.com/correlation-regression/line-of-best-fit-calculator/