Linear Regression Calculator

Enter your x and y values to fit a straight line and predict new values.

Numbers separated by commas, spaces or new lines.

One y for each x, in the same order.

Optional: an x value to predict y for.

Try

Result

You enteredx={1, 2, 3, 4, 5}y={2, 4, 5, 4, 5}x = \left\{ 1,\ 2,\ 3,\ 4,\ 5 \right\} \quad y = \left\{ 2,\ 4,\ 5,\ 4,\ 5 \right\}

y=0.6x+2.2\displaystyle y = 0.6x + 2.2

Slope

0.6\displaystyle 0.6

Intercept

2.2\displaystyle 2.2

r

0.774597\displaystyle 0.774597

r²

0.6\displaystyle 0.6
Show the work
  1. Means of x and y

    Each value is measured from the mean of its own variable, so the means come first. There are n=5n = 5 pairs.

    xˉ=155=3yˉ=205=4\begin{gathered} \bar{x} = \frac{15}{5} = 3 \\ \bar{y} = \frac{20}{5} = 4 \end{gathered}
  2. Sums of squares and products

    SxxS_{xx} measures how spread out the x values are, and SxyS_{xy} how x and y move together: it is positive when they rise together and negative when one rises as the other falls.

    Using Sxy=∑(x−xˉ)(y−yˉ)S_{xy} = \sum \left(x - \bar{x}\right)\left(y - \bar{y}\right)

    Sxx=(1−3)2+(2−3)2+(3−3)2+(4−3)2+(5−3)2=10Sxy=(1−3)(2−4)+(2−3)(4−4)+(3−3)(5−4)+(4−3)(4−4)+(5−3)(5−4)=6\begin{gathered} S_{xx} = \left(1 - 3\right)^2 + \left(2 - 3\right)^2 + \left(3 - 3\right)^2 + \left(4 - 3\right)^2 + \left(5 - 3\right)^2 = 10 \\ S_{xy} = \left(1 - 3\right)\left(2 - 4\right) + \left(2 - 3\right)\left(4 - 4\right) + \left(3 - 3\right)\left(5 - 4\right) + \left(4 - 3\right)\left(4 - 4\right) + \left(5 - 3\right)\left(5 - 4\right) = 6 \end{gathered}
  3. Slope of the least-squares line

    The line of best fit is the line that makes the sum of the squared vertical distances to the points as small as possible. Its slope is the shared movement of x and y divided by the spread of x.

    m=SxySxx=610=0.6\begin{aligned} m &= \frac{S_{xy}}{S_{xx}} \\ &= \frac{6}{10} \\ &= 0.6 \end{aligned}
  4. Intercept of the least-squares line

    The best-fit line always passes through the point of means, so the intercept follows from putting xˉ\bar{x} and yˉ\bar{y} into y=mx+cy = mx + c.

    c=yˉ−mxˉ=4−0.6×3=4−1.8=2.2\begin{aligned} c &= \bar{y} - m\bar{x} \\ &= 4 - 0.6 \times 3 \\ &= 4 - 1.8 \\ &= 2.2 \end{aligned}
  5. Equation of the line of best fit

    Putting m=0.6m = 0.6 and c=2.2c = 2.2 into the equation of a line gives y=0.6x+2.2y = 0.6x + 2.2.

    Using y=mx+cy = mx + c

  6. Result

    So the line of best fit is y=0.6x+2.2y = 0.6x + 2.2.

Worked examples