Skip to main content
ponderloop

Linear Regression Calculator

For the example data (hours studied against exam score, 8 points) the line of best fit is y ≈ 47.357 + 4.1429x: each extra hour goes with about 4.1429 more points. The correlation is r ≈ 0.99236 and r² ≈ 0.98478, so a straight line explains almost all of the variation in the scores. Paste your own x, y pairs (one pair per line, or x and y as two lists, up to 500 points) to get the slope, intercept, r, r², the standard error of the estimate, a prediction for any x (or the x for a given y), a residual table and a scatter plot with the fitted line.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

One pair per line, x then y, separated by a comma, space or tab (a pasted spreadsheet range works). Up to 500 points. A first line of column names is skipped. Or put the x values here and the y values in the second box.

Up to 500 points.

Line of best fit (least squares): y = a + bx

y ≈ 47.357 + 4.1429x

r ≈ 0.99236, r² ≈ 0.98478, from 8 points.

  • At x = 5.5, the predicted y is ≈ 70.143.
  • For y = 70, the x on the fitted line is ≈ 5.4655.
Equation
y ≈ 47.357 + 4.1429x
Slope (b)
≈ 4.1429
Intercept (a)
≈ 47.357
Correlation coefficient (r)
≈ 0.99236
Coefficient of determination (r²)
≈ 0.98478
Standard error of the estimate (s)
≈ 1.3628
Residual sum of squares
≈ 11.143
Number of points (n)
8
Mean of x (x̄)
4.5
Mean of y (ȳ)
66
Sxx = Σ(x − x̄)²
42
Sxy = Σ(x − x̄)(y − ȳ)
174
Syy = Σ(y − ȳ)²
732
Σx
36
Σy
528
Σx²
204
Σy²
35,580
Σxy
2,550

Scatter plot and fitted line

Scatter plot of the data with the fitted lineScatter plot of the data with the fitted line: 8 points, x from 1 to 8; the line is y ≈ 47.357 + 4.1429x. The table below lists the points.506070809002468xy
Each dot is one point; the line is the least-squares fit. The table under the plot gives the same numbers.

Residual table

A residual is the actual y minus the fitted y (ŷ): positive when the point is above the line. They add up to zero.

Each point, the y the line predicts for its x, and the residual
#xyFitted y (ŷ)Residual
115251.50.5
2255≈ 55.643≈ −0.64286
3361≈ 59.786≈ 1.2143
4464≈ 63.929≈ 0.071429
5566≈ 68.071≈ −2.0714
6673≈ 72.214≈ 0.78571
7775≈ 76.357≈ −1.3571
888280.51.5

Anscombe’s quartet: four data sets, almost the same line

Each row is the calculator’s own answer for one of Anscombe’s four data sets (11 points each), shown to 5 significant digits.
Data setPointsMean of xMean of ySlopeInterceptrr²Standard error
I119≈ 7.5009≈ 0.50009≈ 3.0001≈ 0.81642≈ 0.66654≈ 1.2366
II119≈ 7.50090.5≈ 3.0009≈ 0.81624≈ 0.66624≈ 1.2372
III1197.5≈ 0.49973≈ 3.0025≈ 0.81629≈ 0.66632≈ 1.2363
IV119≈ 7.5009≈ 0.49991≈ 3.0017≈ 0.81652≈ 0.66671≈ 1.2357

Anscombe’s quartet: the data

The 11 points of each set. Sets I, II and III share their x values; set IV has its own.
Pointx (sets I to III)y, set Iy, set IIy, set IIIx, set IVy, set IV
1108.049.147.4686.58
286.958.146.7785.76
3137.588.7412.7487.71
498.818.777.1188.84
5118.339.267.8188.47
6149.968.18.8487.04
767.246.136.0885.25
844.263.15.391912.5
91210.849.138.1585.56
1074.827.266.4287.91
1155.684.745.7386.89

What each set looks like, and why the numbers alone mislead

The four sets give nearly the same line and r, yet only set I is a straight-line pattern.
Data setShape of the scatter plotLiner
IA rough straight-line trend: the textbook casey ≈ 3.0001 + 0.50009x≈ 0.81642
IIA smooth curve: a straight line is the wrong modely ≈ 3.0009 + 0.5x≈ 0.81624
IIIA tight line with one outlier pulling the fity ≈ 3.0025 + 0.49973x≈ 0.81629
IVEvery x is 8 except one, which alone decides the slopey ≈ 3.0017 + 0.49991x≈ 0.81652

What r and r² look like

Small data sets run through the calculator, from a perfect fit to no straight-line pattern at all.
PatternData (x, y)Linerr²
Perfect rise1, 2; 2, 4; 3, 6; 4, 8y = 2x11
Steady fall1, 20; 2, 18.5; 3, 17; 4, 14.5; 5, 13; 6, 11y ≈ 22.067 − 1.8286x≈ −0.99728≈ 0.99458
Scattered, slight rise1, 4; 2, 1; 3, 5; 4, 2; 5, 6; 6, 3y = 2.8 + 0.2x0.20.04
A curve, y = x² for x from 1 to 51, 1; 2, 4; 3, 9; 4, 16; 5, 25y = −7 + 6x≈ 0.98110≈ 0.96257
A symmetric curve, y = x² for x from −3 to 3-3, 9; -2, 4; -1, 1; 0, 0; 1, 1; 2, 4; 3, 9y = 400
y never changes1, 5; 2, 5; 3, 5y = 5Undefined (all y values are equal)Undefined (all y values are equal)

Worked examples

Seven short problems, each answered by the calculator on the data shown (x, y pairs separated by semicolons).
ProblemDataLine of best fitrAnswer
Hours studied and exam score (the example above). Predicted score after 5.5 hours?1, 52; 2, 55; 3, 61; 4, 64; 5, 66; 6, 73; 7, 75; 8, 82y ≈ 47.357 + 4.1429x≈ 0.99236At x = 5.5, the predicted y is ≈ 70.143.
The same data, turned around. Hours needed for a score of 70?1, 52; 2, 55; 3, 61; 4, 64; 5, 66; 6, 73; 7, 75; 8, 82y ≈ 47.357 + 4.1429x≈ 0.99236For y = 70, the x on the fitted line is ≈ 5.4655.
Price against age of a used car, in $1,000s. Predicted price at 4.5 years?1, 20; 2, 18.5; 3, 17; 4, 14.5; 5, 13; 6, 11y ≈ 22.067 − 1.8286x≈ −0.99728At x = 4.5, the predicted y is ≈ 13.838.
A perfect straight line. Predicted y at x = 10?1, 2; 2, 4; 3, 6; 4, 8y = 2x1At x = 10, the predicted y is 20. This x is outside the range of your data, so it is an extrapolation.
Only two points. Predicted y at x = 3?1, 3; 5, 11y = 1 + 2x1At x = 3, the predicted y is 7.
y never changes. Predicted y at x = 10?1, 5; 2, 5; 3, 5y = 5Undefined (all y values are equal)At x = 10, the predicted y is 5. This x is outside the range of your data, so it is an extrapolation.
Every x is the same. Predicted y at x = 3?3, 1; 3, 4; 3, 9None (vertical line)Undefined (all x values are equal)At x = 3 there is no predicted y: all x values are equal, so the fitted line is vertical.

How to find a line of best fit by hand

  1. Find the means. Add up the x values and the y values and divide each sum by the number of points. For the example, Σx = 36 and Σy = 528, so x̄ = 4.5 and ȳ = 66 with n = 8.
  2. Add up the spreads. Sxx = Σx² − (Σx)² ÷ n and Sxy = Σxy − Σx × Σy ÷ n. For the example Sxx = 42 and Sxy = 174. (Syy = 732, used for r.)
  3. Divide for the slope. The slope is b = Sxy ÷ Sxx = 174 ÷ 42 ≈ 4.1429.
  4. Find the intercept. The line passes through the point of means, so a = ȳ − b × x̄ = 66 − 4.1429 × 4.5 ≈ 47.357. The equation is y ≈ 47.357 + 4.1429x.
  5. Predict and check. Put an x into the equation to predict y. At x = 5.5, the predicted y is ≈ 70.143. To check the fit, find r = Sxy ÷ √(Sxx × Syy) ≈ 0.99236: close to 1 means the points hug the line.

Frequently asked questions

What is a line of best fit?
It is the straight line y = a + bx that passes as close as possible to a set of points, where “close” means the smallest possible sum of squared vertical distances (the least-squares line). For the example data of 8 points it is y ≈ 47.357 + 4.1429x. It is also called the regression line or trend line.
How do you calculate the slope and intercept?
Slope b = Sxy ÷ Sxx, where Sxx is the sum of squared x deviations from the mean and Sxy is the sum of the products of the x and y deviations. Intercept a = ȳ − b × x̄. For the example, Sxy = 174 and Sxx = 42, so b ≈ 4.1429 and a ≈ 47.357. The calculator does this with exact fractions and rounds only the result you see.
What do r and r² mean?
The correlation coefficient r runs from −1 (a perfect downhill line) through 0 (no straight-line relationship) to +1 (a perfect uphill line); its sign is the sign of the slope. The coefficient of determination r² is r squared: the share of the variation in y that the line accounts for. For the example r ≈ 0.99236 and r² ≈ 0.98478. A perfect line gives r = 1, and the falling example in the table above gives r ≈ −0.99728.
How do I predict y for a given x?
Type the x in “Predict y for x =”. The calculator puts it into y = a + bx using the exact slope and intercept, not the rounded ones shown, so the prediction is not thrown off by rounding. At x = 5.5, the predicted y is ≈ 70.143. It warns when the x is outside the range of your data, because a line fitted to hours 1 to 8 says nothing reliable about hour 80.
How do I find the x for a given y?
Type the y in “Find x for y =”. The calculator solves y = a + bx for x, which is (y − a) ÷ b. For y = 70, the x on the fitted line is ≈ 5.4655. Note that this inverts the line fitted for y on x; it is not the same as fitting x on y, which minimises distances in the other direction and gives a slightly different line unless r is ±1. A horizontal line (slope 0) has no single x for a given y.
What is the standard error of the estimate?
It is s = √(SSE ÷ (n − 2)), where SSE is the sum of the squared residuals, and it measures the typical distance of the points from the line in the units of y. For the example it is ≈ 1.3628, with SSE ≈ 11.143. It needs at least 3 points, because a line through 2 points fits them exactly and leaves no degrees of freedom (n − 2 = 0); with 2 points the calculator says so instead of showing a number.
What is a residual?
A residual is the actual y minus the y the line predicts for that x. In the example the first point (x = 1, y = 52) has a fitted y of 51.5 and a residual of 0.5. Residuals of a least-squares line with an intercept always add up to zero. The table lists the first 50 points; look for patterns in it: a curve or a funnel shape means a straight line is not the right model.
Why is the slope undefined when all the x values are equal?
Then Sxx = 0, and the slope Sxy ÷ Sxx would divide by zero: the points lie on a vertical line, which is not a function y = a + bx. For 3, 1; 3, 4; 3, 9 the calculator reports slope undefined: all x values are equal, still plots the points, and shows the sums and means. Change at least one x value to get a line.
What if all the y values are equal?
The best-fit line is horizontal, y = that constant (y = 5 for 1, 5; 2, 5; 3, 5), the slope is 0 and the fit is perfect. But r is undefined, because it divides by the spread of y, which is zero: the calculator shows “Undefined (all y values are equal)” rather than a made-up number.
Why do four different data sets give the same line (Anscombe’s quartet)?
Because the line, r and the means depend only on a few sums. Anscombe built four sets of 11 points with almost the same sums: each gives a slope of about 0.50009, an intercept of about 3.0001 and r of about 0.81642, yet one is a clean trend, one a curve, one has an outlier and one hangs on a single point. Always look at the scatter plot and the residuals; the tables above give all four sets so you can paste them in.
Does a high r² mean x causes y?
No. r² only says how closely the points follow a straight line. Two things can move together because one causes the other, because a third factor drives both, or by coincidence. A high r² also does not mean a line is the right model: y = x² for x from 1 to 5 has r² ≈ 0.96257, yet is a curve, and for x from −3 to 3 the same curve gives r = 0.
How do I enter the data, and what happens with mistakes?
Put one pair per line, with x and y separated by a comma, a space, a tab or a semicolon, so a copied spreadsheet range works. A first line of column names is skipped. Or put the x values in the first box and the y values in the second. Up to 500 points are used; more are refused rather than cut, so the result always covers everything you pasted. A line that is not two numbers is reported with its line number, such as “Line 3: “abc” is not a number”. Exponent notation (1e3) is not read; write the number in full.
Is the result exact, and what do the significant digits do?
Every number you type is an exact decimal and the sums, means, slope, intercept, r² and the residual sum of squares are computed as exact fractions, so there is no rounding drift. Only r and the standard error need a square root, found to 30 decimals. The “Significant digits” menu (3 to 10) rounds what is displayed, half away from zero; a value that is not exact is marked ≈ so that you can tell. The sums are shown in full whenever they end within 12 decimals.
Does it give a p-value or confidence interval for the slope?
No. This calculator fits the line and measures how well it fits (r, r², the standard error of the estimate); it does not test whether the slope differs from zero or build intervals around the line, which also need assumptions about the errors.

How it works

Each x and y is read as an exact decimal (up to 30 digits) and all arithmetic uses exact fractions of whole numbers. With n points: Sxx = Σx² − (Σx)² ÷ n, Sxy = Σxy − Σx × Σy ÷ n and Syy = Σy² − (Σy)² ÷ n; these exact forms avoid the cancellation error that makes the textbook formulas unreliable in floating point. The least-squares slope is b = Sxy ÷ Sxx and the intercept a = ȳ − b x̄ (x̄ and ȳ are the means), which minimise the sum of squared vertical distances. The correlation coefficient is r = Sxy ÷ √(Sxx × Syy), with the sign of the slope, and r² = Sxy² ÷ (Sxx × Syy) is computed exactly. The residual sum of squares is SSE = Syy − Sxy² ÷ Sxx and the standard error of the estimate is s = √(SSE ÷ (n − 2)), which needs n ≥ 3. Square roots are found with integer arithmetic to 30 decimals and are exact when the radicand is a perfect square of fractions. Predictions put x into a + bx with the exact a and b; the reverse is x = (y − a) ÷ b. If every x is equal (Sxx = 0) the slope is undefined and there is no line; if every y is equal (Syy = 0) the line is horizontal and r is undefined. Display: the chosen number of significant digits, rounded half away from zero, with ≈ in front of any rounded value; sums that end within 12 decimals are shown in full. The scatter plot converts the numbers to floating point only to place them on the page; the axis ticks are round numbers (1, 2 or 5 times a power of ten) and the table under the plot lists the same points. The residual table shows the first 50 points in the order entered. Anscombe’s quartet values are those published by Anscombe (1973); the page shows what this calculator computes for them. This is a description of the data you enter, not a forecast: a line fitted to a sample says little outside the range of that sample.

Sources

More: Statistics, probability and gradesAll math & numbersAll toolsHome & DIY calculators