ThisCalculator

Everyday

Linear Regression Calculator with Steps

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

Linear Regression Calculator with Steps

A linear regression calculator with steps finds the line of best fit (y = a + bx) for paired data and shows the full working: sums, slope, intercept, and the final equation. You enter two columns of numbers (x and y values), and the calculator returns the slope, intercept, and often the correlation coefficient and R². Below is a working version that computes the key values, followed by a step-by-step explanation of the formulas.

Formula

The least-squares regression line is written as:

y = a + b·x

where:

  • b (slope) = [ Σ(xy) − (Σx·Σy)/n ] / [ Σ(x²) − (Σx)²/n ]
  • a (intercept) = ȳ − b·x̄
  • n = number of data pairs
  • x̄ = mean of x values, ȳ = mean of y values
  • Σ means “sum of�

The slope measures how much y changes for each one-unit increase in x. The intercept is the predicted y when x = 0. Both are in the same units as your data: if x is in hours and y is in dollars, then b is in dollars per hour and a is in dollars.

Worked Example

Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.

  1. Count: n = 5
  2. Sum x: 1+2+3+4+5 = 15, so Σx = 15
  3. Sum y: 2+4+5+4+5 = 20, so Σy = 20
  4. Sum xy: (1×2)+(2×4)+(3×5)+(4×4)+(5×5) = 2+8+15+16+25 = 56
  5. Sum x²: 1+4+9+16+25 = 55
  6. Sum y²: 4+16+25+16+25 = 86
  7. Slope b = (56 − (15×20)/5) / (55 − (15²)/5) = (56 − 60) / (55 − 45) = (−4)/10 = −0.4
  8. Intercept a = (20/5) − (−0.4 × 15/5) = 4 − (−0.4 × 3) = 4 + 1.2 = 5.2

So the regression line is y = 5.2 − 0.4x. The calculator shows slope = −0.4 and intercept = 5.2, matching this hand calculation.

How the Least-Squares Method Works

The “line of best fit� is the line that minimizes the sum of the squared vertical distances between each data point and the line. Those vertical distances are called residuals (actual y minus predicted y). Squaring them prevents positive and negative errors from cancelling out and penalizes large errors more heavily. The formulas for b and a are derived by taking the partial derivatives of the sum of squared residuals with respect to a and b, setting them to zero, and solving. This is why the method is called ordinary least squares (OLS). No other straight line will produce a smaller total squared error for the same data.

Reading the Results: Slope, Intercept, R²

The slope tells you the direction and strength of the linear relationship per unit of x. A slope of −0.4 means that for each additional unit of x, y decreases by 0.4 on average. The intercept is the predicted y when x is zero — but only interpret it if zero is within the range of your x data; otherwise it is just a mathematical anchor. R² (coefficient of determination) is the square of the Pearson correlation coefficient. It ranges from 0 to 1 and tells you the proportion of variance in y that the model explains. For the default data, R² = 0.16, meaning only 16% of the variation in y is explained by x — a weak fit, which is visible in the scattered points.

Common Mistakes and Pitfalls

  • Swapping x and y: Regression is not symmetric. If you swap the columns, you get a different line (unless the correlation is exactly ±1). Decide which variable is the predictor (x) and which is the response (y) based on your question.
  • Ignoring units: The slope has units of y per x. If x is in years and y is in dollars, the slope is dollars per year. Mixing units (e.g., using months for x but years for y) gives a meaningless slope.
  • Rounding too early: Round intermediate sums to fewer decimals than the final answer, and you can get noticeably different slope and intercept. Keep full precision until the end.
  • Extrapolating beyond your data: The line is only valid within the observed range of x. Predicting y for x values far outside that range assumes the relationship continues, which is often false.
  • Assuming causation: A strong R² does not prove x causes y. There may be a lurking variable, or the correlation may be coincidental.

Choosing Between Linear Regression and Other Tools

Linear regression is appropriate when you expect a roughly straight-line relationship and your data has no severe outliers. If the scatter plot shows a curve (exponential, quadratic, logarithmic), a simple linear model will underfit. For two variables only, correlation (r) is a simpler summary of association, but it gives no equation for prediction. If you have multiple predictors, you need multiple linear regression, which uses matrix algebra and is beyond this simple two-column calculator. If your data has outliers that heavily influence the line, consider robust regression or removing the outliers after justification.

How to Use the Calculator Properly

  1. Enter your x values as a comma-separated list (e.g., 1, 2, 3).
  2. Enter your y values in the same order, same length.
  3. Check that each list has the same number of entries — the calculator uses the count of the first list.
  4. Read the slope and intercept; plug them into y = a + bx to make predictions.
  5. Look at R² to judge fit quality. Values above 0.7 typically indicate a strong linear relationship, but context matters.

If your lists have different lengths, the calculator will use the shorter one — always verify the counts match before trusting the output.

Limitations of the Simple Linear Regression Formula

The formulas assume a linear relationship, independence of observations, constant variance of residuals (homoscedasticity), and normally distributed errors for valid confidence intervals. The slope and intercept are still computable even if those assumptions fail, but the standard errors and p-values become unreliable. Also, the formula is sensitive to outliers: a single extreme point can dramatically shift the slope. Always plot your data first. The calculator gives you the numbers, but a scatter plot reveals curvature, clusters, or influential points that the equation alone will hide.

Frequently Asked Questions

What does a negative slope mean in linear regression?

A negative slope means that as x increases, y tends to decrease. The steeper the negative number, the faster y drops per unit of x. For example, a slope of −2 means y decreases by 2 for each 1-unit increase in x.

Can I use linear regression with only 3 data points?

Technically yes — the formulas work with any n ≥ 2. But with 3 points, the line is extremely sensitive to each point, and R² is often misleading. You can fit a line, but you have almost no evidence that the relationship is real or stable. More data (at least 10–20 pairs) is recommended before drawing conclusions.

What is the difference between correlation and regression?

Correlation (r) measures the strength and direction of a linear relationship on a scale from −1 to +1, with no prediction equation. Regression produces an actual equation (y = a + bx) that lets you predict y from x. Regression also gives you the intercept and slope, which correlation does not.

Why is my R² very low even though the line looks reasonable?

A low R² means the points are far from the line relative to the total spread of y. If your data has a lot of scatter (noise), even a correctly estimated slope will have low R². It does not necessarily mean the slope is wrong — it means x explains only a small fraction of y’s variation. Check the residual plot to see if the pattern is random.

Should I force the intercept to be zero?

Only if you have a strong theoretical reason that y must be 0 when x is 0 (e.g., zero sales with zero advertising). Forcing a zero intercept changes the slope formula and usually worsens fit for typical data. Unless your domain demands it, keep the free intercept.

What if my data has units like inches and pounds?

The calculator treats numbers as pure values, so you must be consistent. If x is in inches and y is in pounds, the slope will be in pounds per inch. If you convert x to centimeters, the slope changes accordingly (it becomes pounds per centimeter), but the line’s predictions remain correct after conversion. Always state units when reporting the equation.

Related

Formula used
in x | X values (comma separated) | - | 1, 2, 3, 4, 5 | 0
in y | Y values (comma separated) | - | 2, 4, 5, 4, 5 | 0
out slope | Slope (b) | - | (sum(x*y) - sum(x)*sum(y)/count(x)) / (sum(x*x) - sum(x)^2/count(x)) | 4
out intercept | Intercept (a) | - | mean(y) - slope*mean(x) | 4
out r2 | R-squared | - | ( (sum(x*y) - sum(x)*sum(y)/count(x)) / sqrt((sum(x*x) - sum(x)^2/count(x)) * (sum(y*y) - sum(y)^2/count(y))) )^2 | 4

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.