Video Transcript: Next Steps with Data - Part 3
Welcome. Can you believe it? We're on the last lecture of the last section. Let's introduce the idea of linear regression. So, let's review correlation really quick. It's been a while since we've talked about it. The Pearson correlation coefficient, little r, is a measure of strength of the linear relationship between two variables. Remember, negative values of correlation imply a negative linear relationship. Positive values of correlation imply a positive linear relationship, where values near zero basically imply that there's no real relationship, but no real linear relationship between two variables. However, correlation is not everything. Correlation is a measure of strength of a linear relationship, however, it does not say what the linear relationship actually is. The plot that you see here on the right-hand side has two different sets of data, one of them being the circles, one of them being the x's. Both of those two sets of data have a correlation of 0.99. The x's have a correlation of 0.99 with themselves, and the circles have a correlation of 0.99 with themselves. However, if you notice the actual linear relationship is different between them, they have the same strength of relationship, but they don't have the same direction of relationship. One of them is a little bit more steep than the other. That gets to the idea of regression modeling. Many people across industries devote tons of research funding to discover how variables are related and correlated with each other. This is what we refer to as modeling. The simplest graphical technique to relate two quantitative variables is through a straight line relationship. We call this a simple linear regression model. Sometimes people will call these SLRs. The most models are, in all honesty, more extensive and more complicated than a simple linear regression, but simple linear regression models form a good foundation. If you can get the idea of simple linear regression. You can extend that idea to have as many variables as you want. Let me give you an example through our bike data set. What if you wanted to predict the number of registered users in a single day based on the temperature outside? Well, what is the best guess line for the following equation? So I have predicted users and I have temperature, so I want to take temperature, multiply it by a number, add a number, and that is going to be my predicted users. I want a straight line on a scatter plot. Well, let's take a look, this B with a little zero that's actually a beta, this b with a little one that's also a beta. So you have beta zero plus beta one times temperature. What we have is a slope and an intercept. Let me show you what I mean. The intercept is what you see here. The intercept would be where this line crosses the y axis, that is this value that you see highlighted. So, where this line crosses the y axis, whatever value on the y axis that is, that is the intercept. The slope, on the other hand, is how steep this line is pointed upwards. So, again, we can think about this as what we call rise over run. How high up does it go for every joint, for every movement over? So, again, think about the angle at which you're actually pointing upwards or downwards, that's the slope. The intercept would be again where you, where that line connects with the y axis. So, through the
use of linear regression and through the use of computers, we can find out that the best guess line for the relationship between temperature and total daily registered users is the following: it would be take the temperature, multiply that by 54.4 add 418.42 and that would give you the predicted number of users, that would give you this straight line you see here, the intercept being 418 again taking a look here, where it crosses the y axis is at 418 the slope is. 54.4 that is the slope of this line here, but how do we figure this out? Like we call this line the best guess line, how do we determine the best guess? Well, let's talk about the idea behind the best guess line in linear regression. We're going to talk about it with a very simple example with only five data points. Let's imagine you were predicting sales revenue in 1000s of dollars with advertising expenditure, like you see here on this plot. Well, what is the best line through these five data points? Mean, maybe you draw this line. This line connects three of the data points, it misses two of the others, but it connects these three. Maybe you draw this line, maybe you draw this line. Let's be honest, there's so many different lines that you could pick, and we could all try and eyeball it. But how does a computer tell us one single line? Well, let's look at one of these lines. We'll pick the line that goes through all three of these points here and work through how we could approach this. So, the question now becomes, let's pick this line. How wrong were you at each point? I mean, you got three of the data points basically right, however, you did miss a couple data points, but how wrong were you? Well, what we're going to do is look at the vertical deviations from the data point, which is the circle, to the line, which is our guess. We're going to call these deviations residuals, so by looking at these residuals, we can get an idea of how wrong we are. In fact, we can sum up all of the deviations to calculate some idea of total, quote unquote, error. Right? How wrong were you on each point? Let's figure out how wrong we are across all of the data points, and wouldn't it make sense that we want to try and minimize that. The downside, though, is these errors have both positive and negative values, so for example, we missed one high and missed one low, so they would just cancel each other out if we just added them together. So, what we do instead of just adding the raw errors together, we sum the squared errors. We take each error, each residual, each vertical distance that you see here, we square that number to make it positive. If you square a number, it's always going to be positive. Okay, so now what I'm going to do is I'm going to look at all of those squared distances. That's going to remove the effect of positive and negative, and then I'm going to sum all these squared distances together again, still trying to get an idea of how close to each data point my line is, because I imagine that the best line is going to be the line that is closest to each data point. Well, it can be shown mathematically again. This is beyond this course, but it can be shown mathematically. There is only one line for which the sum of the squared vertical deviations, the sum of the squared residuals, the sum of the squared errors is minimized. That line is called
the line of best fit, also known as the least squares regression line. Why is it called least squares? Well, again, what are we doing? I'm looking at the actual distances from each point to my line. Basically, how wrong is your line on each point? I'm squaring those distances to get some idea of how far away the line is from each data point, and I want to sum all of those things up, and I want to minimize them, so I want again there to be the least amount of squared distance from the line to each point, hence the idea least squares. So let's go back to our five data point example. Oh, wait a minute. See, you may have thought just because you got those three data points exactly right, that that was the best line. Actually, it's not the line of best fit for the five data points in the scatter plot, is actually shown in the darker line here. This is not the original line we use for our predictions. Computers can actually quite easily and quickly calculate this best line for us. One way you can imagine it, even though it's not the exact way computers do it, you can imagine they're testing many, many, many, many, many different lines and figuring out the one that has the least amount of error. In fact, the line that had the least amount of error, your predicted sales revenue was negative 0.1 plus 0.7 times the advertising expenditure. This darker line is closest to all of the points compared to any other line, at least in terms of squared vertical distances, so let's go back to our bike data example. This line that you see here on the right-hand plot, that is the line that is closest to all of the data points that you see highlighted there again in terms of squared vertical distances, so that line is our quote unquote best line for being able to predict users based on temperature. Isn't that neat? Now, again, we're not going to get into all the mathematics behind how we can do this, and how we can test this, and why this is the best line that's for another class and another day, but this is just to give you a taste of what statistics can really do now that you have the foundation of this course. So, let's summarize the simplest graphical technique to relate two quantitative variables is through a straight line relationship on a scatterplot. This is called the simple linear regression model. What we do to calculate that simple linear regression model is we look at the vertical deviations from each data point to that line. These vertical deviations are called residuals. It can be shown that there is only one line for which the sum of the squared residuals is minimized, that is called the line of best fit, or the least squares regression line. Wow, can you believe it? Congratulations, you are now through the course. Thank you all so much for viewing this course. I look forward to seeing you in another one.