Linear Regression Calculator
Free linear regression calculator: paste paired x,y data and get the least-squares slope, intercept, correlation coefficient r, and R-squared instantly.
Linear Regression Calculator
Background.
The Quanta linear regression calculator fits the best-fit straight line through any set of paired (x, y) data points using ordinary least squares (OLS), the standard method taught in every introductory statistics course. Paste your x values and your matching y values, and the calculator returns the slope and intercept of the line y = a + bx that best fits your data, along with the correlation coefficient r and R-squared, the two standard measures of how well that line actually describes the relationship.
'Best fit' has a precise mathematical meaning here: the least-squares line is the one specific line, among all possible lines, that minimises the sum of the squared vertical distances between each data point and the line itself. Squaring the distances (rather than, say, just summing them directly) does two things: it makes points above the line and points below the line contribute in the same direction rather than cancelling out, and it penalises large deviations disproportionately more than small ones, which turns out to produce a uniquely solvable, closed-form answer — no trial and error, no iteration, just two formulas plugged directly into your data.
The slope tells you the rate of change: for every one-unit increase in x, the model predicts y changes by exactly the slope. The intercept tells you the model's prediction when x is exactly zero — sometimes meaningful (a baseline cost before any usage), sometimes not (a temperature model evaluated at zero years of experience makes no real-world sense, even though the arithmetic is fine). The correlation coefficient r captures how tightly the data actually clusters around that fitted line, on a scale from −1 (a perfect straight line sloping downward) through 0 (no linear relationship at all) to +1 (a perfect straight line sloping upward). R-squared, simply r squared, has an even more direct interpretation: it is literally the fraction of the total variation in y that the linear model with x explains — an R-squared of 0.6 means 60% of the variability you see in y is accounted for by its linear relationship with x, and the remaining 40% is due to other factors, noise, or genuine nonlinearity that a straight line can't capture.
Two situations make a straight-line fit mathematically undefined rather than merely a poor fit, and this calculator catches both explicitly rather than returning a meaningless number. If every x value you enter is identical, there is no variation in x to relate to y at all — the 'best fit line' would have to be perfectly vertical, which has no defined slope, and the calculator reports this clearly rather than dividing by zero. If every y value is identical, there is nothing for x to explain — the correlation coefficient's own formula divides by the variability in y, which is zero in that case, so the calculator flags this too rather than returning a nonsensical result.
A related but different tool on Quanta, the slope calculator, computes the slope between exactly two points — a geometric primitive useful for coordinate-geometry problems. This calculator is built for the statistical case: many (x, y) observations, genuine scatter around the fit, and the accompanying question of how good that fit actually is (via r and R-squared) — questions that simply don't arise with only two points, which is why this calculator requires at least three paired observations.
What is linear regression calculator?
Simple linear regression models the relationship between two numeric variables — an independent variable x and a dependent variable y — as a straight line, y = a + bx, chosen to minimise the sum of the squared vertical distances (residuals) between the observed y values and the line's predicted values. This 'ordinary least squares' (OLS) criterion has a closed-form solution: the slope is b = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)², and the intercept is a = ȳ − b·x̄, where x̄ and ȳ are the sample means of x and y. The Pearson correlation coefficient r = Σ(xᵢ − x̄)(yᵢ − ȳ) / √[Σ(xᵢ − x̄)²·Σ(yᵢ − ȳ)²] measures the strength and direction of the linear association, ranging from −1 (perfect negative linear relationship) to +1 (perfect positive linear relationship), with 0 indicating no linear relationship. R-squared, the square of r in simple linear regression, is the proportion of the total variance in y that is explained by the fitted line — a value of 1 means the line passes through every point exactly, and a value of 0 means the line explains none of the variation in y beyond its own mean. A meaningful correlation coefficient requires at least three data points; with exactly two points, r is always exactly +1 or −1 by construction (a line drawn through any two distinct points fits them perfectly), which carries no information about the strength of any underlying relationship.
How to use this calculator.
- Paste your x values (the independent/predictor variable) as comma-separated numbers.
- Paste your y values (the dependent/response variable) as comma-separated numbers, in the same order and count as the x values.
- You need at least 3 paired points for a meaningful result.
- Read the slope and intercept to get the fitted line's equation: y = intercept + slope × x.
- Read r and R-squared to judge how well that line actually fits your data — values of r close to +1 or −1 (R-squared close to 1) indicate a strong linear relationship; values close to 0 indicate a weak or absent one.
- If you get an error about identical x or y values, check your data — a regression line cannot be fit when there's no variation in one of the two variables.
The formula.
Given n paired observations (xᵢ, yᵢ), compute the means x̄ and ȳ. Define three sums of squares: Sxy = Σ(xᵢ − x̄)(yᵢ − ȳ), Sxx = Σ(xᵢ − x̄)², and Syy = Σ(yᵢ − ȳ)². The least-squares slope is b = Sxy / Sxx, derived by minimising the sum of squared residuals Σ(yᵢ − a − bxᵢ)² with respect to a and b and solving the resulting normal equations. The intercept is a = ȳ − b·x̄, which follows immediately once b is known (the fitted line always passes through the point (x̄, ȳ)). The Pearson correlation coefficient is r = Sxy / √(Sxx·Syy), and R-squared is simply r². Two degenerate cases must be guarded explicitly: if Sxx = 0 (every x value identical), the slope formula divides by zero and no line can be fit — a vertical 'line' has no defined slope. If Syy = 0 (every y value identical), the correlation formula divides by zero — there is no variability in y for x to explain, so r is undefined even though a trivial slope of exactly 0 could technically still be computed. This calculator raises a clear error in both cases rather than returning NaN or Infinity.
A worked example.
Five paired observations: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5. The mean of x is x̄ = 3 and the mean of y is ȳ = 4. Computing the three sums of squares: Sxy = (1−3)(2−4) + (2−3)(4−4) + (3−3)(5−4) + (4−3)(4−4) + (5−3)(5−4) = 4+0+0+0+2 = 6. Sxx = (1−3)²+(2−3)²+(3−3)²+(4−3)²+(5−3)² = 4+1+0+1+4 = 10. Syy = (2−4)²+(4−4)²+(5−4)²+(4−4)²+(5−4)² = 4+0+1+0+1 = 6. The slope is b = Sxy/Sxx = 6/10 = 0.6, and the intercept is a = ȳ − b·x̄ = 4 − 0.6×3 = 2.2, giving the fitted line y = 2.2 + 0.6x. The correlation coefficient is r = Sxy/√(Sxx·Syy) = 6/√(10×6) = 6/√60 ≈ 0.7746, and R-squared = r² = 0.6 exactly — the linear model explains 60% of the variability in y. An r of 0.77 indicates a moderately strong positive linear relationship: as x increases, y tends to increase too, though the fit isn't perfect (the data doesn't lie exactly on a single line).
Frequently asked questions.
What does the slope of a regression line tell you?
What is the difference between r and R-squared?
Why does this calculator require at least 3 data points?
What happens if all my x values are the same?
What happens if all my y values are the same?
Does a high R-squared mean the linear model is a good fit?
What is the difference between this calculator and the slope calculator?
References& sources.
- [1]NIST/SEMATECH e-Handbook of Statistical Methods, §4.4.3.1 — Estimating the Parameters of a Simple Linear Regression Model. Provides the exact least-squares slope and intercept formulas implemented here.
- [2]NIST/SEMATECH e-Handbook of Statistical Methods, §4.4.4 — How Can I Tell if a Model Fits My Data? Defines R-squared and cautions against relying on it alone to judge model fit.
- [3]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed., Duxbury). Covers the Pearson correlation coefficient and its relationship to R-squared in simple linear regression.
In this category
Embed
Quanta Pro
Paid features are coming later.
- All 313 calculators remain free
- No billing is enabled