How to calculate a linear regression line by hand

The linear regression line, y^=b0+b1x\hat{y} = b_0 + b_1x, is calculated by minimizing the sum of squared vertical residuals between observed data and the line. This method applies when you have a set of bivariate numerical data (x,y)(x, y) and need to model the linear relationship without statistical software.

The setup

Define your data pairs as (xi,yi)(x_i, y_i) and count the number of pairs nn. Compute the five fundamental sums: Σx\Sigma x, Σy\Sigma y, Σx2\Sigma x^2, Σy2\Sigma y^2, and Σxy\Sigma xy. Calculate the sample means xˉ=Σxn\bar{x} = \frac{\Sigma x}{n} and yˉ=Σyn\bar{y} = \frac{\Sigma y}{n}.

The steps

  1. Calculate the sums of squares for xx and xyxy: SSxx=Σx2(Σx)2nSS_{xx} = \Sigma x^2 - \frac{(\Sigma x)^2}{n} and SSxy=Σxy(Σx)(Σy)nSS_{xy} = \Sigma xy - \frac{(\Sigma x)(\Sigma y)}{n}. 2. Calculate the slope b1=SSxySSxxb_1 = \frac{SS_{xy}}{SS_{xx}}. 3. Calculate the y-intercept b0=yˉb1xˉb_0 = \bar{y} - b_1\bar{x}. 4. Write the final equation as y^=b0+b1x\hat{y} = b_0 + b_1x.

Checking the result

Verify that the point (xˉ,yˉ)(\bar{x}, \bar{y}) lies exactly on your calculated line by testing if yˉ=b0+b1xˉ\bar{y} = b_0 + b_1\bar{x}. Additionally, verify that the sign of your slope b1b_1 matches the sign of SSxySS_{xy}.

Common errors

Rounding intermediate calculations, especially the means or sums of squares, causes severe compounding errors in the final slope and intercept. Another common error is swapping the independent variable xx and dependent variable yy, which produces an entirely different regression line.

Worked example

Calculate the linear regression line for the following five data points: (1,2),(2,4),(3,5),(4,4),(5,5)(1, 2), (2, 4), (3, 5), (4, 4), (5, 5).

First, find n=5n = 5. Compute the sums: Σx=1+2+3+4+5=15\Sigma x = 1 + 2 + 3 + 4 + 5 = 15. Σy=2+4+5+4+5=20\Sigma y = 2 + 4 + 5 + 4 + 5 = 20. Σx2=12+22+32+42+52=55\Sigma x^2 = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55. Σxy=(1)(2)+(2)(4)+(3)(5)+(4)(4)+(5)(5)=2+8+15+16+25=66\Sigma xy = (1)(2) + (2)(4) + (3)(5) + (4)(4) + (5)(5) = 2 + 8 + 15 + 16 + 25 = 66. Compute the means: xˉ=15/5=3\bar{x} = 15 / 5 = 3. yˉ=20/5=4\bar{y} = 20 / 5 = 4. Next, calculate the sums of squares: SSxx=551525=5545=10SS_{xx} = 55 - \frac{15^2}{5} = 55 - 45 = 10. SSxy=66(15)(20)5=6660=6SS_{xy} = 66 - \frac{(15)(20)}{5} = 66 - 60 = 6. Calculate the slope: b1=610=0.6b_1 = \frac{6}{10} = 0.6. Calculate the intercept: b0=4(0.6)(3)=41.8=2.2b_0 = 4 - (0.6)(3) = 4 - 1.8 = 2.2. The final regression line is y^=2.2+0.6x\hat{y} = 2.2 + 0.6x.

FAQ

Run your own problem

References: OpenStax Introductory Statistics, Chapter 12: Linear Regression and Correlation · Moore, McCabe, Craig: Introduction to the Practice of Statistics

See also