Linear regression is one of the most useful tools in statistics, but the formula is a little hard to remember. If you’re trying to find the “best fit” in the equation , here is the solution:

If you’re expecting me to be able to produce that formula from memory… don’t hold your breath.

However, if you understand what a linear regression is, then re-deriving this formula is actually shockingly easy. And, importantly, remembering “what a linear regression is” is much easier than remembering some (relatively) complicated formula. I suspect this is often true - that remembering how to derive a formula from simple ideas is easier than remembering the formula itself.

An aside: I often struggle with finding the appropriate level at which to target my explanations. Ideally, I’d like to assume no previous knowledge and explain things from scratch, but then the posts become so long as to be useless. But if I explain things too tersely, the only people following along are the people who already understand the explanation! So, in this case, I’m going to write two versions: a short version and a long one.

What is a linear regression: the short version

With real life (noisy) data, there won’t be an exact solution to the equation . Put another way, does not live in the column space of . We need to find a vector which does live in the column space of and which minimizes the squared errors between itself and . Let’s call this vector .

What are the errors between and ? Simply , which is itself another vector. Let’s call this (for errors). We don’t care about the errors themselves as much as the sum of the squared errors, which is just the squared length of .

To recap, we want to find the that lives in the column space of and which minimizes the length of . Note that our three vectors form a triangle, i.e. . At this point, the solution might become clear. If we take to be the projection of onto the column space of , then and will form a right triangle with hypotenuse , and that will minimize the length of .

If that’s not obvious, consider a line and a point which is not already on the line. What’s the minimum distance from to ? It’s the distance of the line which connects to and which is perpendicular to , i.e. it’s the line between and ’s projection onto .

That’s all you have to remember in order to derive the formula for linear regression.

What is a linear regression: the long version

In general, a linear regression is trying to find coefficients to a linear equation that minimize the sum of the squared errors. For example, let’s say you think there’s roughly a linear relationship between the square footage of a house (sqft), the median price of all houses in that house’s neighborhood (medprice), and the price of the house (price), i.e.

Furthermore, you have some data. For each data point (house), you have the three relevant values (sqft, medprice, and price). We can organize this data into a single equation, , using matrices where:

Each row in will contain the two predictor values (sqft and medprice) for a given home, along with a constant 1 (to account for the in our linear equation) and the corresponding row in will have the responder variable (price).

Now, importantly, this equation will almost always have no solution. To understand why, notice that we are trying to find a linear combination of the three columns of that equals the vector . We haven’t yet specified how many data points we have, but for the sake of this part of the explanation let’s assume it’s 100. That means we have a vector which lives in a 100-dimensional space. If that throws you for a loop, think about how a vector with 2 elements lives in the x-y coordinate plane - a 2-D space, while a vector with three elements lives in the x-y-z coordinate system - a 3-D space. So, the vector - with 100 elements - lives in a 100-dimensional space. So do the columns of . They are, after all, each vectors with 100 elements.

If you consider a single column of by itself, and take all linear combinations of it (i.e. you scale it by any value), you will end up with a single line in that 100-dimensional space. We call that a 1-D subspace of the 100-dimensional space. If you consider two columns of A, and take all linear combinations of them, you will end up with a 2-D subspace (a plane through the origin) within that 100-dimensional space. And, probably obviously now, if you consider all three column vectors of A, and take all linear combinations of them, you will end up with a 3-D subspace of the 100-dimensional space. To throw some terminology at you, that 3-D subspace is spanned by the three column vectors of , and it called the column space of A.

Understanding how linear combinations of vectors span a space is critical, so I’ll include the following gif from 3Blue1Brown to help you understand it visually. Notice how by changing the coefficients and , their linear combination () can point anywhere in the plane. That means they span the plane.

I previously said that it’s unlikely that our equation has a solution. Why is that? We can now understand that our equation only has a solution if the 100-dimensional vector happens to lie within the 3-dimensional column space of . That’s kind of like hoping that a bunch of points in 3-D space happen to fall exactly on a single (1-D) line (although a much more extreme version of that). It might happen, but with real data that likely has some noise in it, it’s extremely unlikely.

So, since there’s no solution, we can’t just solve the equation directly by computing . In fact, why don’t we stop writing , because that’s a little misleading given there’s no solution (it’s like writing and , solve for ). Instead, let’s write , where we assert that this equation has a solution. In other words, the only candidates for are the vectors in the column space of .

The next step is to figure out which is “best”. Linear regression is defined as trying to minimize the squared errors, so we want the that minimizes the sum of the squares of the elements of . At this point, I’m going to refer you to the short version. I’ve hopefully filled in the relevant background information to make that explanation accessible.

Quick demonstration that it works