Simple Linear Regression
Yaser Rahmati | یاسر رحمتی
Simple linear regression aims to model the relationship between a dependent variable 𝑦 and a single independent variable 𝑥 using a linear equation. The linear equation is represented as:
where:
is the dependent variable.
is the independent variable.
is the intercept (the value of when .
is the slope.
is the error term (residual).
Objective
The goal is to estimate the coefficients and that minimize the sum of squared residuals (errors). The residual for each observation is the difference between the observed value and the predicted value :
Estimating Coefficients
To find the estimates of and , we use the least squares method:
1. Slope
2. Intercept
Numerical Example
Let's consider a simple dataset with five observations to illustrate the calculation.
Dataset
1
1
2
2
2
3
3
3
5
4
4
4
5
5
6
Step-by-Step Calculation
1. Calculate the means
2. Calculate the slope
3. Calculate the intercept
Regression Line
The regression line is:
Predictions and Residuals
Let's compute the predicted values and residuals for each observation:
1
1
2
2.2
-0.2
2
2
3
3.1
-0.1
3
3
5
4
1
4
4
4
4.9
-0.9
5
5
6
5.8
0.2
Sum of Squared Residuals (RSS)
Python Code for Simple Linear Regression
Using NumPy and Manual Calculation
And the output is:
Last updated