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 PointDistance_kmDelivery_Hours
P1103
P2205
P3307
P4409
P55011
P66014
P77015
P88018
Target55?

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).

Formula: xˉ=XNyˉ=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.00

Mean of Y (yˉ\bar{y})

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.

Formulas: Dev. (x)=Xxˉx') = X - \bar{x}\quad | \quad Dev. (y)=Yyˉy') = Y - \bar{y}
XYDev (X)(X')Dev (Y)(Y')Dev (X)×(X') \times Dev (Y)(Y')Dev (X)2(X')^2
103-35.00-7.25253.751225.00
205-25.00-5.25131.25625.00
307-15.00-3.2548.75225.00
409-5.00-1.256.2525.00
50115.000.753.7525.00
601415.003.7556.25225.00
701525.004.75118.75625.00
801835.007.75271.251225.00
SUMS (Σ\Sigma):890.004200.00

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

Using the Sums Σ\Sigma from the bottom of our table, we can finally calculate the angle of our line (Slope) and where it crosses the Y-axis (Intercept).

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

m = 890.00 / 4200.00

m = 0.21

Intercept (b)

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.

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

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.