Linear Regression: A Complete Solved Numerical Example
Scenario: Delivery Time Prediction
The Objective: Predict parcel delivery time (in hours) based on the distance from the warehouse (in km).
Step 1: The Historical Data & Target Point
To predict a continuous value using Linear Regression, we first need to find the "line of best fit" through our historical data. We want to predict the Delivery_Hours when the Distance_km is exactly 55 km.
| Data Point | Distance_km | Delivery_Hours |
|---|---|---|
| P1 | 10 | 3 |
| P2 | 20 | 5 |
| P3 | 30 | 7 |
| P4 | 40 | 9 |
| P5 | 50 | 11 |
| P6 | 60 | 14 |
| P7 | 70 | 15 |
| P8 | 80 | 18 |
| Target | 55 | ? |
Step 2: Calculate Means (Average) for X and Y
First, we find the center point of all our data by taking the average of the independent variable (X) and the dependent variable (Y).
Sum of X / N
= 45.00
Sum of Y / N
= 10.25
Step 3: Calculate Deviations, Products, and Squares
We need to see how much each point "deviates" or wanders away from the averages we calculated in Step 2. We will sum these deviations up at the bottom of the table.
| X | Y | Dev | Dev | Dev Dev | Dev |
|---|---|---|---|---|---|
| 10 | 3 | -35.00 | -7.25 | 253.75 | 1225.00 |
| 20 | 5 | -25.00 | -5.25 | 131.25 | 625.00 |
| 30 | 7 | -15.00 | -3.25 | 48.75 | 225.00 |
| 40 | 9 | -5.00 | -1.25 | 6.25 | 25.00 |
| 50 | 11 | 5.00 | 0.75 | 3.75 | 25.00 |
| 60 | 14 | 15.00 | 3.75 | 56.25 | 225.00 |
| 70 | 15 | 25.00 | 4.75 | 118.75 | 625.00 |
| 80 | 18 | 35.00 | 7.75 | 271.25 | 1225.00 |
| SUMS (): | 890.00 | 4200.00 | |||
Step 4: Calculate Slope (m) and Intercept (b)
Using the Sums from the bottom of our table, we can finally calculate the angle of our line (Slope) and where it crosses the Y-axis (Intercept).
m = 890.00 / 4200.00
m = 0.21
b = 10.25 - (0.21 * 45.00)
b = 0.71
Step 5: Final Prediction
Now that we have the equation for our line, we simply plug in our target X value to predict the Y value.
Line: Y = 0.21x + 0.71
Plugging in Target X (55):
Y = (0.21 * 55) + 0.71
Y = 12.37
Final Takeaway
Based on the historical trend line, if the distance from the warehouse is 55 km, the model predicts the delivery will take approximately 12.37 hours.