Skip to content
Calculator Collection

Linear Regression Calculator

Fit a least-squares linear regression and read off the slope of the best-fit line y = a + b·x, using summary statistics (n, Σx, Σy, Σxy, Σx²) of paired x and y data. Used for trend analysis, forecasting, and quantifying how much y changes per unit change in x.

Last updated: May 2026

Fill in the required fields to see your result.

Compare with similar

About this calculator

The least-squares slope is b = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), where n is the number of paired (x, y) observations, Σxy is the sum of products, Σx is the sum of x values, Σy is the sum of y values, and Σx² is the sum of squared x values. The intercept (computed separately) is a = (Σy − b·Σx) / n, giving the full line y = a + b·x. This slope minimises the sum of squared vertical residuals — the squared distance from each observed y to the fitted line. The numerator (n·Σxy − Σx·Σy) is proportional to the covariance between x and y; the denominator (n·Σx² − (Σx)²) is proportional to the variance of x. Variables: b is the slope (rise per unit run, positive when y increases with x); a is the intercept (predicted y when x = 0). Edge cases: if all x values are identical (no variation in x), the denominator is zero and the slope is undefined — there is no way to estimate how y changes with x when x doesn’t change. The least-squares slope is highly sensitive to outliers because residuals are squared; a single extreme point can swing the line substantially. The model assumes a linear relationship; curvature in the data produces biased slope estimates and large residuals. Always check residuals (observed minus predicted) for patterns, and pair the slope with r² (coefficient of determination) to assess fit quality.

How to use

Example 1 — Hours studied vs exam score. n = 5 pairs: (1,50), (2,60), (3,70), (4,80), (5,90). Σx = 1+2+3+4+5 = 15. Σy = 50+60+70+80+90 = 350. Σxy = 1·50 + 2·60 + 3·70 + 4·80 + 5·90 = 50+120+210+320+450 = 1150. Σx² = 1+4+9+16+25 = 55. b = (5·1150 − 15·350) / (5·55 − 15²) = (5750 − 5250) / (275 − 225) = 500/50 = 10. ✓ Each additional hour of study adds 10 points to the exam score in this dataset. Compute intercept separately: a = (350 − 10·15)/5 = 200/5 = 40. Full line: score = 40 + 10·hours. Example 2 — Square footage vs house price. n = 4 homes: (1200, 250), (1500, 290), (1800, 340), (2100, 410), with price in $1000s. Σx = 6600. Σy = 1290. Σxy = 1200·250 + 1500·290 + 1800·340 + 2100·410 = 300,000 + 435,000 + 612,000 + 861,000 = 2,208,000. Σx² = 1,440,000 + 2,250,000 + 3,240,000 + 4,410,000 = 11,340,000. b = (4·2,208,000 − 6600·1290) / (4·11,340,000 − 6600²) = (8,832,000 − 8,514,000) / (45,360,000 − 43,560,000) = 318,000/1,800,000 ≈ 0.1767. ✓ Each additional square foot adds about $177 to home price in this dataset. (a = (1290 − 0.1767·6600)/4 ≈ 31.0 thousand, so price ≈ 31 + 0.177·sqft.)

Frequently asked questions

What does linear regression actually compute?

Linear regression finds the unique straight line y = a + b·x that minimises the sum of squared vertical distances from observed points to the line. The slope b represents the average change in y per one-unit increase in x; the intercept a represents the predicted y when x = 0 (often outside the range of the data, in which case it lacks a direct physical interpretation). The least-squares procedure gives unbiased and minimum-variance estimates of slope and intercept under the standard assumptions (linearity, independent errors, constant variance, normality). Linear regression also provides r² (the proportion of variance in y explained by x), confidence intervals on the slope and intercept, and prediction intervals for individual future observations. It is the workhorse of all parametric statistical modelling — multiple regression, ANOVA, ANCOVA, mixed models, and most machine learning regression methods extend this core framework in various directions.

How do I interpret slope and intercept in real-world terms?

The slope is in units of (y per unit of x): salary per year of experience ($/yr), price per square foot ($/ft²), exam score per hour studied (points/hr). It tells you the marginal rate at which y is associated with x — not a deterministic prediction but an average pattern. The intercept is in units of y, and represents the predicted y value when x = 0. The intercept is interpretable only when x = 0 is meaningful for your data: if you fit weight vs height in adults (where height = 0 is meaningless), the intercept is just a mathematical artefact. For interpretive purposes, sometimes researchers center x at its mean (using x − x̄ instead of x), which makes the intercept the predicted y at the average x — often more meaningful. The slope’s interpretation does not change when x is centred or scaled, but the units do.

What is r² and how does it relate to the regression slope?

r² (coefficient of determination) is the fraction of variance in y explained by x: r² = SSR/SST = 1 − SSE/SST, where SSR is the sum of squares explained by the regression, SSE is the sum of squared residuals, and SST is the total sum of squares of y. r² ranges from 0 (the model explains nothing — slope is essentially 0) to 1 (the model explains all variance — every point lies exactly on the line). For simple linear regression, r² equals the square of the Pearson correlation coefficient r. The slope b and r² are linked but distinct: a steep slope with a noisy scatter has high slope and low r²; a small slope with a tight cluster has low slope and high r². Reporting only one is misleading — slope is the rate of change, r² is how reliably the line predicts. Always report both, plus a scatter plot.

What are the most common mistakes people make with linear regression?

The first is interpreting slope as causation; a strong slope just shows correlation, and many confounders can produce apparent linear relationships. The second is extrapolating outside the observed x range; a model fit to x ∈ [1, 5] may be wildly wrong at x = 100. The third is fitting linear models to nonlinear data; visual inspection of residuals reveals curvature that the model misses, and r² alone is not enough to detect this. The fourth is ignoring outliers, which can flip both slope and intercept dramatically. The fifth is using regression on autocorrelated time-series data without correcting for temporal dependence; classical inference assumes independent errors. The sixth is reporting only the slope without standard error, confidence interval, or hypothesis test on whether slope differs significantly from zero. The seventh is fitting models to summary statistics that hide group-level structure — Simpson’s paradox can reverse the slope’s sign when within-group patterns differ from the pooled pattern.

When should I not use this calculator?

Skip it for non-linear relationships — try polynomial regression, log-transformations, or non-linear models (exponential, logistic). Avoid it for small samples (n < 5) where the slope estimate is highly unstable and inference is unreliable. It is the wrong tool when ordinary least squares assumptions are violated: heteroscedastic errors (variance depends on x), autocorrelated errors (time series), or non-normal errors with heavy tails — these need weighted least squares, ARIMA, or robust regression. Do not use it for categorical predictors without dummy-coding them properly first. Skip it for high-dimensional regression (many predictors), which requires multiple regression with potential regularisation (ridge, lasso, elastic net). And for predictions outside the observed x range, treat results with extreme caution — linear extrapolation can be drastically wrong far from the data, especially when the true relationship is curved or saturates.

Sources & references