Skip to content

Linear Regression Calculator

Paste two columns to fit the straight line that best predicts y from x…

Paste two columns to fit the straight line that best predicts y from x, with the uncertainty in its slope, a residual plot and a prediction for any x.

Read 12 pairs; left out the header row (“Distance (km)”, “Fare”).

Type an x

More options95% confidence
Confidence level

ExampleLine of best fit

ŷ = 3.8427 + 1.674x

Each 1 more Distance (km) goes with 1.674 more Fare (95% CI 1.653 to 1.695). R² = 0.9997: the line accounts for 99.97% of the variation in Fare.

Paste your own two columns, or drag a point.

Distance (km)Fare

Drag any point and watch the line and R² change. With the plot focused, arrow keys move the highlighted point (the one furthest from the line) and [ ] pick another.

Residuals: what the line misses

0+0.2−0.2Distance (km)

No pattern here is good news for a straight line; a curve or a funnel means the line misses something.

R² (r)
0.9997 (0.9998)
Slope p-value
9.28 × 10⁻¹⁹
Residual SE
0.1089
How this works: 9 sources,

How this works

Method

Simple linear regression fits the straight line ŷ = a + bx that makes the squared vertical distances from the points to the line as small as possible. In the example, twelve taxi trips give fare = 3.84 + 1.674 × distance: about 1.67 per kilometre plus 3.84 to start, which is how most meters are set up. Full method

How it’s tested

2 worked examples for this page are checked by automated tests before every release: given the inputs, the tool must show the expected answer.

Privacy

On your device. Nothing you enter or open leaves this browser. About privacy labels

Changes

  • First published: live curves with draggable cut-offs, and tests on pasted data (t-tests, ANOVA, chi-square, Fisher’s exact, correlation, regression, A/B), checked against SciPy.

Every change on What’s new

How we make toolsReport a mistake

How least-squares regression works

Simple linear regression fits the straight line ŷ = a + bx that makes the squared vertical distances from the points to the line as small as possible. In the example, twelve taxi trips give fare = 3.84 + 1.674 × distance: about 1.67 per kilometre plus 3.84 to start, which is how most meters are set up.

The slope is Sxy ÷ Sxx, the cross-products of the deviations over the squared x deviations, and the line always passes through the point of means (x̄, ȳ). What the line misses are the residuals; their standard deviation, with n − 2 degrees of freedom, is the residual standard error s. It sets the standard errors of the slope (s ÷ √Sxx) and the intercept, and from those come their t tests and confidence intervals.

R² is the share of the variation in y the line explains. A high R² does not prove a straight line is right: check the residual plot, where the points should scatter evenly around zero. A curve in the residuals means a straight line is the wrong shape; a funnel means the spread grows with x. And predictions far outside the range of your x values are guesses, however good the fit.

b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² · a = ȳ − b x̄ · SE(b) = s ÷ √Σ(x − x̄)²

Predictions from the example’s line

The taxi example’s fitted fare at different distances, with the 95% confidence interval for the average fare at that distance and the 95% prediction interval for a single trip.

The taxi example’s fitted fare at different distances, with the 95% confidence interval for the average fare at that distance and the 95% prediction interval for a single trip.
Distance (km)Predicted fare95% CI (average)95% prediction interval
15.525.4 to 5.645.25 to 5.79
27.197.09 to 7.296.93 to 7.45
38.868.78 to 8.958.61 to 9.12
512.2112.14 to 12.2811.96 to 12.47
817.2317.15 to 17.3216.98 to 17.49
1020.5820.46 to 20.720.31 to 20.85
1223.9323.78 to 24.0823.64 to 24.22
1528.9528.74 to 29.1728.63 to 29.27
2037.3237.01 to 37.6436.92 to 37.72

Beyond 12.4 km the line is extrapolating past the trips it was fitted on.

Common questions

What is the difference between the confidence interval and the prediction interval?

The confidence interval is for the average y at a given x (the line itself); the prediction interval is for one new observation, so it adds the scatter of single points and is always wider.

Is a high R² enough to trust the line?

No. Look at the residual plot: an even band around zero is what a straight line should leave. A curve, a funnel or one point far off all mean the model needs a second look, whatever R² says.

What does the slope’s p-value test?

Whether the true slope could be zero, meaning y doesn’t change with x at all. It is the same p-value as the test of the correlation r.

Can I fit a curve here?

This page fits a straight line. For a curve such as exponential growth, transform first (fit y against log x, or log y against x) and paste the transformed columns.