Linear Regression Calculator

Published on: November 26, 2026
Final Answer: Free | Full Steps: Plus

This Linear Regression Calculator finds the straight line that best fits a set of points and shows the working line by line. It finds the mean of x and of y, works out how far each value is from its mean, and uses the totals of those differences to get the slope. The intercept then makes the line pass through the point of the two means. The answer is the line of best fit, drawn through the points on a scatter plot, and the same pattern works for any paired data.

Step-by-step method

  1. Set up the problem.
  2. Write the formulas.
  3. Find the mean of the x values and of the y values.
  4. Subtract the means from each value.
  5. Multiply and square the differences.
  6. Add up the two new columns.
  7. Find the slope b.
  8. Find the intercept a.
  9. Write the equation of the line.
  10. Draw the line through the points.

Formula: Line of Best Fit Formula

This is the equation of the line. a is where it crosses the y-axis and b is its slope.

\(\hat{y} = a + bx\)

Formula: Slope Formula

This formula gives the slope b. Divide the total of the products by the total of the squared x differences.

\(b = \frac{\Sigma (x - \bar{x})(y - \bar{y})}{\Sigma (x - \bar{x})^{2}}\)

Formula: Intercept Formula

This formula gives the intercept a. Take b times the mean of x away from the mean of y.

\(a = \bar{y} - b\bar{x}\)

Useful Tips to Remember

The regression line \(\hat{y} = a + bx\) predicts \(y\) from \(x\), where \(b\) is the slope and \(a\) is the intercept. Predictions are only reliable inside the range of the data, so extending the line far beyond it is risky. The line always passes through the point \((\bar{x}, \bar{y})\). Check the correlation coefficient to see how well the line fits.

Required knowledge

Where Is Linear Regression Used?

Linear regression fits the straight line \(\hat{y} = a + bx\) that best matches a set of data points, so one quantity can be predicted from another. Businesses use it to forecast sales from advertising spend, scientists use it to find trends in experimental results, and economists use it to study how prices affect demand. The slope shows how much \(y\) changes for each unit of \(x\), which is often the most useful number in the analysis. It is also one of the simplest and most widely used models in machine learning.

  • Forecasting sales from advertising spend
  • Predicting house prices from their size
  • Finding trends in scientific experiments
  • Estimating how demand changes with price
  • Building simple prediction models in machine learning

Linear Regression Practice Problems

Example 1 fits a line with a positive slope, and Example 2 fits one with a negative slope. Try finding the slope and intercept yourself before checking each step. For two more free worked examples, check out the Linear Regression Notes.

The worked examples below go through the same pattern from start to finish, so you can compare every step with your own working. For more practice, the notes page goes further with extra worked examples and fuller explanations of the ideas behind each step. Use them together to check your answers and to understand why each step works.

See Example 1 Hide Example 1

Problem

\(\begin{array}{c|c} x & y \\ \hline 1 & 2 \\ 2 & 4 \\ 3 & 5 \\ 4 & 4 \\ 5 & 5 \end{array}\)

Approach

Step-by-step method

  1. Set up the problem.
  2. Write the formulas.
  3. Find the mean of the x values and of the y values.
  4. Subtract the means from each value.
  5. Multiply and square the differences.
  6. Add up the two new columns.
  7. Find the slope b.
  8. Find the intercept a.
  9. Write the equation of the line.
  10. Draw the line through the points.

Formula

Formula: Line of Best Fit Formula

This is the equation of the line. a is where it crosses the y-axis and b is its slope.

\(\hat{y} = a + bx\)

Formula: Slope Formula

This formula gives the slope b. Divide the total of the products by the total of the squared x differences.

\(b = \frac{\Sigma (x - \bar{x})(y - \bar{y})}{\Sigma (x - \bar{x})^{2}}\)

Formula: Intercept Formula

This formula gives the intercept a. Take b times the mean of x away from the mean of y.

\(a = \bar{y} - b\bar{x}\)

Step 1

Step 1 - Set up the problem.

In this problem: We are finding the straight line that fits the points \((x, y)\) best.

\(\begin{array}{c|c} x & y \\ \hline 1 & 2 \\ 2 & 4 \\ 3 & 5 \\ 4 & 4 \\ 5 & 5 \end{array}\)

Step 2

Step 2 - Write the formulas.

In this problem: The line is \(\hat{y} = a + bx\). The slope b compares how x and y move together with how much x spreads out, and the intercept a makes the line pass through \((\bar{x}, \bar{y})\).

\(\hat{y} = a + bx\)
\(b = \frac{\Sigma (x - \bar{x})(y - \bar{y})}{\Sigma (x - \bar{x})^{2}}\)
\(a = \bar{y} - b\bar{x}\)

Step 3

Step 3 - Find the mean of the x values and of the y values.

In this problem: Both means are needed before the differences can be found.

\(x: 1,\; 2,\; 3,\; 4,\; 5 \qquad y: 2,\; 4,\; 5,\; 4,\; 5\)

Step 3a

Step 3a - Find the mean of the x values.

In this problem: The mean of the x values is \(\bar{x} = 3\).

\(\bar{x} = 3\)

Step 3b

Step 3b - Find the mean of the y values.

In this problem: The mean of the y values is \(\bar{y} = 4\).

\(\bar{y} = 4\)

Step 4

Step 4 - Subtract the means from each value.

In this problem: Taking \(\bar{x} = 3\) from each x value and \(\bar{y} = 4\) from each y value gives the differences.

\(\begin{array}{c|c|c|c} x & y & x - \bar{x} & y - \bar{y} \\ \hline 1 & 2 & -2 & -2 \\ 2 & 4 & -1 & 0 \\ 3 & 5 & 0 & 1 \\ 4 & 4 & 1 & 0 \\ 5 & 5 & 2 & 1 \end{array}\)

Step 5

Step 5 - Multiply and square the differences.

In this problem: Each row's two differences are multiplied together, and each difference is squared.

\(\begin{array}{c|c|c|c} x - \bar{x} & y - \bar{y} & (x - \bar{x})(y - \bar{y}) & (x - \bar{x})^{2} \\ \hline -2 & -2 & 4 & 4 \\ -1 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 \\ 2 & 1 & 2 & 4 \end{array}\)

Step 6

Step 6 - Add up the two new columns.

In this problem: The slope formula needs the total of the products and the total of the squared x differences.

\(\Sigma (x - \bar{x})(y - \bar{y}),\; \Sigma (x - \bar{x})^{2}\)

Step 6a

Step 6a - Add the products.

In this problem: Adding the column gives \(4 + 0 + 0 + 0 + 2 = 6\).

\(\Sigma (x - \bar{x})(y - \bar{y}) = 4 + 0 + 0 + 0 + 2 = 6\)

Step 6b

Step 6b - Add the squared x differences.

In this problem: Adding the column gives \(4 + 1 + 0 + 1 + 4 = 10\).

\(\Sigma (x - \bar{x})^{2} = 4 + 1 + 0 + 1 + 4 = 10\)

Step 7

Step 7 - Find the slope b.

In this problem: Replacing the two totals in the slope formula and dividing gives \(b = 0.6\).

\(b = \frac{6}{10} = 0.6\)

Step 8

Step 8 - Find the intercept a.

In this problem: Replacing \(\bar{y}\), \(b\) and \(\bar{x}\) in the intercept formula gives \(a = 2.2\).

\(a = 4 - 0.6 \times 3 = 2.2\)

Step 9

Step 9 - Write the equation of the line.

In this problem: Putting \(a\) and \(b\) into \(\hat{y} = a + bx\) gives the line of best fit.

\(\hat{y} = 2.2 + 0.6x\)

Step 10

Step 10 - Draw the line through the points.

In this problem: The orange line is \(\hat{y} = 2.2 + 0.6x\). It passes as close as it can to every point.

Scatter plot with the line of best fit

Final Answer

\(\hat{y} = 2.2 + 0.6x\)
See Example 2 Hide Example 2

Problem

\(\begin{array}{c|c} x & y \\ \hline 1 & 8 \\ 2 & 6 \\ 3 & 5 \\ 4 & 1 \end{array}\)

Approach

Step-by-step method

  1. Set up the problem.
  2. Write the formulas.
  3. Find the mean of the x values and of the y values.
  4. Subtract the means from each value.
  5. Multiply and square the differences.
  6. Add up the two new columns.
  7. Find the slope b.
  8. Find the intercept a.
  9. Write the equation of the line.
  10. Draw the line through the points.

Formula

Formula: Line of Best Fit Formula

This is the equation of the line. a is where it crosses the y-axis and b is its slope.

\(\hat{y} = a + bx\)

Formula: Slope Formula

This formula gives the slope b. Divide the total of the products by the total of the squared x differences.

\(b = \frac{\Sigma (x - \bar{x})(y - \bar{y})}{\Sigma (x - \bar{x})^{2}}\)

Formula: Intercept Formula

This formula gives the intercept a. Take b times the mean of x away from the mean of y.

\(a = \bar{y} - b\bar{x}\)

Step 1

Step 1 - Set up the problem.

In this problem: We are finding the straight line that fits the points \((x, y)\) best.

\(\begin{array}{c|c} x & y \\ \hline 1 & 8 \\ 2 & 6 \\ 3 & 5 \\ 4 & 1 \end{array}\)

Step 2

Step 2 - Write the formulas.

In this problem: The line is \(\hat{y} = a + bx\). The slope b compares how x and y move together with how much x spreads out, and the intercept a makes the line pass through \((\bar{x}, \bar{y})\).

\(\hat{y} = a + bx\)
\(b = \frac{\Sigma (x - \bar{x})(y - \bar{y})}{\Sigma (x - \bar{x})^{2}}\)
\(a = \bar{y} - b\bar{x}\)

Step 3

Step 3 - Find the mean of the x values and of the y values.

In this problem: Both means are needed before the differences can be found.

\(x: 1,\; 2,\; 3,\; 4 \qquad y: 8,\; 6,\; 5,\; 1\)

Step 3a

Step 3a - Find the mean of the x values.

In this problem: The mean of the x values is \(\bar{x} = 2.5\).

\(\bar{x} = 2.5\)

Step 3b

Step 3b - Find the mean of the y values.

In this problem: The mean of the y values is \(\bar{y} = 5\).

\(\bar{y} = 5\)

Step 4

Step 4 - Subtract the means from each value.

In this problem: Taking \(\bar{x} = 2.5\) from each x value and \(\bar{y} = 5\) from each y value gives the differences.

\(\begin{array}{c|c|c|c} x & y & x - \bar{x} & y - \bar{y} \\ \hline 1 & 8 & -1.5 & 3 \\ 2 & 6 & -0.5 & 1 \\ 3 & 5 & 0.5 & 0 \\ 4 & 1 & 1.5 & -4 \end{array}\)

Step 5

Step 5 - Multiply and square the differences.

In this problem: Each row's two differences are multiplied together, and each difference is squared.

\(\begin{array}{c|c|c|c} x - \bar{x} & y - \bar{y} & (x - \bar{x})(y - \bar{y}) & (x - \bar{x})^{2} \\ \hline -1.5 & 3 & -4.5 & 2.25 \\ -0.5 & 1 & -0.5 & 0.25 \\ 0.5 & 0 & 0 & 0.25 \\ 1.5 & -4 & -6 & 2.25 \end{array}\)

Step 6

Step 6 - Add up the two new columns.

In this problem: The slope formula needs the total of the products and the total of the squared x differences.

\(\Sigma (x - \bar{x})(y - \bar{y}),\; \Sigma (x - \bar{x})^{2}\)

Step 6a

Step 6a - Add the products.

In this problem: Adding the column gives \(-4.5 + \left(-0.5\right) + 0 + \left(-6\right) = -11\).

\(\Sigma (x - \bar{x})(y - \bar{y}) = -4.5 + \left(-0.5\right) + 0 + \left(-6\right) = -11\)

Step 6b

Step 6b - Add the squared x differences.

In this problem: Adding the column gives \(2.25 + 0.25 + 0.25 + 2.25 = 5\).

\(\Sigma (x - \bar{x})^{2} = 2.25 + 0.25 + 0.25 + 2.25 = 5\)

Step 7

Step 7 - Find the slope b.

In this problem: Replacing the two totals in the slope formula and dividing gives \(b = -2.2\).

\(b = \frac{-11}{5} = -2.2\)

Step 8

Step 8 - Find the intercept a.

In this problem: Replacing \(\bar{y}\), \(b\) and \(\bar{x}\) in the intercept formula gives \(a = 10.5\).

\(a = 5 - \left(-2.2\right) \times 2.5 = 10.5\)

Step 9

Step 9 - Write the equation of the line.

In this problem: Putting \(a\) and \(b\) into \(\hat{y} = a + bx\) gives the line of best fit.

\(\hat{y} = 10.5 - 2.2x\)

Step 10

Step 10 - Draw the line through the points.

In this problem: The orange line is \(\hat{y} = 10.5 - 2.2x\). It passes as close as it can to every point.

Scatter plot with the line of best fit

Final Answer

\(\hat{y} = 10.5 - 2.2x\)

Notes and Practice Mode

Example Problems

Click one to load it into the box and solve it.