How to Use a Line of Best Fit Calculator (With Formula & Examples)

The line of best fit is one of the most useful tools in statistics — it turns a messy cloud of data points into a single equation you can use to make predictions. Whether you’re analyzing exam scores, tracking sales figures, or interpreting a science experiment, this calculator does the heavy arithmetic in under a second.
A line of best fit (least-squares regression line) is the straight line ŷ = mx + b that minimizes the sum of squared vertical distances from each data point to the line. The slope m and intercept b are computed from your x and y values using the least-squares method, and the resulting equation lets you predict y for any new x value.
A line of best fit is the straight line y = mx + b that minimizes squared vertical distances to all data points. Calculate slope m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) and intercept b = (Σy − mΣx) / n. Enter your data in the calculator below to get the equation, R², and a full step-by-step solution instantly.
Line of Best Fit Calculator
Least-squares regression — enter data, get y = mx + b instantly
Enter numbers separated by commas or new lines.
Step-by-step solution
How to use this calculator
- Enter your x values in the first box — separate them with commas or put each on a new line.
- Enter your y values in the second box in the exact same order as the x values.
- Click Calculate. The result card shows the equation ŷ = mx + b, the slope, intercept, R², and a numbered solution.
- Read the step-by-step solution to verify each calculation or copy the working for your assignment.
- Click Reset to clear everything and start a new calculation.
You need at least 2 data points. The calculator accepts up to hundreds of pairs — paste a full data set from a spreadsheet if you like.
What is a line of best fit?
A line of best fit is the single straight line that best represents the overall trend in a scatter plot. It is defined as the line that minimizes the sum of the squared vertical distances (called residuals) between each observed data point and the line itself — hence the formal name least-squares regression line.
The line takes the form ŷ = mx + b, where ŷ (y-hat) is the predicted value of y for a given x. It always passes through the point (x̄, ȳ) — the mean of all x values and the mean of all y values.
Quick Facts
| Property | Detail |
|---|---|
| Full name | Least-squares regression line |
| Equation form | ŷ = mx + b |
| Always passes through | (x̄, ȳ) — the means of x and y |
| Fit quality measure | R² (coefficient of determination) |
| Assumes | A linear relationship between x and y |
| Minimum data needed | 2 points (meaningful from ~5+) |
The formula explained
Slope: m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) Intercept: b = (Σy − mΣx) / n Equation: ŷ = mx + b R²: 1 − (SS_residual / SS_total)
where n = number of data pairs, Σ = “sum of all”
| Symbol | Meaning | Unit |
|---|---|---|
| n | Number of data points | dimensionless |
| Σx | Sum of all x values | same as x |
| Σy | Sum of all y values | same as y |
| Σxy | Sum of each x multiplied by its paired y | x·y units |
| Σx² | Sum of each x value squared | x² units |
| m | Slope — change in ŷ per unit increase in x | y/x units |
| b | Y-intercept — predicted y when x = 0 | same as y |
| R² | Coefficient of determination (0 to 1) | dimensionless |
Why squared distances? Squaring the residuals penalizes large errors more than small ones, and it makes the algebra tractable — there is a unique closed-form solution. Minimizing absolute distances instead leads to a harder optimization problem with no clean formula.
Worked examples
Example 1 — Study hours vs. exam score
A teacher records hours studied (x) and exam score out of 100 (y) for five students:
| x (hours) | y (score) | xy | x² |
|---|---|---|---|
| 1 | 50 | 50 | 1 |
| 2 | 60 | 120 | 4 |
| 3 | 65 | 195 | 9 |
| 4 | 75 | 300 | 16 |
| 5 | 80 | 400 | 25 |
| Σ = 15 | Σ = 330 | Σ = 1065 | Σ = 55 |
Step 1 — Slope: m = (5×1065 − 15×330) / (5×55 − 15²) = (5325 − 4950) / (275 − 225) = 375 / 50 = 7.5
Step 2 — Intercept: b = (330 − 7.5×15) / 5 = (330 − 112.5) / 5 = 217.5 / 5 = 43.5
Equation: ŷ = 7.5x + 43.5 — for each additional hour studied, the predicted score rises by 7.5 points.
Score
80 | * ← (5, 80)
75 | *
70 | / ← regression line ŷ = 7.5x + 43.5
65 | *
60 | *
50 |*
+--+--+--+--+--→ Hours
1 2 3 4 5
Example 2 — Temperature vs. ice cream sales
A vendor tracks daily high temperature °C (x) and units sold (y) over six days:
| x (°C) | y (units) | xy | x² |
|---|---|---|---|
| 20 | 40 | 800 | 400 |
| 22 | 46 | 1012 | 484 |
| 25 | 55 | 1375 | 625 |
| 28 | 59 | 1652 | 784 |
| 30 | 65 | 1950 | 900 |
| 32 | 70 | 2240 | 1024 |
| Σ = 157 | Σ = 335 | Σ = 9029 | Σ = 4217 |
Slope: m = (6×9029 − 157×335) / (6×4217 − 157²) = (54174 − 52595) / (25302 − 24649) = 1579 / 653 ≈ 2.418
Intercept: b = (335 − 2.418×157) / 6 = (335 − 379.6) / 6 ≈ −7.43
Equation: ŷ = 2.418x − 7.43 — each 1°C rise predicts about 2.4 more units sold. Enter these exact values into the calculator above to verify.
Regression line vs. other trend lines
| Type | Equation form | Best for | R² interpretation |
|---|---|---|---|
| Linear (this calculator) | y = mx + b | Straight-line trends | Proportion of variance explained |
| Polynomial | y = ax² + bx + c | Curved, single-peak data | Same — but can overfit |
| Exponential | y = ae^(bx) | Growth/decay data | Computed on log-transformed y |
| Logarithmic | y = a·ln(x) + b | Diminishing-returns data | Computed on log-transformed x |
Most tutorials tell you to check R² and stop there. But a high R² does not mean your regression line is valid — it only means the line fits the data you gave it. If your data contains an outlier, R² can look strong while the slope is completely distorted. In my experience teaching statistics, the single most useful habit is to plot the residuals (each observed y minus the predicted ŷ) against x. If the residuals fan out, curve, or cluster, the linear model is wrong regardless of what R² says. The calculator’s step-by-step output gives you every residual implicitly — subtract ŷ from each y and plot them before trusting the equation.
Common mistakes — and how to avoid them
Mistake 1: Swapping x and y. The regression of y on x is NOT the same as the regression of x on y. Always put the variable you want to predict as y and the explanatory variable as x. Swapping them gives a different slope.
Mistake 2: Extrapolating far beyond the data range. The equation ŷ = mx + b is only reliable within (or close to) the range of x values you used to build it. Predicting y for an x value far outside that range is unreliable — the linear relationship may not hold there.
Mistake 3: Confusing correlation with causation. A high R² tells you the line fits well. It does NOT tell you that x causes y. Two unrelated variables can have a strong regression line if they both trend over time.
Mistake 4: Using a linear model on curved data. If your scatter plot bends, a straight line will give a misleading equation. Always look at the plot before trusting the regression output.
In my 15 years of teaching statistics, the biggest source of errors I see is students plugging numbers into the formula without first drawing the scatter plot. The formula always produces an answer — even when the data is non-linear, contains outliers, or has the x and y columns reversed. Spend 30 seconds sketching the data first. If the points don’t roughly follow a straight line, the regression equation will mislead you no matter how carefully you compute it.
Browse the Complete Geometry Formula Sheet
Practice problems
Problem 1 — Find the regression line for: x = {2, 4, 6, 8}, y = {3, 7, 9, 13}
n = 4. Σx = 20, Σy = 32, Σxy = 2×3 + 4×7 + 6×9 + 8×13 = 6 + 28 + 54 + 104 = 192, Σx² = 4 + 16 + 36 + 64 = 120.
m = (4×192 − 20×32) / (4×120 − 20²) = (768 − 640) / (480 − 400) = 128 / 80 = 1.6
b = (32 − 1.6×20) / 4 = (32 − 32) / 4 = 0
Answer: ŷ = 1.6x (the line passes through the origin in this case).
Problem 2 — A data set has Σx = 30, Σy = 50, Σxy = 320, Σx² = 200, n = 5. Find m and b.
m = (5×320 − 30×50) / (5×200 − 30²) = (1600 − 1500) / (1000 − 900) = 100 / 100 = 1.0
b = (50 − 1.0×30) / 5 = 20 / 5 = 4.0
Answer: ŷ = 1.0x + 4
Quick quiz — test your understanding
1. The line of best fit always passes through which point?
2. An R² value of 0.95 means:
3. Which formula gives the slope of the least-squares regression line?
Frequently asked questions
What is a line of best fit?
A line of best fit (least-squares regression line) is the straight line y = mx + b that minimizes the total squared vertical distance between each data point and the line. It summarizes the linear trend in a scatter plot and lets you predict y values for new x values.
How is the slope of the line of best fit calculated?
The slope m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²). Here n is the number of data points, Σxy is the sum of each x times its paired y, Σx and Σy are the totals of all x and y values, and Σx² is the sum of squared x values. The calculator above computes this automatically and shows every intermediate sum.
What does R² mean in a regression?
R² (the coefficient of determination) measures how well the regression line fits the data. A value of 1.0 means the line passes through every point perfectly; 0 means the line explains none of the variation in y. An R² above 0.8 generally indicates a strong linear relationship, though what counts as “good” depends on the field.
Can I use this calculator for non-linear data?
No. This calculator fits a straight line and assumes a linear relationship. If your scatter plot shows a curve, a low R² will signal the poor fit. For curved data you need polynomial, exponential, or logarithmic regression — different models entirely. Always plot your data before choosing a model.
Does the line of best fit always pass through the origin?
No. The standard least-squares regression line passes through (x̄, ȳ) — the means of x and y — but not necessarily through the origin (0, 0). A regression forced through the origin is a special constrained model used only when theory requires the intercept to be zero, such as in certain physics or engineering contexts.
Sources & references
- Wikipedia — Simple linear regression
- Khan Academy — Describing relationships in quantitative data
- OpenStax Introductory Statistics — The Regression Equation
Editorial note: This article was written and fact-checked by Dr. Irfan Mansuri. All formulas are derived from standard least-squares regression theory. No statistics, quotes, or sources were fabricated.
