Linear Regression Calculator
Comma or space separated. X and Y must have the same count.
A linear regression calculator finds the best-fit line y = mx + b through your data points, giving you the slope (m), intercept (b), correlation r, and R² in one step. Enter your X and Y values above, press Calculate, and you get the full regression equation plus a worked-out deviation table. Below you will find the exact formulas, several solved examples with real numbers, and a clear method for doing linear regression by hand.
What Is a Linear Regression Calculator?
It is a tool that fits a straight line to two sets of paired numbers and tells you the equation of that line. You give it X values (the input) and Y values (the output), and it returns y = mx + b, where m is the slope and b is the y-intercept.
The line it draws is the “line of best fit,” the single straight line that sits as close as possible to every data point at once. Statisticians call the underlying method least squares, because it minimizes the total of the squared gaps between each point and the line.
The Linear Regression Formula
Two formulas do all the work. First the slope, then the intercept.
Slope: m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
Intercept: b = ȳ − m · x̄
Here x̄ is the mean of the X values and ȳ is the mean of the Y values. Once you have m and b, the correlation coefficient r tells you how tightly the points hug the line:
Correlation: r = Σ(x − x̄)(y − ȳ) ÷ √( Σ(x − x̄)² · Σ(y − ȳ)² )
R² is simply r multiplied by itself. It tells you what fraction of the change in Y is explained by X. An R² of 0.90 means 90% of the variation in Y lines up with X.
How Do You Do Linear Regression by Hand?
Six steps get you from raw numbers to a finished equation. Take this small dataset:
X = 1, 2, 3, 4, 5 and Y = 50, 60, 65, 70, 75.
Step 1. Find the means.
x̄ = (1 + 2 + 3 + 4 + 5) ÷ 5 = 15 ÷ 5 = 3
ȳ = (50 + 60 + 65 + 70 + 75) ÷ 5 = 320 ÷ 5 = 64
Step 2. Build the deviation table. For each point, subtract the mean from x and from y, then multiply and square.
| x | y | x − x̄ | y − ȳ | (x−x̄)(y−ȳ) | (x−x̄)² |
|---|---|---|---|---|---|
| 1 | 50 | −2 | −14 | 28 | 4 |
| 2 | 60 | −1 | −4 | 4 | 1 |
| 3 | 65 | 0 | 1 | 0 | 0 |
| 4 | 70 | 1 | 6 | 6 | 1 |
| 5 | 75 | 2 | 11 | 22 | 4 |
| Sums Σ | 60 | 10 | |||
Step 3. Calculate the slope.
m = 60 ÷ 10 = 6
Step 4. Calculate the intercept.
b = 64 − (6 × 3) = 64 − 18 = 46
Step 5. Write the equation.
y = 6x + 46
Step 6. Check the fit. Running the correlation formula gives r = 0.986 and R² = 0.973, so about 97% of the change in Y is explained by X. That is a very strong straight-line relationship.
Worked Examples
Four quick examples covering a positive slope, a weak fit, a negative slope, and an uneven dataset. Every answer is calculated and verified.
Example 1: Strong positive trend
X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 8, 10.
Slope m = 2, intercept b = −0.2, so the equation is y = 2x − 0.2. With r = 0.99 and R² = 0.98, the points sit almost perfectly on the line.
Example 2: Weak fit
X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5.
Slope m = 0.6, intercept b = 2.2, giving y = 0.6x + 2.2. Here r = 0.775 and R² = 0.60, so only 60% of the variation is explained. The trend exists but the scatter is wide.
Example 3: Negative slope
X = 1, 2, 3, 4, 5 and Y = 10, 8, 7, 5, 3.
Slope m = −1.7, intercept b = 11.7, so y = −1.7x + 11.7. The negative r of −0.995 confirms a strong downward trend: as X rises, Y falls.
Example 4: Uneven data
X = 2, 4, 6, 8 and Y = 3, 7, 5, 10.
Slope m = 0.95, intercept b = 1.5, giving y = 0.95x + 1.5. With R² = 0.675, the line explains about 68% of the pattern, a moderate fit.
How to Read the Correlation (r) Value
The sign of r tells you direction. The size tells you strength. This table is the quick reference most students need:
| r value | Strength | What it means |
|---|---|---|
| ±0.90 to ±1.00 | Very strong | Points hug the line closely |
| ±0.70 to ±0.89 | Strong | Clear trend with some scatter |
| ±0.40 to ±0.69 | Moderate | Trend is visible but loose |
| ±0.10 to ±0.39 | Weak | Barely a pattern |
| 0.00 to ±0.09 | None | No linear relationship |
A positive r means the line slopes up. A negative r means it slopes down. An r near zero means a straight line is the wrong model for that data.
What Are b0 and b1 in Regression?
They are just other names for the same two numbers. In many statistics textbooks the regression line is written ŷ = b0 + b1x instead of y = mx + b.
b0 is the intercept (the same as b). b1 is the slope (the same as m). So in Example 1 above, b0 = −0.2 and b1 = 2. Different notation, identical math.
Simple vs Multiple Linear Regression
This calculator handles simple linear regression: one X predicting one Y. That covers most homework and coursework.
Multiple linear regression uses two or more predictors at once, for example predicting house price from size, age, and location together. The idea is the same, but the math needs matrix algebra rather than the two formulas above.
Quadratic Regression vs Linear Regression
Linear regression fits a straight line and works when Y changes at a steady rate. Quadratic regression fits a curve and works when the data bends or peaks.
The tell is in the shape. If a scatter of points rises then falls (or curves), a straight line will underfit it and a quadratic model with an x² term will match it far better. Fit a line first; if R² is low and the points clearly curve, switch to quadratic.
Common Mistakes to Avoid
- Forcing a line onto curved data. If the points bend, a straight line gives a poor, misleading equation. Check the scatter first.
- Reading slope as cause. A slope shows that X and Y move together, not that X causes Y. Correlation is not causation.
- Ignoring outliers. One extreme point can drag the whole line and inflate or crush your slope. Spot outliers before you trust the fit. An outlier calculator flags them fast.
- Confusing r and R². r ranges from −1 to +1 and shows direction. R² ranges from 0 to 1 and shows explained variation. They answer different questions.
How Do You Do Linear Regression in Excel?
Put your X values in one column and Y values in the next. Select both columns, insert a scatter chart, then right-click any point and choose “Add Trendline.” Tick “Linear,” then tick “Display Equation on chart.” Excel prints the same y = mx + b you get here. For the r value, use the formula =CORREL(X range, Y range) in any empty cell.