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.

Â