# Intro

Simple Linear Regression (SLR) has been tickled to death. One interesting tidbit about SLR is that of the different Sum of Squares formulations that exist and how they tie into just about everything. This posts tries to deconstruct the sum of squares formulations into alternative equations.

# Definitions

In the least technical terms possible….

Sum of Squares provides a measurement of the total variability of a data set by squaring each point and then summing them.

More often, we use the Corrected Sum of Squares, which compares each data point to the mean of the data set to obtain a deviation and then square it.

, where the mean is defined as: $\bar x = \frac{1}{n}\sum\limits_{i = 1}^n { {x_i} }$

When we talk about Sum of Squares it will always be the later definition. Why? Well, using the initial definition is sure to cause a data overflow when working with large number (e.g. 1000000000000^2 vs. (1000000000 - 1000000)^2).

# Arrangements

There are three key equations:

1. Sum of Squares over $x$: ${S_{xx} } = \sum\limits_{i = 1}^n { { {\left( { {x_i} - \bar x} \right)}^2} }$
2. Sum of Squares over $y$: ${S_{yy} } = \sum\limits_{i = 1}^n { { {\left( { {y_i} - \bar y} \right)}^2} }$
3. Sum of $x$ times $y$: ${S_{xy} } = \sum\limits_{i = 1}^n {\left( { {x_i} - \bar x} \right)\left( { {y_i} - \bar y} \right)}$

Psst… The last one isn’t a square! In fact, it’s part of what’s called covariance. It’s listed here because of the similarities in manipulations that you will see later on.

These initial arrangements can be modified to take on different forms such as:

and

The next two sections go into depth on how to manipulate these equations. The main point behind manipulating these equations is the use of the mean definition and some series properties.

# Providing different forms of the Sum of Squares for $S_{xx}$ and $S_{yy}$

These arrangements can be modified rather nicely to alternative expressions.

For instance, both 1 and 2 can be modified to be:

We’ll call this result the alternative definition.

We can further manipulate this expression…

The last result we’ll refer to as the exterior definition.

Therefore, as stated previously, we have:

Psst… For $S_{yy}$, simply replace every $x$ you see above with a $y$.

e.g.

# Exploring the different forms of $S_{xy}$

Based on the previous section, what comes next should not be very surprising. The only real difference between these two sections is the inclusion of a different variable AND the fact that the number of observations between $x$ and $y$ are the same (e.g. $n_{x} = n_{y} = n$).

The result is a modified verison of the alternative definition.

We can obtain the similar form as the previous section, except this time we must choose to either have $y_i$ or $x_i$ on the exterior… Let’s start by opting for $y_i$ on the exterior:

Alternatively, we can go the opposite route and have $x_i$ on the exterior:

Both are results from the exterior definition.

Therefore, we have the following equations:

# A simple test

The above manipulation can be further scrutinized by seeing if it is accurate. To do so, let’s quickly right a few R functions to check the output.

Let’s formulize the definitions.

Now, let’s see the results of each function:

# Timing

Aside from the derivations and the simple tests, there is one other item to consider… The amount of time it takes to calculate each equation.

In this case, we see that for the $S_{xx}$ and $S_{yy}$ the alternative definition is best whereas if we have $S_{xy}$ then the best speed is from the exterior definition.