Skip to main content

Least Squares Trendline and Correlation

Introduction

In the real world, we often gather data that looks like it would be well fit by a certain function or model, but not perfectly. For example, you might have data that looks like it's well fit by a line, but it's a bit scattered. In order to find the line that best fits the data, we need to figure out the correct slope and y-intercept, but it's not always easy to tell what the best line will be...the "least squares" method helps us find this!

LEAST SQUARES LINE

In order to find the best line, we want to minimize the distance of the data points to the line that we select. The most common way to do this is the "least squares" method, where we minimize the square of the distance of the points to the line. This method is illustrated in the diagram below, where we minimize the area of the yellow boxes.

Trendline Graph

If we want to minimize the sum of the squares, the appropriate formula for the slope and y-intercept of the line of best fit becomes:

Trendline Equation

Example: Consider the three data points: (2, 10), (2, 14), (4, 20). Find the least squares line for this set of data.

Solution: First, let's find the quantities that we need to substitute into the formulas.

Trendline Equation

At this point, we just have to plug in the values we found above into the formula for the slope and the y-intercept:

Trendline and Correlation Equation

So, finally, our least squares line (ie. the line of best fit) is y= 5x-1/3

Correlation

In order to determine whether out data is well fit by a line, we define something called the coefficient of correlation, denoted r. Here's how to interpret it:

  • A value close to 1 means that the data is extremely well fit by a line with positive slope.
  • A value close to -1 means that the data is extremely well fit by a line with a negative slope.
  • A value close to 0 means that the data is not well fit by a line.

Trendline Equation

Example: Let's again consider the three data points: (2,10), (3, 14), (4, 20) that we found the least squares line for in the example above. Find the coefficient of correlation.

Solution: We first need to find the quantities that we need to substitute into the formula (all of these were already found above except the last one).

Correlation Coefficient equation

Now, we just go ahead and substitute everything into the formula:

Correlation Coefficient Formula

Since we obtain value that is very close to 1, it appears that the data is well fit by a line with positive slope.

University of Ontario Institute of Technology logo