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, shows the working table and every residual, and predicts y from x or x from y. Your numbers stay in your browser.

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.
  5. Scroll down for the working table and the residuals. 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.

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.

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.