Regression Model Building


Regression Model Building

Michael
Welcome to Data Analytics with Python. Today we’re looking at regression model building: how to decide which variables and what kind of relationship belong in an estimated regression equation. We’ve already seen simple and multiple linear regression; now we’ll use a real example to see why choosing a model involves more than just running a regression.

Hope
So when we say “model building,” we mean using data to develop an estimated equation that describes how a dependent variable relates to one or more independent variables. The challenge is deciding which independent variables to include—and whether the relationship should be represented in a straight-line form or something more flexible.

Michael
Exactly. Suppose we collect observations on a dependent variable, y, and k possible predictors, x1 through xk. The goal is to use those observations to find a useful estimated relationship. In a general linear regression model, we can write y as beta zero plus beta one times z one, and so on, up to beta p times z p, plus an error term. Each z can be a function of the original x variables.

Hope
That distinction matters. A z might simply be one of the original predictors, but it could also be a transformed version—like a squared predictor. With just one predictor entered as x, the simplest first-order model is y equals beta zero plus beta one x plus an error term.

Michael
Our example comes from Reynolds, a manufacturer of industrial scales and laboratory equipment. The company wants to investigate whether the length of time a salesperson has worked there is related to the number of electronic laboratory scales that person sells. The data are for 15 randomly selected salespeople: months employed is x, and scales sold in the most recent sales period is y.

Hope
The initial expectation is that more experienced salespeople may sell more. A scatter plot is a sensible first look, because it lets us see the shape and direction of the relationship before settling on a model. Here, the points show a positive trend: sales generally rise as months employed increases.

Michael
In Python, the lecture reads the Reynolds Excel file with pandas and uses NumPy, Matplotlib, and statsmodels. It plots months employed against scales sold, then fits an ordinary least squares model with statsmodels. Adding a constant to the predictor matrix includes the intercept in the regression.

Hope
The fitted simple linear equation is approximately scales sold equals 111.2 plus 2.37 times months employed. In this equation, the positive slope describes the model’s estimated increase in sales for an additional month employed. The output reports an R-squared of about 0.781, and the overall F-test has a very small p-value—around 1.24 times ten to the minus five—so the linear model is statistically significant overall.

Michael
The predictor’s p-value is also below 0.05, so months employed is statistically significant in this model. But those results don’t settle whether a straight line is the right shape. A significant model and a fairly high R-squared don’t guarantee that its assumptions or its functional form describe the data adequately.

Hope
That’s why we examine residuals—the differences between observed and predicted values. The lecture plots standardized residuals against the predicted values, often written as y-hat. Ideally, the points should look like a roughly patternless rectangular cloud. Instead, the plot suggests a curve, which is evidence that the straight-line model may be missing a curvilinear pattern.

Michael
The dataset is small, so we shouldn’t claim that the plot proves an exact curve. But it gives us a reason to try a more flexible form. One straightforward next step is to add the square of months employed as another predictor. In Python, that means creating x squared, combining it with the original x, and fitting OLS again with a constant.

Hope
The new equation is approximately scales sold equals 45.3 plus 6.34 times months employed, minus 0.0345 times months employed squared. The squared term allows the relationship to bend rather than forcing it to follow one constant straight-line slope. The model is still called a general linear regression model, even though its relationship between x and y can be curved.

Michael
And that name can be confusing. “Linear” here refers to how the regression coefficients enter the equation: beta zero, beta one, and beta two each have an exponent of one. It does not mean that y must have a straight-line relationship with the original x. In this example, the coefficient multiplies x squared, but the coefficients themselves still enter linearly.

Hope
The quadratic model’s output improves the fit: the lecture reports an adjusted R-squared of 88.6 percent, compared with the simple model’s R-squared of about 78.1 percent. Its overall F-test is significant, and the p-value for the squared-months term is 0.002. The lecture also reports that the original months term is significant, with its p-value below 0.05.

Michael
Still, we don’t choose the quadratic model just because its R-squared is higher or its p-values look good. We check its residual plot too. In the second model, the earlier curvilinear pattern is removed, and the residuals look more consistent with a patternless, rectangular spread. That supports the quadratic relationship as a better fit for these data than the simple straight-line model.

Hope
So the model-building lesson is to use several kinds of evidence together: consider the form of the relationship, evaluate overall and individual statistical significance, compare fit measures, and inspect residuals. Here, the residual plot revealed what the initial summary statistics couldn’t: the simple linear model missed a curve, and adding months employed squared helped account for it.

Michael
And model building can also mean selecting which independent variables to include—whether to keep, drop, or add predictors. This lecture’s example focuses on changing the functional form by adding a transformed predictor, rather than exploring a full variable-selection procedure. Next, the course moves to another way predictors can work together: interaction between two independent variables.

Hope
The takeaway is simple: don’t stop at a significant regression output. Plot the data, fit a reasonable starting model, inspect its residuals, and revise the model when the diagnostics suggest the relationship needs a different form. For Reynolds, the squared term captured a curvilinear pattern that the first-order model did not.

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