MathsStatistics & Probability

Linear Regression
calculator.

A least-squares line turns paired data into a rule for how y moves with x.

How it's worked out

Simple linear regression finds the straight line that best predicts a response y from one predictor x. Best has a precise meaning here: of all possible lines, it is the one with the smallest sum of squared vertical distances from the points to the line. If you only need the strength of the link, a correlation is enough. With two or more predictors, use multiple regression.

yˆ\displaystyle \hat{y}=b0+b1x\displaystyle {}= b_0 + b_1 x
b1\displaystyle b_1=SxySxx\displaystyle {}= \frac{S_{xy}}{S_{xx}}
b0\displaystyle b_0=y‾−b1x‾\displaystyle {}= \bar{y} - b_1 \bar{x}

Here x‾\bar{x} and y‾\bar{y} are the two means. SxxS_{xx} adds up each squared distance of x from its mean, and SxyS_{xy} adds up each product of the distances of x and y from their means. The second formula forces the line through the point made of the two means, as the standard derivation shows.

s\displaystyle s=SSEn−2\displaystyle {}= \sqrt{\frac{\text{SSE}}{n - 2}}
R2\displaystyle R^2=1−SSESST\displaystyle {}= 1 - \frac{\text{SSE}}{\text{SST}}

SSE is the sum of the squared misses from the line, and SST is the sum of the squared distances of y from its mean. The residual standard error, s, is the typical miss in y units. You divide by n − 2 because two numbers, the slope and the intercept, were fitted from the data.

SE(b1)\displaystyle \text{SE}(b_1)=sSxx\displaystyle {}= \frac{s}{\sqrt{S_{xx}}}
SE(b0)\displaystyle \text{SE}(b_0)=s1n+x‾2Sxx\displaystyle {}= s\sqrt{\frac{1}{n} + \frac{\bar{x}^2}{S_{xx}}}

Each coefficient divided by its standard error is a t statistic on n − 2 degrees of freedom, and the intervals use the matching Student t value. At a new x, the average y has the first standard error below, and one new y has the second, with an extra 1 under the root.

s1n+(x0−x‾)2Sxxs\sqrt{\frac{1}{n} + \frac{(x_0 - \bar{x})^2}{S_{xx}}}
s1+1n+(x0−x‾)2Sxxs\sqrt{1 + \frac{1}{n} + \frac{(x_0 - \bar{x})^2}{S_{xx}}}

Worked examples

Lena tracks study hours against exam scores

Six students studied from 2 to 7 hours and scored 55, 65, 72, 78, 85, 90. The means are 4.5 hours and 74.17 points, with SxxS_{xx} of 17.5 and SxyS_{xy} of 120.5.

b1\displaystyle b_1=120.517.5\displaystyle {}= \frac{120.5}{17.5}=6.886\displaystyle {}= 6.886

The line is ŷ = 6.886x + 43.181. Each extra hour goes with about 6.89 more points (95% interval 5.88 to 7.89), and R² is 0.9891.

For a student who studies 8 hours, the line gives 98.27. The 95% interval for the average such student is 94.37 to 102.17, and for one student it widens to 92.54 to 103.99. Eight hours is outside the 2 to 7 she measured, so this is an extrapolation. To reach a score of 80 inside her data, the line says about 5.35 hours.

Leaving out the first student moves the slope from 6.89 to 6.3, and leaving out the last moves it to 7.3. With six points, the ends of the line carry the most weight.

Open this example in the tool

A shop owner checks ad spend against weekly sales

Over eight weeks a shop spent 1 to 8 hundred dollars on ads and sold 14, 17, 22, 25, 31, 35, 40, 44 thousand dollars of goods. The fit gives ŷ = 4.405x + 8.679, so each extra hundred dollars of ads goes with about 4.4 thousand dollars more in sales.

At 9 hundred dollars the line gives 48.32, with a 95% prediction interval of 46.08 to 50.56. Sales of 50 would take about 9.38 hundred dollars. Both figures sit past the data, and a real shop would also ask whether sales drove the ad budget rather than the reverse.

Open this example in the tool

One odd point in Anscombe set III

Set III looks like a clean straight line with a single point far above it. The fit still reads ŷ = 0.5x + 3 with an R² of 0.67, close to the other sets.

The point at x = 13 has a studentized residual of 3, the largest this measure can reach with 11 points, since it cannot pass the square root of n − 2. Leave that one point out and the slope drops from 0.5 to 0.35, and the other ten sit almost exactly on a line, with an R² that rounds to 1.

Open this example in the tool

Reading your result

Slope. The change in predicted y for each extra 1 in x, in y units per x unit. If its interval leaves out 0, the slope p-value is below 1 minus your confidence level.

Intercept. The predicted y at x = 0. It is often meaningless by itself, such as the weight of a person with a height of zero, and it only anchors the line.

Typical miss, s. The usual vertical distance of a point from the line. If the misses are roughly normal, about 95% of points sit within about 2s.

Leverage. How unusual a point's x is. Leverages add up to the number of fitted terms), which is 2 here, and a point above 4n\frac{4}{n} deserves a second look. In a set of 11 points that means anything above about 0.36.

Studentized residual. A residual divided by s times the root of 1 minus its leverage. It lets you compare misses at points with different leverage. A value far from the rest marks the point to check first.

Why you still need the plot

Anscombe built data sets in 1973 that share a line of y = 3.00 + 0.500x, a correlation of 0.816 and an R² of 0.67, yet look nothing alike. Three of them, run through this engine, all give about ŷ = 0.5x + 3.

Three of Anscombe's sets, same x values
SetShapeR²SE of slope
IStraight with scatter0.6670.118
IIA smooth curve0.6660.118
IIIStraight, one outlier0.6660.118

Only set I suits a straight line, and only the scatter plot and the residuals tell you so. NIST warns that a high R² does not guarantee a good fit.

Common mistakes

  • Trusting the line past your data. NIST notes that extreme inputs give extreme outputs, and the fit says nothing about whether the straight shape continues. Relationships often bend or level off, so this tool flags any x outside your range.
  • Quoting the narrow interval for one case. The confidence interval is about the average y at that x. For one new case, use the prediction interval, which is always wider.
  • Letting one point decide. One or two outliers can seriously skew a least-squares fit. Check how far the slope moves when each point is left out.
  • Fitting a straight line to a curve. A curved residual pattern means the model is wrong, not just noisy. Residuals that fan out as x grows or drift over time also break the conditions behind the p-values and intervals.
  • Reading a slope as a cause. A clear slope says two things move together in these data. It does not say that changing x will change y.

Questions people ask

A confidence interval says where the average y lies at that x, and it narrows as you add data. A prediction interval says where one new value is likely to fall, so it adds the scatter of single points. Extra data cannot remove that scatter, so it stays wide even with thousands of rows.

Sources