🏆 US-Registered Digital Marketing Agency
Advertisement
Advertisement
EDUCATION

Polynomial Regression Calculator — fit, R² and overfitting check

Fit a polynomial of any degree to paired data by least squares, get the coefficients and R², and see plainly when the degree has gone too high.

Commas, tabs or spaces all separate the pair. Lines that do not hold two numbers are skipped.
Fitted coefficients are rarely known to more precision than this, however many digits the arithmetic produces.
Fitted polynomial
 
0
0
Adjusted R²
0
RMSE
0
Prediction at your x
Tip: R² never falls when you raise the degree, so a rising R² is not evidence of a better model. Watch the adjusted R² instead — it can and does fall, which is the point of it.
Advertisement

A polynomial regression calculator finds the polynomial of a chosen degree that comes closest to a set of (x, y) points, in the least-squares sense of making the sum of the squared vertical gaps as small as possible. It is the natural next step when a straight line clearly does not describe your data but the relationship is still smooth and single-valued.

Arb Digital built this page with the overfitting problem in the foreground rather than the footnotes. Raising the degree always improves the fit as measured by R², right up to the point where the curve passes exactly through every data point and R² hits 1. That perfect fit is worthless, and this tool says so out loud rather than presenting it as a success.

What This Calculator Does

Paste your paired data, choose a degree, and the tool solves the least-squares problem and returns the fitted polynomial's coefficients, R², adjusted R², the root mean squared error, and a prediction at any x you name. It plots the points with the fitted curve over them, so you can see whether the shape is describing the data or chasing it.

Degree 1 is ordinary straight-line regression, and it will agree exactly with our linear regression calculator — that page is the right one if a line is all you need, because it also gives the slope's interpretation and the correlation coefficient. If your data curves because it is growing by a constant percentage rather than a constant amount, the exponential regression calculator is the correct model instead, and forcing a polynomial onto exponential data produces a fit that looks reasonable in the middle of the range and fails badly at both ends.

This is also distinct from our polynomial arithmetic calculator, which manipulates a polynomial you already have. Here the coefficients are the output rather than the input. Once you have them, the polynomial graph plotter will show you the fitted curve's roots and turning points.

How to Use It

  1. Paste the pairs. One per line as "x, y". A column pair copied from a spreadsheet pastes cleanly.
  2. Start at degree 2. If a line is not enough, a quadratic usually is. Go higher only when the residuals show a pattern the quadratic missed.
  3. Compare adjusted R² as you step the degree. The two buttons move it one at a time so you can watch the two figures diverge.
  4. Read the warning line. The tool tells you when the degree is high relative to how many points you have, which is the condition under which a fit stops meaning anything.
  5. Keep predictions inside the data range. The prediction field will happily extrapolate, and a polynomial extrapolates worse than almost any other model.

The Method: How the Fit Is Calculated

Least squares chooses the coefficients that minimise the sum of squared residuals, Σ(yi − ŷi)². Setting each partial derivative to zero gives a square system called the normal equations, which for degree d has d + 1 unknowns and can be solved directly. Despite the curved output, this is a linear regression problem, because the model is linear in its coefficients even though it is not linear in x — the distinction is explained in the NIST/SEMATECH e-Handbook's section on linear least squares regression, which uses a quadratic as its example of a model that is still linear in the statistical sense.

The normal equations for a polynomial are notoriously ill-conditioned, because the columns 1, x, x², x³ become nearly indistinguishable when the x values are all large or all clustered. This tool therefore centres and scales x before fitting — solving in the variable u = (x − x̄) ÷ s — and then expands the result back into ordinary powers of x. The answer is the same fit, computed far more accurately.

R² is then 1 − SSres ÷ SStot, where SSres is the sum of squared residuals and SStot is the sum of squared deviations of y from its own mean. It is the proportion of the variance in y that the fitted curve accounts for. Adjusted R² applies a penalty for the number of coefficients used: 1 − (1 − R²)(n − 1) ÷ (n − d − 1). Our R-squared calculator covers that statistic on its own.

Advertisement

Overfitting: Why Degree 9 on Ten Points Is a Trap

Here is the fact that makes polynomial regression dangerous. A polynomial of degree n − 1 can be made to pass exactly through any n points with distinct x values. Ten points, degree 9: the residuals are all zero, R² is exactly 1, and the fit is perfect. It is also completely worthless.

The reason is that the model has used up every degree of freedom in the data. With ten points and ten coefficients there is nothing left over to test the model against — you have not summarised the data, you have re-encoded it in a different and less useful format. Change any single y value by a small amount and the entire set of coefficients lurches, because the curve is obliged to chase it.

Between the data points, a high-degree fit oscillates wildly, a phenomenon known as Runge's phenomenon after the classic example of fitting high-degree polynomials to evenly spaced samples of a smooth function: the fit gets worse near the edges as the degree rises, not better. Outside the data range it is worse still. A degree-9 polynomial fitted to ten points and evaluated one step beyond the last one can return a value orders of magnitude away from anything plausible.

Three habits protect you. Keep the degree low — 2 or 3 covers the overwhelming majority of genuinely curved relationships. Insist on many more points than coefficients; a common rule of thumb is at least ten observations per coefficient. And judge the fit by adjusted R² or by performance on data the fit has not seen, never by R² alone.

Why R² Cannot Choose the Degree for You

R² is monotonically non-decreasing in the number of coefficients. Adding a term can never make the least-squares fit worse, because the optimiser can always set the new coefficient to zero and recover the previous fit. So R² rising when you raise the degree tells you nothing whatsoever — it is guaranteed by the arithmetic, not earned by the model.

Adjusted R² fixes this by penalising each extra coefficient, and it genuinely can fall. When it does, the extra term has bought less explanatory power than it cost in degrees of freedom, and the simpler model is the better one. Step the degree up from 1 with the buttons above and watch for the point where R² keeps creeping up while adjusted R² turns over. That turning point is the honest answer to "what degree should I use?".

The residuals carry the other half of the answer, and they are worth plotting separately. If the residuals from a straight-line fit show a clear arch — negative at both ends and positive in the middle, or the reverse — a quadratic term is genuinely warranted. If they look like unstructured scatter, adding terms will only fit noise. Our correlation coefficient calculator and descriptive statistics calculator help with that diagnostic work.

When a Polynomial Is the Wrong Model Entirely

Polynomials are flexible, and that flexibility makes them easy to misuse. Three situations call for something else.

If the underlying process has a known functional form, use that form. Radioactive decay, compound growth and cooling curves are exponential; enzyme kinetics and market saturation are asymptotic. A polynomial can approximate any of these over a narrow range, but it has no asymptote and must eventually turn and run off to infinity, so it will always fail outside the fitted window. The e-Handbook's section on nonlinear least squares regression covers fitting those genuinely nonlinear forms.

If you need to interpolate smoothly through many points, splines are the correct tool. A cubic spline uses low-degree pieces joined smoothly, which gives local flexibility without the global oscillation that ruins a single high-degree polynomial.

And if you intend to extrapolate at all, be extremely cautious. A quadratic fitted to rising data will always predict acceleration forever, and a cubic will eventually predict a reversal. Neither behaviour comes from your data; both come from the algebra of the model you chose.

Need more free statistics tools?

Arb Digital publishes hundreds of free calculators covering regression, statistics, algebra and finance — all free to use, with no sign-up and no limit on how often you run them.

Browse All Free Tools Suggest a Tool

Common Mistakes to Avoid

  • Choosing the degree by R². It can only rise as the degree rises, so it cannot distinguish a better model from a bigger one.
  • Fitting degree n − 1 to n points. The fit is exact and meaningless: every degree of freedom has been consumed by the model.
  • Extrapolating beyond the data. Polynomials have no asymptotes and diverge quickly, so predictions outside the fitted range are unreliable by construction.
  • Using a polynomial for exponential data. Constant-percentage growth needs an exponential model; a polynomial will fit the middle and miss both ends.
  • Reporting more digits than the fit supports. Coefficients from noisy data are not known to eight significant figures, whatever the arithmetic prints.

Related Free Tools From Arb Digital

Use the linear regression calculator when a straight line is enough, the exponential regression calculator for constant-percentage growth, the R-squared calculator to interpret the fit statistic on its own, the polynomial graph plotter to examine the fitted curve, and the polynomial arithmetic calculator to work with the coefficients afterwards. More live in the free tools hub.

Frequently Asked Questions

What degree should I use for polynomial regression?

Start at two and raise it only while the adjusted R square keeps improving. Degrees of two or three describe the overwhelming majority of genuinely curved relationships. High degrees fit noise rather than signal and behave badly between and beyond the data points.

Why does R squared always increase with degree?

Because adding a coefficient can never make a least-squares fit worse: the optimiser can always set the new coefficient to zero and recover the previous fit. A rising R square is therefore guaranteed by the arithmetic rather than earned by the model, which is why it cannot be used to choose the degree.

What is adjusted R squared for?

It applies a penalty for each coefficient used, so it can fall when an added term buys less explanatory power than it costs in degrees of freedom. Watching the point at which it turns over while plain R square keeps rising is the practical way to choose a degree.

What happens if I fit degree nine to ten points?

The curve passes exactly through every point, every residual is zero and R square equals one. That perfect fit has consumed every degree of freedom in the data, so nothing is left to test the model against, and the curve oscillates wildly between the points.

Is polynomial regression a linear model?

Yes, in the statistical sense. The model is linear in its coefficients even though it is not linear in x, so it is solved by ordinary linear least squares. Nonlinear regression means a model whose parameters appear nonlinearly, which requires iterative fitting instead.

What is Runge's phenomenon?

It is the tendency of a high-degree polynomial fitted to evenly spaced points to oscillate violently near the ends of the range, with the oscillation getting worse as the degree rises. It is the main reason splines are preferred to single high-degree polynomials for interpolation.

Can I extrapolate from a polynomial fit?

Only with great caution. Polynomials have no asymptotes and diverge rapidly outside the fitted range, so a quadratic predicts acceleration forever and a cubic eventually predicts a reversal. That behaviour comes from the algebra of the model, not from your data.

How many data points do I need?

At an absolute minimum one more than the degree, or the fit is not determined. A useful working rule is around ten observations per coefficient, so a quadratic with its three coefficients would want roughly thirty points before its estimates are stable.

This calculator is provided for study and for exploring your own data. It describes how each statistic is computed and does not recommend which model to publish in any particular analysis.

Advertisement
Advertisement

Take it further