Skip to content
CalcWorld

Browse all tools

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

Same number of values as x, in the same order.

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
5.661.730.685.32
Show steps Here’s how we got it
  1. Count the points

    n = 5

  2. Build the sums you need

    xyxyx2
    1221
    2484
    35159
    441616
    552525
    Σx = 15Σy = 20Σxy = 66Σx2 = 55

    Σ means “add up the whole column”.

  3. 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.

  4. 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.

  5. Write the line of best fit

    y = 0.6x + 2.2

  6. 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 y^=a+bx\hat{y} = a + bx has this slope and intercept, as given in OpenStax Introductory Statistics:

b=n∑xy−∑x∑yn∑x2−(∑x)2a=yˉ−b xˉb = \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - \left(\sum x\right)^2} \qquad a = \bar{y} - b\,\bar{x}

The correlation coefficient uses the same sums plus Σy²:

r=n∑xy−∑x∑y[n∑x2−(∑x)2][n∑y2−(∑y)2]r = \frac{n\sum xy - \sum x \sum y}{\sqrt{\left[n\sum x^2 - \left(\sum x\right)^2\right]\left[n\sum y^2 - \left(\sum y\right)^2\right]}}

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.

  1. Count the points

    n = 6

  2. Build the sums you need

    xyxyx2
    152521
    2581164
    3651959
    47028016
    57437025
    68349836
    Σx = 21Σy = 402Σxy = 1511Σx2 = 91

    Σ means “add up the whole column”.

  3. 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.

  4. 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.

  5. Write the line of best fit

    y = 5.9429x + 46.2

  6. 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.

  7. 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

87470.66.4
Linear regression scatter plot of six students' hours studied against quiz scores, with the line of best fit y = 5.9429x + 46.2, r = 0.9952 and the prediction 87.8 at 7 hours.
All six points sit close to the line, which is why r is 0.9952. The marked point at 7 hours is a prediction, one hour beyond the data.

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.

Bar chart of the six residuals of the linear regression example, from −1.91 at x = 5 to 1.14 at x = 6, whose squares add up to 5.9429.
The least-squares line is the one that makes the sum of these squared misses 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

  1. Count the points

    n = 4

  2. Build the sums you need

    xyxyx2
    0100
    1331
    2484
    38249
    Σx = 6Σy = 16Σxy = 35Σx2 = 14

    Σ means “add up the whole column”.

  3. 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.

  4. 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.

  5. Write the line of best fit

    y = 2.2x + 0.7

  6. 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