Fitting a Line and Making Predictions with Linear Regression

Last updated

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

Formula: xˉ=∑XN∣yˉ=∑YN\bar{x} = \dfrac{\sum X}{N} \quad | \quad \bar{y} = \dfrac{\sum Y}{N}
Mean of X (xˉ\bar{x})
Sum of X / N
= 45
Mean of Y (yˉ\bar{y})
Sum of Y / N
= 10.25

Step 2: Calculate Deviations, Products, and Squares

Formulas: Dev. (x′)=X−xˉ∣x') = X - \bar{x}\quad | \quad Dev. (y′)=Y−yˉy') = Y - \bar{y}
XYDev (X′)(X')Dev (Y′)(Y')Dev (X′)×(X') \times Dev (Y′)(Y')Dev (X′)2(X')^2
103-35-7.25253.751225
205-25-5.25131.25625
307-15-3.2548.75225
409-5-1.256.2525
501150.753.7525
6014153.7556.25225
7015254.75118.75625
8018357.75271.251225
SUMS (Σ\Sigma):8904200

Step 3: Calculate Slope (m) and Intercept (b)

Formulas: m=∑(x′y′)∑(x′)2∣b=yˉ−mxˉm = \dfrac{\sum (x'y')}{\sum (x')^2} \quad | \quad b = \bar{y} - m\bar{x}
Slope (m)

m = 890 / 4200

m≈0.212m \approx 0.212

Intercept (b)

b = 10.25 - (0.212 * 45)

b≈0.714b \approx 0.714

Step 4: Final Prediction

Formula: Y=mx+bY = mx + b
Equation of the Best-Fit Line

Line: Y ≈ 0.212 X + 0.714

Plugging in Target X (55):

Y ≈ (0.212 * 55) + 0.714

Y≈12.369Y \approx 12.369

* Note: Intermediate coefficients are displayed in rounded form. The final prediction is calculated using full precision.

Final Answer

Regression equation:y^≈0.212x+0.714\hat{y} \approx 0.212x + 0.714

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 mm represents the predicted change in YY for a one-unit increase in XX. Its sign indicates whether the fitted relationship is positive or negative. The intercept bb is the predicted YY value when X=0X=0, 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, ∑(yi−y^i)2\sum (y_i-\hat{y}_i)^2. 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.