Linear Regression Calculator
This linear regression calculator finds the straight line y = a + bx that fits your points most closely. Enter the x values and the matching y values; for the six students below, the line is y = 5.9429x + 46.2.
Sources: OpenStax Introductory Statistics 2e Updated
Line of best fit
y = 0.6x + 2.2
- Slope (b)
- 0.6
- Intercept (a)
- 2.2
- Correlation (r)
- 0.7746
- r²
- 0.6
- Points (n)
- 5
Show steps Here’s how we got it
Count the points
n = 5
Build the sums you need
x y xy x2 1 2 2 1 2 4 8 4 3 5 15 9 4 4 16 16 5 5 25 25 Σx = 15 Σy = 20 Σxy = 66 Σx2 = 55 Σ means “add up the whole column”.
Work out the slope
b = nΣxy − ΣxΣy/nΣx2 − (Σx)2 = 5 × 66 − 15 × 20/5 × 55 − 152
= 30/50 = 0.6
The slope says how much y changes, on average, when x goes up by 1.
Work out the intercept
a = mean of y − b × mean of x = 4 − 0.6 × 3 = 2.2
The intercept is the predicted y when x is 0.
Write the line of best fit
y = 0.6x + 2.2
Measure how well the line fits
r = nΣxy − ΣxΣy/√((nΣx2 − (Σx)2)(nΣy2 − (Σy)2)) = 30/√(50 × 30) ≈ 0.7746
r2 = 0.6
r runs from −1 to 1. r2 is the share of the variation in y that the line accounts for. Σy2 = 86.
The least-squares regression formulas
For n points (x, y), the line of best fit has this slope and intercept, as given in OpenStax Introductory Statistics:
The correlation coefficient uses the same sums plus Σy²:
The slope b is the same rise-over-run idea as the slope of a line through two points, worked out from all the points at once.
How to calculate a linear regression: worked example
Hours studied (x) and quiz score (y) for six students: (1, 52), (2, 58), (3, 65), (4, 70), (5, 74), (6, 83). Fit the line and predict the score for 7 hours.
Count the points
n = 6
Build the sums you need
x y xy x2 1 52 52 1 2 58 116 4 3 65 195 9 4 70 280 16 5 74 370 25 6 83 498 36 Σx = 21 Σy = 402 Σxy = 1511 Σx2 = 91 Σ means “add up the whole column”.
Work out the slope
b = nΣxy − ΣxΣy/nΣx2 − (Σx)2 = 6 × 1511 − 21 × 402/6 × 91 − 212
= 624/105 ≈ 5.942857
The slope says how much y changes, on average, when x goes up by 1.
Work out the intercept
a = mean of y − b × mean of x = 67 − 5.942857 × 3.5 = 46.2
The intercept is the predicted y when x is 0.
Write the line of best fit
y = 5.9429x + 46.2
Measure how well the line fits
r = nΣxy − ΣxΣy/√((nΣx2 − (Σx)2)(nΣy2 − (Σy)2)) = 624/√(105 × 3744) ≈ 0.9952
r2 ≈ 0.9905
r runs from −1 to 1. r2 is the share of the variation in y that the line accounts for. Σy2 = 27558.
Predict y for x = 7
y = 46.2 + 5.942857 × 7 = 87.8
This x is outside the range of your data, so treat the prediction with care.
Answer: y = 5.9429x + 46.2
What least squares means
For each point, take the vertical distance from the point to the line, square it and add the squares up. Least squares picks the line that makes this total as small as possible.
In the example the biggest miss is the student at 5 hours, who scored 74 where the line predicts about 75.9. Squaring makes every miss positive, so misses above and below the line cannot cancel. It is the same reason the variance and the standard deviation square their differences from the mean.
To see your own data before fitting a line, plot it with the graph generator. A curved or clustered pattern is a sign that a straight line is the wrong model.
Linear regression practice question
Find the line of best fit for the points (0, 1), (1, 3), (2, 4) and (3, 8).
Check your answer
Answer: y = 2.2x + 0.7
Count the points
n = 4
Build the sums you need
x y xy x2 0 1 0 0 1 3 3 1 2 4 8 4 3 8 24 9 Σx = 6 Σy = 16 Σxy = 35 Σx2 = 14 Σ means “add up the whole column”.
Work out the slope
b = nΣxy − ΣxΣy/nΣx2 − (Σx)2 = 4 × 35 − 6 × 16/4 × 14 − 62
= 44/20 = 2.2
The slope says how much y changes, on average, when x goes up by 1.
Work out the intercept
a = mean of y − b × mean of x = 4 − 2.2 × 1.5 = 0.7
The intercept is the predicted y when x is 0.
Write the line of best fit
y = 2.2x + 0.7
Measure how well the line fits
r = nΣxy − ΣxΣy/√((nΣx2 − (Σx)2)(nΣy2 − (Σy)2)) = 44/√(20 × 104) ≈ 0.9648
r2 ≈ 0.9308
r runs from −1 to 1. r2 is the share of the variation in y that the line accounts for. Σy2 = 90.
Linear Regression Calculator: FAQ
How do you calculate a linear regression by hand?
Make a table with columns x, y, xy and x² and add up each column. The slope is b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²). The intercept is a = (mean of y) − b × (mean of x). The line is y = a + bx.
What does the slope of the regression line mean?
It is the average change in y when x goes up by 1. In the worked example the slope is 5.9429, so each extra hour of study goes with about 5.9 more points on the quiz, on average, for these six students.
What is r?
The correlation coefficient. It runs from −1 to 1 and measures how closely the points follow a straight line. Values near 1 or −1 mean a strong linear pattern, and values near 0 mean little or none. Its sign matches the sign of the slope.
What does r² tell you?
The share of the variation in y that the line accounts for. An r² of 0.6 means the line explains 60% of the variation and 40% is left unexplained.
Does a strong correlation mean x causes y?
No. Regression describes a pattern in the data. It cannot tell you why the pattern is there. Two quantities can move together because one causes the other, because something else drives both, or by chance.
Can I use the line to predict values outside my data?
With care. Inside the range of your x values the line summarises what you observed. Outside it you are assuming the same straight-line pattern continues, which the data cannot confirm. The calculator warns you when a prediction is outside the range.
Sources
- OpenStax Introductory Statistics 2e, 12.3 The Regression Equation — the least-squares line ŷ = a + bx with a = ȳ − b x̄, and the formula for the correlation coefficient r (checked October 12, 2026)