Simple Linear Regression

Understand linear regression through relatable examples, clear math, and a visual demo.

RegressionBeginner

Why Regression? A Story of Fair Comparison

Imagine a 5-year-old child and a 20-year-old adult both score the same on a vocabulary test.

Would you say they have the same language ability?

Probably not. You naturally consider age. A 5-year-old scoring that high is impressive. A 20-year-old? Maybe just average.

What you did intuitively is control for a background factor. You “regressed” their scores to account for age.

Regression, in statistics, formalizes this intuition. It lets us adjust for background variables when making predictions or comparisons.

What Is Simple Linear Regression?

Simple Linear Regression is the most basic kind of regression. It models the relationship between two variables:

  • One explanatory variable xx — something we know (like age)
  • One response variable yy — something we want to predict (like vocabulary score)

The relationship is assumed to be a straight line.

The regression equation is:

y=β0+β1x+εy = \beta_0 + \beta_1 x + \varepsilon

Where:

  • β0\beta_0: the intercept — where the line crosses the y-axis
  • β1\beta_1: the slope — how much yy changes when xx increases by 1
  • ε\varepsilon: the error term — the part of yy not explained by xx

Finding the Best Line

Given data points (xi,yi)(x_i, y_i), we want to find the line that fits them best.

How? By minimizing the sum of squared errors between the observed yiy_i and the predicted y^i\hat{y}_i:

Errori=yi(β0+β1xi)\text{Error}_i = y_i - (\beta_0 + \beta_1 x_i)

We choose β0\beta_0 and β1\beta_1 to minimize:

i=1n(yi(β0+β1xi))2\sum_{i=1}^n \left( y_i - (\beta_0 + \beta_1 x_i) \right)^2

This method is called Ordinary Least Squares (OLS).

Step-by-Step: Calculating the Slope and Intercept

To find the best-fitting line, use these formulas:

1. Compute the slope β1\beta_1:

β1=(xixˉ)(yiyˉ)(xixˉ)2\beta_1 = \frac{ \sum (x_i - \bar{x})(y_i - \bar{y}) }{ \sum (x_i - \bar{x})^2 }

2. Then the intercept β0\beta_0:

β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}

Where xˉ\bar{x} and yˉ\bar{y} are the means of the xx and yy data.

Visual Intuition: Scatterplot + Regression Line

Picture a scatterplot of your data points.

Now imagine a straight line drawn through them — one that balances the points as evenly as possible.

That’s the regression line. It “predicts” yy from xx, minimizing the overall squared error.

Interactive Demo

Try adjusting data points and watch the regression line update live. Notice how a single point can tilt the line, especially if it’s far from the rest.

Simple Linear Regression Interactive Demo

Linear Equation:
y = β₀ + β₁x
β₀ (Intercept)
0.000
Where the line crosses the y-axis
β₁ (Slope)
0.000
How much y changes when x increases by 1

Interactive Demo

Try adjusting data points and watch the regression line update live. Notice how the slope and intercept change!

Click on chart to add points, drag to move them
R² (Fit Quality)
0.000
Sum of Squared Errors
0.000

Summary

Regression is about fair comparisons and informed predictions. Simple linear regression does this with:

  • A clear mathematical formula
  • A meaningful slope and intercept
  • A visual way to model relationships

And it all starts with a very human idea: “Let’s account for the background before we compare.”

← Back to Encyclopedia