Multiple Linear Regression › Example

Michael BrenndoerferApril 16, 20255 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 31Multiple Linear Regression › Example
Reading section 6 of 11 · Example · 5 minSection 6 of 11 · 5 min

Book contents

Section 6 of 11 · 5 min read

Multiple Linear Regression › ExampleOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete example to see how multiple linear regression works step by step. We'll use a small dataset with two features to make the calculations manageable, and then verify our results using computational tools.

The Dataset

Suppose you're analyzing house prices and have collected data on three houses with two features: size (in square feet) and age (in years). Your target variable is price (in thousands of dollars).

Here's your data:

HouseSize (x₁)Age (x₂)Price (y)
12,00016
20310
34,000516

Setting Up the Matrices

First, let's organize this data into matrix form. The feature matrix X\mathbf{X} includes a column of ones for the intercept:

X=[120001103140005]y=[61016]\mathbf{X} = \begin{bmatrix} 1 & 2000 & 1 \\ 1 & 0 & 3 \\ 1 & 4000 & 5 \\ \end{bmatrix} \qquad \mathbf{y} = \begin{bmatrix} 6 \\ 10 \\ 16 \\ \end{bmatrix}

Notice that the first column of X\mathbf{X} is all ones.that column allows us to estimate the intercept term β0\beta_0.

Step-by-Step OLS Calculation

Now let's compute the OLS solution: β^=(X⊤X)−1X⊤y\hat{\boldsymbol{\beta}} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}

Step 1: Compute X⊤X\mathbf{X}^\top \mathbf{X}

First, we transpose X\mathbf{X}:

X⊤=[111200004000135]\mathbf{X}^\top = \begin{bmatrix} 1 & 1 & 1 \\ 2000 & 0 & 4000 \\ 1 & 3 & 5 \\ \end{bmatrix}

Now multiply X⊤\mathbf{X}^\top by X\mathbf{X}:

X⊤X=[111200004000135][120001103140005]\mathbf{X}^\top \mathbf{X} = \begin{bmatrix} 1 & 1 & 1 \\ 2000 & 0 & 4000 \\ 1 & 3 & 5 \\ \end{bmatrix} \begin{bmatrix} 1 & 2000 & 1 \\ 1 & 0 & 3 \\ 1 & 4000 & 5 \\ \end{bmatrix}

Let's compute each element:

  • (1,1)(1,1): 1×1+1×1+1×1=31 \times 1 + 1 \times 1 + 1 \times 1 = 3
  • (1,2)(1,2): 1×2000+1×0+1×4000=60001 \times 2000 + 1 \times 0 + 1 \times 4000 = 6000
  • (1,3)(1,3): 1×1+1×3+1×5=91 \times 1 + 1 \times 3 + 1 \times 5 = 9
  • (2,1)(2,1): 2000×1+0×1+4000×1=60002000 \times 1 + 0 \times 1 + 4000 \times 1 = 6000
  • (2,2)(2,2): 2000×2000+0×0+4000×4000=20,000,0002000 \times 2000 + 0 \times 0 + 4000 \times 4000 = 20{,}000{,}000
  • (2,3)(2,3): 2000×1+0×3+4000×5=22,0002000 \times 1 + 0 \times 3 + 4000 \times 5 = 22{,}000
  • (3,1)(3,1): 1×1+3×1+5×1=91 \times 1 + 3 \times 1 + 5 \times 1 = 9
  • (3,2)(3,2): 1×2000+3×0+5×4000=22,0001 \times 2000 + 3 \times 0 + 5 \times 4000 = 22{,}000
  • (3,3)(3,3): 1×1+3×3+5×5=351 \times 1 + 3 \times 3 + 5 \times 5 = 35

So:

X⊤X=[360009600020,000,00022,000922,00035]\mathbf{X}^\top \mathbf{X} = \begin{bmatrix} 3 & 6000 & 9 \\ 6000 & 20{,}000{,}000 & 22{,}000 \\ 9 & 22{,}000 & 35 \\ \end{bmatrix}

Step 2: Compute X⊤y\mathbf{X}^\top \mathbf{y}

X⊤y=[111200004000135][61016]\mathbf{X}^\top \mathbf{y} = \begin{bmatrix} 1 & 1 & 1 \\ 2000 & 0 & 4000 \\ 1 & 3 & 5 \\ \end{bmatrix} \begin{bmatrix} 6 \\ 10 \\ 16 \\ \end{bmatrix}

Computing each element:

  • First row: 1×6+1×10+1×16=321 \times 6 + 1 \times 10 + 1 \times 16 = 32
  • Second row: 2000×6+0×10+4000×16=12,000+0+64,000=76,0002000 \times 6 + 0 \times 10 + 4000 \times 16 = 12{,}000 + 0 + 64{,}000 = 76{,}000
  • Third row: 1×6+3×10+5×16=6+30+80=1161 \times 6 + 3 \times 10 + 5 \times 16 = 6 + 30 + 80 = 116

So:

X⊤y=[3276,000116]\mathbf{X}^\top \mathbf{y} = \begin{bmatrix} 32 \\ 76{,}000 \\ 116 \\ \end{bmatrix}

Step 3: Compute (X⊤X)−1(\mathbf{X}^\top \mathbf{X})^{-1}

This is the most complex step. For a 3×3 matrix, we need to compute the determinant and the adjugate matrix. Given the large numbers involved, let's use a computational approach for accuracy.

Step 4: Final Calculation

Using computational tools (which we'll verify in the implementation section), the final result is:

β^=[3.00.0003332.333]\hat{\boldsymbol{\beta}} = \begin{bmatrix} 3.0 \\ 0.000333 \\ 2.333 \\ \end{bmatrix}

Interpreting the Results

The fitted model is:

y^=3.0+0.000333×Size+2.333×Age\hat{y} = 3.0 + 0.000333 \times \text{Size} + 2.333 \times \text{Age}

where:

-y^\hat{y}: predicted house price (in thousands of dollars)

  • Size\text{Size}: house size in square feet
  • Age\text{Age}: house age in years

This means:

  • Intercept (β0=3.0\beta_0 = 3.0): A house with zero size and zero age would cost $3{,}000 (though this interpretation doesn't make practical sense)
  • Size coefficient (β1=0.000333\beta_1 = 0.000333): For each additional square foot, the price increases by $0.33 (0.000333 × 1000)
  • Age coefficient (β2=2.333\beta_2 = 2.333): For each additional year of age, the price increases by $2{,}333

Verification

Let's check how well this model fits our data by calculating predictions for each house:

House 1 (Size = 2000, Age = 1):

y^1=3.0+0.000333(2000)+2.333(1)=3.0+0.666+2.333=6.0\hat{y}_1 = 3.0 + 0.000333(2000) + 2.333(1) = 3.0 + 0.666 + 2.333 = 6.0

Actual: y1=6y_1 = 6 → Perfect prediction!

House 2 (Size = 0, Age = 3):

y^2=3.0+0.000333(0)+2.333(3)=3.0+0+7.0=10.0\hat{y}_2 = 3.0 + 0.000333(0) + 2.333(3) = 3.0 + 0 + 7.0 = 10.0

Actual: y2=10y_2 = 10 → Perfect prediction!

House 3 (Size = 4000, Age = 5):

y^3=3.0+0.000333(4000)+2.333(5)=3.0+1.332+11.665=16.0\hat{y}_3 = 3.0 + 0.000333(4000) + 2.333(5) = 3.0 + 1.332 + 11.665 = 16.0

Actual: y3=16y_3 = 16 → Perfect prediction!

The model provides perfect predictions for this dataset. This is expected because we have exactly as many parameters (3) as data points (3), which allows the model to fit the data exactly. In practice with larger datasets, you would typically see some prediction errors due to noise and the inherent complexity of real-world relationships.

Why Manual Calculations Are Challenging

As you can see, even with just three data points and two features, the matrix calculations become quite involved, especially the matrix inversion step. The large numbers (like 20{,}000{,}000) make hand calculations error-prone. This is why we always use computational tools in practice.they're faster, more accurate, and less prone to errors.