0:04

In this lesson, we're going to describe what linear regressions are,

discuss some use cases for them.

We're also going to look at common methods for performing linear regressions.

And then finally, we're going to look at how we can calculate the error when you're

performing a linear regression analysis.

Before we dive into what linear regressions are,

let's first discuss when you use them.

Let's go ahead and imagine that we've collected a set of data where we've

collected the height and weight of a bunch of people.

And now given this set of data, we want to predict someone's weight,

given their height.

0:39

So what we can do is, is we can plot all these data points on a graph,

where the y axis is the weight, and the x axis is the height.

And then, what we can do is we can see

visually that there's a positive correlation in our data.

So as one's height increases, so does their weight.

Moreover, we can see that this correlation is fairly linear.

There is some variance, which is the measure of how far a random value is

from its expected mean, but for the most part this data follows a line.

So if we wanted to predict a person's weight, how would we do it?

Well, this is where linear regression comes in.

We can perform a linear regression on this data to create a line of best fit.

And then, given a particular height, the independent variable,

we can estimate a predicted weight.

1:40

And now this is pretty awesome, but what is even more awesome is that you can apply

linear regressions just like this to any number of independent variables.

And so, rather than finding a line of best fit, you might be finding a plane of best

fit, or even more complex shapes as you move into higher and higher dimensions.

1:59

Now there are many different methods for performing a linear regression.

And there are pros and cons to each method.

And we don't have time to sketch each and every one.

And if you're curious, I highly recommend that you look at the Wikipedia article

linked in the lesson notes to find out more.

But in this lesson, we're going to discuss one of the most common methods for

performing a linear regression, which is called ordinary least squares.

And we're going to go ahead and walk through how this technique actually works.

So what this technique does, is it calculates the distance

between a predicted point on our line and an actual data point.

2:37

And then, we do that for every single point of data that we have.

And then once we have all these distances, we then square them.

Thus, the name least squares.

Now you might say, hey, those aren't squares those, are rectangles, and

that's because our axes aren't scaled the same way.

So if these axes were actually the same,

these would be nice little green squares instead of green rectangles.

Anyway, back to the method.

It's called least squares, because we're trying to find a line of best fit

that minimizes the sum of all of these areas.

And that's pretty much how ordinary least squares works.

But now, it's really important to point out that linear regression isn't great for

all types of data.

Say that we have data that tracks the temperature every hour of the day for

a certain geographic location.

If we were to apply a linear regression to this data,

we can totally get a line of best fit.

But as you can see, this line of best fit doesn't really fit this data very well.

And while we could find a prediction of temperature given an hour in the day,

we can see that it's not going to be very effective.

And that's because linear regressions work on data with linear correlations.

Sometimes determining this is pretty obvious, like in this visualization,

where you can pretty clearly see that there's a linear correlation.

But especially when you have many different independent variables that

you're trying to do a linear regression on, it's not going to be quite this easy.

4:11

And that's why we need to calculate error.

And again, there are many different ways to calculate error for

a line of best fit or a plane of best fit, what have you.

But we're going to talk about one of the most common,

which is called mean squared error.

And this is actually very tightly related to our least squares method for

how we determined our line of best fit.

4:32

Similarly, how this method works is that we calculate the difference between

a predicted value on the line in an actual value.

And again, we then square those differences which are called residuals.

And then, we take all these different squares and we average those together.

And that's the mean squared error.

And so we can use the single value as a value for error for

this line of best fit, for the set of data.

So now lets talk about what we learned in this lesson.

We saw what linear regressions are at a very high level.

We saw a very basic use case for them.

We saw a big list of different methods for calculating linear regression.

And we walked through one of the most common ones,

which is the least squares method.

And then finally we saw how it's important that you

use a linear regression on data with a linear correlation.

And how you can use error calculation using mean squared error to

verify that your line of best fit actually is fitting your data properly.