Introduction to Linear Regression

355 words
2 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Introduction to Linear Regression ## ๐ŸŽฏ Learning Objectives - Formulate the simple linear regression model - Estimate parameters using **ordinary least squares (OLS)** - Interpret regression coefficients - Understand assumptions underpinning the model * * * ## 1.1 Intuition: Finding the Line of Best Fit Given a sc...

Introduction to Linear Regression

๐ŸŽฏ Learning Objectives

  • Formulate the simple linear regression model
  • Estimate parameters using ordinary least squares (OLS)
  • Interpret regression coefficients
  • Understand assumptions underpinning the model

1.1 Intuition: Finding the Line of Best Fit

Given a scatter plot of points (xi,yi)(x_i, y_i), we want to find the line that best predicts yy from xx. "Best" means minimising the sum of squared vertical distances.
๐Ÿ”‘ Key Insight: Regression is about explaining variation in an outcome variable yy using one or more predictor variables xx.

1.2 The Simple Linear Regression Model

yi=ฮฒ0+ฮฒ1xi+ฮตiy_i = \beta_0 + \beta_1 x_i + \varepsilon_i
Where:
  • yiy_i = response (dependent) variable
  • xix_i = predictor (independent) variable
  • ฮฒ0\beta_0 = intercept (expected yy when x=0x = 0)
  • ฮฒ1\beta_1 = slope (expected change in yy per unit change in xx)
  • ฮตi\varepsilon_i = random error term, ฮตiโˆผN(0,ฯƒ2)\varepsilon_i \sim N(0, \sigma^2)

1.3 Least Squares Estimation

Minimise S(ฮฒ0,ฮฒ1)=โˆ‘i=1n(yiโˆ’ฮฒ0โˆ’ฮฒ1xi)2S(\beta_0, \beta_1) = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_i)^2 Normal equations:
โˆ‚Sโˆ‚ฮฒ0=โˆ’2โˆ‘(yiโˆ’ฮฒ0โˆ’ฮฒ1xi)=0\frac{\partial S}{\partial \beta_0} = -2\sum(y_i - \beta_0 - \beta_1 x_i) = 0 โˆ‚Sโˆ‚ฮฒ1=โˆ’2โˆ‘xi(yiโˆ’ฮฒ0โˆ’ฮฒ1xi)=0\frac{\partial S}{\partial \beta_1} = -2\sum x_i(y_i - \beta_0 - \beta_1 x_i) = 0
Solution:
ฮฒ^1=โˆ‘(xiโˆ’xห‰)(yiโˆ’yห‰)โˆ‘(xiโˆ’xห‰)2=SxySxx\hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} = \frac{S_{xy}}{S_{xx}} ฮฒ^0=yห‰โˆ’ฮฒ^1xห‰\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}

1.4 Interpretation

  • ฮฒ^1\hat{\beta}_1 = "a one-unit increase in xx is associated with a ฮฒ^1\hat{\beta}_1 change in yy"
  • ฮฒ^0\hat{\beta}_0 = "when x=0x = 0, the predicted yy is ฮฒ^0\hat{\beta}_0" (may not be meaningful if x=0x=0 is outside data range)

โœ… Practice Questions

Q1: Given data: (1,2), (2,4), (3,5), find ฮฒ^0\hat{\beta}_0 and ฮฒ^1\hat{\beta}_1.
Solution
xห‰=2\bar{x} = 2, yห‰=11/3โ‰ˆ3.67\bar{y} = 11/3 \approx 3.67 Sxx=(1โˆ’2)2+(2โˆ’2)2+(3โˆ’2)2=1+0+1=2S_{xx} = (1-2)^2 + (2-2)^2 + (3-2)^2 = 1 + 0 + 1 = 2 Sxy=(1โˆ’2)(2โˆ’3.67)+(2โˆ’2)(4โˆ’3.67)+(3โˆ’2)(5โˆ’3.67)=(โˆ’1)(โˆ’1.67)+0+(1)(1.33)=1.67+1.33=3S_{xy} = (1-2)(2-3.67) + (2-2)(4-3.67) + (3-2)(5-3.67) = (-1)(-1.67) + 0 + (1)(1.33) = 1.67 + 1.33 = 3 ฮฒ^1=3/2=1.5\hat{\beta}_1 = 3/2 = 1.5 ฮฒ^0=3.67โˆ’1.5(2)=0.67\hat{\beta}_0 = 3.67 - 1.5(2) = 0.67 y^=0.67+1.5x\hat{y} = 0.67 + 1.5x Join Discord NextRegression Applications
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.