Linear Regression
calculator.
A least-squares line turns paired data into a rule for how y moves with x.
| # | X | Y |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
- Correlation r
- 0.9946
- Slope p-value
- < 0.0001
- ŷ at x = 8
- 98.2667
Each extra 1 in x goes with 6.8857 more in y (95% interval 5.8844 to 7.887). That interval leaves out 0, so these data point to a real slope.
The points, the line and its band
Drag along the line to predict6 points with the line ŷ = 6.8857x + 43.181. The dotted lines are the 95% confidence limits for the average y. At x = 8 the line gives 98.2667.
Predict a new value
- Predicted y
- 98.2667
- 95% interval, average y
- 94.3671 to 102.1663
- 95% interval, one new y
- 92.5436 to 103.9897
- The line reaches 80 at x =
- 5.3472
Residuals against x
Each dot is a point's miss from the line. A curve or a funnel here means a straight line is the wrong shape. The largest studentized residual is −1.88, at x = 2.
The line with one point left out
| Leave out | Slope | Change | R² |
|---|---|---|---|
| (2, 55) | 6.3R² 0.9972 | −0.5857 | 0.9972 |
| (3, 65) | 7.027R² 0.9903 | +0.1413 | 0.9903 |
| (4, 72) | 6.9302R² 0.9915 | +0.0445 | 0.9915 |
| (5, 78) | 6.8721R² 0.9891 | −0.0136 | 0.9891 |
| (6, 85) | 6.8243R² 0.9875 | −0.0614 | 0.9875 |
| (7, 90) | 7.3R² 0.9905 | +0.4143 | 0.9905 |
A large change in slope marks a point that holds the line up on its own.
Coefficients
| Term | Estimate | SE | t | p | 95% interval |
|---|---|---|---|---|---|
| Intercept b₀SE 1.7359t 24.876 | 43.18138.3614 to 48.0005 | 1.7359 | 24.876 | < 0.0001 | 38.3614 to 48.0005 |
| Slope b₁SE 0.3606t 19.093 | 6.88575.8844 to 7.887 | 0.3606 | 19.093 | < 0.0001 | 5.8844 to 7.887 |
Fitted values and residuals
| x | y | Fitted ŷ | Residual | Leverage | Studentized |
|---|---|---|---|---|---|
| 2y 55 | 55 | 56.9524Leverage 0.524 | −1.9524Studentized −1.875 | 0.524 | −1.875 |
| 3y 65 | 65 | 63.8381Leverage 0.295 | 1.1619Studentized 0.917 | 0.295 | 0.917 |
| 4y 72 | 72 | 70.7238Leverage 0.181 | 1.2762Studentized 0.935 | 0.181 | 0.935 |
| 5y 78 | 78 | 77.6095Leverage 0.181 | 0.3905Studentized 0.286 | 0.181 | 0.286 |
| 6y 85 | 85 | 84.4952Leverage 0.295 | 0.5048Studentized 0.399 | 0.295 | 0.399 |
| 7y 90 | 90 | 91.381Leverage 0.524 | −1.381Studentized −1.326 | 0.524 | −1.326 |
Working
- 1Find the two meansx̄ = 4.5ȳ = 74.1667
- 2Add up the squares and productsSxx = 17.5Sxy = 120.5
- 3Divide Sxy by Sxx for the slopeb₁ =120.517.5= 6.8857
- 4Put the line through the meansb₀ = ȳ − b₁x̄= 43.181
- 5Compare the misses with the total spreadR² = 1 −9.1048838.8333= 0.9891
- 6Test the slope on 4 degrees of freedomt =6.88570.3606= 19.093
- 7Check: the line passes through the means and the misses cancelŷ at x̄ = 74.1667Sum of residuals = −2.8422 × 10⁻¹⁴
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.
Here and are the two means. adds up each squared distance of x from its mean, and 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.
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.
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.
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 of 17.5 and of 120.5.
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.
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.
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.
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 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.
| Set | Shape | R² | SE of slope |
|---|---|---|---|
| I | Straight with scatter | 0.667 | 0.118 |
| II | A smooth curve | 0.666 | 0.118 |
| III | Straight, one outlier | 0.666 | 0.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
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods, section 4.1.4.1, Linear least squares regression
- NIST/SEMATECH e-Handbook, section 4.4.4, How can I tell if a model fits my data?
- Wikipedia, Simple linear regression
- Wikipedia, Anscombe's quartet
- R documentation, predict.lm (stats package)
- Wikipedia, Leverage (statistics)
- Wikipedia, Studentized residual