1. Home
  2. Explore
  3. Statistics
  4. Linear Regression Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Linear Regression Fundamentals

Speaker 1Speaker 2

Linear Regression Fundamentals

8:09
0:00-8:09
  1. Simon

    Welcome to Stat Quest. Today we’re tackling linear regression, also called a general linear model. We’ll build it around three big ideas: fitting a model with least squares, measuring how much variation it explains with R-squared, and asking whether that result is statistically convincing with a p-value.

  2. Maria

    Let’s start with a concrete example: we measure the weight and size of several mice, and we want to use weight to predict size. We plot the data and draw a line through it. But how do we decide which line is the best one?

  3. Simon

    For each candidate line, measure the vertical distance from every data point to the line. That distance is called a residual. Square each residual, then add the squares together. The best-fitting line is the one that makes this sum of squared residuals as small as possible.

  4. Maria

    So if we rotate the line, the residuals change, and their squared sum changes too. We can imagine plotting that sum against the line’s rotation. The rotation at the lowest point gives the least-squares fit. That’s why it’s called least squares: it minimizes the sum of squared distances from the data to the line.

  5. Simon

    A straight-line model has two estimated parameters: an intercept and a slope. The slope tells us how the predicted mouse size changes with weight. If the slope isn’t zero, weight helps us make a prediction—but we still need to ask how good that prediction is.

  6. Maria

    That’s where R-squared comes in. First, consider variation in mouse size without using weight at all. We can predict the mean size for every mouse, measure each mouse’s distance from that mean, square those distances, and add them. Call that sum of squares around the mean, or SS mean.

  7. Simon

    Now compare that with the fitted line. Measure each mouse’s residual from the line, square the residuals, and add them. That’s the sum of squares around the fit, or SS fit. Dividing either sum of squares by the number of observations gives the corresponding average squared deviation—what the source calls variation around the mean or around the fit.

  8. Maria

    The fitted line will generally leave less variation than the mean-only prediction. The reduction represents variation explained by taking weight into account. R-squared measures that reduction as a fraction of the original variation: variation around the mean minus variation around the fit, divided by variation around the mean.

  9. Simon

    You can use the sums of squares directly in that same formula, because both variation terms are divided by the same sample size, and that factor cancels. So, in sum-of-squares form, R-squared is SS mean minus SS fit, divided by SS mean.

  10. Maria

    For example, if variation around the mean is 11.1 and variation around the fitted line is 4.4, R-squared is about 0.6. That means the model reduced the variation by 60 percent. We can also say that, in this example, mouse weight explains 60 percent of the variation in mouse size.

  11. Simon

    At the extremes, if the fitted line predicts every observation perfectly, the residual variation is zero and R-squared is one, or 100 percent. If using weight doesn’t reduce the variation at all, the two variation values are equal and R-squared is zero. Weight then explains none of the variation captured by the mean-only comparison.

  12. Maria

    The idea isn’t limited to straight lines or one predictor. R-squared compares the data’s variation around its mean with the variation left around whatever equation you fit—even a more complicated one. You calculate the two sums of squared distances and put them into the same formula.

  13. Simon

    With two predictors, such as mouse weight and tail length, we can visualize the data in three dimensions and fit a plane instead of a line. The model has an intercept plus one coefficient for weight and one for tail length. Substituting a mouse’s measurements into that equation gives a predicted body length.

  14. Maria

    And each coefficient represents a parameter the model estimates. If tail length adds no improvement to the fit, least squares can assign its coefficient a value of zero. More generally, adding predictors cannot make the least-squares sum of squares worse, because the expanded model can effectively ignore a useless predictor.

  15. Simon

    But that creates a catch: a more complicated model can look better just by chance. For example, a meaningless extra variable might happen to line up with the outcome in a particular sample and reduce the residual sum of squares. The more parameters we add, the more opportunities there are for this kind of accidental improvement.

  16. Maria

    That’s why people may report adjusted R-squared as well as R-squared. Adjusted R-squared accounts for how many parameters the model uses, rather than rewarding every added term without qualification. It helps address the tendency for ordinary R-squared to improve when predictors are added.

  17. Simon

    We also need to be careful about interpreting R-squared from very small data sets. With just two points, a straight line can always pass through both, so the residual sum of squares is zero and R-squared becomes 100 percent. But any two points would give that result, so it doesn’t by itself show that there’s a meaningful relationship.

  18. Maria

    That motivates a p-value. In the approach discussed here, we calculate an F statistic: the variation explained by adding predictors divided by the variation that remains unexplained. The numerator captures the reduction in variation; the denominator is based on the residuals left after fitting the model.

  19. Simon

    The F calculation uses degrees of freedom to account for the number of parameters. Let p fit be the number of parameters in the fitted model, and p mean the number in the mean-only model. For a line with an intercept and a slope, p fit is two, while p mean is one. Their difference, one, is the number of extra parameters—here, the weight slope.

  20. Maria

    If we add both weight and tail length, the fitted model has an intercept and two predictor coefficients, so p fit is three. Compared with the one-parameter mean model, that’s two extra parameters. The numerator of the F statistic reflects variation explained by those extra parameters.

  21. Simon

    The denominator reflects unexplained variation, adjusted for the fitted model’s degrees of freedom: the residual sum of squares is divided by n minus p fit, rather than simply by n. Intuitively, estimating more parameters requires more data. A model that explains a lot of variation while leaving little unexplained variation tends to produce a larger F statistic.

  22. Maria

    To understand the p-value conceptually, imagine repeatedly generating data under a no-relationship scenario, calculating F for each generated data set, and building a distribution of those F values. Then compare the F from the real data with that distribution. The p-value is the proportion of null-generated values at least as extreme as the observed one.

  23. Simon

    In practice, rather than generating an enormous number of random data sets, people use an F distribution to obtain the p-value. Its shape depends on the degrees of freedom, including the sample size relative to the number of fitted parameters. With more samples relative to the number of parameters, the distribution described here tapers off faster, which can lead to a smaller p-value for a given result.

  24. Maria

    So the two measures answer different questions. R-squared tells us how much of the variation the model explains, while the p-value based on F tells us how surprising that result would be under the no-relationship comparison. A compelling result calls for both a reasonably large R-squared and a small p-value.

  25. Simon

    That’s the core of linear regression in this lesson: least squares finds the fit by minimizing squared residuals, R-squared summarizes the reduction in variation, and the F-based p-value helps assess whether that reduction is statistically convincing. Keep those three ideas together when you interpret a model.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent