Linear Regression Calculator
Fit a least-squares regression line to paired x and y data, returning the slope, the intercept, and the equation in y = mx + b form.
Linear Regression Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser — no data is sent to any server.
Formula: y = mx + b | m = (nΣxy - ΣxΣy) / (nΣx² - (Σx)²) | b = (Σy - mΣx) / n
Worked example — y = 0.6x + 2.2, R² = 0.60
Formula
y = mx + b | m = (nΣxy - ΣxΣy) / (nΣx² - (Σx)²) | b = (Σy - mΣx) / n
Least squares linear regression finds the best-fit line through data points by minimizing the sum of squared residuals.
Worked Examples
Example 1: Simple dataset
Problem:X: 1,2,3,4,5 and Y: 2,4,5,4,5
Solution:m=(5×66-15×20)/(5×55-225)=30/50=0.6, b=(20-0.6×15)/5=2.2
Result:y = 0.6x + 2.2, R² = 0.60
Frequently Asked Questions
What is R²?
R² (coefficient of determination) measures how well the regression line fits the data. R²=1 means perfect fit, R²=0 means no linear relationship.
What is the difference between correlation and causation in regression?
Linear regression and R² measure the strength of a linear association between variables, not whether one causes the other. Ice cream sales and drowning rates are correlated (both rise in summer) but one does not cause the other. Always consider confounding variables and domain knowledge before concluding that the x variable causes changes in y.
When is linear regression not appropriate?
Linear regression assumes a linear relationship, homoscedasticity (constant variance of residuals), and independence of observations. It is not appropriate when the relationship is curved (use polynomial regression), when outliers are present and not addressed, when the residuals show clear patterns, or when predicting categorical outcomes (use logistic regression instead).
What is the difference between the slope m and the correlation coefficient r?
The slope m is expressed in units of y per unit of x, so it changes if you rescale either variable, and it answers 'how much does y move when x moves by one?'. The correlation coefficient r is dimensionless and bounded between -1 and +1, and it answers 'how tightly do the points cluster around a straight line?'. They are linked by m = r x (sy/sx), where sx and sy are the sample standard deviations, so both always share the same sign.
What are residuals, and why does least squares square them?
A residual is the vertical gap between an observed point and the fitted line, e = y - (mx + b). Squaring makes every residual positive so that gaps above and below the line cannot cancel out, and it makes the total a smooth function whose minimum can be solved exactly with the normal equations Linear Regression Calculator uses. Squaring also penalises large misses far more heavily than small ones, which is why a single distant point can pull the line noticeably.
Why does the regression line always pass through the point (x-bar, y-bar)?
Because the intercept is computed as b = (Sum y - m Sum x) / n, which is the same as b = y-bar - m x-bar. Substituting x = x-bar into y = mx + b therefore returns exactly y-bar. This is a useful sanity check: compute the mean of your x values and the mean of your y values, feed the mean x into the fitted equation, and you should recover the mean y. If you do not, an input value has been mistyped.
How does a high-leverage outlier distort the fitted line?
Leverage depends on how far a point's x value sits from x-bar. A point with an extreme x acts on the line like a long lever arm, so even a modest vertical error there can swing the slope substantially, while an unusual y value near the centre of the x range mostly shifts the intercept. Recompute the fit with the suspect point removed; if the slope, intercept or R-squared change materially, that single observation is driving your conclusion and needs justification.
What is the difference between regressing y on x and regressing x on y?
Ordinary least squares minimises vertical distances only, so it treats y as the response and x as fixed. Swapping the roles minimises horizontal distances instead and gives a different line. The two slopes are not reciprocals: their product equals r-squared, so they coincide only when the fit is perfect and diverge more as the correlation weakens. Decide before fitting which variable you actually want to predict, and put that one in the Y field.
How many data points do I need for a meaningful regression?
Two points are the mathematical minimum, but they define a line exactly and always give R-squared = 1, which tells you nothing about fit quality. Meaningful slope estimates need enough points to leave real residual degrees of freedom, which for simple regression is n - 2. More important than a raw count is the spread of x: points spanning a wide, well-covered range pin the slope down far more precisely than the same number of points bunched into a narrow interval.
How do I read a residual plot to check whether a straight line is the right model?
Plot the residuals against x. If a line is appropriate, they should scatter randomly in a horizontal band centred on zero with roughly constant width. A U or arch shape means the true relationship is curved and a polynomial or transformed model fits better. A funnel that widens with x indicates heteroscedasticity, so the slope is still usable but standard errors and prediction intervals become unreliable. A drifting pattern in time-ordered data signals autocorrelation.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator · Editorial policy
Related Calculators
🧮Least Squares Regression Line Calculator
Calculate least squares regression line with inputs, formulas, and instant results.
🧮Linear Interpolation Calculator
Calculate linear interpolation with inputs, formulas, and instant results.
🧮Linear Combination Calculator
Calculate linear combination with inputs, formulas, and instant results.
🧮Linear Independence Calculator
Calculate linear independence with inputs, formulas, and instant results.
🧮Regression Equation Calculator
Calculate the regression equation, R-squared, and prediction intervals from data points.
🧮Linear Programming Calculator
linear programming calculator. Get instant, accurate results.
🧮Sum of a Linear Number Sequence Calculator
Calculate sum of alinear number sequence with inputs, formulas, and instant results.
🧮Equation Solver
Solve linear, quadratic, cubic, and polynomial equations with step-by-step solutions.