Linear Regression

Fit a least squares line to your X and Y pairs, then look at the residual plot before you trust the R² above it. A straight line fits almost any data. The residuals tell you whether it should.

Regression calculator

0 usable pairs
Try a dataset

No fit yet

Enter at least two pairs with different X values, then press Fit the line. Sample datasets above load instantly if you want to see the output first.

What the least squares line is doing to your data

Every straight line drawn through a scatter of points misses most of them. Least squares picks the one line where the squared vertical misses add up to the smallest total. Nothing about the method asks whether a line was the right shape in the first place.

Two numbers come out of the fit. The slope says how much Y moves for a one unit move in X. The intercept says what the line predicts when X sits at zero. A slope of 4.2 on ad spend and revenue reads as four dollars and twenty cents of revenue per extra dollar spent, inside the spending range you actually observed.

Squaring the misses is the part with consequences. A point sitting ten units off the line pulls a hundred times harder than a point sitting one unit off. One mistyped row drags the whole line toward itself, and the fit still returns a clean equation with no complaint. Load the One bad outlier sample above to watch a good fit collapse from a single point.

Why the intercept is often meaningless

The intercept is the value of the line at X equals zero, and X equals zero is frequently nowhere near your data. Fit height against age using adults aged 20 to 60 and the intercept describes a newborn, which the model never saw. Read the intercept as an anchor that positions the line, not as a prediction, unless zero falls inside your observed X range.

R² is a fit score, not a proof

R² reports the share of variation in Y the line accounts for. An R² of 0.87 means the line explains 87 percent of how Y spreads out, leaving 13 percent to everything else.

High values feel reassuring and mislead in three specific ways.

  • A curve returns high R². Data bending along a smooth arc still fits a line at R² above 0.9. The residual plot shows the arc immediately. The number hides it.
  • Few points inflate it. Three points give R² close to 1 by arithmetic alone. Two points give exactly 1, because a line through two points is perfect by definition.
  • Direction stays unknown. Ice cream sales and drowning deaths correlate at high R². Temperature drives both. Regression measures association and has no way to tell you which variable moved first.

Reading an R² value

  • 0.90 to 1.00 Tight fit. Check for a curve or a duplicated column before celebrating.
  • 0.70 to 0.89 Strong association. Normal for clean physical or engineering measurements.
  • 0.40 to 0.69 Real but partial. Common in business and marketing data.
  • 0.10 to 0.39 Weak. Typical in social science, where a small effect still matters.
  • Under 0.10 The line adds close to nothing over the mean of Y.

Thresholds shift by field. A 0.30 in psychology research and a 0.30 in a calibration lab mean opposite things about the quality of the work.

Read the residual plot before you read anything else

Each vertical stub in the scatter above is one residual, the distance from an observed point down or up to the line. Their pattern is the honest diagnostic, and four shapes cover nearly everything you will see.

Scattered with no pattern

Residuals fall above and below the line at random across the X range. A line was the right model. Read the slope and move on.

A U shape or an arch

Residuals sit above the line at both ends and below in the middle, or the reverse. The relationship bends. Fit a curve, or log-transform one variable and refit.

A widening fan

Small misses at low X, large misses at high X. Variance grows with the level, which happens constantly with money and counts. The slope stays usable, the standard errors do not.

One enormous stub

A single point far off while the rest sit close. Go back to the source row. Data entry beats statistics as an explanation most of the time.

The four sample datasets on the calculator produce these four shapes in order, so you get a feel for the difference between a fit that works and a fit that only scores well.

A worked example, start to finish

A shop records monthly ad spend in thousands and revenue in thousands for six months.

MonthAd spend (X)Revenue (Y)
January218
February323
March530
April636
May841
June948

The fit returns ŷ = 4.08x + 10.2267 with R² of 0.9888. Each extra thousand in ad spend lines up with about 4,080 in revenue across the range from 2 to 9.

Now the part the numbers do not cover. The intercept near 10.23 suggests about 10,230 in revenue at zero ad spend, which is plausible for a shop with existing customers, so the intercept survives a sanity check. Predicting at X equals 40 does not survive one. No month in the record spent above 9, the line has no evidence about what happens at four times the maximum, and returns diminish in real ad markets. The prediction box flags any X outside the observed range for exactly that reason.

Getting your own data in

  1. Copy two columns from a spreadsheet and paste them into the box. Tabs are parsed as separators, so no reformatting is needed.
  2. Check the pair count under the box. Rows with text, blanks, or a missing second value are skipped, and a shortfall shows up here first.
  3. Press Fit the line and read the residual plot before the R².
  4. Open the residual table to find which specific rows the line misses worst.
  5. Enter an X in the prediction box. Stay inside the observed range or accept the extrapolation warning.

Where this calculator stops

Know these limits before a result goes into a report.

  • One predictor only. This fits Y against a single X. Sales driven by ad spend, price, and season together need multiple regression, which no browser tool of this shape handles.
  • Straight lines only. Exponential growth, saturation curves, and seasonal cycles all need a different model. Log-transform one variable and refit if the residuals arch.
  • No p-value on the slope. The t statistic is reported, and turning that into a significance level needs the t distribution with n minus 2 degrees of freedom. Take the t value to the P-Value Calculator to finish the test.
  • Vertical distances only. Least squares assumes X is measured without error. When both variables carry measurement error, such as two instruments compared against each other, ordinary regression underestimates the slope.
  • No time series handling. Consecutive observations from the same process are usually correlated with each other, which breaks the independence the standard errors assume. Fitting a line to a stock price gives a slope and no useful uncertainty around it.
  • Precision limits. Numbers are held as double precision floats. Values spanning more than about 15 digits of precision lose accuracy in the sums of squares.
The one that costs the most: a slope is not a causal effect. Regression finds association in the data you supplied. Establishing that X moves Y takes an experiment, a control group, or a design that rules out the other explanations. Plenty of decisions have been justified by a slope that reversed sign once an omitted variable was added.

Regression questions people actually ask

Slope reading, R² thresholds, outliers, sample size, and what regression will not tell you.

What counts as a good R² value?

It depends entirely on the field. Physics and calibration work routinely see 0.95 and above, because the underlying relationship is close to deterministic. Marketing data at 0.60 is a strong result. Human behaviour research treats 0.20 as meaningful. Compare your R² against typical values in your own domain rather than against a universal threshold, and always look at the residual plot alongside it.

How many data points do I need?

The math runs from two points, but two points give a perfect fit and zero information. Ten pairs is a reasonable floor for a slope you would report, and thirty starts giving stable standard errors. Below ten, a single unusual observation controls the result, so check the residual table for one point dominating the fit.

Why does one point change my line so much?

Least squares minimises squared vertical distances, so a point twice as far off pulls four times as hard. Points at the extreme ends of the X range pull harder still, because the line pivots around the middle of the data. A far point at high X swings the slope more than the same distance at the centre would.

What is the difference between r and R²?

The correlation coefficient r runs from -1 to +1 and carries the direction of the relationship. R² is r squared, runs from 0 to 1, and drops the sign in exchange for a clean interpretation as the share of variance explained. An r of -0.9 and an r of +0.9 both produce R² of 0.81, describing equally tight fits sloping opposite ways.

Can I predict outside my X range?

The calculator will compute the value and mark it as extrapolation. Whether the number means anything depends on knowledge the data does not contain. A linear relationship holding between X values of 2 and 9 says nothing about behaviour at 40, and most real relationships bend or saturate eventually. Treat any extrapolated prediction as a hypothesis rather than a result.

My residuals form a curve. What should I do?

A curved residual pattern means a line is the wrong shape. Try taking the log of Y and refitting, which straightens exponential growth. Log both variables for power relationships. Where the pattern is a genuine arch with a peak or trough, a quadratic term is needed and a straight line will never describe it.

Does the tool handle X and Y in the wrong order?

Swapping the columns gives a different line. Regressing Y on X minimises vertical distances, and regressing X on Y minimises horizontal ones, so the two slopes are not reciprocals of each other. Put the variable you are predicting in the Y column. The correlation r stays identical either way.

Is any data sent to a server?

No. Parsing, fitting, and plotting all run in your browser with JavaScript. Nothing is uploaded and nothing is stored, so closing the tab discards the dataset. Copy the equation or the residual table before you leave if you need a record.