Linear Regression Calculator

Linear Regression Calculator

Paste x and y pairs to fit the least squares line y = a + bx: slope and intercept with standard errors, t and p values, R², r and the residual standard error, a prediction with both kinds of interval, and scatter and residual plots.

Every figuretap a card to copy its value

Scatter plot with the fitted line

Residual plot

Analysis of variance
Residuals

Slope b = Sxy ÷ Sxx and intercept a = ȳ − b × x̄, from the centred data; s = √(SSE ÷ (n − 2)); SE(b) = s ÷ √Sxx; R² = 1 − SSE ÷ Syy. Intervals use t on n − 2 degrees of freedom and p values are two tailed. The CSV, image and printed report contain only the values you entered.

How to use this tool

Paste your data one pair per line, x first and y second. Two columns copied from a spreadsheet work as they are, and a first line of column names labels the equation, the plots and the tables. For two separate lists, switch “Enter the data as” to lists. Type a value in “Predict y at x” to get both kinds of interval, and change the confidence level if your field works at 90% or 99%.

The input people most often get wrong is which variable goes first. Regression is not symmetric: the line of y on x makes the vertical misses as small as possible, so x is the variable you know and y the one you want to predict. Swap them and you get a different line, not the same line rearranged. The other common slip is pasting two columns that were sorted separately.

What the calculation is doing

Least squares picks the intercept a and slope b that make the sum of the squared vertical distances from the points to the line as small as possible. With x̄ and ȳ the means, Sxx = Σ(x − x̄)² and Sxy = Σ(x − x̄)(y − ȳ), the answer is b = Sxy ÷ Sxx and a = ȳ − b × x̄. The calculator centres the data first. The textbook shortcut, n Σxy − Σx Σy over n Σx² − (Σx)², subtracts two huge and nearly equal numbers when x is large: shift the five point example below by 100,000,000 in x and it gives a slope of 0.46875 instead of 0.6.

The residuals e = y − ŷ are what the line misses. Their sum of squares SSE gives the residual standard error s = √(SSE ÷ (n − 2)), where n − 2 counts the two parameters already estimated. The slope’s standard error is s ÷ √Sxx and the intercept’s is s × √(1 ÷ n + x̄² ÷ Sxx). Each estimate divided by its standard error is a t statistic on n − 2 degrees of freedom, giving a two tailed p value. R² = 1 − SSE ÷ Syy, r = Sxy ÷ √(Sxx × Syy) and F = t².

For a prediction at x₀, ŷ = a + b × x₀. The interval for the mean of y there is ŷ ± t × s × √(1 ÷ n + (x₀ − x̄)² ÷ Sxx); the interval for one new observation adds 1 under the root, the scatter of single points around the line. The (x₀ − x̄)² term is why both widen away from x̄.

Two panels built from the calculator's default data, the heights and weights of 10 students. The left panel shows the scatter with the least squares line weight = −266.53 + 6.1376 × height and every residual drawn as a vertical orange segment labelled with its value, from +14.2 to −13.2, whose squares add up to SSE 597.4. The right panel shows the same line with the shaded 95% confidence band for the mean and the dashed prediction band, with bars at height 70 marking the mean interval 156.7 to 169.5 and the single student interval 142.2 to 184.0.
With 1,000 students of the same scatter the mean band at height 70 would shrink to ±0.5 pounds, but the interval for one new student would still be about ±17, close to 2s. More data pins down the line, never the scatter around it.

Why R² is not enough

In 1973 the statistician Frank Anscombe published four small data sets built to give the same regression output. Each gives y = 3.00 + 0.500x, an R² of 0.67, a slope standard error of 0.118 and t = 4.24, so p = 0.0022. The first is an ordinary noisy line. The second is a smooth curve, where a straight line is the wrong model. In the third, one outlier tilts an otherwise exact line. In the fourth, every point but one has x = 8, and the single point at x = 19 sets the slope on its own. The summary numbers cannot tell them apart; the residual plots can.

A patternless band around zero is what you want in a residual plot. An arch or a U means the relationship bends, and a log or square root of x or y often straightens it. A funnel means the scatter grows with x, so the intervals are too narrow at one end and too wide at the other. A standardized residual beyond ±2 is read like a z score: about one point in 20 lands there by chance, but one beyond ±3 deserves a second look at the raw data.

How strong a correlation has to be to count

In simple regression the t test on the slope is the same test as the one on the correlation: t = r × √(n − 2) ÷ √(1 − r²). Turned round, that gives the smallest r that reaches significance for a given number of points, two tailed. Small samples need very strong correlations and large samples flag weak ones, so significant is not the same as useful.

Degrees of freedom, n − 2Points nt, p < 0.05r needed, p < 0.05R² neededr needed, p < 0.01
1312.7060.9970.9941.000
244.3030.9500.9030.990
353.1820.8780.7710.959
462.7760.8110.6580.917
572.5710.7540.5690.875
682.4470.7070.4990.834
8102.3060.6320.3990.765
10122.2280.5760.3320.708
13152.1600.5140.2640.641
18202.1010.4440.1970.561
23252.0690.3960.1570.505
28302.0480.3610.1300.463
30322.0420.3490.1220.449
40422.0210.3040.0930.393
50522.0090.2730.0750.354
60622.0000.2500.0630.325
80821.9900.2170.0470.283
1001021.9840.1950.0380.254

A worked example you can check on paper

The calculator opens with the heights, in inches, and weights, in pounds, of 10 students from the Penn State STAT 501 course notes. The sums are Σx = 693 and Σy = 1,588, so x̄ = 69.3 and ȳ = 158.8. From the deviations, Sxx = 138.1, Sxy = 847.6 and Syy = 5,799.6. The slope is 847.6 ÷ 138.1 = 6.1376 pounds per inch and the intercept is 158.8 − 6.1376 × 69.3 = −266.53. The residuals square and sum to SSE = 597.39, so s = √(597.39 ÷ 8) = 8.641 and R² = 1 − 597.39 ÷ 5,799.6 = 0.897.

The slope’s standard error is 8.641 ÷ √138.1 = 0.7353, so t = 6.1376 ÷ 0.7353 = 8.35 on 8 degrees of freedom and p = 0.00003. At 70 inches, ŷ = −266.53 + 6.1376 × 70 = 163.10. The standard error of the mean there is 8.641 × √(1 ÷ 10 + 0.7² ÷ 138.1) = 2.781, and with t = 2.306 the 95% interval for the mean is 163.10 ± 6.41, or 156.68 to 169.51. For one new student the root becomes √(1 + 1 ÷ 10 + 0.7² ÷ 138.1) and the interval is 142.16 to 184.03. Penn State’s output gives the same −266.53, 6.1376, 0.7353, 8.641 and 89.7%.

Mistakes that catch people out

Swapping x and y. Regressing height on weight for the same students gives 0.1461 inches per pound, which turns round to 6.84 pounds per inch, not 6.14. Decide which variable you are predicting before you fit.

Reading the confidence band as the range for individual values. The shaded band covers the average y at each x. Single observations scatter far more widely, so use the prediction interval for one new case.

Extrapolating past the data. The height example predicts −266.5 pounds at a height of zero, which is nonsense: the line is only supported between 63 and 75 inches. Treat any prediction outside the range of x with suspicion.

Trusting R² without the residual plot. Anscombe’s four sets share an R² of 0.67 and only one suits a straight line. A curve, an outlier or one point far out in x can all hide behind a respectable R².

Reading a small p as a strong relationship, or a slope as a cause. With enough points a trivial slope becomes significant, and x predicting y says nothing about x driving y. Report the slope and its interval, not just p.

Popular calculations

Each link opens the calculator with that data filled in, ready to edit. The hours, ice cream, shoe size, car and price sets are made up practice examples; the others are published or exact data. PI is the prediction interval for one new value.

Teaching examples

Predictions with both intervals

Anscombe’s quartet

NIST reference data

Exact lines and negative slopes

How this was checked

The engine was run on the NIST Statistical Reference Datasets Norris data, 36 points whose results NIST certifies to 15 significant digits. The slope came out as 1.00211681802045 against the certified 1.00211681802045, the residual standard deviation as 0.884796396144386 against 0.884796396144373, and R² as 0.999993745883712, identical to the certified value. Across all ten certified figures the smallest agreement was 12.8 significant digits, for the intercept.

The NIST/SEMATECH Handbook fits pressure against temperature for 40 measurements and tabulates 95% intervals at 25, 45 and 65 degrees. The calculator reproduces every printed fitted value (106.0025, 184.6053 and 263.2081), both standard deviations at each point and the t value of 2.024394. Every printed bound matches but one: the handbook gives 193.5 for the upper prediction limit at 45 degrees, while its own figures add up to 184.6053 + 8.811369 = 193.4167, which is what the calculator shows. The Penn State output and all four Anscombe sets were reproduced to the digits printed.

A separate script recomputed everything with exact fractions in big integer arithmetic from the uncentred sums and compared it with the calculator for every data set above and 2,000 random ones: the largest relative difference was 2 × 10⁻¹¹. The t critical values match the NIST table for 20 sample sizes at 95% and 99%, the p values match a numerical integration of the t density to 10 digits, and the critical r table above matches the University of Sussex table to within 0.001.

Common questions

How do you calculate a linear regression line by hand?

Find the means of x and y, then Sxx = Σ(x − x̄)² and Sxy = Σ(x − x̄)(y − ȳ). The slope is b = Sxy ÷ Sxx and the intercept is a = ȳ − b × x̄. For the 10 students, b = 847.6 ÷ 138.1 = 6.1376 and a = 158.8 − 6.1376 × 69.3 = −266.53.

What does R² tell you?

The share of the variation in y that the line accounts for, R² = 1 − SSE ÷ Syy. An R² of 0.897 means the line explains 89.7% of the spread in y. It says nothing about whether a straight line is the right shape, so read it with the residual plot.

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

The confidence interval is for the average y at a given x, the height of the line itself. The prediction interval is for one new observation there, so it adds the scatter of single points around the line. At 70 inches the 95% interval for the mean weight is 156.7 to 169.5 pounds; one new student could weigh 142.2 to 184.0.

What does the p value for the slope mean?

It is the probability of a t value at least as far from zero as yours if y had no straight line relationship with x. The 0.00003 in the height example says a slope that steep would be very unlikely by chance. It does not measure how strong or useful the relationship is.

How many data points do you need for linear regression?

At least three, because two points always fit a line exactly and leave nothing to estimate the scatter from. Few points mean wide intervals: t is 3.182 at 95% with 5 points and 2.048 with 30. The table above shows the r needed for significance, 0.632 with 10 points but only 0.197 with 100.

What is the residual standard error?

It is s = √(SSE ÷ (n − 2)), the typical vertical distance of a point from the line in the units of y. The divisor is n − 2 because two parameters were estimated from the data. About 95% of points lie within 2s of the line, the dashed lines on the residual plot.

Related Calculators and Reference