Skip to content

Linear & Quadratic Regression Calculator

Fit a least-squares line or quadratic to x, y data: slope, intercept, r, R², standard error, covariance, Spearman rank correlation, residuals and predictions.

Calculator Details

Line of best fit:y = 0.0357143 + 1.99762xSee results ↓

One point per line: x and y separated by a comma, tab or space (paste two spreadsheet columns).

Linear: y = b0 + b1·x. Quadratic: y = b0 + b1·x + b2·x²

The fitted equation evaluated at this x

Your Results

Line of best fit: y = 0.0357143 + 1.99762x

Line of best fit

y = 0.0357143 + 1.99762x

R² = 0.998839 from 8 points

  • R²

    0.9988392866

  • Correlation r

    0.9994194748

  • Standard error (s)

    0.1801674706

  • Predicted y at x = 10

    20.01190476

    Outside the range of your x values (extrapolation)

Scatter plot and fit

1.13411.0220.9115.510x
Your 8 points (dots), the fitted linear line and the prediction at x = 10 (square).

Covariance and rank correlation

Covariance of x and y (sample and population) and Spearman's rank correlation
StatisticValueFormula
Sample covariance11.98571429Σ(x − x̄)(y − ȳ) ÷ (n − 1)
Population covariance10.4875Σ(x − x̄)(y − ȳ) ÷ n
Spearman's ρ1Pearson r of the ranks (ties: average rank)
ρ by 1 − 6Σd² ÷ n(n² − 1)1No ties: same as ρ

Coefficients

Coefficients with their standard errors
CoefficientEstimateStandard error
b0 (intercept)0.0357142860.14038536
b1 (slope)1.9976190.027800444

Residuals

Each point with its fitted value and residual
#xyŷy − ŷ
112.12.0333330.066667
223.94.030952-0.13095
336.26.0285710.17143
447.88.02619-0.22619
5510.110.023810.07619
6612.212.021430.17857
7713.814.01905-0.21905
8816.116.016670.083333
Sum of squared residuals (SSE)0.1947619

The least-squares linear fit to 8 points is y = 0.0357143 + 1.99762x (R² = 0.998839); at x = 10 it predicts y = 20.0119.

Compare scenarios

Save these inputs as a scenario, change them, and compare the results side by side.

Loading, available in a moment

How This Calculator Works

How it's calculated

Least squares chooses the coefficients that minimise SSE = Σ(y − ŷ)². For a line the solution is b1 = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and b0 = ȳ − b1·x̄; a quadratic solves the 3 × 3 normal equations (XᵀX)b = Xᵀy. Then R² = 1 − SSE/SST with SST = Σ(y − ȳ)², and the standard error is s = √(SSE / (n − p)).

With your numbers

n = 8, p = 2, x̄ = 4.5, ȳ = 9.025
y = 0.03571428571 + 1.997619048x
SSE = 0.1947619048, SST = 167.795
R² = 1 − 0.1947619048 / 167.795 = 0.9988392866
s = √(0.1947619048 / 6) = 0.1801674706
r = ±√R² = 0.9994194748
Sample covariance = Σ(x − x̄)(y − ȳ) ÷ (n − 1) = 11.98571429; population ÷ n = 10.4875
Spearman ρ = r(rank x, rank y) = 1
ŷ(10) = 20.01190476

Frequently Asked Questions

What does linear regression calculate?

It finds the straight line y = b0 + b1·x that best fits your points, in the sense that the sum of the squared vertical distances from the points to the line (the residuals) is as small as possible. b1, the slope, is the change in y for each one-unit increase in x; b0, the intercept, is the value of y when x = 0.

When should I use quadratic regression instead?

When the points follow a curve that bends once, such as a projectile’s height over time, costs that fall then rise, or a growth curve that is levelling off. Quadratic regression fits y = b0 + b1·x + b2·x². Look at the residual plot or table: if the linear residuals go from positive to negative and back in a U or ∩ pattern, a quadratic usually fits better.

What is R² and what is a good value?

R², the coefficient of determination, is the share of the variation in y that the model explains: 1 − SSE/SST. 1 means every point is on the line; 0 means the model does no better than the mean of y. What counts as good depends on the field: lab calibrations often exceed 0.99, while social-science data can be meaningful at 0.3. A high R² doesn’t prove that x causes y.

What is the correlation coefficient r?

For a straight-line fit, Pearson’s r is the square root of R², with the sign of the slope. It runs from −1 (perfect downward line) to +1 (perfect upward line). For the default data, r is very close to +1. The calculator shows r only for the linear model.

What is the standard error of the regression?

The residual standard deviation, s = √(SSE / (n − p)), where p is the number of coefficients (2 for a line, 3 for a quadratic). It is the typical size of a residual in the units of y. Excel calls it the standard error of y (STEYX or LINEST). The standard errors of the coefficients, shown in the details, measure how precisely each coefficient is estimated.

How accurate is this calculator?

It solves the least-squares equations in 50-digit decimal arithmetic after centring x on its mean, and reproduces the certified values of the NIST Statistical Reference Datasets for linear (Norris) and quadratic (Pontius) regression to at least 10 significant digits. Results are shown to 10 significant digits.

Can I use the equation to predict values outside my data?

You can, but be careful: that is extrapolation, and the relationship may not hold beyond the range you measured. A quadratic in particular can turn sharply outside the data. The calculator flags predictions outside the range of your x values.

How do I enter my data?

One point per line, x first then y, separated by a comma, a tab or spaces. You can paste two columns straight from a spreadsheet. Semicolons also separate points. A line needs exactly two numbers; don’t use thousands separators.

Ordinary least-squares fits of a line or quadratic, solved in 50-digit decimal arithmetic and checked against the NIST StRD certified values (Norris, Pontius). Not included: higher-degree polynomials, exponential or logarithmic models, weighted fits, confidence or prediction intervals and p-values (including significance tests for the covariance and Spearman's ρ). Covariance is shown both ways (sample ÷ n − 1, population ÷ n); Spearman's ρ uses average ranks for ties, with the 6Σd² shortcut beside it.

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

OutputMeaning
Slope b1Change in y for each 1-unit increase in x (linear model)
Intercept b0Predicted y when x = 0
b2Curvature: positive opens upward, negative downward
R²Share of the variation in y explained by the model, 0 to 1
rCorrelation, −1 to +1, the signed square root of R² (linear only)
Standard error sTypical size of a residual, in y's units: √(SSE ÷ (n − p))
Coefficient standard errorsPrecision 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).

  1. Means. x̄ = 15 ÷ 5 = 3, ȳ = 20 ÷ 5 = 4.
  2. Sums. Σ(x − x̄)(y − ȳ) = (−2)(−2) + (−1)(0) + 0(1) + 1(0) + 2(1) = 6, and Σ(x − x̄)² = 4 + 1 + 0 + 1 + 4 = 10.
  3. Slope and intercept. b1 = 6 ÷ 10 = 0.6; b0 = 4 − 0.6 × 3 = 2.2. The line is y = 2.2 + 0.6x.
  4. 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.
  5. R². SST = Σ(y − ȳ)² = 4 + 0 + 1 + 0 + 1 = 6, so R² = 1 − 2.4 ÷ 6 = 0.6, and r = √0.6 ≈ 0.775.
  6. 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

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.

Spotted a wrong result or a bug? Report a problem

May we count your visits with Invoker Analytics? It keeps a random visitor number in your browser. Your calculator inputs are never sent. Privacy policy