Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 13

Multiple Linear Regression

Multiple regression extends simple linear regression by including multiple explanatory variables. This allows us to assess the influence of each predictor on the response while controlling for others. This chapter covers model specification, interpretation, diagnostics and variable selection, with examples and code in R, Python and Julia.

Model Formulation

Suppose we have a response variable YY and pp predictors X1,…,XpX_1,\dots,X_p. The multiple regression model is Yi=β0+β1xi1+⋯+βpxip+εi,εi∼Normal(0,σ2).Y_i = \beta_0 + \beta_1 x_{i1} + \cdots + \beta_p x_{ip} + \varepsilon_i, \quad \varepsilon_i \sim \mathrm{Normal}(0, \sigma^2). We collect the predictors in a design matrix 𝐗\mathbf{X} and estimate the coefficients by least squares: 𝛃̂=(𝐗⊤𝐗)−1𝐗⊤𝐲\hat{\boldsymbol{\beta}} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}.

Interpretation of Coefficients

Each coefficient βj\beta_j represents the expected change in YY for a one–unit increase in XjX_j, holding all other predictors constant. This “partial effect” interpretation differs from simple regression, where the effect of XX is not adjusted for other variables. Coefficients can change substantially when predictors are added or removed, especially when predictors are correlated (multicollinearity).

Adjusted R2R^2 and Model Fit

The coefficient of determination R2R^2 measures the proportion of variance explained by the model. However, R2R^2 increases as more predictors are added even if they contribute little. The adjusted R2R^2 penalises model complexity: Radj2=1−(n−1)(1−R2)n−p−1.R^2_{\mathrm{adj}} = 1 - \frac{(n-1)(1-R^2)}{n-p-1}.

Example: Predicting House Prices

Consider a dataset containing house prices (YY), square footage (X1X_1) and number of bedrooms (X2X_2). We fit a multiple regression model.

Worked example

Multiple regression in Python, R and Julia.

import pandas as pd
import statsmodels.api as sm

data = pd.DataFrame({
    'price': [250, 300, 320, 360, 400],
    'sqft': [1400, 1600, 1700, 1875, 2000],
    'bedrooms': [3, 3, 4, 4, 5]
})
X = data[['sqft', 'bedrooms']]
X = sm.add_constant(X)
model = sm.OLS(data['price'], X).fit()
print(model.summary())
df <- data.frame(price = c(250,300,320,360,400),
                 sqft = c(1400,1600,1700,1875,2000),
                 bedrooms = c(3,3,4,4,5))
fit <- lm(price ~ sqft + bedrooms, data = df)
summary(fit)
using DataFrames, GLM
df = DataFrame(price=[250,300,320,360,400],
               sqft=[1400,1600,1700,1875,2000],
               bedrooms=[3,3,4,4,5])
lm_fit = lm(@formula(price ~ sqft + bedrooms), df)
coef(lm_fit)

The summaries show estimated coefficients for square footage and bedrooms. Each slope reflects the expected price increase per unit of the predictor, adjusting for the other variable.

Diagnostics

As with simple regression, we must check linearity, homoscedasticity and normality of errors. Additional issues arise in multiple regression:

Variable Selection

Selecting a subset of predictors balances model complexity and predictive accuracy. Common approaches include:

Exercises

Practice

  1. Use a public dataset (e.g., cars dataset with mpg, horsepower, weight) to fit a multiple regression predicting mpg. Interpret the coefficients. Compute the adjusted R2R^2.

  2. Simulate data with two predictors where one is highly correlated with the other. Fit multiple regression and compute VIF. Observe the instability in coefficient estimates.

  3. Use forward selection to choose predictors for predicting house prices from square footage, bedrooms and age of house. Perform this in R or Python.

  4. Show that the least–squares estimator 𝛃̂\hat{\boldsymbol{\beta}} minimises the sum of squared residuals. Derive its unbiasedness and variance under the model assumptions.

References