Calculating Line Regression by Hand

When there are more than 2 points of data it is usually impossible to find a line that goes exactly through all the points. But, usually we can find a line (or curve) that is a good approximation to the data. For most science fair projects, a line of best fit is what is needed, and that's what we will be finding on this page.

y = y1 + (x-x1)
y2 - y1
x2 - x1

If you have a calculator, bring it out because it will be helpful in adding up all of these numbers. Here's our sample data given below:


It is important for us to keep our numbers straight, so we have created a few variables below which we defined to the right.

For our example, here's how you would calculate these:

The best form for our line is slope-intercept form, which looks like y = mx + b. Therefore, it is only necessary to compute m and b to determine the best fit line. Those values can be computed by the following equations:

m =
(Nxysum) - (xsumysum)
(Nx2sum) - (xsumxsum)
b =
(x2sumysum) - (xsumxysum)
(Nx2sum) - (xsumxsum)

After plugging in the values that we found, we get: m = .99992 and b = -2.0067.

This means that the equation of the line is y = .99992x + -2.0067, or y = .99992x - 2.0067. We graphed the results using Matlab, but you can even make a graph by hand.

Example best fit line

For a more detailed mathematical explanation of this topic, see MathWorld. Beware, calculus is needed.

This line looks like it accurately depicts the data, but can we be sure simply by looking at it?

The Correlation

Correlation is a measue of how close the line fits the points that you found in your experiment. Anyone can say whether they think the line is a good fit or not, but to measure it exactly, we use the correlation coefficient: R2.

Look at the following two plots. In the first, the line fits very well, and in the second, then line fits very poorly.

R2 = .99992
R2 = .000142

Notice the value R2 differs in both graphs. The lines that fit the points best have a value of R. R is the correlation coefficient.

R2 =
(Nxysum - xsumysum) 2
(Nx2sum - xsumxsum) (Ny2sum - ysumysum)

From comparing the graphs to the R values, you can probably see that the closer R is to 1, the better the line fits your data. When R is far from 1, your line will not represent the data at all. This is easily seen above, and for more information please see MathWorld.

To see how to quickly find the equation of the best fit line and the correlation coefficient using Microsoft Excel (or Open Office Software), visit our Excel Line Regression webpage.