Black Friday: 50% off every paid plan with code BF50. Ends 5 December.

See prices

Free tool

Linear regression calculator, the line and the working.

Paste your x and y numbers, one pair on each line, or open a CSV file, and this linear regression calculator finds the best fit line by least squares: the slope, the intercept, the correlation r, R squared, the standard error and the equation. It draws a scatter chart with the line and an optional 95 percent band, shows the working table, the analysis of variance and every residual with a residual plot, and predicts y from x with confidence and prediction intervals, or x from y. Your numbers stay in your browser.

Need a custom tool, app or website? Contact us 

Examples

Predict

Show

The line is worked out in your browser. Your numbers are never uploaded. Numbers you type are kept in this browser for next time; opened files are not.

What you get

Points in, line and proof out.

01 / Fit

Least squares line

The line that makes the squared gaps between your y values and the line as small as they can be.

02 / Working

Every step shown

A table of deviations, squares and products, so you can check the answer or copy it into homework.

03 / Residuals

How far off each point is

The actual y, the y on the line, and the gap between them for every point.

Guide

How to do linear regression, with a worked example.

A linear regression calculator finds the straight line that best describes how y changes with x. It gives the line as an equation, y = mx + b, where m is the slope and b is the intercept. This one uses ordinary least squares, the standard method: the line that makes the sum of the squared vertical gaps between your points and the line as small as possible. It also reports r, R squared and the standard error, so you can tell how well a straight line fits your data.

How to use the linear regression calculator

  1. Put your numbers in the Data box, one pair on each line: the x value, then the y value, like 3 61. A tab, comma, semicolon or space between them all work, so you can paste straight from Excel, Google Sheets or Numbers.
  2. Or click Open a CSV file, or drop a file on the box. If the first row holds names, they label the chart axes.
  3. Read the cards: the equation, slope, intercept, correlation r, R squared and standard error. They update as you type.
  4. To predict, type an x under y for this x or a y under x for this y. The y prediction also shows a 95% interval for the mean y at that x and a wider one for a single new y.
  5. Under Band around the line, pick a 95% confidence band or a 95% prediction band to draw it on the chart.
  6. Scroll down for the working, the analysis of variance table, the residual plot and the residual table. A residual more than 2 standard errors from the line is shaded. Use Download CSV for a file with each point, its value on the line and its residual, Save chart PNG for the picture, or Copy link to share the data and settings.

The formulas

First find the mean of x and the mean of y. Then add up two sums over all points: Sxx is the sum of (x − mean x) squared, and Sxy is the sum of (x − mean x) times (y − mean y). The slope is m = Sxy ÷ Sxx. The intercept is b = mean y − m × mean x. The correlation is r = Sxy ÷ √(Sxx × Syy), where Syy is the sum of (y − mean y) squared, and R squared is r times r. The standard error of the estimate is s = √(sum of squared residuals ÷ (n − 2)).

A worked example you can check by hand

Click the 5 points example: x is 1, 2, 3, 4, 5 and y is 2, 4, 5, 4, 5.

xyx − 3y − 4(x − 3)²(y − 4)²Product
12−2−2444
24−10100
3501010
4410100
5521412
Sum1066

The means are 3 and 4. The slope is 6 ÷ 10 = 0.6. The intercept is 4 − 0.6 × 3 = 2.2, so the line is y = 0.6x + 2.2. The correlation is 6 ÷ √(10 × 6) = 0.7746, and R squared is 0.6. The line gives 2.8, 3.4, 4.0, 4.6 and 5.2, so the residuals are −0.8, 0.6, 1.0, −0.6 and −0.2. Their squares add up to 2.4, and with n − 2 = 3 the standard error is √(2.4 ÷ 3) = 0.8944. To predict y at x = 6, work out 0.6 × 6 + 2.2 = 5.8. To find x when y is 4.6, work out (4.6 − 2.2) ÷ 0.6 = 4.

Reading r, R squared and the p value

  • r runs from −1 to 1. Near 1, y rises with x along a straight line. Near −1, y falls as x rises. Near 0, there is no straight-line link.
  • R squared is the share of the changes in y that the line explains. An R squared of 0.6 means 60 percent.
  • Standard error is the typical size of a residual, in the units of y.
  • p value of the slope tests whether the true slope could be zero. With only five points, the example above has a p value of 0.124, so five points are too few to be sure.

Intervals, bands and the residual plot

The line is an estimate, so the page gives ranges for it. The 95% interval for the slope and the intercept are in the cards. For a predicted y at one x, there are two ranges. The confidence interval is for the mean y at that x: it is s × √(1/n + (x − mean x)² ÷ Sxx) either side of the line, times the t value for n − 2 degrees of freedom. The prediction interval is for one new point, so it adds 1 inside the square root and is always wider. Both are narrowest at the mean of x and widen toward the ends. The chart can draw either one as a band. For the 5 point example, the prediction at x = 6 is 5.8, with a mean interval of 2.815 to 8.785 and a one-point interval of 1.675 to 9.925 (checked against the matrix formula in numpy).

The analysis of variance table splits the total variation in y into the part the line explains (regression) and the part it leaves over (residual). F is the regression mean square divided by the residual mean square. With one x, F is the square of the slope's t value and its p value is the same as the slope's. The residual plot shows each point's gap from the line against x. A good straight-line fit leaves a shapeless cloud around zero. A curve, a fan or one lonely dot means a straight line may be the wrong choice.

Things to watch

  • A high R squared does not show that x causes y.
  • One far-off point can tilt the whole line. Look at the chart and the residuals.
  • A prediction outside your x values is a guess. The page tells you when you do that.
  • If all x values are equal the line is vertical and has no slope. If all y values are equal the slope is 0 and r is undefined.
  • This is a straight line only. For a curve, try the scatter plot maker, which can fit a curved trend line.

For the spread of a single list of numbers, use the standard deviation calculator or the average calculator.

Questions

Good to know.

How do you calculate linear regression?

Find the mean of x and y, then the slope m = Sxy ÷ Sxx and the intercept b = mean y − m × mean x. The guide above works through a five-point example with every sum shown.

What is the linear regression equation?

y = mx + b, where m is the slope and b is the intercept. The calculator writes it with your numbers, such as y = 0.6x + 2.2.

What does R squared mean in linear regression?

It is the share of the changes in y that the line explains, from 0 to 1. An R squared of 0.97 means the line explains 97 percent.

What is a good R squared value?

It depends on the field. Careful lab work can give values near 1, while data about people is often much lower. Look at the chart and the residuals as well as the number.

How do I find the correlation coefficient r?

r = Sxy ÷ √(Sxx × Syy). It is shown in the cards. Its sign is the sign of the slope, and its square is R squared.

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

The confidence interval is a range for the average y at an x. The prediction interval is a range for one new y at that x, so it is wider. Type an x under y for this x to see both.

How do I read the residual plot?

Each dot is a point's gap from the line. A straight line fits well when the dots look random around zero. A curved pattern or a widening fan suggests the line is not the right model.

How do I predict y from x?

Type the x under y for this x. The page puts it into y = mx + b. It also works the other way, from y to x, and warns you when the value is outside your data.

What is a residual?

The gap between a real y value and the y value on the line. Residuals add up to zero, and the least squares line is the one with the smallest sum of their squares.

How many points do I need?

Two points give a line but no error estimate. Three or more give the standard error, the p value and the 95 percent intervals. More points make the line more trustworthy.

Can I paste data from Excel or open a CSV file?

Yes. Paste two columns from a spreadsheet, or click Open a CSV file. The file is read in your browser and not uploaded.

Listed on

Listed on JustMacAppsMotionShine on Mac App IndexMotionShine on ScrollLaunchMotionShine featured on StartupBaseFeatured on FazierFeatured on Twelve ToolsMotionShine - Featured on Smol Launch