Solved.tools — Free Online Calculators & Tools

We use cookies for analytics and advertising. Learn more about our cookie policy

Linear Regression Calculator

Last updated: 16 August 2026

Reviewed by Gavin · Research and drafting assisted by AI

Linear Regression Calculator

Fit an ordinary-least-squares line y = α + βx to paired X and Y data. Get the slope, intercept, R², standard error, 95% confidence interval for the slope, and a scatter plot with the fitted regression line.

Comma, space, semicolon, or newline-separated numbers. 5 parsed.
Must be paired with the X values. 5 parsed.
Used for the CI on the slope. Default 95%.
Load example:

Fitted regression line

y = 2.2 + 0.6·x
Sample size (n)5
Slope (β)0.6
Intercept (α)2.2
Mean of x (x̄)3
Mean of y (ȳ)4
R² (coefficient of determination)0.6
Residual SS (SSres)2.4
Total SS (SStot)6
Standard error of estimate (SE)0.8944
Standard error of slope (SE_β)0.2828
t-statistic for slope2.1213
Degrees of freedom (n − 2)3
p-value for slope (two-sided)0.5168
95% CI for slope[-0.178, 1.378]
InterpretationSlope is not statistically significant at α = 0.05.

Scatter plot with fitted line

1.742.673.64.535.460.841.9234.085.16xyRegression lineData point (xᵢ, yᵢ)

Method: ordinary least squares (OLS). β = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)², α = ȳ − β·x̄, R² = 1 − Σ(yᵢ−ŷᵢ)² / Σ(yᵢ−ȳ)².

Reference: NIST/SEMATECH e-Handbook of Statistical Methods, §6.1 – Simple Linear Regression; Draper & Smith, Applied Regression Analysis (3rd ed., Wiley).

Was this helpful?


Linear Regression Calculator

Linear regression is the workhorse of statistics, a single straight line that summarises how one quantity (y) moves with another (x). Given a list of paired observations, the ordinary-least-squares (OLS) fit picks the slope and intercept that minimise the sum of squared vertical residuals. The calculator on this page does that fit for you: paste your X and Y values, change a confidence level if you like, and the page reports the slope (β), intercept (α), R², residual standard error, a confidence interval for the slope, a two-sided p-value, and an inline scatter plot with the fitted line drawn through the data.

The output is the same algebra any introductory statistics textbook teaches (Draper & Smith; NIST/SEMATECH §6.1), but it is also the same algebra that powers real forecasting, calibration, A/B testing, scientific instrument control, and machine-learning baselines. You will encounter OLS again in trend lines drawn through economic data, in dose-response curves fitted to pharmaceutical assays, in the linear models that predictive text uses to weight token probabilities, and in finance as the capital-asset-pricing-model beta. A calculator that produces the same numbers, to the same conventions, as those tools is one you can trust.

How to Use This Tool

  1. Paste your X values into the first box. Numbers can be separated by commas, spaces, semicolons, tabs, or newlines, any mixture is fine.
  2. Paste your Y values into the second box using the same separator rules. The two lists must be paired one-to-one in the order they appear; extra trailing values are silently dropped so a length mismatch does not produce a wrong fit.
  3. Optionally change the confidence level (defaults to 95%). This sets the width of the reported confidence interval for the slope.
  4. Read the fitted equation y = α + β·x, the R², the standard error of the estimate, the standard error of the slope, the t-statistic, the p-value, and the lower/upper CI bounds, all updated live as you type.
  5. Scroll to the scatter plot to inspect the data and the fitted line visually. If a point lies far above or below the line, it is a candidate outlier, large residuals inflate the standard error and shrink the t-statistic.
  6. Click any of the example buttons to load a worked dataset (perfect line, negative slope, two-point fit, two realistic samples) into the inputs.
  7. Use Copy summary to put a plain-text "y = α + β·x, R² = …, 95% CI = [lo, hi]" block on your clipboard for a report or lab notebook.

If you have fewer than two pairs, the calculator surfaces a clear error and does not attempt a fit. If all X values are identical the slope is mathematically undefined (the denominator is zero); the calculator reports that explicitly rather than producing NaN.

The Formulas

The model is a straight line with intercept and slope, written as y = α + β·x. The OLS estimator chooses α and β so that the sum of squared vertical residuals Σ(yᵢ − ŷᵢ)² is as small as possible, where ŷᵢ = α + β·xᵢ is the predicted y for observation i.

Slope. The closed-form OLS slope is the ratio of the cross-product about the mean to the variance about the mean of x:

β = Σ (xᵢ − x̄)(yᵢ − ȳ) / Σ (xᵢ − x̄)²

Both sums run from i = 1 to n. The numerator is the sample covariance Sxy multiplied by n; the denominator is the sample variance of x multiplied by n. The slope is in the units of y divided by the units of x.

Intercept. Once β is known, the line is forced to pass through the centroid (x̄, ȳ):

α = ȳ − β·x̄

This is not an assumption; it follows from differentiating the residual-sum-of-squares with respect to α and setting the derivative to zero.

Coefficient of determination. R² is the fraction of variance in y that is explained by x under the linear model:

R² = 1 − SSres / SStot = 1 − Σ(yᵢ − ŷᵢ)² / Σ(yᵢ − ȳ)²

R² ranges from 0 (the line explains nothing) to 1 (the line fits every point exactly). A negative R² is mathematically possible when the model is evaluated on data outside the fitting range but never arises on training data because the OLS fit always does at least as well as the horizontal mean.

Standard error of the estimate. This is the square root of the residual variance with n − 2 degrees of freedom (one degree is consumed by β, another by α):

SE = sqrt( SSres / (n − 2) )

It is in the units of y, and it estimates the standard deviation of the unobserved noise term.

Standard error of the slope. This is the SE of the estimate divided by the square root of the variance of x:

SE_β = SE / sqrt( Σ (xᵢ − x̄)² )

A larger spread of x makes SE_β smaller and the slope estimate more precise, that is why designed experiments try to push the explanatory variable to its extremes.

Confidence interval for the slope. With df = n − 2 degrees of freedom:

CI = β ± t_{α/2, n−2} · SE_β

For 95% confidence, the multiplier is 1.96 for very large n and grows toward 12.71 for n = 3. The calculator carries a hard-coded table for df in 1..60 and a Cornish-Fisher-style approximation above that.

Two-sided p-value. The t-statistic t = β / SE_β is compared against the Student's t distribution with n − 2 degrees of freedom; the p-value is the two-sided tail probability. This calculator computes it exactly from the regularised incomplete beta function (Lentz continued fraction) and reports values below 10⁻⁴ as "< 0.0001".

Worked Examples

Example 1, Sample n = 5. Data: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5). x̄ = 3, ȳ = 4. Σ(xᵢ − x̄)² = 10. Σ(xᵢ − x̄)(yᵢ − ȳ) = 6. So β = 6 / 10 = 0.6, α = 4 − 0.6·3 = 2.2. Predictions are 2.8, 3.4, 4.0, 4.6, 5.2; residuals are −0.8, 0.6, 1.0, −0.6, −0.2; SSres = 0.64 + 0.36 + 1.00 + 0.36 + 0.04 = 2.4. SStot = (4 − 2)² + 0 + 1 + 0 + 1 = 6. R² = 1 − 2.4/6 = 1 − 0.4 = 0.6. SE = √(2.4 / 3) = √0.8 ≈ 0.894. SE_β = 0.894 / √10 ≈ 0.283. 95% CI at df = 3 (textbook t = 3.182): the interval is 0.6 ± 3.182 · 0.283 ≈ 0.6 ± 0.900 = [−0.300, 1.500]. The slope is not statistically distinguishable from zero at α = 0.05 because the interval contains 0, the data is too noisy relative to its range.

Example 2, Perfect line. Data: (1, 2), (2, 4), (3, 6), (4, 8). β = (1·2 + 2·4 + 3·6 + 4·8) / (1 + 4 + 9 + 16) = 60 / 30 = 2, α = 5 − 2·2.5 = 0. SSres = 0. R² = 1. SE = 0; the SE_β is undefined in the textbook sense because there are no residuals, and the CI collapses to a point. This is the limiting case of "the model fits the data exactly"; the OLS fit has zero uncertainty in slope because every point sits on the line.

Example 3, Negative slope. Data: (1, 5), (2, 4), (3, 3), (4, 2), (5, 1). x̄ = 3, ȳ = 3. Σ(xᵢ − x̄)² = 10. Σ(xᵢ − x̄)(yᵢ − ȳ) = (1−3)(5−3) + (2−3)(4−3) + (3−3)(3−3) + (4−3)(2−3) + (5−3)(1−3) = −4 − 1 + 0 − 1 − 4 = −10. So β = −10 / 10 = −1, α = 3 − (−1)·3 = 6. Equation: y = 6 − x. SSres = 0, R² = 1. SE = 0; CI collapses to [−1, −1]. This is a perfectly linear, monotonically decreasing relationship, typical of physical laws where two quantities trade off (for example, resistance versus conductance at constant voltage, or remaining daylight versus latitude in mid-latitude summer).

Example 4, Two-point fit. Data: (0, 0) and (10, 10). x̄ = 5, ȳ = 5. Σ(xᵢ − x̄)² = 25 + 25 = 50. Σ(xᵢ − x̄)(yᵢ − ȳ) = (−5)(−5) + (5)(5) = 50. β = 50 / 50 = 1, α = 5 − 1·5 = 0. Equation: y = x. SSres = 0, R² = 1. Note that with n = 2, df = n − 2 = 0, so the standard error and confidence interval are undefined; the calculator returns ", " for those rows. A two-point fit is a perfect line by construction, but it has no measure of uncertainty, any straight line through two points has zero residual.

Example 5, Realistic noisy data. Data: x = (1, 2, 3, 4, 5, 6, 7); y = (2.1, 3.9, 6.2, 7.8, 10.1, 11.7, 14.0). x̄ = 4, ȳ ≈ 7.971. Σ(xᵢ − x̄)² = 28. Σ(xᵢ − x̄)(yᵢ − ȳ) ≈ 55.2. β ≈ 55.2 / 28 ≈ 1.971, α ≈ 7.971 − 1.971·4 ≈ 0.086. R² is very close to 1 (about 0.998) because the noise is small. SE ≈ 0.21; SE_β ≈ 0.039. t ≈ 50, p ≈ 10⁻⁸. The slope is overwhelmingly significant. This is the kind of clean laboratory dataset where OLS works as advertised.

Where Linear Regression Shows Up

Forecasting. Trend lines on retail sales, GDP, electricity demand, and visitor arrivals are all OLS fits. The slope is the average change per unit time, and the intercept is the fitted baseline level. Forecasts are ŷ at future x values with prediction intervals that come from the same SE.

A/B testing. When you compare two variants of a web page, the per-user metric (clicks, conversions, revenue) is regressed on a treatment indicator (0 for control, 1 for variant). The slope is the average treatment effect, the SE_β is its standard error, and the p-value is the test of whether the effect is zero. The same algebra underlies most published A/B-test reports.

Calibration. Analytical instruments, pH meters, dissolved-oxygen probes, spectrophotometers, scales, are calibrated by regressing the instrument reading on a set of certified reference standards. The fitted slope is the calibration factor and the intercept is the blank offset. Drift in either is detected by re-running the regression against fresh standards.

Econometrics and finance. The capital-asset-pricing model (CAPM) expresses the expected return of a stock as α + β·(market return), where β is the OLS slope of the stock's excess returns on the market's excess returns. The same approach is used to estimate hedge ratios, factor loadings, and term premia.

Machine-learning baselines. Before fitting a random forest or a neural network, an experienced practitioner fits an OLS line and treats its R² as the floor. If a complex model cannot beat the OLS baseline, the data either carries no signal or the model is overfitting.

Pharmaceutical dose-response curves. Phase-I and Phase-II dose-finding studies fit log-dose against percent inhibition to estimate the half-maximal effective concentration (EC50). The slope quantifies how steeply the response rises with dose, and the CI on the slope is part of the regulatory submission.

Common Mistakes

Treating R² as a goodness-of-fit measure for prediction. R² tells you how much variance your model accounts for on the training data. A model can have R² = 0.99 and still produce terrible forecasts out of sample if the underlying relationship is non-linear or the data is over-fit. Always check the residuals, the calculator exposes them via the predicted line on the scatter plot.

Confusing correlation with regression slope. The Pearson correlation r is bounded by ±1 and dimensionless; the regression slope β is unbounded and carries units. For standardised data (z-scores) the slope equals r, but in raw data they are different numbers and answer different questions.

Fitting OLS on heteroscedastic data without weighting. If the residual variance grows with x (for example, a volatility that scales with price), the OLS slope is still unbiased but the SE_β is wrong and the p-value is misleading. Weighted least squares with weights proportional to 1/σᵢ² fixes both.

Ignoring gearing points. A single x value that sits far from the rest of the data, for example, one observation at x = 100 in a dataset that runs from x = 1 to 10, can dominate the slope. Look at the spread of x in the scatter plot; if you see a single far-out point, jackknife it out and refit to see how much the slope moves.

Extrapolating far outside the fitted range. The line is only justified between the smallest and largest x value you fitted on. Outside that range, ŷ is a mathematical extrapolation with no statistical backing; the standard error formula assumes the underlying relationship is linear across the whole range.

Reading the p-value as effect size. A statistically significant slope of 0.001 with a tight CI may be useless in practice. Always report the slope and the CI in original units, alongside the p-value, so readers can judge substantive importance.

Frequently Asked Questions

What does "least squares" actually mean?

Least squares means you choose the slope and intercept that minimise the sum of squared vertical distances from each data point to the line. The arithmetic happens to have a closed form, the so-called normal equations, which is why the calculator can give you the answer instantly without iterating. "Vertical" is important: the residuals are measured in the y direction only. If you wanted to minimise the perpendicular distance, you would use total least squares, which has different formulas.

When is R² a useful number and when is it misleading?

R² is useful when you want to know what fraction of y's variance the linear model accounts for on the same sample, and when you are comparing two linear models on the same dataset. R² is misleading when comparing models on different datasets, when the model is non-linear, when you are interested in out-of-sample prediction, or when the data is heavily skewed. A high R² does not guarantee a useful model; a low R² does not mean the model is wrong.

What is the difference between R² and adjusted R²?

Adjusted R² penalises the addition of explanatory variables that do not improve the fit enough to justify the loss of a degree of freedom. It is defined as 1 − (1 − R²) · (n − 1) / (n − k − 1), where k is the number of predictors. For simple linear regression (k = 1), adjusted R² is always smaller than R² and is closer to R² when n is much larger than k. This calculator reports plain R²; for k = 1 the difference is small.

Why does my confidence interval contain zero when R² is 0.7?

R² measures how well the line fits in a relative sense, the slope may be far from zero in raw units, but the noise around it is large enough that the CI straddles zero. The CI is testing whether the slope is statistically distinguishable from zero, not how big it is. A high R² with a CI containing zero is a tell that you have very little data and should collect more.

Can I use linear regression when the relationship is curved?

Not directly. You can convert x or y (for example, take logs, square roots, or reciprocals) and fit a line in the converted space. Log-linear regression fits ln(y) = α + β·x; log-log regression fits ln(y) = α + β·ln(x). Both are linear regressions on converted data and produce a fitted curve when you convert back. The OLS assumptions about residuals apply in the converted space, not in the original space, so you must check residuals there.

What does "two-sided p-value" mean?

It is the probability of observing a t-statistic at least as extreme as the one you got, in either direction, under the null hypothesis that the true slope is zero. One-sided p-values are half of two-sided p-values when the t-statistic is in the expected direction; they are used when you have an a-priori reason to care about only one direction. Most published A/B tests report two-sided p-values because they make no prior directional commitment.

Why does the standard error of the slope depend on the spread of x?

The denominator of SE_β is √Σ(xᵢ − x̄)². If x values are tightly clustered, that sum is small, SE_β is large, and the slope is imprecise. If x values are spread out, the sum is large, SE_β is small, and the slope is precise. This is the statistical justification for designed experiments: by deliberately choosing extreme x values (within ethical and physical limits), experimenters shrink the SE_β and detect smaller effects with the same sample size.

Can this calculator handle thousands of observations?

Yes. The OLS formulas are O(n) operations; the calculator parses and fits in milliseconds for any n that fits in a browser textarea. The p-value computation uses a continued-fraction expansion of the regularised incomplete beta function, which converges to machine precision in well under 200 iterations for typical t-statistics.

What is the relationship between linear regression and the t-test?

The two-sample t-test is a special case of a regression where the explanatory variable is an indicator (0 for control, 1 for treatment). The slope is the mean difference, the SE_β is the standard error of the difference, and the two-sided p-value is the same number the t-test reports. Regression is the more general framework.

How do I know if my residuals are normally distributed?

Plot them. The scatter plot on this page lets you eyeball whether residuals are symmetric around the line and whether any single observation is far from the rest. For a formal check, run a Shapiro-Wilk test or look at a Q-Q plot. Severe non-normality biases the p-value but does not bias the slope itself.

References

  • NIST/SEMATECH e-Handbook of Statistical Methods, §6.1 "Simple Linear Regression" and §7.1 "One-way ANOVA". The canonical reference for OLS formulas, residual diagnostics, and confidence-interval derivations used in this calculator.
  • Draper, N. R. & Smith, H., Applied Regression Analysis (3rd edition, Wiley, 1998). The standard graduate-level reference; the notation (β, α, R²) used here follows this text.
  • Montgomery, D. C., Peck, E. A. & Vining, G. G., Introduction to Linear Regression Analysis (5th edition, Wiley, 2012). Covers diagnostics, weighted least squares, and robust regression in a more applied register.
  • Seber, G. A. F. & Lee, A. J., Linear Regression Analysis (2nd edition, Wiley, 2003). A rigorous matrix-based treatment; useful when extending OLS to multiple predictors.
  • Student (W. S. Gosset), "The probable error of a mean", Biometrika (1908). The original paper introducing the t-distribution used for confidence intervals and p-values.
  • Correlation Calculator, for the Pearson correlation coefficient r without fitting a line.
  • Statistics Confidence Interval Calculator, for a confidence interval on a single mean rather than a slope.
  • Standard Deviation Calculator, for the variance calculations behind R².
  • T-Test Calculator, for the special case of a regression with an indicator variable.
  • Anova Calculator, for comparing more than two means, the natural extension of regression with categorical predictors.