Linear regression calculator with a scatter plot
Linear regression fits the straight line ŷ = a + bx that best matches a set of paired data, by making the squared vertical gaps between the points and the line as small as possible (least squares). The gradient is b = Sxy ÷ Sxx, and the line passes through the mean point (x̄, ȳ). This linear regression calculator gives the line, the correlation coefficient r, r squared, the residuals and predictions, and you can drag points on the scatter plot to see the line respond.
It opens on eight points that lie close to a line. Replace them with your own, or open the full linear regression calculator over this page.
Linear Regression Calculator
Fit a line of best fit and measure the correlation
How to use the linear regression calculator
- Type your data into the Paired x and y values table, one pair per row, up to 200 rows. You can paste two columns from a spreadsheet.
- Or work on the scatter plot: drag any point to move it, or click to add one.
- Read the results: the line of best fit, the correlation coefficient r, the coefficient of determination r², a word description of the strength, a p-value for no correlation and the number of points.
- Under Predict, type an x value in Predict y at x = to read the line there. The calculator warns you if the x is outside your data.
- Turn on Show residuals to see each point's distance from the line, in a table and a plot.
- The steps show the means, Sxx, Syy and Sxy, the gradient, the intercept and r. To practise, press Random problem in the Practice section.
The least squares regression line
Sxx = Σ(x − x̄)² measures the spread of x, and Sxy = Σ(x − x̄)(y − ȳ) measures how x and y move together. ŷ (y hat) means a predicted value, not a measured one. Some courses write the line as y = mx + c or ŷ = b₀ + b₁x: it is the same line.
Correlation coefficient calculator
r, the product moment correlation coefficient (Pearson's r), is always between −1 and 1. Its sign matches the gradient of the line, and its size says how closely the points follow a straight line. r² is the share of the variation in y that the line explains.
| Size of r | Calculator's description |
|---|---|
| 0.9 to 1 | Very strong |
| 0.7 to 0.9 | Strong |
| 0.4 to 0.7 | Moderate |
| 0.2 to 0.4 | Weak |
| Below 0.2 | Very weak or no linear correlation |
These bands are the calculator's own wording. Different courses draw the lines in slightly different places, so use the words your teacher uses.
Worked examples you can check in the calculator
Example 1: hours studied and test score
Six students record how many hours they revised (x) and their test score (y):
| Hours, x | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Score, y | 52 | 55 | 61 | 64 | 70 | 72 |
The means are x̄ = 3.5 and ȳ = 62.33. Working out the sums gives Sxx = 17.5, Syy = 317.33 and Sxy = 74.
The line of best fit is ŷ = 47.53 + 4.229x: each extra hour goes with about 4.2 more marks. r = 74 ÷ √(17.5 × 317.33) = 0.993, a very strong positive correlation, and r² = 0.986, so the line explains about 98.6% of the variation in scores.
Example 2: making a prediction
For a student who revised 4.5 hours, the line predicts ŷ = 47.53 + 4.229 × 4.5 = 66.6. That is interpolation, inside the data, so it is reasonable. For 12 hours the line gives 98.3, but 12 is far outside the data, and nothing says the pattern carries on that far. That is extrapolation, and the calculator flags it.
Example 3: residuals
A residual is the actual y minus the predicted ŷ. For the student who revised 5 hours, ŷ = 47.53 + 4.229 × 5 = 68.68, and they scored 70, so the residual is +1.32: above the line. The residuals of a least-squares line always add up to 0. If they show a curve rather than random scatter, a straight line is the wrong model.
Example 4: the calculator's opening data
The eight points it opens with give x̄ = 4.5, ȳ = 9, Sxx = 42 and Sxy = 83.2, so b = 83.2 ÷ 42 = 1.981 and a = 9 − 1.981 × 4.5 = 0.086. The line is ŷ = 1.981x + 0.086, with r = 0.9990. Drag one point well off the line and watch r drop: a single outlier can change both the line and r a lot.
Try one
Correlation is not causation
A high r says two things rise and fall together, not that one causes the other. Ice cream sales and sunburn cases are strongly correlated because both go up in hot weather. And r only measures straight-line patterns: data that follows a curve can have a strong relationship but an r near 0. Always look at the scatter plot as well as the number.
Common mistakes
- Swapping x and y. The line of y on x predicts y from x. Put the variable you control or know in the x column.
- Extrapolating. Predictions far outside the data range aren't reliable.
- Reading r² as r. An r² of 0.81 means r = ±0.9, not 0.81.
- Forgetting that the line passes through (x̄, ȳ). It is a quick check on any line you work out by hand.
- Rounding b before working out a. Keep the full gradient, or the intercept drifts.
Common questions
How do you calculate a linear regression line?
What does the correlation coefficient tell you?
What is the difference between r and r squared?
How many points do I need?
What is the p-value in the results?
Revision cards
Read the front, say your answer, then flip to check.
Where to go next
- Solve it
Linear Regression Calculator
Fit a line of best fit and measure the correlation
Open the tool - Test it
AP Statistics
A new exam-style paper every day, with instant marking and a mark scheme
Start today's paper