Simple Linear Regression › Example

Michael BrenndoerferApril 7, 20255 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 29Simple Linear Regression › Example
Reading section 5 of 10 · Example · 5 minSection 5 of 10 · 5 min

Book contents

Section 5 of 10 · 5 min read

Simple Linear Regression › ExampleOne section · Sections in this chapter5 / 10 ↓
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 with actual numbers to see how simple linear regression works step by step. Suppose you're studying the relationship between study hours and test scores. You have collected the following data from five students:

Study Hours (x)Test Score (y)
165
270
380
485
590

Step 1: Calculate the means

First, we need to find the average values of both variables:

xˉ=1+2+3+4+55=155=3\bar{x} = \frac{1 + 2 + 3 + 4 + 5}{5} = \frac{15}{5} = 3 yˉ=65+70+80+85+905=3905=78\bar{y} = \frac{65 + 70 + 80 + 85 + 90}{5} = \frac{390}{5} = 78

Step 2: Calculate the slope (β1\beta_1)

Now we'll use the slope formula. Let's break this down into manageable pieces:

First, calculate (xi−xˉ)(yi−yˉ)(x_i - \bar{x})(y_i - \bar{y}) for each data point:

  • Point 1: (1−3)(65−78)=(−2)(−13)=26(1 - 3)(65 - 78) = (-2)(-13) = 26
  • Point 2: (2−3)(70−78)=(−1)(−8)=8(2 - 3)(70 - 78) = (-1)(-8) = 8
  • Point 3: (3−3)(80−78)=(0)(2)=0(3 - 3)(80 - 78) = (0)(2) = 0
  • Point 4: (4−3)(85−78)=(1)(7)=7(4 - 3)(85 - 78) = (1)(7) = 7
  • Point 5: (5−3)(90−78)=(2)(12)=24(5 - 3)(90 - 78) = (2)(12) = 24

Numerator: 26+8+0+7+24=6526 + 8 + 0 + 7 + 24 = 65

Next, calculate (xi−xˉ)2(x_i - \bar{x})^2 for each data point:

  • Point 1: (1−3)2=(−2)2=4(1 - 3)^2 = (-2)^2 = 4
  • Point 2: (2−3)2=(−1)2=1(2 - 3)^2 = (-1)^2 = 1
  • Point 3: (3−3)2=(0)2=0(3 - 3)^2 = (0)^2 = 0
  • Point 4: (4−3)2=(1)2=1(4 - 3)^2 = (1)^2 = 1
  • Point 5: (5−3)2=(2)2=4(5 - 3)^2 = (2)^2 = 4

Denominator: 4+1+0+1+4=104 + 1 + 0 + 1 + 4 = 10

Therefore:

β1=6510=6.5\beta_1 = \frac{65}{10} = 6.5

Step 3: Calculate the intercept (β0\beta_0)

Using the intercept formula:

β0=yˉ−β1xˉ=78−6.5×3=78−19.5=58.5\beta_0 = \bar{y} - \beta_1 \bar{x} = 78 - 6.5 \times 3 = 78 - 19.5 = 58.5

Our final fitted equation is:

y=58.5+6.5xy = 58.5 + 6.5x

Step 4: Make a prediction

If a student studies for 6 hours, their predicted test score would be:

y=58.5+6.5×6=58.5+39=97.5y = 58.5 + 6.5 \times 6 = 58.5 + 39 = 97.5

Step 5: Interpret the results

Our results can be interpreted as follows.

  • Intercept (58.5): A student who studies 0 hours would be expected to score 58.5 points (though this might not be realistic in practice)
  • Slope (6.5): For each additional hour of study, the test score increases by an average of 6.5 points
  • Prediction: A student studying 6 hours would be expected to score about 97.5 points

The reason that we can simply interpret the slope and intercept as the change in the test score for each additional hour of study and the baseline score for no study, respectively, is because the linear model is a linear function of the form y=β0+β1xy = \beta_0 + \beta_1 x. This is why linear regression models are so interpretable.

Let's visualize this example to see how our calculated regression line fits the data and how we can use it to make predictions. The plot will show the original data points, the fitted regression line, and a prediction for a student studying 6 hours. This visualization helps confirm that our manual calculations are correct and demonstrates the practical application of simple linear regression.

Out[6]:
Visualization
Scatter plot showing study hours vs test scores with regression line and prediction.
Predicting test scores from study hours. The blue circles show observed data points, the red line represents the fitted regression equation y = 58.5 + 6.5x, and the green square shows a prediction for 6 hours of study (97.5 points). This example demonstrates how simple linear regression can be used to make predictions: for each additional hour of study, test scores increase by an average of 6.5 points. The model provides a clear, interpretable relationship between study time and academic performance.