Linear Regression Calculator
Calculate the linear regression line (y = mx + b) for a dataset. Find slope, intercept, and predict Y for any X value.
Did this tool work for you?
How to use this calculator
The least-squares regression line minimises the sum of squared vertical distances from each data point to the line.
- 1
Enter five paired X and Y data points. Empty boxes are read as 0.
- 2
Enter an X value in "Predict Y" to get the expected Y from the regression line.
- 3
Check R² — values above 80% indicate a strong linear fit.
Frequently asked questions
Which regression does this fit, and with which formulas?
Ordinary least squares of Y on X with the closed-form solution: slope m = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) and intercept b = (Σy − mΣx) ÷ n, with n fixed at 5. The line minimises the sum of squared vertical gaps between each Y and the line. Regressing X on Y gives a different line, so keep the predictor in the X boxes.
How many points can I use, and what do empty boxes do?
Exactly five X–Y pairs. The form converts an empty box to 0 before calculating, so a blank is fitted as a real data point at 0, not ignored. If you only have four observations, you cannot leave one pair out, and you need a spreadsheet function such as SLOPE or LINEST for longer series.
When does the calculator refuse to fit a line?
When all five X values are identical. The denominator nΣx² − (Σx)² is then zero, a vertical line has no slope as a function of X, and the tool shows "All X values are identical". Identical Y values are allowed and give a flat line.
Why does R² show 100% when all my Y values are the same?
If every Y is equal, the total variation in Y is zero and the formula R² = 1 − SSres ÷ SStot would divide by zero. The code returns 1 in that case, so a flat line through constant data reads as a "perfect" 100% fit. Treat that as meaningless, since there was no variation to explain.
Can I trust a prediction outside the range of my X values?
Be wary. The line is only supported where you have data. With X from 1 to 5 and Y = 2, 4, 5, 4, 5, the line y = 0.6x + 2.2 predicts 5.8 at x = 6 and 8.2 at x = 10, but nothing in five points shows the relationship stays straight that far out. Predictions are unrounded results of m × x + b, not intervals.
What does the tool not tell me?
It reports no standard error of the slope, no confidence or prediction intervals, no p-value and no residuals. With five points the slope is very uncertain, and R² = 60% for the example data would not by itself establish a real trend. It also assumes a straight line, which only a plot can justify.
Does a high R² mean the straight line is the right model?
No. R² is the share of Y’s variation captured by the line; it cannot detect curvature, outliers or unequal spread. The classic Anscombe quartet has four very different datasets sharing nearly the same line and correlation. Check the pattern visually before using the equation.
Why do the equation and slope show different numbers of decimals?
The Slope and Intercept rows show four decimals, while the Equation row rounds to three, and a negative intercept is displayed with a minus sign, as in y = 4.000x − 6.000. The predicted Y uses the full-precision slope and intercept, so it can differ slightly from a hand calculation using the rounded figures.
Linear regression calculator for five X–Y points: slope, intercept and R²
The line that minimises squared error
Least squares picks the one straight line for which the sum of squared vertical distances to your points is as small as possible. The slope comes straight from the sums of the data, and the line is forced through the point formed by the mean of X and the mean of Y. The calculator returns the slope, the intercept, the equation, R², and a predicted Y for an X you choose.
| Quantity | Formula |
|---|---|
| Slope m | (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) |
| Intercept b | (Σy − m·Σx) ÷ n |
| R² | 1 − Σ(y − ŷ)² ÷ Σ(y − ȳ)² |
| Prediction | m × (chosen x) + b |
Worked example: a modest upward trend
Enter X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5. The tool gives a slope of 0.6, an intercept of 2.2, the equation y = 0.600x + 2.200, and R² = 60%. Predicting at x = 6 returns 5.8, and at x = 10 it returns 8.2. The second figure is far outside the data and rests entirely on the straight-line assumption.
Worked example: a declining series
Suppose a reading is taken at X = 0, 1, 2, 3, 4 and gives Y = 100, 90, 75, 70, 55. The fit has slope −11, intercept 100 and R² = 98.37%, so each step in X lowers Y by about 11 units. Asking for x = 5 gives a prediction of 45. A different set, X = 10, 20, 30, 40, 50 with Y = 15, 25, 38, 41, 60, gives slope 1.06, intercept 4, R² = 96.3% and a prediction of 41.1 at x = 35, an interpolation inside the data.
How one point can steer the line
With only five observations, a single unusual value carries heavy weight. For X = 1–5 and Y = 1, 2, 3, 4, 20, the slope jumps to 4 and the intercept to −6, and R² falls to 64% even though four of the five points lie on y = x. Without the last point the line would simply be y = x. Always look at where each point sits relative to the line before trusting the equation.
Choosing between correlation and regression
Use correlation to describe how closely two variables move together without naming a predictor. Use regression when you want to predict Y from a known X and need an actual equation. The R² here equals the square of the Pearson r from the correlation calculator for the same data. Neither establishes cause and effect, and neither reports uncertainty around the slope.
Learn more from an authoritative source:
WikipediaZ-Score Calculator
Calculate the z-score (standard score) and percentile rank for any value in a normal distribution. Convert raw scores to z-scores instantly.
T-Test Calculator
Perform a one-sample t-test to determine if a sample mean is significantly different from a known population mean. Returns t-statistic and p-value.
Chi-Square Test Calculator
Calculate the chi-square (χ²) statistic for goodness-of-fit or independence tests. Enter observed and expected frequencies to get χ² and p-value.
Confidence Interval Calculator
Calculate the confidence interval for a population mean from sample data. Supports 90%, 95%, and 99% confidence levels.
Sample Size Calculator
Calculate the minimum sample size required for a statistically valid survey or experiment. Specify confidence level, margin of error, and population size.
Percentile Calculator
Find the percentile rank of a score in a dataset, or find the value at a given percentile. Enter up to 10 data values.
Results are estimates for informational purposes only and do not constitute professional financial, medical, legal, or technical advice. Read full disclaimer →