Multiple Linear Regression › Example
Machine Learning from ScratchAll chapters 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:
| House | Size (x₁) | Age (x₂) | Price (y) |
|---|---|---|---|
| 1 | 2,000 | 1 | 6 |
| 2 | 0 | 3 | 10 |
| 3 | 4,000 | 5 | 16 |
Setting Up the Matrices
First, let's organize this data into matrix form. The feature matrix includes a column of ones for the intercept:
Notice that the first column of is all ones.that column allows us to estimate the intercept term .
Step-by-Step OLS Calculation
Now let's compute the OLS solution:
Step 1: Compute
First, we transpose :
Now multiply by :
Let's compute each element:
- :
- :
- :
- :
- :
- :
- :
- :
- :
So:
Step 2: Compute
Computing each element:
- First row:
- Second row:
- Third row:
So:
Step 3: Compute
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:
Interpreting the Results
The fitted model is:
where:
-: predicted house price (in thousands of dollars)
- : house size in square feet
- : house age in years
This means:
- Intercept (): A house with zero size and zero age would cost $3{,}000 (though this interpretation doesn't make practical sense)
- Size coefficient (): For each additional square foot, the price increases by $0.33 (0.000333 × 1000)
- Age coefficient (): 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):
Actual: → Perfect prediction!
House 2 (Size = 0, Age = 3):
Actual: → Perfect prediction!
House 3 (Size = 4000, Age = 5):
Actual: → 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.
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.