Linear Regression Calculator: Line of Best Fit, Quadratic Fit and R²
By FindMySum · Last reviewed
A linear regression calculator finds the straight line that best fits a set of (x, y) points, and this one can also fit a quadratic curve. Paste your data, and it gives the equation, R², the correlation coefficient r, the standard error, the standard error of each coefficient, a residual for every point and a prediction for any x you choose, with a scatter plot of the points and the fitted line or curve. Its arithmetic is checked against the certified answers in the NIST Statistical Reference Datasets.
What "least squares" means
For any candidate line, each point has a residual: its actual y minus the y the line predicts (ŷ). Some residuals are positive and some negative. Least squares picks the line that makes the sum of squared residuals (SSE) as small as possible. Squaring stops positive and negative residuals from cancelling and penalises big misses more than small ones.
For a straight line y = b0 + b1·x, the minimising coefficients have a closed form:
- Slope: b1 = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
- Intercept: b0 = ȳ − b1·x̄
where x̄ and ȳ are the means. The fitted line always passes through the point (x̄, ȳ), and the residuals always add up to zero.
A quadratic y = b0 + b1·x + b2·x² is still "linear regression" in the statistical sense, because it is linear in the coefficients. It is found by solving three equations, the normal equations, in three unknowns.
Reading the output
| Output | Meaning |
|---|---|
| Slope b1 | Change in y for each 1-unit increase in x (linear model) |
| Intercept b0 | Predicted y when x = 0 |
| b2 | Curvature: positive opens upward, negative downward |
| R² | Share of the variation in y explained by the model, 0 to 1 |
| r | Correlation, −1 to +1, the signed square root of R² (linear only) |
| Standard error s | Typical size of a residual, in y's units: √(SSE ÷ (n − p)) |
| Coefficient standard errors | Precision of each estimate |
Here n is the number of points and p the number of coefficients (2 or 3). The spreadsheet equivalents are SLOPE, INTERCEPT, RSQ, CORREL and STEYX, or LINEST for everything at once.
Worked example
Take five points: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5).
- Means. x̄ = 15 ÷ 5 = 3, ȳ = 20 ÷ 5 = 4.
- Sums. Σ(x − x̄)(y − ȳ) = (−2)(−2) + (−1)(0) + 0(1) + 1(0) + 2(1) = 6, and Σ(x − x̄)² = 4 + 1 + 0 + 1 + 4 = 10.
- Slope and intercept. b1 = 6 ÷ 10 = 0.6; b0 = 4 − 0.6 × 3 = 2.2. The line is y = 2.2 + 0.6x.
- Residuals. Predicted values 2.8, 3.4, 4.0, 4.6, 5.2 give residuals −0.8, 0.6, 1.0, −0.6, −0.2, which add to 0. SSE = 0.64 + 0.36 + 1 + 0.36 + 0.04 = 2.4.
- R². SST = Σ(y − ȳ)² = 4 + 0 + 1 + 0 + 1 = 6, so R² = 1 − 2.4 ÷ 6 = 0.6, and r = √0.6 ≈ 0.775.
- Standard error. s = √(2.4 ÷ 3) ≈ 0.894.
Switch the model to quadratic and the fit becomes y ≈ 0.2 + 2.314x − 0.286x², with R² rising to about 0.790. Whether that curvature is real is a judgement call with five points, which is where the coefficient standard errors and adjusted R² help.
The calculator opens with eight points that rise steadily; their line has a slope close to 2 and an R² above 0.99.
Checked against NIST reference data
The US National Institute of Standards and Technology publishes regression datasets with answers certified to 15 significant digits, specifically to test statistical software. This calculator reproduces two of them:
- Norris (36 points, linear): slope 1.00211681802045, intercept −0.262323073774029, R² 0.999993745883712.
- Pontius (40 points, quadratic, with x values in the millions): b2 = −3.16081871345029 × 10⁻¹⁵ and R² 0.999999900178537.
Pontius is a classic trap: x² reaches about 9 × 10¹², and naive formulas in ordinary floating point lose most of their accuracy. The calculator centres x on its mean before solving and uses 50-digit decimal arithmetic, so it matches the certified values to more than 10 significant digits.
Covariance and Spearman's rank correlation
Below the fit, the calculator gives two more measures of how x and y move together. Covariance is the average product of the deviations, Σ(x − x̄)(y − ȳ), divided either by n − 1 (the sample covariance, what Excel's COVARIANCE.S and most statistics courses use) or by n (the population covariance, COVARIANCE.P). Both are shown rather than one picked silently. For the default eight points the sum of products is 83.9, so the sample covariance is 83.9 ÷ 7 = 11.9857 and the population covariance 83.9 ÷ 8 = 10.4875. Covariance carries the units of x times y, so its size depends on the scale; dividing it by both standard deviations gives Pearson's r, which is unit-free.
Spearman's rank correlation, ρ, asks a looser question: do larger x values come with larger y values, whatever the shape? Each x is replaced by its rank (1 for the smallest) and likewise each y, and ρ is Pearson's r of the ranks. Tied values share the average of the ranks they would occupy, the rule NIST's Dataplot and most software follow. The familiar shortcut ρ = 1 − 6Σd² ÷ n(n² − 1), where d is the difference between a point's two ranks, gives the same answer when there are no ties, so the calculator shows it alongside and says when ties make it only approximate. In the classic textbook example of ten people's IQ against weekly hours of television, Σd² = 194 and ρ = −29/165 ≈ −0.176: almost no association.
Linear or quadratic?
Start with a line, then look at the residuals in order of x. If they look like random scatter around zero, the line is adequate. If they form a smile or a frown (positive at both ends and negative in the middle, or the reverse), the data curve and a quadratic will usually fit better. Don't add curvature just to push R² up: a quadratic always fits at least as well as a line on the same data, so compare adjusted R² and check that b2 is large relative to its standard error.
Predicting with the equation
Enter an x in "Predict y at x" to evaluate the fitted equation there. Predictions inside the range of your data (interpolation) are usually reasonable. Outside it (extrapolation) the calculator adds a warning: the pattern may not continue, and a quadratic can bend away sharply.
Limits
This calculator fits ordinary least-squares lines and quadratics with one x variable. It doesn't fit higher-degree polynomials, exponential, logarithmic or power models, weighted regressions or multiple x variables, and it doesn't report confidence intervals, prediction intervals or p-values.
More questions
Does a strong correlation mean x causes y?
No. Regression measures how well one variable tracks another, not why. Ice-cream sales and drownings rise together because both increase in summer. Establishing cause needs a controlled experiment or a careful study design, not just a high R².
What is the difference between sample and population covariance?
Both add up (x − x̄)(y − ȳ) over your points; the sample covariance divides by n − 1 and the population covariance by n. Use the sample version (Excel COVARIANCE.S) when your points are a sample from something larger, which is almost always, and the population version (COVARIANCE.P) only when you have every member of the group. The calculator shows both, labelled.
When should I use Spearman's rank correlation instead of r?
When the relationship is monotonic but not a straight line, when the data are ranks or ordered categories, or when an outlier would dominate Pearson's r. Spearman's ρ replaces each value by its rank and correlates the ranks, so y = x² for positive x gives ρ = 1 even though r is below 1. Tied values share their average rank.
How many data points do I need?
At least one more than the number of coefficients to get a standard error: three for a line and four for a quadratic. In practice, use as many as you reasonably can. With few points a single unusual value can swing the slope, and R² can look impressive by chance.
What do the coefficient standard errors tell me?
How much each estimate would vary if you repeated the study with new data. A rough rule: if a coefficient is less than about twice its standard error, the data are consistent with that coefficient being zero. For a quadratic, that check on b2 tells you whether the curvature is real or noise.
Why does the quadratic always have a higher R² than the line?
Adding a term can never make the least-squares fit worse, so R² never falls when you go from a line to a quadratic, even if the extra curvature is just fitting noise. That is why the calculator also shows adjusted R², which penalises the extra coefficient: if adjusted R² barely moves, the line is probably enough.
What is an outlier's effect on the fit?
Because residuals are squared, one point far from the others pulls the line strongly toward itself, especially if its x value is also far from the rest (a high-leverage point). Check the residual table for any residual much larger than the others, and confirm the value is real before deciding whether to keep it.
Sources
- NIST Statistical Reference Datasets: Linear Least Squares Regression (Norris, Pontius)
- NIST/SEMATECH e-Handbook of Statistical Methods, section 4.4.3.1: Least Squares
- Introductory Statistics 2e, chapter 12: Linear Regression and Correlation, OpenStax (Rice University)
- NIST Dataplot Reference Manual: RANK CORRELATION (Spearman's rho, average ranks for ties)
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.