Fitting a Line and Making Predictions with Linear Regression
Problem Setup
Scenario:Estimate the delivery time for a shipment based on the distance it needs to travel.
Given:Eight historical delivery records containing the distance traveled in kilometers and the corresponding delivery time in hours.
Task:Use simple linear regression to fit the best-fit line to the historical data, then use that line to estimate the delivery time for a new shipment traveling 55 km.
Need a quick refresher? Review Linear theory
Delivery Distance & Time
Each row contains a delivery distance in kilometers and its observed delivery time in hours. Distance is the input variable (X), and delivery time is the target variable (Y) that the regression model predicts.
| 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-by-Step Solution
Step 1: Calculate Mean (Average) for X and Y
= 45
= 10.25
Step 2: Calculate Deviations, Products, and Squares
| X | Y | Dev | Dev | Dev Dev | Dev |
|---|---|---|---|---|---|
| 10 | 3 | -35 | -7.25 | 253.75 | 1225 |
| 20 | 5 | -25 | -5.25 | 131.25 | 625 |
| 30 | 7 | -15 | -3.25 | 48.75 | 225 |
| 40 | 9 | -5 | -1.25 | 6.25 | 25 |
| 50 | 11 | 5 | 0.75 | 3.75 | 25 |
| 60 | 14 | 15 | 3.75 | 56.25 | 225 |
| 70 | 15 | 25 | 4.75 | 118.75 | 625 |
| 80 | 18 | 35 | 7.75 | 271.25 | 1225 |
| SUMS (): | 890 | 4200 | |||
Step 3: Calculate Slope (m) and Intercept (b)
m = 890 / 4200
b = 10.25 - (0.212 * 45)
Step 4: Final Prediction
Line: Y ≈ 0.212 X + 0.714
Plugging in Target X (55):
Y ≈ (0.212 * 55) + 0.714
* Note: Intermediate coefficients are displayed in rounded form. The final prediction is calculated using full precision.
Final Answer
Regression equation:
Predicted delivery time:12.37 hours
The positive slope indicates that longer delivery distances are associated with longer delivery times. The slope represents an estimated increase of approximately 0.212 hours for each additional kilometer. This is a fitted relationship based on the historical records, not a guarantee of actual delivery time.
Exam Takeaway
- Interpret the slope in the original units: In simple linear regression, the slope represents the predicted change in for a one-unit increase in . Its sign indicates whether the fitted relationship is positive or negative. The intercept is the predicted value when , although that interpretation may not be meaningful outside the observed data range.
- Understand what least squares minimizes: The fitted line minimizes the sum of squared residuals, . A residual is the difference between an observed value and its prediction. A best-fit line does not need to pass through every training point.
- Avoid rounding intermediate calculations: Keep the slope and intercept at full precision when calculating predictions, then round the final result as required. Using rounded coefficients too early can produce a different answer, even when the regression formula is applied correctly.