Linear Regression


Linear Regression

Michael
Alright Maria, today we're tackling something huge in statistics: regression. And honestly, it's probably the main reason most people take a stats class. It's all about prediction—you have some data and you want to predict one variable based on another. Think stock prices, or demand based on price. The simplest version is linear regression, and that's where we start.

Maria
So linear regression just fits a straight line to data, right? But the data never actually lies perfectly on a line. So how do we decide which line is the best?

Michael
Exactly. The idea is that the true relationship is y equals a plus b x plus some noise. The noise accounts for everything we don't know. To find the best a and b, we use the 'least squares' criterion: we measure the vertical distance from each data point to the line, square those distances, and sum them up. The line that makes that sum smallest is our best fit.

Maria
Why vertical distances? And why square them instead of, say, taking absolute values?

Michael
Great question. Vertical distances are the natural choice because we're trying to predict y given x. Squaring makes the math nice—we can take derivatives and get a closed-form solution. And it turns out that if the noise has mean zero and is uncorrelated with x, then the least squares line gives you the true parameters. You could use absolute deviations, but you wouldn't get a simple formula.

Maria
So what's the actual formula for the slope and intercept? I remember something about covariance.

Michael
Right. If we think about the population—the true distribution—then the slope b is the covariance between x and y divided by the variance of x. And the intercept a is the expected value of y minus b times the expected value of x. That's derived by minimizing the expected squared error. Of course, we don't know those expectations, so we estimate them with sample averages.

Maria
And that gives us the familiar b_hat and a_hat formulas. But you mentioned assumptions about the noise. What exactly do we need to assume?

Michael
The key assumptions are that the noise has mean zero and is uncorrelated with x. That ensures the least squares estimates are unbiased for the true parameters. Often we assume independence, which is stronger, but the minimal condition is zero covariance. If those hold, the formulas we derived for the population work out nicely.

Maria
Okay, so that's univariate regression with one x. But what if we have many predictors? Like trying to predict body density from wrist circumference, height, and age—that's multivariate, right?

Michael
Exactly. In multivariate regression, we have p predictors. We write the model as y = a + b1 x1 + b2 x2 + ... + bp xp + noise. To make it cleaner, we include the intercept as a predictor that's always 1, so it becomes y = x^T beta + noise, where beta is a vector of all coefficients. Then we can stack all the data into a matrix: each row is one observation's x values, and we have a column of ones for the intercept.

Maria
That's a lot of parameters to estimate at once. How do we find the best beta vector?

Michael
Same principle: minimize the sum of squared errors. In matrix form, that's minimizing the squared norm of y minus X beta. Setting the gradient to zero gives a system of equations that leads to a closed-form solution: beta_hat equals (X^T X) inverse times X^T y. That's the least squares estimator for the multivariate case.

Maria
So the whole idea extends naturally from one predictor to many. And all because we started with the simple idea of drawing a line through a scatterplot.

Michael
Exactly. Linear regression is simple, but it's incredibly powerful. Even when the true relationship isn't linear, you can transform variables—like taking logs—to make it linear. That's why it's the cornerstone of prediction and machine learning.

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