Module 14

Simple Linear Regression

Predicting one variable from another

Regression Fundamentals

  • 01

    The Line

    y = mx + b in statistics

  • 02

    Least Squares

    Finding the best-fit line

  • 03

    R-Squared

    How well does the line fit?

  • 04

    Residuals

    What the model misses

01

The Regression Equation

Interactive Regression Explorer

Interactive Regression
Click to add points, drag to move
Slope (b₁)-
Intercept (b₀)-
R²-
Correlation (r)-
Points-
ŷ = b₀ + b₁x

Click on the canvas to add data points and watch the regression line update in real time! The equation shows the slope (b₁) and intercept (b₀), while R² tells you how well the line fits your data. Try creating different patterns—a tight linear relationship, a scattered cloud, or even a curve to see how the line responds.

The Line of Best Fit

ŷ = b₀ + b₁x
ŷ
Predicted Y
b₀
Intercept (where line crosses Y-axis)
b₁
Slope (change in Y per unit X)

The regression line predicts Y from X. b₀ is the Y-intercept (predicted Y when X = 0). b₁ is the slope (how much Y changes for each 1-unit increase in X).

Interpreting the Slope

Δx = 1Δy = b₁
Example: b₁ = 2.5
"For each 1-unit increase in X, Y increases by 2.5 on average"

The slope tells you the rate of change. If b₁ = 2.5, then each 1-unit increase in X is associated with a 2.5-unit increase in Y (on average).

Interpreting the Intercept

b₀ = interceptX=0
Caution: The intercept is often meaningless if X = 0 is impossible or outside your data range (e.g., height can't be 0).

The intercept is where the line crosses the Y-axis (when X = 0). It's often just a mathematical anchor—don't over-interpret it if X = 0 isn't meaningful.

Concrete Example

Study Hours → Exam Score
Score = 50 + 5 × Hours
Interpretation:
• b₀ = 50: With 0 hours of study, expected score is 50
• b₁ = 5: Each additional hour of study adds 5 points

Predictions:
• 2 hours → 50 + 5(2) = 60 points
• 6 hours → 50 + 5(6) = 80 points

A student studying 0 hours is predicted to score 50 (baseline). Each hour of study adds 5 points. After 6 hours, predicted score is 80.

02

Least Squares Method

Minimizing Error

Red lines = residuals (errors)
Goal: Make these as small as possible

The 'best' line minimizes the distance between observed points and the line. These vertical distances are called residuals (errors). We want small residuals.

Sum of Squared Residuals

Minimize: Σ(yᵢ - ŷᵢ)²
Why not just sum?
Positive and negative errors cancel out. A line missing all points could have sum = 0!
Why square?
• Makes all errors positive
• Penalizes large errors more
• Has nice math properties

We square residuals so positive and negative errors don't cancel. Squaring also penalizes large errors more heavily—a residual of 10 contributes 100, not just 10.

Calculating b₀ and b₁

b₁ = r × (sᵧ / sₓ)
or: b₁ = Σ(x-x̄)(y-ȳ) / Σ(x-x̄)²
b₀ = ȳ - b₁x̄
The line passes through (x̄, ȳ)

The slope b₁ depends on correlation (r) and the ratio of standard deviations. The intercept b₀ ensures the line passes through the point (x̄, ȳ)—the center of the data.

03

R-Squared (R²)

Proportion of Variance Explained

R² = 1 - (SSE / SST)
■ Explained (70%)■ Unexplained (30%)
R² = 0.70: Model explains 70% of variance in Y

R² measures how much of Y's variance is explained by X. R² = 0.70 means 70% of the variation in Y can be predicted from X; 30% is unexplained (residual variation).

What R² Values Mean

R² = 0.95
Excellent fit
R² = 0.50
Moderate fit
R² = 0.10
Poor fit

High R² (>0.7) means tight fit. Moderate R² (0.3-0.7) shows a relationship but with scatter. Low R² (<0.3) means X barely predicts Y—lots of unexplained variance.

R² Limitations

⚠️ High R² ≠ Good Model
Can still have violated assumptions or be non-linear
⚠️ Low R² ≠ Useless
In some fields (psychology), R² = 0.10 is meaningful
⚠️ Always Increases
Adding predictors always increases R² (use adjusted R²)

R² can be misleading. A high R² doesn't guarantee a valid model—always check assumptions and residual plots. A low R² can still represent a real, practically important relationship.

04

Residual Analysis

Understanding Residuals

Residual = Observed - Predicted = y - ŷ
Positive Residual
Observed > Predicted
Point above line
Negative Residual
Observed < Predicted
Point below line

A residual is the difference between what you observed and what the model predicted. Positive residuals are above the line; negative residuals are below.

What Good Residuals Look Like

Residual Plot (residuals vs fitted values)
✓ Random scatter around zero
✓ Constant spread (homoscedasticity)
✓ No patterns

Good residuals are randomly scattered around zero with constant variance across all X values. No patterns, no fanning out, no curves—just random noise.

Red Flags in Residual Plots

Curved Pattern
Need non-linear term
Fan Shape
Heteroscedasticity

Curved residuals suggest non-linearity—add polynomial terms. Fan-shaped residuals indicate heteroscedasticity (unequal variance)—try transforming Y or use robust methods.

Checking Normality of Residuals

Histogram of Residuals
Should be bell-shaped
Q-Q Plot
Points should follow line

For valid inference, residuals should be approximately normal. Check with a histogram (should be bell-shaped) or Q-Q plot (points should follow the diagonal line).

★

Real-World Examples

Apply your knowledge to practical scenarios

01

Predicting House Prices

A real estate analyst builds a regression model: Price = -50,000 + 200 × SquareFeet. The model has R² = 0.65 and is based on 100 homes in a suburban neighborhood.

Sample Data
{
  "equation": "Price = -50000 + 200 × SquareFeet",
  "r_squared": 0.65,
  "n": 100
}
Q Interpret the slope coefficient (b₁ = 200). +
A For each additional square foot, the predicted house price increases by $200 on average. A house 100 sq ft larger would be predicted to cost $20,000 more.
Q What does the intercept (-$50,000) mean? Is it meaningful? +
A Mathematically, it's the predicted price when SquareFeet = 0. But a 0 sq ft house is impossible! The intercept is just a mathematical anchor—don't interpret it literally.
Q With R² = 0.65, how much of price variation is explained by size? +
A 65% of the variation in house prices can be explained by square footage. The remaining 35% is due to other factors (location, age, condition, etc.) not in our model.
Q Predict the price of a 2,000 sq ft home. +
A Price = -50,000 + 200(2,000) = -50,000 + 400,000 = $350,000
Key Insights
  • Slope is the key interpretable coefficient in simple regression
  • Intercept often has no practical meaning if X=0 is impossible
  • R² tells you what proportion of variance is explained
02

Study Hours and Exam Scores

A professor regresses exam scores on study hours for 50 students. Results: Score = 45 + 8(Hours), R² = 0.72, SE of slope = 1.5.

Sample Data
{
  "n": 50,
  "intercept": 45,
  "slope": 8,
  "r_squared": 0.72,
  "se_slope": 1.5
}
Q Is the relationship between study hours and exam score significant? +
A t = b/SE = 8/1.5 = 5.33. With df = 48 and critical t ≈ 2.01, this is highly significant (p < 0.001). Study hours significantly predicts exam scores.
Q A student who studied 0 hours is predicted to score 45. Does this seem reasonable? +
A Yes, the intercept of 45 is meaningful here—it represents baseline knowledge a student might have even without studying. Unlike house size, 0 study hours is a realistic value.
Q A student asks: 'If I study 20 hours, will I definitely get Score = 45 + 8(20) = 205?' +
A No! Two issues: (1) Extrapolation—20 hours may be outside the study range; (2) Regression predicts AVERAGE score, not individual score. There's still variation (R² = 0.72, not 1.0).
Q What's the 95% CI for the slope? +
A CI = b ± t×SE = 8 ± 2.01(1.5) = 8 ± 3.0 = [5.0, 11.0]. We're 95% confident each study hour adds between 5 and 11 points.
Key Insights
  • Test slope significance with t = b/SE
  • Predictions are for averages, not individuals
  • Confidence intervals quantify uncertainty in the slope
03

The Diagnostic Detective

You fit a regression of Sales on Advertising spend. The R² is good (0.78), but your residual plot shows a clear curved pattern—points above the line at low and high X, below in the middle.

Q What does this residual pattern indicate? +
A A curved (U-shaped or inverted-U) pattern in residuals indicates non-linearity. The relationship between Advertising and Sales isn't linear—possibly diminishing returns at high spending.
Q Should you trust the R² = 0.78? +
A Be cautious. R² measures fit to the LINE, but if the true relationship is curved, R² is misleading. A quadratic model might have even higher R² and more accurate predictions.
Q What should you do next? +
A Try adding a quadratic term: Sales = b₀ + b₁(Ads) + b₂(Ads²). Check if R² improves and if residuals become randomly scattered.
Q If residuals showed a 'fan' shape (spreading out for larger fitted values), what would that indicate? +
A Heteroscedasticity—variance isn't constant. Predictions become less precise for larger values. Solutions include log-transforming Y or using weighted least squares.
Key Insights
  • Good R² doesn't guarantee model validity—always check residuals
  • Curved residuals → add polynomial terms
  • Fanning residuals → transform data or use robust methods
?

Simple Linear Regression Quiz

Test your understanding • Passing score: 70%

0 of 10 answered
Q1 multiple-choice

In the equation ŷ = 10 + 3x, what does the '3' represent?

Explanation: The coefficient 3 is the slope, representing the change in predicted y for each 1-unit increase in x. Here, each unit increase in x adds 3 to the predicted y.
Q2 multiple-choice

What does R² = 0.81 tell you about a regression model?

Explanation: R² = 0.81 means 81% of the variance in y can be explained by the linear relationship with x. The remaining 19% is unexplained (residual) variance.
Q3 multiple-choice

What is a residual?

Explanation: Residual = Observed - Predicted = y - ŷ. It measures how far each data point is from the regression line (positive if above, negative if below).
Q4 multiple-choice

The least squares method minimizes which quantity?

Explanation: Least squares minimizes Σ(y - ŷ)² — the sum of squared residuals. Squaring ensures positive and negative errors don't cancel and penalizes large errors more.
Q5 calculation

Given ŷ = 5 + 2x, what is the predicted value when x = 4?

Explanation: ŷ = 5 + 2(4) = 5 + 8 = 13
Q6 multiple-choice

A residual plot shows a clear U-shaped pattern. What does this indicate?

Explanation: A curved pattern in residuals indicates non-linearity—the true relationship is curved, but we're forcing a straight line. Consider adding polynomial terms.
Q7 multiple-choice

In simple linear regression, what is the relationship between r (correlation) and R²?

Explanation: In simple linear regression (one predictor), R² equals the square of the correlation coefficient. If r = 0.9, then R² = 0.81.
Q8 multiple-choice

The regression line always passes through which point?

Explanation: The least squares regression line always passes through the point (x̄, ȳ)—the center of the data (mean of x, mean of y).
Q9 multiple-choice

If b₁ = -2.5 in a regression, what does this mean?

Explanation: A negative slope means y decreases as x increases. Here, each 1-unit increase in x is associated with a 2.5-unit decrease in predicted y.
Q10 multiple-choice

Which assumption is NOT required for simple linear regression?

Explanation: X doesn't need to be normally distributed. Key assumptions are: linearity, independence, normality of RESIDUALS (not X), and homoscedasticity (constant variance).

Quiz Results

0 %

← Correlation Next: Multiple Regression →